Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nameSuffix transformer not honoring fieldspec #4883

Closed
ChristopherFry opened this issue Nov 22, 2022 · 3 comments · Fixed by #4919
Closed

nameSuffix transformer not honoring fieldspec #4883

ChristopherFry opened this issue Nov 22, 2022 · 3 comments · Fixed by #4919
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@ChristopherFry
Copy link
Contributor

Describe the bug

Following Transformer Configurations, the nameSuffix transformer is not honoring the FieldSpec to limit the number of resources to be transformed.

Files that can reproduce the issue

mkdir kustomize-test
cd kustomize-test

cat <<EOF> kustomization.yaml
nameSuffix: -suffix
resources:
- resources.yaml
configurations:
- skip-suffix.yaml
EOF

cat <<EOF> skip-suffix.yaml
nameSuffix:
- path: metadata/name
  apiVersion: apps/v1
  kind: Deployment
EOF

cat <<EOF> resources.yaml
apiVersion: apps/v1
metadata:
  name: deployment1
kind: Deployment
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: config
---
apiVersion: v1
kind: Secret
metadata:
  name: secret
---
apiVersion: v1
kind: Namespace
metadata:
  name: ns1
EOF

Expected output

The expectation is when kustomize build . is executed, only the Deployment resource's name will be updated with the suffix -suffix appended.

Actual output

Running kustomize build, the names of all the resources (excluding the Namespace resource) have the -suffix appended, with the Deployment resource having the suffix appended twice.

>  kustomize build .
apiVersion: v1
kind: Namespace
metadata:
  name: ns1
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-suffix
---
apiVersion: v1
kind: Secret
metadata:
  name: secret-suffix
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment1-suffix-suffix

Kustomize version

>  kustomize version
{Version:kustomize/v4.5.7 GitCommit:56d82a8378dfc8dc3b3b1085e5a6e67b82966bd7 BuildDate:2022-08-02T16:35:54Z GoOs:linux GoArch:amd64}
@ChristopherFry ChristopherFry added the kind/bug Categorizes issue or PR as related to a bug. label Nov 22, 2022
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Nov 22, 2022
@KnVerey
Copy link
Contributor

KnVerey commented Nov 23, 2022

The issue seems to be that it is not overridding the default config that targets metadata/name in all resources with the more specific one you provided. Or, perhaps it is somehow running twice, once with the default config and once with the one you specified? That seems less likely but would also have the observed result.

Interestingly, namePrefix--a nearly identical transformer--is behaving correctly. If I do:

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - resources.yaml

namePrefix: prefix-
nameSuffix: -suffix

configurations:
  - my-config.yaml
# my-config.yaml
namePrefix:
- path: metadata/name
  apiVersion: apps/v1
  kind: Deployment
nameSuffix:
- path: metadata/name
  apiVersion: apps/v1
  kind: Deployment

Then I get:

apiVersion: v1
kind: Namespace
metadata:
  name: ns1 # good
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: config-suffix # prefix correctly skipped, suffix incorrectly present
---
apiVersion: v1
kind: Secret
metadata:
  name: secret-suffix  # prefix correctly skipped, suffix incorrectly present
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prefix-deployment1-suffix-suffix # prefix correctly applied, suffix duplicated

This is a bug that we should fix, but I also have a workaround for you: if you invoke the transformer through the transformers field, the fieldspecs are replaced rather than merged, so you should get the behaviour you want:

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - resources.yaml

transformers:
  - transformer.yaml
# transformer.yaml
apiVersion: builtin
kind: PrefixSuffixTransformer
metadata:
  name: suffixer
suffix: "-suffix"
fieldSpecs:
- path: metadata/name
  apiVersion: apps/v1
  kind: Deployment

/triage accepted

@ChristopherFry I see you've been working on tests in #4882 that currently demonstrate the prefix transformer behaves correctly in this respect. Perhaps you'd be interested in looking into this related bug?

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Nov 23, 2022
@brianpursley
Copy link
Member

The cause looks like it might be due to sort.Sort(t.NameSuffix) being missing from sortFields()...

func (t *TransformerConfig) sortFields() {
	sort.Sort(t.NamePrefix)
	sort.Sort(t.NameSuffix)   # Missing
	sort.Sort(t.NameSpace)
	sort.Sort(t.CommonLabels)
	sort.Sort(t.TemplateLabels)
	sort.Sort(t.CommonAnnotations)
	sort.Sort(t.NameReference)
	sort.Sort(t.VarReference)
	sort.Sort(t.Images)
	sort.Sort(t.Replicas)
}

@ChristopherFry
Copy link
Contributor Author

@ChristopherFry I see you've been working on tests in #4882 that currently demonstrate the prefix transformer behaves correctly in this respect. Perhaps you'd be interested in looking into this related bug?

I'll be happy to take this bug on. Thanks!

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants