| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package yaml |
| 5 |
|
| 6 |
import ( |
| 7 |
yaml "go.yaml.in/yaml/v3" |
| 8 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type AnnotationClearer struct { |
| 14 |
Kind string `yaml:"kind,omitempty"` |
| 15 |
Key string `yaml:"key,omitempty"` |
| 16 |
} |
| 17 |
|
| 18 |
func (c AnnotationClearer) Filter(rn *RNode) (*RNode, error) { |
| 19 |
return rn.Pipe( |
| 20 |
PathGetter{Path: []string{MetadataField, AnnotationsField}}, |
| 21 |
FieldClearer{Name: c.Key}) |
| 22 |
} |
| 23 |
|
| 24 |
func ClearAnnotation(key string) AnnotationClearer { |
| 25 |
return AnnotationClearer{Key: key} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
func ClearEmptyAnnotations(rn *RNode) error { |
| 31 |
_, err := rn.Pipe(Lookup(MetadataField), FieldClearer{ |
| 32 |
Name: AnnotationsField, IfEmpty: true}) |
| 33 |
if err != nil { |
| 34 |
return errors.Wrap(err) |
| 35 |
} |
| 36 |
_, err = rn.Pipe(FieldClearer{Name: MetadataField, IfEmpty: true}) |
| 37 |
if err != nil { |
| 38 |
return errors.Wrap(err) |
| 39 |
} |
| 40 |
return nil |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
type k8sMetaSetter struct { |
| 46 |
Key string `yaml:"key,omitempty"` |
| 47 |
Value string `yaml:"value,omitempty"` |
| 48 |
} |
| 49 |
|
| 50 |
func (s k8sMetaSetter) Filter(rn *RNode) (*RNode, error) { |
| 51 |
_, err := rn.Pipe( |
| 52 |
PathGetter{Path: []string{MetadataField}, Create: yaml.MappingNode}, |
| 53 |
FieldSetter{Name: s.Key, Value: NewStringRNode(s.Value)}) |
| 54 |
return rn, err |
| 55 |
} |
| 56 |
|
| 57 |
func SetK8sName(value string) k8sMetaSetter { |
| 58 |
return k8sMetaSetter{Key: NameField, Value: value} |
| 59 |
} |
| 60 |
|
| 61 |
func SetK8sNamespace(value string) k8sMetaSetter { |
| 62 |
return k8sMetaSetter{Key: NamespaceField, Value: value} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
type AnnotationSetter struct { |
| 68 |
Kind string `yaml:"kind,omitempty"` |
| 69 |
Key string `yaml:"key,omitempty"` |
| 70 |
Value string `yaml:"value,omitempty"` |
| 71 |
} |
| 72 |
|
| 73 |
func (s AnnotationSetter) Filter(rn *RNode) (*RNode, error) { |
| 74 |
v := NewStringRNode(s.Value) |
| 75 |
|
| 76 |
v.YNode().Style = yaml.SingleQuotedStyle |
| 77 |
if err := ClearEmptyAnnotations(rn); err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return addMetadataNode(rn, AnnotationsField, s.Key, v) |
| 81 |
} |
| 82 |
|
| 83 |
func SetAnnotation(key, value string) AnnotationSetter { |
| 84 |
return AnnotationSetter{Key: key, Value: value} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
type AnnotationGetter struct { |
| 90 |
Kind string `yaml:"kind,omitempty"` |
| 91 |
Key string `yaml:"key,omitempty"` |
| 92 |
Value string `yaml:"value,omitempty"` |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
func (g AnnotationGetter) Filter(rn *RNode) (*RNode, error) { |
| 98 |
v, err := rn.Pipe( |
| 99 |
PathGetter{Path: []string{MetadataField, AnnotationsField, g.Key}}) |
| 100 |
if v == nil || err != nil { |
| 101 |
return v, err |
| 102 |
} |
| 103 |
if g.Value == "" || v.value.Value == g.Value { |
| 104 |
return v, err |
| 105 |
} |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
|
| 109 |
func GetAnnotation(key string) AnnotationGetter { |
| 110 |
return AnnotationGetter{Key: key} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
type LabelSetter struct { |
| 116 |
Kind string `yaml:"kind,omitempty"` |
| 117 |
Key string `yaml:"key,omitempty"` |
| 118 |
Value string `yaml:"value,omitempty"` |
| 119 |
} |
| 120 |
|
| 121 |
func (s LabelSetter) Filter(rn *RNode) (*RNode, error) { |
| 122 |
v := NewStringRNode(s.Value) |
| 123 |
|
| 124 |
v.YNode().Style = yaml.SingleQuotedStyle |
| 125 |
return addMetadataNode(rn, LabelsField, s.Key, v) |
| 126 |
} |
| 127 |
|
| 128 |
func addMetadataNode(rn *RNode, field, key string, v *RNode) (*RNode, error) { |
| 129 |
return rn.Pipe( |
| 130 |
PathGetter{ |
| 131 |
Path: []string{MetadataField, field}, Create: yaml.MappingNode}, |
| 132 |
FieldSetter{Name: key, Value: v}) |
| 133 |
} |
| 134 |
|
| 135 |
func SetLabel(key, value string) LabelSetter { |
| 136 |
return LabelSetter{Key: key, Value: value} |
| 137 |
} |