-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore status fields during drift detection (#2546)
- Loading branch information
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ spec: | |
enabled: true | ||
paths: | ||
- drift | ||
- drift-ignore-status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package normalizers | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
// StatusNormalizer removes a top-level "status" fields from the object, if present | ||
type StatusNormalizer struct{} | ||
|
||
func (_ StatusNormalizer) Normalize(un *unstructured.Unstructured) error { | ||
unstructured.RemoveNestedField(un.Object, "status") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package normalizers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func TestStatusNormalizer_Normalize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj runtime.Object | ||
check func(object runtime.Object) error | ||
}{ | ||
{ | ||
name: "object with status", | ||
obj: &corev1.Pod{ | ||
Status: corev1.PodStatus{ | ||
PodIP: "1.2.3.4", | ||
}, | ||
}, | ||
check: func(obj runtime.Object) error { | ||
if obj.(*corev1.Pod).Status.PodIP != "" { | ||
return errors.New("status was not removed") | ||
} | ||
return nil | ||
}, | ||
}, | ||
{ | ||
name: "object without status", | ||
obj: &corev1.ConfigMap{}, | ||
check: func(_ runtime.Object) error { return nil }, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
un, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tt.obj) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := (StatusNormalizer{}).Normalize(&unstructured.Unstructured{Object: un}); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(un, tt.obj); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := tt.check(tt.obj); err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
} | ||
} |