| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types |
| 5 |
|
| 6 |
// GeneratorOptions modify behavior of all ConfigMap and Secret generators. |
| 7 |
type GeneratorOptions struct { |
| 8 |
// Labels to add to all generated resources. |
| 9 |
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` |
| 10 |
|
| 11 |
// Annotations to add to all generated resources. |
| 12 |
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"` |
| 13 |
|
| 14 |
// DisableNameSuffixHash if true disables the default behavior of adding a |
| 15 |
// suffix to the names of generated resources that is a hash of the |
| 16 |
// resource contents. |
| 17 |
DisableNameSuffixHash bool `json:"disableNameSuffixHash,omitempty" yaml:"disableNameSuffixHash,omitempty"` |
| 18 |
|
| 19 |
// Immutable if true add to all generated resources. |
| 20 |
Immutable bool `json:"immutable,omitempty" yaml:"immutable,omitempty"` |
| 21 |
} |
| 22 |
|
| 23 |
// MergeGlobalOptionsIntoLocal merges two instances of GeneratorOptions. |
| 24 |
// Values in the first 'local' argument cannot be overridden by the second |
| 25 |
// 'global' argument, except in the case of booleans. |
| 26 |
// |
| 27 |
// With booleans, there's no way to distinguish an 'intentional' |
| 28 |
// false from 'default' false. So the rule is, if the global value |
| 29 |
// of the value of a boolean is true, i.e. disable, it trumps the |
| 30 |
// local value. If the global value is false, then the local value is |
| 31 |
// respected. Bottom line: a local false cannot override a global true. |
| 32 |
// |
| 33 |
// boolean fields are always a bad idea; should always use enums instead. |
| 34 |
func MergeGlobalOptionsIntoLocal( |
| 35 |
localOpts *GeneratorOptions, |
| 36 |
globalOpts *GeneratorOptions) *GeneratorOptions { |
| 37 |
if globalOpts == nil { |
| 38 |
return localOpts |
| 39 |
} |
| 40 |
if localOpts == nil { |
| 41 |
localOpts = &GeneratorOptions{} |
| 42 |
} |
| 43 |
overrideMap(&localOpts.Labels, globalOpts.Labels) |
| 44 |
overrideMap(&localOpts.Annotations, globalOpts.Annotations) |
| 45 |
if globalOpts.DisableNameSuffixHash { |
| 46 |
localOpts.DisableNameSuffixHash = true |
| 47 |
} |
| 48 |
if globalOpts.Immutable { |
| 49 |
localOpts.Immutable = true |
| 50 |
} |
| 51 |
return localOpts |
| 52 |
} |
| 53 |
|
| 54 |
func overrideMap(localMap *map[string]string, globalMap map[string]string) { |
| 55 |
if *localMap == nil { |
| 56 |
if globalMap != nil { |
| 57 |
*localMap = CopyMap(globalMap) |
| 58 |
} |
| 59 |
return |
| 60 |
} |
| 61 |
for k, v := range globalMap { |
| 62 |
_, ok := (*localMap)[k] |
| 63 |
if !ok { |
| 64 |
(*localMap)[k] = v |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// CopyMap copies a map. |
| 70 |
func CopyMap(in map[string]string) map[string]string { |
| 71 |
out := make(map[string]string) |
| 72 |
for k, v := range in { |
| 73 |
out[k] = v |
| 74 |
} |
| 75 |
return out |
| 76 |
} |