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

Directory called Kustomization incorrectly implies kustomization #223

Closed
mflendrich opened this issue Dec 23, 2020 · 1 comment · Fixed by #224
Closed

Directory called Kustomization incorrectly implies kustomization #223

mflendrich opened this issue Dec 23, 2020 · 1 comment · Fixed by #224

Comments

@mflendrich
Copy link
Contributor

Hi there! 👋

It seems that I have hit a corner case in Kustomization detection.

Summary

Current behavior: kustomization-controller fails if it encounters a directory called Kustomization in the repo.
Expected behavior:

  1. reconciliation not failing
  2. flux not refusing to descend into Kustomization/'s parent directory

I believe that this happens because the condition of this if should only fire for non-directories.

Reproduction

Suppose that I have source-controller and kustomization-controller running in my cluster and the following manifests applied:

apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
  name: cluster-contents
  namespace: something
spec:
  url: https://github.com/some-org/some-repo.git
  interval: 30s
  secretRef:
    name: some-https-basic-auth-secret
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
  name: cluster-contents
  namespace: something
spec:
  interval: 30s
  path: "./"
  prune: true
  sourceRef:
    kind: GitRepository
    name: cluster-contents

The repo contains only plain Kubernetes manifests (i.e. no Kustomizations, just plain k8s resources). That repo follows a naming convention for resource filenames: /<metadata.namespace>/<kind>/<metadata.name>.yaml

This works flawlessly until I add a resource of Kustomization kind (the use case is that I want to manage my flux2 Kustomizations from this repo). Per my naming convention, it lands in a file called along the lines of /michals-namespace/Kustomization/michals-kustomization.yaml. Once a directory called Kustomization gets pushed to the repository, reconciliation of the Kustomization resource cluster-contents begins to fail:

Status:
  Conditions:
    Last Transition Time:  2020-12-23T21:04:46Z
    Message:               kustomize build failed: accumulating resources: 2 errors occurred:
                           * accumulateFile error: "accumulating resources from './gitops': read /tmp/cluster-contents240645327/gitops: is a directory"
                           * accumulateDirector error: "couldn't make target for path '/tmp/cluster-contents240645327/gitops': unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/tmp/cluster-contents240645327/gitops'"


    Reason:                 BuildFailed
    Status:                 False
    Type:                   Ready
  Last Applied Revision:    (redacted)
  Last Attempted Revision:  (redacted)
  Observed Generation:      1
  Snapshot:
    Checksum:  (redacted)
    Entries:
      Kinds:
        /v1, Kind=Namespace:                                          Namespace
        apiextensions.k8s.io/v1, Kind=CustomResourceDefinition:       CustomResourceDefinition
        apiextensions.k8s.io/v1beta1, Kind=CustomResourceDefinition:  CustomResourceDefinition
        rbac.authorization.k8s.io/v1, Kind=ClusterRoleBinding:        ClusterRoleBinding
        rbac.authorization.k8s.io/v1beta1, Kind=ClusterRole:          ClusterRole
        rbac.authorization.k8s.io/v1beta1, Kind=ClusterRoleBinding:   ClusterRoleBinding
      Namespace:                                                      
      Kinds:
        /v1, Kind=Service:         Service
        /v1, Kind=ServiceAccount:  ServiceAccount
        apps/v1, Kind=Deployment:  Deployment
      Namespace:                   (redacted)
      Kinds:
        /v1, Kind=Service:                               Service
        apps/v1, Kind=Deployment:                        Deployment
        networking.k8s.io/v1, Kind=NetworkPolicy:        NetworkPolicy
        rbac.authorization.k8s.io/v1, Kind=Role:         Role
        rbac.authorization.k8s.io/v1, Kind=RoleBinding:  RoleBinding
      Namespace:                                         flux-system
      Kinds:
        apps/v1, Kind=Deployment:  Deployment
      Namespace:                   (redacted)
Events:
  Type    Reason  Age   From                  Message
  ----    ------  ----  ----                  -------
  Normal  error   49m   kustomize-controller  kustomize build failed: accumulating resources: 2 errors occurred:
          * accumulateFile error: "accumulating resources from './michals-namespace': read /tmp/cluster-contents051013649/michals-namespace: is a directory"
          * accumulateDirector error: "couldn't make target for path '/tmp/cluster-contents051013649/michals-namespace': unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/tmp/cluster-contents051013649/michals-namespace'"

Proposed fix

This line in kustomization_generator.go

					if fs.Exists(filepath.Join(path, kfilename)) {

should become something equivalent to

					if kpath := filepath.Join(path, kfilename); fs.Exists(kpath) && !fs.IsDir(kpath) {

If I get a green light from maintainers I'm happy to send a PR.

@stefanprodan
Copy link
Member

Your fix looks ok to me, please open a PR. Thanks

mflendrich added a commit to mflendrich/kustomize-controller that referenced this issue Dec 24, 2020
mflendrich added a commit to mflendrich/kustomize-controller that referenced this issue Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants