| 1 | // Copyright 2019 The Kubernetes Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package framework |
| 5 | |
| 6 | import ( |
| 7 | goerrors "errors" |
| 8 | "os" |
| 9 | |
| 10 | "k8s.io/kube-openapi/pkg/validation/spec" |
| 11 | "sigs.k8s.io/kustomize/kyaml/errors" |
| 12 | "sigs.k8s.io/kustomize/kyaml/kio" |
| 13 | "sigs.k8s.io/kustomize/kyaml/yaml" |
| 14 | ) |
| 15 | |
| 16 | // ResourceList is a Kubernetes list type used as the primary data interchange format |
| 17 | // in the Configuration Functions Specification: |
| 18 | // https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md |
| 19 | // This framework facilitates building functions that receive and emit ResourceLists, |
| 20 | // as required by the specification. |
| 21 | type ResourceList struct { |
| 22 | // Items is the ResourceList.items input and output value. |
| 23 | // |
| 24 | // e.g. given the function input: |
| 25 | // |
| 26 | // kind: ResourceList |
| 27 | // items: |
| 28 | // - kind: Deployment |
| 29 | // ... |
| 30 | // - kind: Service |
| 31 | // ... |
| 32 | // |
| 33 | // Items will be a slice containing the Deployment and Service resources |
| 34 | // Mutating functions will alter this field during processing. |
| 35 | // This field is required. |
| 36 | Items []*yaml.RNode `yaml:"items" json:"items"` |
| 37 | |
| 38 | // FunctionConfig is the ResourceList.functionConfig input value. |
| 39 | // |
| 40 | // e.g. given the input: |
| 41 | // |
| 42 | // kind: ResourceList |
| 43 | // functionConfig: |
| 44 | // kind: Example |
| 45 | // spec: |
| 46 | // foo: var |
| 47 | // |
| 48 | // FunctionConfig will contain the RNodes for the Example: |
| 49 | // kind: Example |
| 50 | // spec: |
| 51 | // foo: var |
| 52 | FunctionConfig *yaml.RNode `yaml:"functionConfig,omitempty" json:"functionConfig,omitempty"` |
| 53 | |
| 54 | // Results is ResourceList.results output value. |
| 55 | // Validating functions can optionally use this field to communicate structured |
| 56 | // validation error data to downstream functions. |
| 57 | Results Results `yaml:"results,omitempty" json:"results,omitempty"` |
| 58 | } |
| 59 | |
| 60 | // ResourceListProcessor is implemented by configuration functions built with this framework |
| 61 | // to conform to the Configuration Functions Specification: |
| 62 | // https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md |
| 63 | // To invoke a processor, pass it to framework.Execute, which will also handle ResourceList IO. |
| 64 | // |
| 65 | // This framework provides several ready-to-use ResourceListProcessors, including |
| 66 | // SimpleProcessor, VersionedAPIProcessor and TemplateProcessor. |
| 67 | // You can also build your own by implementing this interface. |
| 68 | type ResourceListProcessor interface { |
| 69 | Process(rl *ResourceList) error |
| 70 | } |
| 71 | |
| 72 | // ResourceListProcessorFunc converts a compatible function to a ResourceListProcessor. |
| 73 | type ResourceListProcessorFunc func(rl *ResourceList) error |
| 74 | |
| 75 | // Process makes ResourceListProcessorFunc implement the ResourceListProcessor interface. |
| 76 | func (p ResourceListProcessorFunc) Process(rl *ResourceList) error { |
| 77 | return p(rl) |
| 78 | } |
| 79 | |
| 80 | // Defaulter is implemented by APIs to have Default invoked. |
| 81 | // The standard application is to create a type to hold your FunctionConfig data, and |
| 82 | // implement Defaulter on that type. All of the framework's processors will invoke Default() |
| 83 | // on your type after unmarshalling the FunctionConfig data into it. |
| 84 | type Defaulter interface { |
| 85 | Default() error |
| 86 | } |
| 87 | |
| 88 | // Validator is implemented by APIs to have Validate invoked. |
| 89 | // The standard application is to create a type to hold your FunctionConfig data, and |
| 90 | // implement Validator on that type. All of the framework's processors will invoke Validate() |
| 91 | // on your type after unmarshalling the FunctionConfig data into it. |
| 92 | type Validator interface { |
| 93 | Validate() error |
| 94 | } |
| 95 | |
| 96 | // ValidationSchemaProvider is implemented by APIs to have the openapi schema provided by Schema() |
| 97 | // used to validate the input functionConfig before it is parsed into the API's struct. |
| 98 | // Use this with framework.SchemaFromFunctionDefinition to load the schema out of a KRMFunctionDefinition |
| 99 | // or CRD (e.g. one generated with KubeBuilder). |
| 100 | // |
| 101 | // func (t MyType) Schema() (*spec.Schema, error) { |
| 102 | // schema, err := framework.SchemaFromFunctionDefinition(resid.NewGvk("example.com", "v1", "MyType"), MyTypeDef) |
| 103 | // return schema, errors.WrapPrefixf(err, "parsing MyType schema") |
| 104 | // } |
| 105 | type ValidationSchemaProvider interface { |
| 106 | Schema() (*spec.Schema, error) |
| 107 | } |
| 108 | |
| 109 | // Execute is the entrypoint for invoking configuration functions built with this framework |
| 110 | // from code. See framework/command#Build for a Cobra-based command-line equivalent. |
| 111 | // Execute reads a ResourceList from the given source, passes it to a ResourceListProcessor, |
| 112 | // and then writes the result to the target. |
| 113 | // STDIN and STDOUT will be used if no reader or writer respectively is provided. |
| 114 | func Execute(p ResourceListProcessor, rlSource *kio.ByteReadWriter) error { |
| 115 | // Prepare the resource list source |
| 116 | if rlSource == nil { |
| 117 | rlSource = &kio.ByteReadWriter{KeepReaderAnnotations: true} |
| 118 | } |
| 119 | if rlSource.Reader == nil { |
| 120 | rlSource.Reader = os.Stdin |
| 121 | } |
| 122 | if rlSource.Writer == nil { |
| 123 | rlSource.Writer = os.Stdout |
| 124 | } |
| 125 | |
| 126 | // Read the input |
| 127 | rl := ResourceList{} |
| 128 | var err error |
| 129 | if rl.Items, err = rlSource.Read(); err != nil { |
| 130 | return errors.WrapPrefixf(err, "failed to read ResourceList input") |
| 131 | } |
| 132 | rl.FunctionConfig = rlSource.FunctionConfig |
| 133 | |
| 134 | // We store the original |
| 135 | nodeAnnos, err := kio.PreprocessResourcesForInternalAnnotationMigration(rl.Items) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | |
| 140 | retErr := p.Process(&rl) |
| 141 | |
| 142 | // If either the internal annotations for path, index, and id OR the legacy |
| 143 | // annotations for path, index, and id are changed, we have to update the other. |
| 144 | err = kio.ReconcileInternalAnnotations(rl.Items, nodeAnnos) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | // Write the results |
| 150 | // Set the ResourceList.results for validating functions |
| 151 | if len(rl.Results) > 0 { |
| 152 | b, err := yaml.Marshal(rl.Results) |
| 153 | if err != nil { |
| 154 | return errors.Wrap(err) |
| 155 | } |
| 156 | y, err := yaml.Parse(string(b)) |
| 157 | if err != nil { |
| 158 | return errors.Wrap(err) |
| 159 | } |
| 160 | rlSource.Results = y |
| 161 | } |
| 162 | if err := rlSource.Write(rl.Items); err != nil { |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | return retErr |
| 167 | } |
| 168 | |
| 169 | // Filter executes the given kio.Filter and replaces the ResourceList's items with the result. |
| 170 | // This can be used to help implement ResourceListProcessors. See SimpleProcessor for example. |
| 171 | // |
| 172 | // Filters that return a Result as error will store the result in the ResourceList |
| 173 | // and continue processing instead of erroring out. |
| 174 | func (rl *ResourceList) Filter(api kio.Filter) error { |
| 175 | if api == nil { |
| 176 | return errors.Errorf("ResourceList cannot run apply nil filter") |
| 177 | } |
| 178 | var err error |
| 179 | rl.Items, err = api.Filter(rl.Items) |
| 180 | if err != nil { |
| 181 | var r Results |
| 182 | if goerrors.As(err, &r) { |
| 183 | rl.Results = append(rl.Results, r...) |
| 184 | return nil |
| 185 | } |
| 186 | return errors.Wrap(err) |
| 187 | } |
| 188 | return nil |
| 189 | } |