Object meta helper functions for federated controller
This commit is contained in:
48
federation/pkg/federation-controller/util/meta.go
Normal file
48
federation/pkg/federation-controller/util/meta.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
api_v1 "k8s.io/kubernetes/pkg/api/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CopyObjectMeta(obj api_v1.ObjectMeta) api_v1.ObjectMeta {
|
||||||
|
return api_v1.ObjectMeta{
|
||||||
|
Name: obj.Name,
|
||||||
|
Namespace: obj.Namespace,
|
||||||
|
Labels: obj.Labels,
|
||||||
|
Annotations: obj.Annotations,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ObjectMetaEquivalent(a, b api_v1.ObjectMeta) bool {
|
||||||
|
if a.Name != b.Name {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if a.Namespace != b.Namespace {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(a.Labels, b.Labels) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(a.Annotations, b.Annotations) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
47
federation/pkg/federation-controller/util/meta_test.go
Normal file
47
federation/pkg/federation-controller/util/meta_test.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
api_v1 "k8s.io/kubernetes/pkg/api/v1"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestObjectMeta(t *testing.T) {
|
||||||
|
o1 := api_v1.ObjectMeta{
|
||||||
|
Namespace: "ns1",
|
||||||
|
Name: "s1",
|
||||||
|
UID: "1231231412",
|
||||||
|
ResourceVersion: "999",
|
||||||
|
}
|
||||||
|
o3 := api_v1.ObjectMeta{
|
||||||
|
Namespace: "ns1",
|
||||||
|
Name: "s1",
|
||||||
|
UID: "1231231412",
|
||||||
|
Annotations: map[string]string{"A": "B"},
|
||||||
|
}
|
||||||
|
|
||||||
|
o2 := CopyObjectMeta(o1)
|
||||||
|
assert.Equal(t, 0, len(o2.UID))
|
||||||
|
assert.Equal(t, 0, len(o2.ResourceVersion))
|
||||||
|
assert.Equal(t, o1.Name, o2.Name)
|
||||||
|
assert.True(t, ObjectMetaEquivalent(o1, o2))
|
||||||
|
assert.False(t, ObjectMetaEquivalent(o1, o3))
|
||||||
|
}
|
Reference in New Issue
Block a user