| 1 | // Copyright 2021 The Kubernetes Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Package command contains a builder for creating cobra.Commands based on configuration functions |
| 5 | // written using the kyaml function framework. The commands this package generates can be used as |
| 6 | // standalone executables or as part of a configuration management pipeline that complies with the |
| 7 | // Configuration Functions Specification (e.g. Kustomize generators or transformers): |
| 8 | // https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md |
| 9 | // |
| 10 | // Example standalone usage |
| 11 | // |
| 12 | // Function template input: |
| 13 | // |
| 14 | // # config.yaml -- this is the input to the template |
| 15 | // apiVersion: example.com/v1alpha1 |
| 16 | // kind: Example |
| 17 | // Key: a |
| 18 | // Value: b |
| 19 | // |
| 20 | // Additional function inputs: |
| 21 | // |
| 22 | // # patch.yaml -- this will be applied as a patch |
| 23 | // apiVersion: apps/v1 |
| 24 | // kind: Deployment |
| 25 | // metadata: |
| 26 | // name: foo |
| 27 | // namespace: default |
| 28 | // annotations: |
| 29 | // patch-key: patch-value |
| 30 | // |
| 31 | // Manually run the function: |
| 32 | // |
| 33 | // # build the function |
| 34 | // $ go build example-fn/ |
| 35 | // |
| 36 | // # run the function |
| 37 | // $ ./example-fn config.yaml patch.yaml |
| 38 | // |
| 39 | // Go implementation |
| 40 | // |
| 41 | // // example-fn/main.go |
| 42 | // func main() { |
| 43 | // // Define the template used to generate resources |
| 44 | // p := framework.TemplateProcessor{ |
| 45 | // MergeResources: true, // apply inputs as patches to the template output |
| 46 | // TemplateData: new(struct { |
| 47 | // Key string `json:"key" yaml:"key"` |
| 48 | // Value string `json:"value" yaml:"value"` |
| 49 | // }), |
| 50 | // ResourceTemplates: []framework.ResourceTemplate{{ |
| 51 | // Templates: framework.StringTemplates(` |
| 52 | // apiVersion: apps/v1 |
| 53 | // kind: Deployment |
| 54 | // metadata: |
| 55 | // name: foo |
| 56 | // namespace: default |
| 57 | // annotations: |
| 58 | // {{ .Key }}: {{ .Value }} |
| 59 | // `)}}, |
| 60 | // } |
| 61 | // |
| 62 | // // Run the command |
| 63 | // if err := command.Build(p, command.StandaloneEnabled, true).Execute(); err != nil { |
| 64 | // fmt.Fprintf(cmd.ErrOrStderr(), "%v\n", err) |
| 65 | // os.Exit(1) |
| 66 | // } |
| 67 | // } |
| 68 | // |
| 69 | // Example function implementation using command.Build with flag input |
| 70 | // |
| 71 | // func main() { |
| 72 | // var value string |
| 73 | // fn := func(rl *framework.ResourceList) error { |
| 74 | // for i := range rl.Items { |
| 75 | // // set the annotation on each resource item |
| 76 | // if err := rl.Items[i].PipeE(yaml.SetAnnotation("value", value)); err != nil { |
| 77 | // return err |
| 78 | // } |
| 79 | // } |
| 80 | // return nil |
| 81 | // } |
| 82 | // cmd := command.Build(framework.ResourceListProcessorFunc(fn), command.StandaloneEnabled, false) |
| 83 | // cmd.Flags().StringVar(&value, "value", "", "annotation value") |
| 84 | // |
| 85 | // if err := cmd.Execute(); err != nil { |
| 86 | // fmt.Println(err) |
| 87 | // os.Exit(1) |
| 88 | // } |
| 89 | // } |
| 90 | package command |