Skip to content

Commit

Permalink
pkg/api: move Semantic equality to k8s.io/apimachinery/pkg/api/equality
Browse files Browse the repository at this point in the history
  • Loading branch information
sttts committed Jan 29, 2017
1 parent 88d9829 commit 79adb99
Show file tree
Hide file tree
Showing 64 changed files with 261 additions and 165 deletions.
5 changes: 3 additions & 2 deletions cmd/kube-aggregator/pkg/apiserver/handler_apis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http/httputil"
"testing"

apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/diff"
Expand Down Expand Up @@ -305,7 +306,7 @@ func TestAPIs(t *testing.T) {
t.Errorf("%s: %v", tc.name, err)
continue
}
if !api.Semantic.DeepEqual(tc.expected, actual) {
if !apiequality.Semantic.DeepEqual(tc.expected, actual) {
t.Errorf("%s: %v", tc.name, diff.ObjectDiff(tc.expected, actual))
continue
}
Expand Down Expand Up @@ -467,7 +468,7 @@ func TestAPIGroup(t *testing.T) {
t.Errorf("%s: %v", tc.name, err)
continue
}
if !api.Semantic.DeepEqual(tc.expected, actual) {
if !apiequality.Semantic.DeepEqual(tc.expected, actual) {
t.Errorf("%s: %v", tc.name, diff.ObjectDiff(tc.expected, actual))
continue
}
Expand Down
11 changes: 6 additions & 5 deletions federation/pkg/kubefed/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"testing"
"time"

apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -767,7 +768,7 @@ func fakeInitHostFactory(federationName, namespaceName, ip, dnsZoneName, image,
if err != nil {
return nil, err
}
if !api.Semantic.DeepEqual(got, namespace) {
if !apiequality.Semantic.DeepEqual(got, namespace) {
return nil, fmt.Errorf("Unexpected namespace object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, namespace))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &namespace)}, nil
Expand All @@ -781,7 +782,7 @@ func fakeInitHostFactory(federationName, namespaceName, ip, dnsZoneName, image,
if err != nil {
return nil, err
}
if !api.Semantic.DeepEqual(got, svc) {
if !apiequality.Semantic.DeepEqual(got, svc) {
return nil, fmt.Errorf("Unexpected service object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, svc))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &svc)}, nil
Expand Down Expand Up @@ -812,7 +813,7 @@ func fakeInitHostFactory(federationName, namespaceName, ip, dnsZoneName, image,
case cmKubeconfigSecretName:
want = cmKubeconfigSecret
}
if !api.Semantic.DeepEqual(got, want) {
if !apiequality.Semantic.DeepEqual(got, want) {
return nil, fmt.Errorf("Unexpected secret object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, want))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &want)}, nil
Expand All @@ -826,7 +827,7 @@ func fakeInitHostFactory(federationName, namespaceName, ip, dnsZoneName, image,
if err != nil {
return nil, err
}
if !api.Semantic.DeepEqual(got, pvc) {
if !apiequality.Semantic.DeepEqual(got, pvc) {
return nil, fmt.Errorf("Unexpected PVC object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, pvc))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &pvc)}, nil
Expand All @@ -846,7 +847,7 @@ func fakeInitHostFactory(federationName, namespaceName, ip, dnsZoneName, image,
case cmName:
want = cm
}
if !api.Semantic.DeepEqual(got, want) {
if !apiequality.Semantic.DeepEqual(got, want) {
return nil, fmt.Errorf("Unexpected deployment object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, want))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(extCodec, &want)}, nil
Expand Down
5 changes: 3 additions & 2 deletions federation/pkg/kubefed/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"testing"

apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -189,7 +190,7 @@ func testJoinFederationFactory(clusterName, secretName, server string) cmdutil.F
if err != nil {
return nil, err
}
if !api.Semantic.DeepEqual(got, want) {
if !apiequality.Semantic.DeepEqual(got, want) {
return nil, fmt.Errorf("Unexpected cluster object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, want))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &want)}, nil
Expand Down Expand Up @@ -265,7 +266,7 @@ func fakeJoinHostFactory(clusterName, clusterCtx, secretName, server, token stri
if err != nil {
return nil, err
}
if !api.Semantic.DeepEqual(got, secretObject) {
if !apiequality.Semantic.DeepEqual(got, secretObject) {
return nil, fmt.Errorf("Unexpected secret object\n\tDiff: %s", diff.ObjectGoPrintDiff(got, secretObject))
}
return &http.Response{StatusCode: http.StatusCreated, Header: kubefedtesting.DefaultHeader(), Body: kubefedtesting.ObjBody(codec, &secretObject)}, nil
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/rand"
"testing"

apiequality "k8s.io/apimachinery/pkg/api/equality"
apitesting "k8s.io/apimachinery/pkg/api/testing"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/diff"
Expand Down Expand Up @@ -80,7 +81,7 @@ func BenchmarkNodeConversion(b *testing.B) {
result = obj.(*api.Node)
}
b.StopTimer()
if !api.Semantic.DeepDerivative(node, *result) {
if !apiequality.Semantic.DeepDerivative(node, *result) {
b.Fatalf("Incorrect conversion: %s", diff.ObjectDiff(node, *result))
}
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func BenchmarkReplicationControllerConversion(b *testing.B) {
result = obj.(*api.ReplicationController)
}
b.StopTimer()
if !api.Semantic.DeepDerivative(replicationController, *result) {
if !apiequality.Semantic.DeepDerivative(replicationController, *result) {
b.Fatalf("Incorrect conversion: expected %v, got %v", replicationController, *result)
}
}
7 changes: 4 additions & 3 deletions pkg/api/deep_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -140,7 +141,7 @@ func BenchmarkPodCopy(b *testing.B) {
}
result = obj.(*api.Pod)
}
if !api.Semantic.DeepEqual(benchmarkPod, *result) {
if !apiequality.Semantic.DeepEqual(benchmarkPod, *result) {
b.Fatalf("Incorrect copy: expected %v, got %v", benchmarkPod, *result)
}
}
Expand All @@ -163,7 +164,7 @@ func BenchmarkNodeCopy(b *testing.B) {
}
result = obj.(*api.Node)
}
if !api.Semantic.DeepEqual(node, *result) {
if !apiequality.Semantic.DeepEqual(node, *result) {
b.Fatalf("Incorrect copy: expected %v, got %v", node, *result)
}
}
Expand All @@ -186,7 +187,7 @@ func BenchmarkReplicationControllerCopy(b *testing.B) {
}
result = obj.(*api.ReplicationController)
}
if !api.Semantic.DeepEqual(replicationController, *result) {
if !apiequality.Semantic.DeepEqual(replicationController, *result) {
b.Fatalf("Incorrect copy: expected %v, got %v", replicationController, *result)
}
}
2 changes: 1 addition & 1 deletion pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NonConvertibleFields(annotations map[string]string) map[string]string {
}

// Semantic can do semantic deep equality checks for api objects.
// Example: api.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true
// Example: apiequality.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true
var Semantic = conversion.EqualitiesOrDie(
func(a, b resource.Quantity) bool {
// Ignore formatting, only care that numeric value stayed the same.
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/serialization_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"

"github.com/gogo/protobuf/proto"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apitesting "k8s.io/apimachinery/pkg/api/testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -68,7 +69,7 @@ func TestUniversalDeserializer(t *testing.T) {
if err != nil {
t.Fatalf("%s: %v", mediaType, err)
}
if !api.Semantic.DeepEqual(expected, obj) {
if !apiequality.Semantic.DeepEqual(expected, obj) {
t.Fatalf("%s: %#v", mediaType, obj)
}
}
Expand All @@ -88,7 +89,7 @@ func TestProtobufRoundTrip(t *testing.T) {
if err := out.Unmarshal(data); err != nil {
t.Fatal(err)
}
if !api.Semantic.Equalities.DeepEqual(out, obj) {
if !apiequality.Semantic.Equalities.DeepEqual(out, obj) {
t.Logf("marshal\n%s", hex.Dump(data))
t.Fatalf("Unmarshal is unequal\n%s", diff.ObjectGoPrintDiff(out, obj))
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
flag "github.com/spf13/pflag"
"github.com/ugorji/go/codec"

apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
apitesting "k8s.io/apimachinery/pkg/api/testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -321,7 +322,7 @@ func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {

// ensure that the deep copy is equal to the original; neither the deep
// copy or conversion should alter the object
if !api.Semantic.DeepEqual(original, item) {
if !apiequality.Semantic.DeepEqual(original, item) {
t.Errorf("0: %v: encode altered the object, diff: %v", name, diff.ObjectReflectDiff(original, item))
return
}
Expand All @@ -335,7 +336,7 @@ func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {

// ensure that the object produced from decoding the encoded data is equal
// to the original object
if !api.Semantic.DeepEqual(original, obj2) {
if !apiequality.Semantic.DeepEqual(original, obj2) {
t.Errorf("\n1: %v: diff: %v\nCodec: %#v\nSource:\n\n%#v\n\nEncoded:\n\n%s\n\nFinal:\n\n%#v", name, diff.ObjectReflectDiff(item, obj2), codec, printer.Sprintf("%#v", item), dataAsString(data), printer.Sprintf("%#v", obj2))
return
}
Expand All @@ -350,7 +351,7 @@ func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {

// ensure that the new runtime object is equal to the original after being
// decoded into
if !api.Semantic.DeepEqual(item, obj3) {
if !apiequality.Semantic.DeepEqual(item, obj3) {
t.Errorf("3: %v: diff: %v\nCodec: %#v", name, diff.ObjectReflectDiff(item, obj3), codec)
return
}
Expand Down Expand Up @@ -383,7 +384,7 @@ func TestEncodePtr(t *testing.T) {
if _, ok := obj2.(*api.Pod); !ok {
t.Fatalf("Got wrong type")
}
if !api.Semantic.DeepEqual(obj2, pod) {
if !apiequality.Semantic.DeepEqual(obj2, pod) {
t.Errorf("\nExpected:\n\n %#v,\n\nGot:\n\n %#vDiff: %v\n\n", pod, obj2, diff.ObjectDiff(obj2, pod))
}
}
Expand Down Expand Up @@ -480,7 +481,7 @@ func TestObjectWatchFraming(t *testing.T) {
}
resultSecret.Kind = "Secret"
resultSecret.APIVersion = "v1"
if !api.Semantic.DeepEqual(v1secret, res) {
if !apiequality.Semantic.DeepEqual(v1secret, res) {
t.Fatalf("objects did not match: %s", diff.ObjectGoPrintDiff(v1secret, res))
}

Expand Down Expand Up @@ -514,7 +515,7 @@ func TestObjectWatchFraming(t *testing.T) {
}
}

if !api.Semantic.DeepEqual(secret, outEvent.Object.Object) {
if !apiequality.Semantic.DeepEqual(secret, outEvent.Object.Object) {
t.Fatalf("%s: did not match after frame decoding: %s", info.MediaType, diff.ObjectGoPrintDiff(secret, outEvent.Object.Object))
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/testing/pod_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"k8s.io/kubernetes/pkg/api/v1"
)

// DeepEqualSafePodSpec returns a PodSpec which is ready to be used with api.Semantic.DeepEqual
// DeepEqualSafePodSpec returns a PodSpec which is ready to be used with apiequality.Semantic.DeepEqual
func DeepEqualSafePodSpec() api.PodSpec {
grace := int64(30)
return api.PodSpec{
Expand All @@ -33,7 +33,7 @@ func DeepEqualSafePodSpec() api.PodSpec {
}
}

// V1DeepEqualSafePodSpec returns a PodSpec which is ready to be used with api.Semantic.DeepEqual
// V1DeepEqualSafePodSpec returns a PodSpec which is ready to be used with apiequality.Semantic.DeepEqual
func V1DeepEqualSafePodSpec() v1.PodSpec {
grace := int64(30)
return v1.PodSpec{
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/unstructured_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"reflect"
"testing"

apiequality "k8s.io/apimachinery/pkg/api/equality"
apitesting "k8s.io/apimachinery/pkg/api/testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
Expand Down Expand Up @@ -89,7 +90,7 @@ func doRoundTrip(t *testing.T, group testapi.TestGroup, kind string) {
t.Errorf("Error when unmarshaling to object: %v", err)
return
}
if !api.Semantic.DeepEqual(item, unmarshalledObj) {
if !apiequality.Semantic.DeepEqual(item, unmarshalledObj) {
t.Errorf("Object changed during JSON operations, diff: %v", diff.ObjectReflectDiff(item, unmarshalledObj))
return
}
Expand All @@ -111,7 +112,7 @@ func doRoundTrip(t *testing.T, group testapi.TestGroup, kind string) {
return
}
if !api.Semantic.DeepEqual(item, newObj) {
if !apiequality.Semantic.DeepEqual(item, newObj) {
t.Errorf("Object changed, diff: %v", diff.ObjectReflectDiff(item, newObj))
}
*/
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/v1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"
"time"

apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -210,7 +211,7 @@ func TestResourceListConversion(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error for case %d: %v", i, err)
}
if !api.Semantic.DeepEqual(test.expected, output) {
if !apiequality.Semantic.DeepEqual(test.expected, output) {
t.Errorf("unexpected conversion for case %d: Expected %+v; Got %+v", i, test.expected, output)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"reflect"
"testing"

apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/kubernetes/pkg/api"
)

func TestAddToNodeAddresses(t *testing.T) {
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestAddToNodeAddresses(t *testing.T) {

for i, tc := range testCases {
AddToNodeAddresses(&tc.existing, tc.toAdd...)
if !api.Semantic.DeepEqual(tc.expected, tc.existing) {
if !apiequality.Semantic.DeepEqual(tc.expected, tc.existing) {
t.Errorf("case[%d], expected: %v, got: %v", i, tc.expected, tc.existing)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/validation/genericvalidation/objectmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

apiequality "k8s.io/apimachinery/pkg/api/equality"
apimachineyvalidation "k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
Expand Down Expand Up @@ -133,7 +134,7 @@ func ValidateNoNewFinalizers(newFinalizers []string, oldFinalizers []string, fld

func ValidateImmutableField(newVal, oldVal interface{}, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if !api.Semantic.DeepEqual(oldVal, newVal) {
if !apiequality.Semantic.DeepEqual(oldVal, newVal) {
allErrs = append(allErrs, field.Invalid(fldPath, newVal, FieldImmutableErrorMsg))
}
return allErrs
Expand Down
Loading

0 comments on commit 79adb99

Please sign in to comment.