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