@@ -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
1642func 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
0 commit comments