Skip to content

Commit 507f2b5

Browse files
github-actions[bot]jiaqiluomatttrach
authored
fix: support exceptions in diff suppression (#1828)
Co-authored-by: Jack Luo <[email protected]> Co-authored-by: Matt Trachier <[email protected]>
1 parent d9e350d commit 507f2b5

File tree

2 files changed

+130
-22
lines changed

2 files changed

+130
-22
lines changed

rancher2/schema_common.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,49 @@ const (
1111
commonAnnotationLabelRancher = "rancher.io/"
1212
)
1313

14+
// exceptions is a list of annotation/label keys that should not be suppressed.
15+
var exceptions = []string{
16+
"rancher.io/imported-cluster-version-management",
17+
}
18+
19+
// suppressFunc is a DiffSuppressFunc that prevents Terraform from removing Rancher-managed annotations or labels.
20+
// It also ignores changes to a predefined set of Rancher-managed annotation/label keys.
21+
//
22+
// Thought it is not recommended, users can still add annotations or labels whose keys contain Rancher-managed keys,
23+
// but they won't be able to remove them once added.
24+
// Note: Terraform prefixes the key `k` with either "annotations." or "labels."
25+
var suppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
26+
for _, exception := range exceptions {
27+
// Explicitly check if the key ends with the exception
28+
if strings.HasSuffix(k, exception) {
29+
return false
30+
}
31+
}
32+
33+
if (strings.Contains(k, commonAnnotationLabelCattle) || strings.Contains(k, commonAnnotationLabelRancher)) && new == "" {
34+
return true
35+
}
36+
37+
return false
38+
}
39+
1440
// Schemas
1541

1642
func commonAnnotationLabelFields() map[string]*schema.Schema {
1743
s := map[string]*schema.Schema{
1844
"annotations": {
19-
Type: schema.TypeMap,
20-
Optional: true,
21-
Computed: true,
22-
Description: "Annotations of the resource",
23-
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
24-
// Suppressing diff for annotations containing cattle.io/
25-
if (strings.Contains(k, commonAnnotationLabelCattle) || strings.Contains(k, commonAnnotationLabelRancher)) && new == "" {
26-
return true
27-
}
28-
return false
29-
},
45+
Type: schema.TypeMap,
46+
Optional: true,
47+
Computed: true,
48+
Description: "Annotations of the resource",
49+
DiffSuppressFunc: suppressFunc,
3050
},
3151
"labels": {
32-
Type: schema.TypeMap,
33-
Optional: true,
34-
Computed: true,
35-
Description: "Labels of the resource",
36-
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
37-
// Suppressing diff for labels containing cattle.io/
38-
if (strings.Contains(k, commonAnnotationLabelCattle) || strings.Contains(k, commonAnnotationLabelRancher)) && new == "" {
39-
return true
40-
}
41-
return false
42-
},
52+
Type: schema.TypeMap,
53+
Optional: true,
54+
Computed: true,
55+
Description: "Labels of the resource",
56+
DiffSuppressFunc: suppressFunc,
4357
},
4458
}
4559
return s

rancher2/schema_common_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package rancher2
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSupressFunc(t *testing.T) {
10+
cases := []struct {
11+
Name string
12+
K string
13+
Old string
14+
New string
15+
ExpectedResult bool
16+
}{
17+
{
18+
Name: "Exception key change should not be suppressed",
19+
K: "annotations.rancher.io/imported-cluster-version-management",
20+
Old: "true",
21+
New: "false",
22+
ExpectedResult: false,
23+
},
24+
{
25+
Name: "Exception key removal should not be suppressed",
26+
K: "labels.rancher.io/imported-cluster-version-management",
27+
Old: "false",
28+
New: "",
29+
ExpectedResult: false,
30+
},
31+
{
32+
Name: "Exception key but not as suffix should not be suppressed",
33+
K: "labels.rancher.io/imported-cluster-version-management-new",
34+
Old: "old_val",
35+
New: "new_val",
36+
ExpectedResult: false,
37+
},
38+
{
39+
Name: "Rancher.io annotation/label removal should be suppressed",
40+
K: "annotations.rancher.io/creator",
41+
Old: "user-123",
42+
New: "",
43+
ExpectedResult: true,
44+
},
45+
{
46+
Name: "Cattle.io annotation/label removal should be suppressed",
47+
K: "annotations.cattle.io/some-state",
48+
Old: "some-val",
49+
New: "",
50+
ExpectedResult: true,
51+
},
52+
{
53+
Name: "Cattle.io annotation/label addition should not be suppressed",
54+
K: "annotations.cattle.io/some-state",
55+
Old: "",
56+
New: "some_val",
57+
ExpectedResult: false,
58+
},
59+
{
60+
Name: "User-managed change to a rancher.io annotation should not be suppressed",
61+
K: "annotations.rancher.io/creator",
62+
Old: "user-123",
63+
New: "user-456",
64+
ExpectedResult: false,
65+
},
66+
{Name: "User-managed change to a cattle.io annotation should not be suppressed",
67+
K: "annotations.cattle.io/creator",
68+
Old: "user-123",
69+
New: "user-456",
70+
ExpectedResult: false,
71+
},
72+
{
73+
Name: "Non-Rancher annotation should not be suppressed",
74+
K: "annotations.my-custom-annotation",
75+
Old: "old-val",
76+
New: "new-val",
77+
ExpectedResult: false,
78+
},
79+
{
80+
Name: "Non-Rancher annotation removal should not be suppressed",
81+
K: "annotations.my-custom-annotation",
82+
Old: "old-val",
83+
New: "",
84+
ExpectedResult: false,
85+
},
86+
}
87+
88+
for _, tc := range cases {
89+
t.Run(tc.Name, func(t *testing.T) {
90+
result := suppressFunc(tc.K, tc.Old, tc.New, nil)
91+
assert.Equal(t, tc.ExpectedResult, result)
92+
})
93+
}
94+
}

0 commit comments

Comments
 (0)