kustomize: KRM Exec Function can't locate executable when referencing a base

Describe the bug

When using a KRM exec function with a relative path to the kustomization.yaml file, the executable can’t be found when referencing a base from a variant.

It appears the function is “exec”-d relative to the base not the variant.

Things are further complicated when the base references another remote base.

Files that can reproduce the issue


# dir
.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── krm-exec
    ├── krm-config.yaml
    ├── kustomization.yaml
    └── plugins
        └── function.sh

# base/kustomization.yaml

resources:
 - deployment.yaml

# base/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1
spec:
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: busybox
        image: busybox:v1.0.0

#krm-exec/kustomization.yaml
resources:
- ../base
transformers:
- krm-config.yaml

#krm-exec/krm-config.yaml
apiVersion: my.example.io/v1
kind: MyPlugin
metadata:
  name: notImportantHere
  annotations:
    config.kubernetes.io/function: |
      exec: 
        path: ./plugins/function.sh
#krm-exec/plugins/function.sh
#!/bin/bash
resourceList=$(cat) 
# dummy
echo "$resourceList"

Expected output

The dummy function should just echo out the resources read from stdin.


apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1
spec:
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        any: thing
    spec:
      containers:
      - name: busybox
        image: busybox:v1.0.0
     

Actual output

#from krm-exec dir
kustomize build --enable-alpha-plugins --enable-exec  .
Error: couldn't execute function: fork/exec ./plugins/function.sh: no such file or directory

Kustomize version

kustomize version
{Version:kustomize/v4.4.1 GitCommit:b2d65ddc98e09187a8e38adc27c30bab078c1dbf BuildDate:2021-11-11T23:36:27Z GoOs:darwin GoArch:amd64}

Platform

macOS ( M1 ) darwin21.0 arm64

Additional context

Some other scenarios:

  • The plugin works as expected when the variant kustomization does not reference the base, but a relative deployment.
#krm-exec/kustomizaiton.yaml
resources:
- deployment.yaml
  • With the original setup If I move the ./plugins/function.sh to the base, the plugin works even if the krm-function.yaml is located in the variant

To further complicate things, if I add a remote base to the local base I get this error:

#base/kustomization.yaml
resources:
 - https://github.com/pmcjury/somerepo/manifests/base?ref=main
 - deployment.yaml
#from krm-exec dir
kustomize build --enable-alpha-plugins --enable-exec  .
Error: couldn't execute function: chdir /private/var/folders/yn/88ww9kqx1b35xylvc0wt0fb80000gn/T/kustomize-124974724/deploy/manifests/base: no such file or directory 

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 17 (14 by maintainers)

Most upvoted comments

/close

Issue should be fixed by #4654

In that case, it will make a full copy instead of referring to the pointer.

It will make a shallow copy. If you look at the loader.Loader struct type, you’ll see that it’s mostly pointers. Copying a Loader gives you the same pointer values, pointing at the same referenced values. Per Katrina’s earlier https://github.com/kubernetes-sigs/kustomize/issues/4347#issuecomment-999960604, at minimum we need to copy the types.PluginConfig referenced by the “pc” field.

Next, how we copy a types.PluginConfig? Two of its fields are ints, two are structs. Both of the structs contain only simple scalar values, so copying a types.PluginConfig can be achieved via assignment.

I am not sure whether there are more fields that we’d need to copy deeply, but we do need to copy the PluginConfig, because its “FnLoadingOptions” field of type 'FnPluginLoadingOptions` has a “WorkingDir” field that holds the crucial value, such that us using the wrong value there leads to this defect.