Skip to content

Commit e03fd59

Browse files
feat: Allow suppressing annotations and labels
Signed-off-by: Antoine Deschênes <antoine.deschenes@linux.com>
1 parent e7b1bbc commit e03fd59

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

cmd/options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
1010
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
1111
f.BoolVar(&o.ShowSecrets, "show-secrets", false, "do not redact secret values in the output")
12-
f.StringArrayVar(&o.SuppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
12+
f.StringArrayVar(&o.SuppressedAnnotations, "suppress-annotation", []string{}, "allows suppression of the specified annotations in the diff output")
13+
f.StringArrayVar(&o.SuppressedLabels, "suppress-label", []string{}, "allows suppression of the specified labels in the diff output")
14+
f.StringArrayVar(&o.SuppressedKinds, "suppress", []string{}, "allows suppression of the kinds listed in the diff output")
1315
f.IntVarP(&o.OutputContext, "context", "C", -1, "output NUM lines of context around changes")
1416
f.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template, dyff. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
1517
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")

diff/diff.go

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/aryann/difflib"
1212
"github.com/mgutz/ansi"
1313
v1 "k8s.io/api/core/v1"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1416
"k8s.io/apimachinery/pkg/runtime/serializer/json"
1517
"k8s.io/apimachinery/pkg/util/yaml"
1618
"k8s.io/client-go/kubernetes/scheme"
@@ -20,12 +22,14 @@ import (
2022

2123
// Options are all the options to be passed to generate a diff
2224
type Options struct {
23-
OutputFormat string
24-
OutputContext int
25-
StripTrailingCR bool
26-
ShowSecrets bool
27-
SuppressedKinds []string
28-
FindRenames float32
25+
OutputFormat string
26+
OutputContext int
27+
StripTrailingCR bool
28+
ShowSecrets bool
29+
SuppressedAnnotations []string
30+
SuppressedLabels []string
31+
SuppressedKinds []string
32+
FindRenames float32
2933
}
3034

3135
// Manifests diff on manifests
@@ -101,6 +105,10 @@ func contentSearch(report *Report, possiblyRemoved []string, oldIndex map[string
101105
redactSecrets(oldContent, newContent)
102106
}
103107

108+
if len(options.SuppressedAnnotations) > 0 || len(options.SuppressedLabels) > 0 {
109+
suppressAnnotationsAndLabels(oldContent, newContent, options)
110+
}
111+
104112
diff := diffMappingResults(oldContent, newContent, options.StripTrailingCR)
105113
delta := actualChanges(diff)
106114
if delta == 0 || len(diff) == 0 {
@@ -135,6 +143,10 @@ func doDiff(report *Report, key string, oldContent *manifest.MappingResult, newC
135143
redactSecrets(oldContent, newContent)
136144
}
137145

146+
if len(options.SuppressedAnnotations) > 0 || len(options.SuppressedLabels) > 0 {
147+
suppressAnnotationsAndLabels(oldContent, newContent, options)
148+
}
149+
138150
if oldContent == nil {
139151
emptyMapping := &manifest.MappingResult{}
140152
diffs := diffMappingResults(emptyMapping, newContent, options.StripTrailingCR)
@@ -151,6 +163,60 @@ func doDiff(report *Report, key string, oldContent *manifest.MappingResult, newC
151163
}
152164
}
153165

166+
func suppressAnnotationsAndLabels(old, new *manifest.MappingResult, options *Options) {
167+
serializer := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme,
168+
scheme.Scheme)
169+
var oldObject, newObject *unstructured.Unstructured
170+
171+
if old != nil {
172+
if err := yaml.NewYAMLToJSONDecoder(bytes.NewBufferString(old.Content)).Decode(&oldObject); err != nil {
173+
old.Content = fmt.Sprintf("Error parsing old object: %s", err)
174+
}
175+
176+
suppressObjectAnnotationsAndLabels(oldObject, options)
177+
178+
var buf bytes.Buffer
179+
if err := serializer.Encode(oldObject, &buf); err != nil {
180+
new.Content = fmt.Sprintf("Error encoding old object: %s", err)
181+
}
182+
old.Content = getComment(old.Content) + buf.String()
183+
}
184+
if new != nil {
185+
if err := yaml.NewYAMLToJSONDecoder(bytes.NewBufferString(new.Content)).Decode(&newObject); err != nil {
186+
new.Content = fmt.Sprintf("Error parsing new object: %s", err)
187+
}
188+
189+
suppressObjectAnnotationsAndLabels(newObject, options)
190+
191+
var buf bytes.Buffer
192+
if err := serializer.Encode(newObject, &buf); err != nil {
193+
new.Content = fmt.Sprintf("Error encoding new object: %s", err)
194+
}
195+
new.Content = getComment(new.Content) + buf.String()
196+
}
197+
}
198+
199+
func suppressObjectAnnotationsAndLabels(object metav1.Object, options *Options) {
200+
if len(options.SuppressedAnnotations) > 0 {
201+
annotations := object.GetLabels()
202+
for _, annotation := range options.SuppressedAnnotations {
203+
if _, ok := annotations[annotation]; ok {
204+
delete(annotations, annotation)
205+
}
206+
}
207+
object.SetAnnotations(annotations)
208+
}
209+
if len(options.SuppressedLabels) > 0 {
210+
labels := object.GetLabels()
211+
for _, label := range options.SuppressedLabels {
212+
if _, ok := labels[label]; ok {
213+
delete(labels, label)
214+
}
215+
}
216+
object.SetLabels(labels)
217+
}
218+
}
219+
154220
func redactSecrets(old, new *manifest.MappingResult) {
155221
if (old != nil && old.Kind != "Secret") || (new != nil && new.Kind != "Secret") {
156222
return

0 commit comments

Comments
 (0)