Skip to content

Commit 14b441f

Browse files
pepovbaluchicken
authored andcommitted
Remove annotations completely instead of setting an empty map in the modified configuration. Fix package name. Fix to use local annotator instead the default in Patchmaker.
1 parent 08f0443 commit 14b441f

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

patch/annotation.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package objectmatcher
15+
package patch
1616

1717
import (
1818
"encoding/json"
@@ -103,6 +103,10 @@ func (a *Annotator) GetModifiedConfiguration(obj runtime.Object, annotate bool)
103103
return nil, err
104104
}
105105

106+
// Do not include an empty annotation map
107+
if len(annots) == 0 {
108+
a.metadataAccessor.SetAnnotations(obj, nil)
109+
}
106110
modified, err = json.Marshal(obj)
107111
if err != nil {
108112
return nil, err

patch/annotation_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © 2019 Banzai Cloud
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package patch
16+
17+
import (
18+
"testing"
19+
20+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21+
)
22+
23+
func TestAnnotationRemovedWhenEmpty(t *testing.T) {
24+
u := unstructured.Unstructured{}
25+
u.SetAnnotations(map[string]string{
26+
LastAppliedConfig: "{}",
27+
})
28+
modified, err := DefaultAnnotator.GetModifiedConfiguration(&u, false)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
if "{\"metadata\":{}}" != string(modified) {
33+
t.Fatalf("Expected {\"metadata\":{} got %s", string(modified))
34+
}
35+
}

patch/deletenull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package objectmatcher
15+
package patch
1616

1717
import (
1818
"encoding/json"

patch/patch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package objectmatcher
15+
package patch
1616

1717
import (
1818
"encoding/json"
@@ -53,7 +53,7 @@ func (p *PatchMaker) Calculate(currentObject, modifiedObject runtime.Object) (*P
5353
return nil, emperror.Wrap(err, "Failed to delete null from modified object")
5454
}
5555

56-
original, err := DefaultAnnotator.GetOriginalConfiguration(currentObject)
56+
original, err := p.annotator.GetOriginalConfiguration(currentObject)
5757
if err != nil {
5858
return nil, emperror.Wrap(err, "Failed to get original configuration")
5959
}

0 commit comments

Comments
 (0)