| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/json" |
| 9 |
"fmt" |
| 10 |
"reflect" |
| 11 |
|
| 12 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 14 |
"sigs.k8s.io/yaml" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
KustomizationVersion = "kustomize.config.k8s.io/v1beta1" |
| 19 |
KustomizationKind = "Kustomization" |
| 20 |
ComponentVersion = "kustomize.config.k8s.io/v1alpha1" |
| 21 |
ComponentKind = "Component" |
| 22 |
MetadataNamespacePath = "metadata/namespace" |
| 23 |
MetadataNamespaceApiVersion = "v1" |
| 24 |
MetadataNamePath = "metadata/name" |
| 25 |
NamespaceKind = "Namespace" |
| 26 |
|
| 27 |
OriginAnnotations = "originAnnotations" |
| 28 |
TransformerAnnotations = "transformerAnnotations" |
| 29 |
ManagedByLabelOption = "managedByLabel" |
| 30 |
) |
| 31 |
|
| 32 |
var BuildMetadataOptions = []string{OriginAnnotations, TransformerAnnotations, ManagedByLabelOption} |
| 33 |
|
| 34 |
// Kustomization holds the information needed to generate customized k8s api resources. |
| 35 |
type Kustomization struct { |
| 36 |
TypeMeta `json:",inline" yaml:",inline"` |
| 37 |
|
| 38 |
// MetaData is a pointer to avoid marshalling empty struct |
| 39 |
MetaData *ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"` |
| 40 |
|
| 41 |
// OpenAPI contains information about what kubernetes schema to use. |
| 42 |
OpenAPI map[string]string `json:"openapi,omitempty" yaml:"openapi,omitempty"` |
| 43 |
|
| 44 |
// |
| 45 |
// Operators - what kustomize can do. |
| 46 |
// |
| 47 |
|
| 48 |
// NamePrefix will prefix the names of all resources mentioned in the kustomization |
| 49 |
// file including generated configmaps and secrets. |
| 50 |
NamePrefix string `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"` |
| 51 |
|
| 52 |
// NameSuffix will suffix the names of all resources mentioned in the kustomization |
| 53 |
// file including generated configmaps and secrets. |
| 54 |
NameSuffix string `json:"nameSuffix,omitempty" yaml:"nameSuffix,omitempty"` |
| 55 |
|
| 56 |
// Namespace to add to all objects. |
| 57 |
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` |
| 58 |
|
| 59 |
// Deprecated: Use the Labels field instead, which provides a superset of the functionality of CommonLabels. |
| 60 |
// CommonLabels to add to all objects and selectors. |
| 61 |
CommonLabels map[string]string `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"` |
| 62 |
|
| 63 |
// Labels to add to all objects but not selectors. |
| 64 |
Labels []Label `json:"labels,omitempty" yaml:"labels,omitempty"` |
| 65 |
|
| 66 |
// CommonAnnotations to add to all objects. |
| 67 |
CommonAnnotations map[string]string `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"` |
| 68 |
|
| 69 |
// Deprecated: Use the Patches field instead, which provides a superset of the functionality of PatchesStrategicMerge. |
| 70 |
// PatchesStrategicMerge specifies the relative path to a file |
| 71 |
// containing a strategic merge patch. Format documented at |
| 72 |
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md |
| 73 |
// URLs and globs are not supported. |
| 74 |
PatchesStrategicMerge []PatchStrategicMerge `json:"patchesStrategicMerge,omitempty" yaml:"patchesStrategicMerge,omitempty"` |
| 75 |
|
| 76 |
// Deprecated: Use the Patches field instead, which provides a superset of the functionality of JSONPatches. |
| 77 |
// JSONPatches is a list of JSONPatch for applying JSON patch. |
| 78 |
// Format documented at https://tools.ietf.org/html/rfc6902 |
| 79 |
// and http://jsonpatch.com |
| 80 |
PatchesJson6902 []Patch `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"` |
| 81 |
|
| 82 |
// Patches is a list of patches, where each one can be either a |
| 83 |
// Strategic Merge Patch or a JSON patch. |
| 84 |
// Each patch can be applied to multiple target objects. |
| 85 |
Patches []Patch `json:"patches,omitempty" yaml:"patches,omitempty"` |
| 86 |
|
| 87 |
// Images is a list of (image name, new name, new tag or digest) |
| 88 |
// for changing image names, tags or digests. This can also be achieved with a |
| 89 |
// patch, but this operator is simpler to specify. |
| 90 |
Images []Image `json:"images,omitempty" yaml:"images,omitempty"` |
| 91 |
|
| 92 |
// Deprecated: Use the Images field instead. |
| 93 |
ImageTags []Image `json:"imageTags,omitempty" yaml:"imageTags,omitempty"` |
| 94 |
|
| 95 |
// Replacements is a list of replacements, which will copy nodes from a |
| 96 |
// specified source to N specified targets. |
| 97 |
Replacements []ReplacementField `json:"replacements,omitempty" yaml:"replacements,omitempty"` |
| 98 |
|
| 99 |
// Replicas is a list of {resourcename, count} that allows for simpler replica |
| 100 |
// specification. This can also be done with a patch. |
| 101 |
Replicas []Replica `json:"replicas,omitempty" yaml:"replicas,omitempty"` |
| 102 |
|
| 103 |
// Deprecated: Vars will be removed in future release. Migrate to Replacements instead. |
| 104 |
// Vars allow things modified by kustomize to be injected into a |
| 105 |
// kubernetes object specification. A var is a name (e.g. FOO) associated |
| 106 |
// with a field in a specific resource instance. The field must |
| 107 |
// contain a value of type string/bool/int/float, and defaults to the name field |
| 108 |
// of the instance. Any appearance of "$(FOO)" in the object |
| 109 |
// spec will be replaced at kustomize build time, after the final |
| 110 |
// value of the specified field has been determined. |
| 111 |
Vars []Var `json:"vars,omitempty" yaml:"vars,omitempty"` |
| 112 |
|
| 113 |
// SortOptions change the order that kustomize outputs resources. |
| 114 |
SortOptions *SortOptions `json:"sortOptions,omitempty" yaml:"sortOptions,omitempty"` |
| 115 |
|
| 116 |
// |
| 117 |
// Operands - what kustomize operates on. |
| 118 |
// |
| 119 |
|
| 120 |
// Resources specifies relative paths to files holding YAML representations |
| 121 |
// of kubernetes API objects, or specifications of other kustomizations |
| 122 |
// via relative paths, absolute paths, or URLs. |
| 123 |
Resources []string `json:"resources,omitempty" yaml:"resources,omitempty"` |
| 124 |
|
| 125 |
// Components specifies relative paths to specifications of other Components |
| 126 |
// via relative paths, absolute paths, or URLs. |
| 127 |
Components []string `json:"components,omitempty" yaml:"components,omitempty"` |
| 128 |
|
| 129 |
// Crds specifies relative paths to Custom Resource Definition files. |
| 130 |
// This allows custom resources to be recognized as operands, making |
| 131 |
// it possible to add them to the Resources list. |
| 132 |
// CRDs themselves are not modified. |
| 133 |
Crds []string `json:"crds,omitempty" yaml:"crds,omitempty"` |
| 134 |
|
| 135 |
// Deprecated: Anything that would have been specified here should be specified in the Resources field instead. |
| 136 |
Bases []string `json:"bases,omitempty" yaml:"bases,omitempty"` |
| 137 |
|
| 138 |
// |
| 139 |
// Generators (operators that create operands) |
| 140 |
// |
| 141 |
|
| 142 |
// ConfigMapGenerator is a list of configmaps to generate from |
| 143 |
// local data (one configMap per list item). |
| 144 |
// The resulting resource is a normal operand, subject to |
| 145 |
// name prefixing, patching, etc. By default, the name of |
| 146 |
// the map will have a suffix hash generated from its contents. |
| 147 |
ConfigMapGenerator []ConfigMapArgs `json:"configMapGenerator,omitempty" yaml:"configMapGenerator,omitempty"` |
| 148 |
|
| 149 |
// SecretGenerator is a list of secrets to generate from |
| 150 |
// local data (one secret per list item). |
| 151 |
// The resulting resource is a normal operand, subject to |
| 152 |
// name prefixing, patching, etc. By default, the name of |
| 153 |
// the map will have a suffix hash generated from its contents. |
| 154 |
SecretGenerator []SecretArgs `json:"secretGenerator,omitempty" yaml:"secretGenerator,omitempty"` |
| 155 |
|
| 156 |
// HelmGlobals contains helm configuration that isn't chart specific. |
| 157 |
HelmGlobals *HelmGlobals `json:"helmGlobals,omitempty" yaml:"helmGlobals,omitempty"` |
| 158 |
|
| 159 |
// HelmCharts is a list of helm chart configuration instances. |
| 160 |
HelmCharts []HelmChart `json:"helmCharts,omitempty" yaml:"helmCharts,omitempty"` |
| 161 |
|
| 162 |
// HelmChartInflationGenerator is a list of helm chart configurations. |
| 163 |
// Deprecated. Auto-converted to HelmGlobals and HelmCharts. |
| 164 |
HelmChartInflationGenerator []HelmChartArgs `json:"helmChartInflationGenerator,omitempty" yaml:"helmChartInflationGenerator,omitempty"` |
| 165 |
|
| 166 |
// GeneratorOptions modify behavior of all ConfigMap and Secret generators. |
| 167 |
GeneratorOptions *GeneratorOptions `json:"generatorOptions,omitempty" yaml:"generatorOptions,omitempty"` |
| 168 |
|
| 169 |
// Configurations is a list of transformer configuration files |
| 170 |
Configurations []string `json:"configurations,omitempty" yaml:"configurations,omitempty"` |
| 171 |
|
| 172 |
// Generators is a list of files containing custom generators |
| 173 |
Generators []string `json:"generators,omitempty" yaml:"generators,omitempty"` |
| 174 |
|
| 175 |
// Transformers is a list of files containing transformers |
| 176 |
Transformers []string `json:"transformers,omitempty" yaml:"transformers,omitempty"` |
| 177 |
|
| 178 |
// Validators is a list of files containing validators |
| 179 |
Validators []string `json:"validators,omitempty" yaml:"validators,omitempty"` |
| 180 |
|
| 181 |
// BuildMetadata is a list of strings used to toggle different build options |
| 182 |
BuildMetadata []string `json:"buildMetadata,omitempty" yaml:"buildMetadata,omitempty"` |
| 183 |
} |
| 184 |
|
| 185 |
const ( |
| 186 |
deprecatedWarningToRunEditFix = "Run 'kustomize edit fix' to update your Kustomization automatically." |
| 187 |
deprecatedWarningToRunEditFixExperimential = "[EXPERIMENTAL] Run 'kustomize edit fix' to update your Kustomization automatically." |
| 188 |
deprecatedBaseWarningMessage = "# Warning: 'bases' is deprecated. Please use 'resources' instead." + " " + deprecatedWarningToRunEditFix |
| 189 |
deprecatedImageTagsWarningMessage = "# Warning: 'imageTags' is deprecated. Please use 'images' instead." + " " + deprecatedWarningToRunEditFix |
| 190 |
deprecatedPatchesJson6902Message = "# Warning: 'patchesJson6902' is deprecated. Please use 'patches' instead." + " " + deprecatedWarningToRunEditFix |
| 191 |
deprecatedPatchesStrategicMergeMessage = "# Warning: 'patchesStrategicMerge' is deprecated. Please use 'patches' instead." + " " + deprecatedWarningToRunEditFix |
| 192 |
deprecatedVarsMessage = "# Warning: 'vars' is deprecated. Please use 'replacements' instead." + " " + deprecatedWarningToRunEditFixExperimential |
| 193 |
deprecatedCommonLabelsWarningMessage = "# Warning: 'commonLabels' is deprecated. Please use 'labels' instead." + " " + deprecatedWarningToRunEditFix |
| 194 |
) |
| 195 |
|
| 196 |
// CheckDeprecatedFields check deprecated field is used or not. |
| 197 |
func (k *Kustomization) CheckDeprecatedFields() *[]string { |
| 198 |
var warningMessages []string |
| 199 |
if k.Bases != nil { |
| 200 |
warningMessages = append(warningMessages, deprecatedBaseWarningMessage) |
| 201 |
} |
| 202 |
if k.CommonLabels != nil { |
| 203 |
warningMessages = append(warningMessages, deprecatedCommonLabelsWarningMessage) |
| 204 |
} |
| 205 |
if k.ImageTags != nil { |
| 206 |
warningMessages = append(warningMessages, deprecatedImageTagsWarningMessage) |
| 207 |
} |
| 208 |
if k.PatchesJson6902 != nil { |
| 209 |
warningMessages = append(warningMessages, deprecatedPatchesJson6902Message) |
| 210 |
} |
| 211 |
if k.PatchesStrategicMerge != nil { |
| 212 |
warningMessages = append(warningMessages, deprecatedPatchesStrategicMergeMessage) |
| 213 |
} |
| 214 |
if k.Vars != nil { |
| 215 |
warningMessages = append(warningMessages, deprecatedVarsMessage) |
| 216 |
} |
| 217 |
return &warningMessages |
| 218 |
} |
| 219 |
|
| 220 |
// FixKustomization fixes things |
| 221 |
// like empty fields that should not be empty, or |
| 222 |
// moving content of deprecated fields to newer |
| 223 |
// fields. |
| 224 |
func (k *Kustomization) FixKustomization() { |
| 225 |
if k.Kind == "" { |
| 226 |
k.Kind = KustomizationKind |
| 227 |
} |
| 228 |
if k.APIVersion == "" { |
| 229 |
if k.Kind == ComponentKind { |
| 230 |
k.APIVersion = ComponentVersion |
| 231 |
} else { |
| 232 |
k.APIVersion = KustomizationVersion |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// 'bases' field was deprecated in favor of the 'resources' field. |
| 237 |
k.Resources = append(k.Resources, k.Bases...) |
| 238 |
k.Bases = nil |
| 239 |
|
| 240 |
// 'imageTags' field was deprecated in favor of the 'images' field. |
| 241 |
k.Images = append(k.Images, k.ImageTags...) |
| 242 |
k.ImageTags = nil |
| 243 |
|
| 244 |
for i, g := range k.ConfigMapGenerator { |
| 245 |
if g.EnvSource != "" { |
| 246 |
k.ConfigMapGenerator[i].EnvSources = |
| 247 |
append(g.EnvSources, g.EnvSource) //nolint:gocritic |
| 248 |
k.ConfigMapGenerator[i].EnvSource = "" |
| 249 |
} |
| 250 |
} |
| 251 |
for i, g := range k.SecretGenerator { |
| 252 |
if g.EnvSource != "" { |
| 253 |
k.SecretGenerator[i].EnvSources = |
| 254 |
append(g.EnvSources, g.EnvSource) //nolint:gocritic |
| 255 |
k.SecretGenerator[i].EnvSource = "" |
| 256 |
} |
| 257 |
} |
| 258 |
charts, globals := SplitHelmParameters(k.HelmChartInflationGenerator) |
| 259 |
if k.HelmGlobals == nil { |
| 260 |
if globals.ChartHome != "" || globals.ConfigHome != "" { |
| 261 |
k.HelmGlobals = &globals |
| 262 |
} |
| 263 |
} |
| 264 |
k.HelmCharts = append(k.HelmCharts, charts...) |
| 265 |
// Wipe it for the fix command. |
| 266 |
k.HelmChartInflationGenerator = nil |
| 267 |
} |
| 268 |
|
| 269 |
// FixKustomizationPreMarshalling fixes things |
| 270 |
// that should occur after the kustomization file |
| 271 |
// has been processed. |
| 272 |
func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem) error { |
| 273 |
// PatchesJson6902 should be under the Patches field. |
| 274 |
k.Patches = append(k.Patches, k.PatchesJson6902...) |
| 275 |
k.PatchesJson6902 = nil |
| 276 |
|
| 277 |
if k.PatchesStrategicMerge != nil { |
| 278 |
for _, patchStrategicMerge := range k.PatchesStrategicMerge { |
| 279 |
// check this patch is file path select. |
| 280 |
if _, err := fSys.ReadFile(string(patchStrategicMerge)); err == nil { |
| 281 |
// path patch |
| 282 |
k.Patches = append(k.Patches, Patch{Path: string(patchStrategicMerge)}) |
| 283 |
} else { |
| 284 |
// inline string patch |
| 285 |
k.Patches = append(k.Patches, Patch{Patch: string(patchStrategicMerge)}) |
| 286 |
} |
| 287 |
} |
| 288 |
k.PatchesStrategicMerge = nil |
| 289 |
} |
| 290 |
|
| 291 |
// this fix is not in FixKustomizationPostUnmarshalling because |
| 292 |
// it will break some commands like `create` and `add`. those |
| 293 |
// commands depend on 'commonLabels' field |
| 294 |
if cl := labelFromCommonLabels(k.CommonLabels); cl != nil { |
| 295 |
// check conflicts between commonLabels and labels |
| 296 |
for _, l := range k.Labels { |
| 297 |
for k := range l.Pairs { |
| 298 |
if _, exist := cl.Pairs[k]; exist { |
| 299 |
return fmt.Errorf("label name '%s' exists in both commonLabels and labels", k) |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
k.Labels = append(k.Labels, *cl) |
| 304 |
k.CommonLabels = nil |
| 305 |
} |
| 306 |
|
| 307 |
return nil |
| 308 |
} |
| 309 |
|
| 310 |
func (k *Kustomization) CheckEmpty() error { |
| 311 |
// generate empty Kustomization |
| 312 |
emptyKustomization := &Kustomization{} |
| 313 |
|
| 314 |
// k.TypeMeta is metadata. It Isn't related to whether empty or not. |
| 315 |
emptyKustomization.TypeMeta = k.TypeMeta |
| 316 |
|
| 317 |
if reflect.DeepEqual(k, emptyKustomization) { |
| 318 |
return fmt.Errorf("kustomization.yaml is empty") |
| 319 |
} |
| 320 |
|
| 321 |
return nil |
| 322 |
} |
| 323 |
|
| 324 |
func (k *Kustomization) EnforceFields() []string { |
| 325 |
var errs []string |
| 326 |
if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind { |
| 327 |
errs = append(errs, "kind should be "+KustomizationKind+" or "+ComponentKind) |
| 328 |
} |
| 329 |
requiredVersion := KustomizationVersion |
| 330 |
if k.Kind == ComponentKind { |
| 331 |
requiredVersion = ComponentVersion |
| 332 |
} |
| 333 |
if k.APIVersion != "" && k.APIVersion != requiredVersion { |
| 334 |
errs = append(errs, "apiVersion for "+k.Kind+" should be "+requiredVersion) |
| 335 |
} |
| 336 |
return errs |
| 337 |
} |
| 338 |
|
| 339 |
// Unmarshal replace k with the content in YAML input y |
| 340 |
func (k *Kustomization) Unmarshal(y []byte) error { |
| 341 |
// TODO: switch to strict decoding to catch duplicate keys. |
| 342 |
// We can't do so until there is a yaml decoder that supports anchors AND case-insensitive keys. |
| 343 |
// See https://github.com/kubernetes-sigs/kustomize/issues/5061 |
| 344 |
j, err := yaml.YAMLToJSON(y) |
| 345 |
if err != nil { |
| 346 |
return errors.WrapPrefixf(err, "invalid Kustomization") |
| 347 |
} |
| 348 |
dec := json.NewDecoder(bytes.NewReader(j)) |
| 349 |
dec.DisallowUnknownFields() |
| 350 |
var nk Kustomization |
| 351 |
err = dec.Decode(&nk) |
| 352 |
if err != nil { |
| 353 |
return errors.WrapPrefixf(err, "invalid Kustomization") |
| 354 |
} |
| 355 |
*k = nk |
| 356 |
return nil |
| 357 |
} |