Merge pull request #34561 from mwielgus/specandmeta

Automatic merge from submit-queue

ObjectMeta and Spec equivalent function in federation utils

To be used in update in controllers.

cc: @quinton-hoole
This commit is contained in:
Kubernetes Submit Queue 2016-10-12 04:31:11 -07:00 committed by GitHub
commit 4d882744d6
2 changed files with 47 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import (
"reflect"
api_v1 "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/runtime"
)
// Copies cluster-independent, user provided data from the given ObjectMeta struct. If in
@ -54,7 +55,7 @@ func DeepCopyObjectMeta(obj api_v1.ObjectMeta) api_v1.ObjectMeta {
return copyMeta
}
// Checks if cluster-independed, user provided data in two given ObjectMeta are eqaul. If in
// Checks if cluster-independent, user provided data in two given ObjectMeta are equal. If in
// the future the ObjectMeta structure is expanded then any field that is not populated
// by the api server should be included here.
func ObjectMetaEquivalent(a, b api_v1.ObjectMeta) bool {
@ -72,3 +73,13 @@ func ObjectMetaEquivalent(a, b api_v1.ObjectMeta) bool {
}
return true
}
// Checks if cluster-independent, user provided data in ObjectMeta and Spec in two given top
// level api objects are equivalent.
func ObjectMetaAndSpecEquivalent(a, b runtime.Object) bool {
objectMetaA := reflect.ValueOf(a).Elem().FieldByName("ObjectMeta").Interface().(api_v1.ObjectMeta)
objectMetaB := reflect.ValueOf(b).Elem().FieldByName("ObjectMeta").Interface().(api_v1.ObjectMeta)
specA := reflect.ValueOf(a).Elem().FieldByName("Spec").Interface()
specB := reflect.ValueOf(b).Elem().FieldByName("Spec").Interface()
return ObjectMetaEquivalent(objectMetaA, objectMetaB) && reflect.DeepEqual(specA, specB)
}

View File

@ -65,3 +65,38 @@ func TestObjectMeta(t *testing.T) {
assert.True(t, ObjectMetaEquivalent(o5, o6))
assert.True(t, ObjectMetaEquivalent(o3, o5))
}
func TestObjectMetaAndSpec(t *testing.T) {
s1 := api_v1.Service{
ObjectMeta: api_v1.ObjectMeta{
Namespace: "ns1",
Name: "s1",
},
Spec: api_v1.ServiceSpec{
ExternalName: "Service1",
},
}
s1b := s1
s2 := api_v1.Service{
ObjectMeta: api_v1.ObjectMeta{
Namespace: "ns1",
Name: "s2",
},
Spec: api_v1.ServiceSpec{
ExternalName: "Service1",
},
}
s3 := api_v1.Service{
ObjectMeta: api_v1.ObjectMeta{
Namespace: "ns1",
Name: "s1",
},
Spec: api_v1.ServiceSpec{
ExternalName: "Service2",
},
}
assert.True(t, ObjectMetaAndSpecEquivalent(&s1, &s1b))
assert.False(t, ObjectMetaAndSpecEquivalent(&s1, &s2))
assert.False(t, ObjectMetaAndSpecEquivalent(&s1, &s3))
assert.False(t, ObjectMetaAndSpecEquivalent(&s2, &s3))
}