| 1 | // Copyright 2019 The Kubernetes Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package container |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | |
| 11 | "sigs.k8s.io/kustomize/kyaml/errors" |
| 12 | runtimeexec "sigs.k8s.io/kustomize/kyaml/fn/runtime/exec" |
| 13 | "sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil" |
| 14 | "sigs.k8s.io/kustomize/kyaml/yaml" |
| 15 | ) |
| 16 | |
| 17 | // Filter filters Resources using a container image. |
| 18 | // The container must start a process that reads the list of |
| 19 | // input Resources from stdin, reads the Configuration from the env |
| 20 | // API_CONFIG, and writes the filtered Resources to stdout. |
| 21 | // If there is a error or validation failure, the process must exit |
| 22 | // non-zero. |
| 23 | // The full set of environment variables from the parent process |
| 24 | // are passed to the container. |
| 25 | // |
| 26 | // Function Scoping: |
| 27 | // Filter applies the function only to Resources to which it is scoped. |
| 28 | // |
| 29 | // Resources are scoped to a function if any of the following are true: |
| 30 | // - the Resource were read from the same directory as the function config |
| 31 | // - the Resource were read from a subdirectory of the function config directory |
| 32 | // - the function config is in a directory named "functions" and |
| 33 | // they were read from a subdirectory of "functions" parent |
| 34 | // - the function config doesn't have a path annotation (considered globally scoped) |
| 35 | // - the Filter has GlobalScope == true |
| 36 | // |
| 37 | // In Scope Examples: |
| 38 | // |
| 39 | // Example 1: deployment.yaml and service.yaml in function.yaml scope |
| 40 | // same directory as the function config directory |
| 41 | // . |
| 42 | // ├── function.yaml |
| 43 | // ├── deployment.yaml |
| 44 | // └── service.yaml |
| 45 | // |
| 46 | // Example 2: apps/deployment.yaml and apps/service.yaml in function.yaml scope |
| 47 | // subdirectory of the function config directory |
| 48 | // . |
| 49 | // ├── function.yaml |
| 50 | // └── apps |
| 51 | // ├── deployment.yaml |
| 52 | // └── service.yaml |
| 53 | // |
| 54 | // Example 3: apps/deployment.yaml and apps/service.yaml in functions/function.yaml scope |
| 55 | // function config is in a directory named "functions" |
| 56 | // . |
| 57 | // ├── functions |
| 58 | // │ └── function.yaml |
| 59 | // └── apps |
| 60 | // ├── deployment.yaml |
| 61 | // └── service.yaml |
| 62 | // |
| 63 | // Out of Scope Examples: |
| 64 | // |
| 65 | // Example 1: apps/deployment.yaml and apps/service.yaml NOT in stuff/function.yaml scope |
| 66 | // . |
| 67 | // ├── stuff |
| 68 | // │ └── function.yaml |
| 69 | // └── apps |
| 70 | // ├── deployment.yaml |
| 71 | // └── service.yaml |
| 72 | // |
| 73 | // Example 2: apps/deployment.yaml and apps/service.yaml NOT in stuff/functions/function.yaml scope |
| 74 | // . |
| 75 | // ├── stuff |
| 76 | // │ └── functions |
| 77 | // │ └── function.yaml |
| 78 | // └── apps |
| 79 | // ├── deployment.yaml |
| 80 | // └── service.yaml |
| 81 | // |
| 82 | // Default Paths: |
| 83 | // Resources emitted by functions will have default path applied as annotations |
| 84 | // if none is present. |
| 85 | // The default path will be the function-dir/ (or parent directory in the case of "functions") |
| 86 | // + function-file-name/ + namespace/ + kind_name.yaml |
| 87 | // |
| 88 | // Example 1: Given a function in fn.yaml that produces a Deployment name foo and a Service named bar |
| 89 | // dir |
| 90 | // └── fn.yaml |
| 91 | // |
| 92 | // Would default newly generated Resources to: |
| 93 | // |
| 94 | // dir |
| 95 | // ├── fn.yaml |
| 96 | // └── fn |
| 97 | // ├── deployment_foo.yaml |
| 98 | // └── service_bar.yaml |
| 99 | // |
| 100 | // Example 2: Given a function in functions/fn.yaml that produces a Deployment name foo and a Service named bar |
| 101 | // dir |
| 102 | // └── fn.yaml |
| 103 | // |
| 104 | // Would default newly generated Resources to: |
| 105 | // |
| 106 | // dir |
| 107 | // ├── functions |
| 108 | // │ └── fn.yaml |
| 109 | // └── fn |
| 110 | // ├── deployment_foo.yaml |
| 111 | // └── service_bar.yaml |
| 112 | // |
| 113 | // Example 3: Given a function in fn.yaml that produces a Deployment name foo, namespace baz and a Service named bar namespace baz |
| 114 | // dir |
| 115 | // └── fn.yaml |
| 116 | // |
| 117 | // Would default newly generated Resources to: |
| 118 | // |
| 119 | // dir |
| 120 | // ├── fn.yaml |
| 121 | // └── fn |
| 122 | // └── baz |
| 123 | // ├── deployment_foo.yaml |
| 124 | // └── service_bar.yaml |
| 125 | type Filter struct { |
| 126 | runtimeutil.ContainerSpec `json:",inline" yaml:",inline"` |
| 127 | |
| 128 | Exec runtimeexec.Filter |
| 129 | |
| 130 | UIDGID string |
| 131 | } |
| 132 | |
| 133 | func (c Filter) String() string { |
| 134 | if c.Exec.DeferFailure { |
| 135 | return fmt.Sprintf("%s deferFailure: %v", c.Image, c.Exec.DeferFailure) |
| 136 | } |
| 137 | return c.Image |
| 138 | } |
| 139 | func (c Filter) GetExit() error { |
| 140 | return c.Exec.GetExit() |
| 141 | } |
| 142 | |
| 143 | func (c *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) { |
| 144 | if err := c.setupExec(); err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | return c.Exec.Filter(nodes) |
| 148 | } |
| 149 | |
| 150 | func (c *Filter) setupExec() error { |
| 151 | // don't init 2x |
| 152 | if c.Exec.Path != "" { |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | if c.Exec.WorkingDir == "" { |
| 157 | wd, err := os.Getwd() |
| 158 | if err != nil { |
| 159 | return errors.Wrap(err) |
| 160 | } |
| 161 | c.Exec.WorkingDir = wd |
| 162 | } |
| 163 | |
| 164 | path, args := c.getCommand() |
| 165 | c.Exec.Path = path |
| 166 | c.Exec.Args = args |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | // getCommand returns the command + args to run to spawn the container |
| 171 | func (c *Filter) getCommand() (string, []string) { |
| 172 | network := runtimeutil.NetworkNameNone |
| 173 | if c.ContainerSpec.Network { |
| 174 | network = runtimeutil.NetworkNameHost |
| 175 | } |
| 176 | // run the container using docker. this is simpler than using the docker |
| 177 | // libraries, and ensures things like auth work the same as if the container |
| 178 | // was run from the cli. |
| 179 | args := []string{"run", |
| 180 | "--rm", // delete the container afterward |
| 181 | "-i", "-a", "STDIN", "-a", "STDOUT", "-a", "STDERR", // attach stdin, stdout, stderr |
| 182 | "--network", string(network), |
| 183 | |
| 184 | // added security options |
| 185 | "--user", c.UIDGID, |
| 186 | "--security-opt=no-new-privileges", // don't allow the user to escalate privileges |
| 187 | // note: don't make fs readonly because things like heredoc rely on writing tmp files |
| 188 | } |
| 189 | |
| 190 | for _, storageMount := range c.StorageMounts { |
| 191 | // convert declarative relative paths to absolute (otherwise docker will throw an error) |
| 192 | if !filepath.IsAbs(storageMount.Src) { |
| 193 | storageMount.Src = filepath.Join(c.Exec.WorkingDir, storageMount.Src) |
| 194 | } |
| 195 | args = append(args, "--mount", storageMount.String()) |
| 196 | } |
| 197 | |
| 198 | args = append(args, runtimeutil.NewContainerEnvFromStringSlice(c.Env).GetDockerFlags()...) |
| 199 | a := append(args, c.Image) //nolint:gocritic |
| 200 | return "docker", a |
| 201 | } |
| 202 | |
| 203 | // NewContainer returns a new container filter |
| 204 | func NewContainer(spec runtimeutil.ContainerSpec, uidgid string) Filter { |
| 205 | f := Filter{ContainerSpec: spec, UIDGID: uidgid} |
| 206 | |
| 207 | return f |
| 208 | } |