20

I am trying to build manifest file for specific environment example:- test and I want to re-use base manifest files as mentioned below.

k8s/kustomize/overlays/test/kustomization.yaml

commonLabels:
  variant: test
  app: test-app
resources:
- ../../base/deployment.yaml
- ../../base/service.yaml
- ../../base/configmap.yaml
- ../../base/secret.yaml
- namespace.yaml
namespace: app-test
patchesStrategicMerge:
- secret.yaml
- configmap.yaml

But I got below error when I run the command - kustomize build k8s/kustomize/overlay/test

2020/02/19 16:04:36 got file 'deployment.yaml', but 'path/k8s/kustomize/base/deployment.yaml' must be a directory to be a root
Error: accumulating resources: accumulating resources from '../../base/deployment.yaml': security; file 'path/k8s/kustomize/base/deployment.yaml' is not in or below 'path/k8s/kustomize/overlay/test'

P.S: kustomize version is - Version: {KustomizeVersion:3.2.0 GitCommit:a3103f1e62ddb5b696daa3fd359bb6f2e8333b49 BuildDate:2019-09-18T18:31:04+01:00 GoOs:darwin GoArch:amd64}

I am new to kubernetes and kustomize. Could Please help me ?

shivakumar sajjan
  • 447
  • 2
  • 5
  • 11
  • Did you use [bases](https://github.com/kubernetes-sigs/kustomize/blob/master/docs/glossary.md#base)? Why you use kustomize build k8s/kustomize/overlay/test when your resources directory is ../../base? Could you fix it and let me know if it works then? If it's not could you show me your file structure, like for example [here](https://github.com/kubernetes-sigs/kustomize#2-create-variants-using-overlays). – Jakub Feb 20 '20 at 08:46
  • FYI, the documentation says that "the bases field was deprecated in v2.1.0" – Lam Le Apr 06 '21 at 10:27

2 Answers2

20

Kustomize doesn't allow you to directly include resource files that are not in the same directory or a subdirectory of where your kustomization.yml file is located.

The usual way to use a base in your overlay is to add a kustomization.yml file in the base and include the base directory in the kustomization.yml of your overlay. For example:

k8s/kustomize/base/kustomization.yaml:

resources:
- deployment.yaml
- service.yaml
- configmap.yaml
- secret.yaml

and in k8s/kustomize/overlays/test/kustomization.yaml:

resources:
- ../../base
- namespace.yaml

namespace: app-test

commonLabels:
  variant: test
  app: test-app

patchesStrategicMerge:
- secret.yaml
- configmap.yaml
ITChap
  • 4,057
  • 1
  • 21
  • 46
  • As noted in the answer below, this answer is incorrect. Kustomize uses go-getter (hashicorp) under the hood. Any git repos should work if noted properly. – Robert Smith Aug 25 '21 at 14:38
  • @RobertSmith I think it still applies. See: https://github.com/kubernetes-sigs/kustomize/pull/700 and https://github.com/kubernetes-sigs/kustomize/issues/865 You are not allowed to load a resource file in a directory that is not bellow the root. You are allowed to load kustomization file from wherever you want. This is not related to go-getter but kustomize logic. – ITChap Aug 26 '21 at 08:08
  • Awesome answer. Clearly explained the problem and solution. Works in 2023 June – RukshanJS Jun 16 '23 at 06:19
1

Maybe something change because the following example does that the question was trying to do: https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/resource/

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- myNamespace.yaml
- sub-dir/some-deployment.yaml
- ../../commonbase
- github.com/kubernetes-sigs/kustomize/examples/multibases?ref=v1.0.6
- deployment.yaml
- github.com/kubernets-sigs/kustomize/examples/helloWorld?ref=test-branch
Eduardo Baitello
  • 10,469
  • 7
  • 46
  • 74
  • I guess this example loads a kustomize file in the ../../commonbase folder and from there resources which are in the same folder or below. – nice_pink Apr 25 '22 at 08:33
  • 2
    The mentioned url has changed to https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/resource/ – oliver nadj May 11 '22 at 07:07