1/* 2Copyright 2017 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17// Package types holds struct definitions that should find a better home. 18package types 19 20import ( 21 "regexp" 22 23 "sigs.k8s.io/kustomize/pkg/image" 24 "sigs.k8s.io/kustomize/pkg/patch" 25) 26 27const ( 28 KustomizationVersion = "kustomize.config.k8s.io/v1beta1" 29 KustomizationKind = "Kustomization" 30) 31 32// TypeMeta copies apimachinery/pkg/apis/meta/v1.TypeMeta 33type TypeMeta struct { 34 // Kind copies apimachinery/pkg/apis/meta/v1.Typemeta.Kind 35 Kind string `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"` 36 37 // APIVersion copies apimachinery/pkg/apis/meta/v1.Typemeta.APIVersion 38 APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,2,opt,name=apiVersion"` 39} 40 41// Kustomization holds the information needed to generate customized k8s api resources. 42type Kustomization struct { 43 TypeMeta `json:",inline" yaml:",inline"` 44 45 // 46 // Operators - what kustomize can do. 47 // 48 49 // NamePrefix will prefix the names of all resources mentioned in the kustomization 50 // file including generated configmaps and secrets. 51 NamePrefix string `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"` 52 53 // NameSuffix will suffix the names of all resources mentioned in the kustomization 54 // file including generated configmaps and secrets. 55 NameSuffix string `json:"nameSuffix,omitempty" yaml:"nameSuffix,omitempty"` 56 57 // Namespace to add to all objects. 58 Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` 59 60 // CommonLabels to add to all objects and selectors. 61 CommonLabels map[string]string `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"` 62 63 // CommonAnnotations to add to all objects. 64 CommonAnnotations map[string]string `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"` 65 66 // PatchesStrategicMerge specifies the relative path to a file 67 // containing a strategic merge patch. Format documented at 68 // https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md 69 // URLs and globs are not supported. 70 PatchesStrategicMerge []patch.StrategicMerge `json:"patchesStrategicMerge,omitempty" yaml:"patchesStrategicMerge,omitempty"` 71 72 // JSONPatches is a list of JSONPatch for applying JSON patch. 73 // Format documented at https://tools.ietf.org/html/rfc6902 74 // and http://jsonpatch.com 75 PatchesJson6902 []patch.Json6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"` 76 77 // Images is a list of (image name, new name, new tag or digest) 78 // for changing image names, tags or digests. This can also be achieved with a 79 // patch, but this operator is simpler to specify. 80 Images []image.Image `json:"images,omitempty" yaml:"images,omitempty"` 81 82 // Vars allow things modified by kustomize to be injected into a 83 // container specification. A var is a name (e.g. FOO) associated 84 // with a field in a specific resource instance. The field must 85 // contain a value of type string, and defaults to the name field 86 // of the instance. Any appearance of "$(FOO)" in the container 87 // spec will be replaced at kustomize build time, after the final 88 // value of the specified field has been determined. 89 Vars []Var `json:"vars,omitempty" yaml:"vars,omitempty"` 90 91 // 92 // Operands - what kustomize operates on. 93 // 94 95 // Resources specifies relative paths to files holding YAML representations 96 // of kubernetes API objects. URLs and globs not supported. 97 Resources []string `json:"resources,omitempty" yaml:"resources,omitempty"` 98 99 // Crds specifies relative paths to Custom Resource Definition files. 100 // This allows custom resources to be recognized as operands, making 101 // it possible to add them to the Resources list. 102 // CRDs themselves are not modified. 103 Crds []string `json:"crds,omitempty" yaml:"crds,omitempty"` 104 105 // Bases are relative paths or github repository URLs specifying a 106 // directory containing a kustomization.yaml file. 107 // URL format: https://github.com/hashicorp/go-getter#url-format 108 Bases []string `json:"bases,omitempty" yaml:"bases,omitempty"` 109 110 // 111 // Generators (operators that create operands) 112 // 113 114 // ConfigMapGenerator is a list of configmaps to generate from 115 // local data (one configMap per list item). 116 // The resulting resource is a normal operand, subject to 117 // name prefixing, patching, etc. By default, the name of 118 // the map will have a suffix hash generated from its contents. 119 ConfigMapGenerator []ConfigMapArgs `json:"configMapGenerator,omitempty" yaml:"configMapGenerator,omitempty"` 120 121 // SecretGenerator is a list of secrets to generate from 122 // local data (one secret per list item). 123 // The resulting resource is a normal operand, subject to 124 // name prefixing, patching, etc. By default, the name of 125 // the map will have a suffix hash generated from its contents. 126 SecretGenerator []SecretArgs `json:"secretGenerator,omitempty" yaml:"secretGenerator,omitempty"` 127 128 // GeneratorOptions modify behavior of all ConfigMap and Secret generators. 129 GeneratorOptions *GeneratorOptions `json:"generatorOptions,omitempty" yaml:"generatorOptions,omitempty"` 130 131 // Configurations is a list of transformer configuration files 132 Configurations []string `json:"configurations,omitempty" yaml:"configurations,omitempty"` 133} 134 135// DealWithMissingFields fills the missing fields 136func (k *Kustomization) DealWithMissingFields() []string { 137 var msgs []string 138 if k.APIVersion == "" { 139 k.APIVersion = KustomizationVersion 140 msgs = append(msgs, "Fixed the missing field by adding apiVersion: "+KustomizationVersion) 141 } 142 if k.Kind == "" { 143 k.Kind = KustomizationKind 144 msgs = append(msgs, "Fixed the missing field by adding kind: "+KustomizationKind) 145 } 146 return msgs 147} 148 149func (k *Kustomization) EnforceFields() []string { 150 var errs []string 151 if k.APIVersion != "" && k.APIVersion != KustomizationVersion { 152 errs = append(errs, "apiVersion should be "+KustomizationVersion) 153 } 154 if k.Kind != "" && k.Kind != KustomizationKind { 155 errs = append(errs, "kind should be "+KustomizationKind) 156 } 157 return errs 158} 159 160// DealWithDeprecatedFields should be called immediately after 161// loading from storage. 162func DealWithDeprecatedFields(data []byte) []byte { 163 deprecateFieldsMap := map[string]string{ 164 "patches:": "patchesStrategicMerge:", 165 "imageTags:": "images:", 166 } 167 for oldname, newname := range deprecateFieldsMap { 168 pattern := regexp.MustCompile(oldname) 169 data = pattern.ReplaceAll(data, []byte(newname)) 170 } 171 return data 172} 173 174// GeneratorArgs contains arguments common to generators. 175type GeneratorArgs struct { 176 // Namespace for the configmap, optional 177 Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` 178 179 // Name - actually the partial name - of the generated resource. 180 // The full name ends up being something like 181 // NamePrefix + this.Name + hash(content of generated resource). 182 Name string `json:"name,omitempty" yaml:"name,omitempty"` 183 184 // Behavior of generated resource, must be one of: 185 // 'create': create a new one 186 // 'replace': replace the existing one 187 // 'merge': merge with the existing one 188 Behavior string `json:"behavior,omitempty" yaml:"behavior,omitempty"` 189 190 // DataSources for the generator. 191 DataSources `json:",inline,omitempty" yaml:",inline,omitempty"` 192} 193 194// ConfigMapArgs contains the metadata of how to generate a configmap. 195type ConfigMapArgs struct { 196 // GeneratorArgs for the configmap. 197 GeneratorArgs `json:",inline,omitempty" yaml:",inline,omitempty"` 198} 199 200// SecretArgs contains the metadata of how to generate a secret. 201type SecretArgs struct { 202 // GeneratorArgs for the secret. 203 GeneratorArgs `json:",inline,omitempty" yaml:",inline,omitempty"` 204 205 // Type of the secret. 206 // 207 // This is the same field as the secret type field in v1/Secret: 208 // It can be "Opaque" (default), or "kubernetes.io/tls". 209 // 210 // If type is "kubernetes.io/tls", then "literals" or "files" must have exactly two 211 // keys: "tls.key" and "tls.crt" 212 Type string `json:"type,omitempty" yaml:"type,omitempty"` 213} 214 215// DataSources contains some generic sources for configmaps. 216type DataSources struct { 217 // LiteralSources is a list of literal sources. 218 // Each literal source should be a key and literal value, 219 // e.g. `somekey=somevalue` 220 // It will be similar to kubectl create configmap|secret --from-literal 221 LiteralSources []string `json:"literals,omitempty" yaml:"literals,omitempty"` 222 223 // FileSources is a list of file sources. 224 // Each file source can be specified using its file path, in which case file 225 // basename will be used as configmap key, or optionally with a key and file 226 // path, in which case the given key will be used. 227 // Specifying a directory will iterate each named file in the directory 228 // whose basename is a valid configmap key. 229 // It will be similar to kubectl create configmap|secret --from-file 230 FileSources []string `json:"files,omitempty" yaml:"files,omitempty"` 231 232 // EnvSource format should be a path to a file to read lines of key=val 233 // pairs to create a configmap. 234 // i.e. a Docker .env file or a .ini file. 235 EnvSource string `json:"env,omitempty" yaml:"env,omitempty"` 236} 237 238// GeneratorOptions modify behavior of all ConfigMap and Secret generators. 239type GeneratorOptions struct { 240 // Labels to add to all generated resources. 241 Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` 242 243 // Annotations to add to all generated resources. 244 Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"` 245 246 // DisableNameSuffixHash if true disables the default behavior of adding a 247 // suffix to the names of generated resources that is a hash of the 248 // resource contents. 249 DisableNameSuffixHash bool `json:"disableNameSuffixHash,omitempty" yaml:"disableNameSuffixHash,omitempty"` 250} 251