Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't prevent updates that only touch ownerrefs
- Loading branch information
Showing
5 changed files
with
157 additions
and
5 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
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,27 @@ | ||
package admission | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
kapi "k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
func IsOnlyMutatingOwnerRefs(obj, old runtime.Object) bool { | ||
// make a copy of the newObj so that we can stomp for comparison | ||
copied, err := kapi.Scheme.Copy(obj) | ||
if err != nil { | ||
// if we couldn't copy, don't fail, just make it do the check | ||
return false | ||
} | ||
copiedMeta, err := meta.Accessor(copied) | ||
if err != nil { | ||
return false | ||
} | ||
oldMeta, err := meta.Accessor(old) | ||
if err != nil { | ||
return false | ||
} | ||
copiedMeta.SetOwnerReferences(oldMeta.GetOwnerReferences()) | ||
|
||
return kapi.Semantic.DeepEqual(copied, old) | ||
} |
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,109 @@ | ||
package admission | ||
|
||
import ( | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
kapi "k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
func newPod() *kapi.Pod { | ||
return &kapi.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{}, | ||
Name: "foo", | ||
OwnerReferences: []metav1.OwnerReference{}, | ||
}, | ||
} | ||
|
||
} | ||
|
||
func TestIsOnlyMutatingOwnerRefs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj func() runtime.Object | ||
old func() runtime.Object | ||
expected bool | ||
}{ | ||
{ | ||
name: "same", | ||
obj: func() runtime.Object { | ||
return newPod() | ||
}, | ||
old: func() runtime.Object { | ||
return newPod() | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "only annotations", | ||
obj: func() runtime.Object { | ||
obj := newPod() | ||
obj.Annotations["foo"] = "bar" | ||
return obj | ||
}, | ||
old: func() runtime.Object { | ||
return newPod() | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "only other", | ||
obj: func() runtime.Object { | ||
obj := newPod() | ||
obj.Spec.RestartPolicy = kapi.RestartPolicyAlways | ||
return obj | ||
}, | ||
old: func() runtime.Object { | ||
return newPod() | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "only ownerRef", | ||
obj: func() runtime.Object { | ||
obj := newPod() | ||
obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) | ||
return obj | ||
}, | ||
old: func() runtime.Object { | ||
return newPod() | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "and annotations", | ||
obj: func() runtime.Object { | ||
obj := newPod() | ||
obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) | ||
obj.Annotations["foo"] = "bar" | ||
return obj | ||
}, | ||
old: func() runtime.Object { | ||
return newPod() | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "and other", | ||
obj: func() runtime.Object { | ||
obj := newPod() | ||
obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"}) | ||
obj.Spec.RestartPolicy = kapi.RestartPolicyAlways | ||
return obj | ||
}, | ||
old: func() runtime.Object { | ||
return newPod() | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
actual := IsOnlyMutatingOwnerRefs(tc.obj(), tc.old()) | ||
if tc.expected != actual { | ||
t.Errorf("%s: expected %v, got %v", tc.name, tc.expected, actual) | ||
} | ||
} | ||
} |
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