From 441e206671130154ea3c4187945fd613a0bdbcf6 Mon Sep 17 00:00:00 2001 From: jianhuiz Date: Wed, 4 May 2016 17:37:03 -0700 Subject: [PATCH] move server client shared annotations to new api/annotations package --- pkg/api/annotations/annotations.go | 23 +++++++ pkg/api/annotations/doc.go | 18 ++++++ .../deployment/deployment_controller.go | 4 +- pkg/kubectl/apply.go | 61 +++++++++---------- pkg/kubectl/cmd/apply_test.go | 6 +- test/e2e/deployment.go | 7 ++- test/e2e/kubectl.go | 4 +- 7 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 pkg/api/annotations/annotations.go create mode 100644 pkg/api/annotations/doc.go diff --git a/pkg/api/annotations/annotations.go b/pkg/api/annotations/annotations.go new file mode 100644 index 0000000000..dbdf11cbab --- /dev/null +++ b/pkg/api/annotations/annotations.go @@ -0,0 +1,23 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +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 annotations + +const kubectlPrefix = "kubectl.kubernetes.io/" + +// LastAppliedConfigAnnotation is the annotation used to store the previous +// configuration of a resource for use in a three way diff by UpdateApplyAnnotation. +const LastAppliedConfigAnnotation = kubectlPrefix + "last-applied-configuration" diff --git a/pkg/api/annotations/doc.go b/pkg/api/annotations/doc.go new file mode 100644 index 0000000000..6a422ea4f4 --- /dev/null +++ b/pkg/api/annotations/doc.go @@ -0,0 +1,18 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +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 annotations defines annotation keys that shared between server and client +package annotations diff --git a/pkg/controller/deployment/deployment_controller.go b/pkg/controller/deployment/deployment_controller.go index 2897583fd4..6895e7e265 100644 --- a/pkg/controller/deployment/deployment_controller.go +++ b/pkg/controller/deployment/deployment_controller.go @@ -25,6 +25,7 @@ import ( "github.com/golang/glog" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/annotations" "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/apis/extensions" @@ -34,7 +35,6 @@ import ( "k8s.io/kubernetes/pkg/client/record" "k8s.io/kubernetes/pkg/controller" "k8s.io/kubernetes/pkg/controller/framework" - "k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/runtime" deploymentutil "k8s.io/kubernetes/pkg/util/deployment" utilerrors "k8s.io/kubernetes/pkg/util/errors" @@ -913,7 +913,7 @@ func setNewReplicaSetAnnotations(deployment *extensions.Deployment, newRS *exten // See https://github.com/kubernetes/kubernetes/pull/20035#issuecomment-179558615 func skipCopyAnnotation(key string) bool { // Skip apply annotations and revision annotations. - return key == kubectl.LastAppliedConfigAnnotation || key == deploymentutil.RevisionAnnotation + return key == annotations.LastAppliedConfigAnnotation || key == deploymentutil.RevisionAnnotation } func getSkippedAnnotations(annotations map[string]string) map[string]string { diff --git a/pkg/kubectl/apply.go b/pkg/kubectl/apply.go index 3149bcb29d..c75c4f8a01 100644 --- a/pkg/kubectl/apply.go +++ b/pkg/kubectl/apply.go @@ -19,6 +19,7 @@ package kubectl import ( "encoding/json" + "k8s.io/kubernetes/pkg/api/annotations" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/runtime" @@ -28,23 +29,19 @@ type debugError interface { DebugError() (msg string, args []interface{}) } -// LastAppliedConfigAnnotation is the annotation used to store the previous -// configuration of a resource for use in a three way diff by UpdateApplyAnnotation. -const LastAppliedConfigAnnotation = kubectlAnnotationPrefix + "last-applied-configuration" - // GetOriginalConfiguration retrieves the original configuration of the object // from the annotation, or nil if no annotation was found. func GetOriginalConfiguration(info *resource.Info) ([]byte, error) { - annotations, err := info.Mapping.MetadataAccessor.Annotations(info.Object) + annots, err := info.Mapping.MetadataAccessor.Annotations(info.Object) if err != nil { return nil, err } - if annotations == nil { + if annots == nil { return nil, nil } - original, ok := annotations[LastAppliedConfigAnnotation] + original, ok := annots[annotations.LastAppliedConfigAnnotation] if !ok { return nil, nil } @@ -60,17 +57,17 @@ func SetOriginalConfiguration(info *resource.Info, original []byte) error { } accessor := info.Mapping.MetadataAccessor - annotations, err := accessor.Annotations(info.Object) + annots, err := accessor.Annotations(info.Object) if err != nil { return err } - if annotations == nil { - annotations = map[string]string{} + if annots == nil { + annots = map[string]string{} } - annotations[LastAppliedConfigAnnotation] = string(original) - if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annotations); err != nil { + annots[annotations.LastAppliedConfigAnnotation] = string(original) + if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil { return err } @@ -93,14 +90,14 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime. } // Get the current annotations from the object. - annotations := accessor.GetAnnotations() - if annotations == nil { - annotations = map[string]string{} + annots := accessor.GetAnnotations() + if annots == nil { + annots = map[string]string{} } - original := annotations[LastAppliedConfigAnnotation] - delete(annotations, LastAppliedConfigAnnotation) - accessor.SetAnnotations(annotations) + original := annots[annotations.LastAppliedConfigAnnotation] + delete(annots, annotations.LastAppliedConfigAnnotation) + accessor.SetAnnotations(annots) // TODO: this needs to be abstracted - there should be no assumption that versioned object // can be marshalled to JSON. modified, err = json.Marshal(info.VersionedObject) @@ -109,8 +106,8 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime. } if annotate { - annotations[LastAppliedConfigAnnotation] = string(modified) - accessor.SetAnnotations(annotations) + annots[annotations.LastAppliedConfigAnnotation] = string(modified) + accessor.SetAnnotations(annots) // TODO: this needs to be abstracted - there should be no assumption that versioned object // can be marshalled to JSON. modified, err = json.Marshal(info.VersionedObject) @@ -120,24 +117,24 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime. } // Restore the object to its original condition. - annotations[LastAppliedConfigAnnotation] = original - accessor.SetAnnotations(annotations) + annots[annotations.LastAppliedConfigAnnotation] = original + accessor.SetAnnotations(annots) } else { // Otherwise, use the server side version of the object. accessor := info.Mapping.MetadataAccessor // Get the current annotations from the object. - annotations, err := accessor.Annotations(info.Object) + annots, err := accessor.Annotations(info.Object) if err != nil { return nil, err } - if annotations == nil { - annotations = map[string]string{} + if annots == nil { + annots = map[string]string{} } - original := annotations[LastAppliedConfigAnnotation] - delete(annotations, LastAppliedConfigAnnotation) - if err := accessor.SetAnnotations(info.Object, annotations); err != nil { + original := annots[annotations.LastAppliedConfigAnnotation] + delete(annots, annotations.LastAppliedConfigAnnotation) + if err := accessor.SetAnnotations(info.Object, annots); err != nil { return nil, err } @@ -147,8 +144,8 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime. } if annotate { - annotations[LastAppliedConfigAnnotation] = string(modified) - if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annotations); err != nil { + annots[annotations.LastAppliedConfigAnnotation] = string(modified) + if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil { return nil, err } @@ -159,8 +156,8 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime. } // Restore the object to its original condition. - annotations[LastAppliedConfigAnnotation] = original - if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annotations); err != nil { + annots[annotations.LastAppliedConfigAnnotation] = original + if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil { return nil, err } } diff --git a/pkg/kubectl/cmd/apply_test.go b/pkg/kubectl/cmd/apply_test.go index a46e0699d2..050dfd9256 100644 --- a/pkg/kubectl/cmd/apply_test.go +++ b/pkg/kubectl/cmd/apply_test.go @@ -28,9 +28,9 @@ import ( "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/annotations" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/client/unversioned/fake" - "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/runtime" ) @@ -117,7 +117,7 @@ func annotateRuntimeObject(t *testing.T, originalObj, currentObj runtime.Object, if currentAnnotations == nil { currentAnnotations = make(map[string]string) } - currentAnnotations[kubectl.LastAppliedConfigAnnotation] = string(original) + currentAnnotations[annotations.LastAppliedConfigAnnotation] = string(original) currentAccessor.SetAnnotations(currentAnnotations) current, err := json.Marshal(currentObj) if err != nil { @@ -151,7 +151,7 @@ func validatePatchApplication(t *testing.T, req *http.Request) { } annotationsMap := walkMapPath(t, patchMap, []string{"metadata", "annotations"}) - if _, ok := annotationsMap[kubectl.LastAppliedConfigAnnotation]; !ok { + if _, ok := annotationsMap[annotations.LastAppliedConfigAnnotation]; !ok { t.Fatalf("patch does not contain annotation:\n%s\n", patch) } diff --git a/test/e2e/deployment.go b/test/e2e/deployment.go index e61e7faae1..b32b74a068 100644 --- a/test/e2e/deployment.go +++ b/test/e2e/deployment.go @@ -38,6 +38,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "k8s.io/kubernetes/pkg/api/annotations" ) const ( @@ -220,7 +221,7 @@ func testNewDeployment(f *framework.Framework) { replicas := int32(1) framework.Logf("Creating simple deployment %s", deploymentName) d := newDeployment(deploymentName, replicas, podLabels, nginxImageName, nginxImage, extensions.RollingUpdateDeploymentStrategyType, nil) - d.Annotations = map[string]string{"test": "should-copy-to-replica-set", kubectl.LastAppliedConfigAnnotation: "should-not-copy-to-replica-set"} + d.Annotations = map[string]string{"test": "should-copy-to-replica-set", annotations.LastAppliedConfigAnnotation: "should-not-copy-to-replica-set"} _, err := c.Extensions().Deployments(ns).Create(d) Expect(err).NotTo(HaveOccurred()) defer stopDeployment(c, f.Client, ns, deploymentName) @@ -238,9 +239,9 @@ func testNewDeployment(f *framework.Framework) { Expect(err).NotTo(HaveOccurred()) // Check new RS annotations Expect(newRS.Annotations["test"]).Should(Equal("should-copy-to-replica-set")) - Expect(newRS.Annotations[kubectl.LastAppliedConfigAnnotation]).Should(Equal("")) + Expect(newRS.Annotations[annotations.LastAppliedConfigAnnotation]).Should(Equal("")) Expect(deployment.Annotations["test"]).Should(Equal("should-copy-to-replica-set")) - Expect(deployment.Annotations[kubectl.LastAppliedConfigAnnotation]).Should(Equal("should-not-copy-to-replica-set")) + Expect(deployment.Annotations[annotations.LastAppliedConfigAnnotation]).Should(Equal("should-not-copy-to-replica-set")) } func testRollingUpdateDeployment(f *framework.Framework) { diff --git a/test/e2e/kubectl.go b/test/e2e/kubectl.go index 541a0e5e25..0ce44b6682 100644 --- a/test/e2e/kubectl.go +++ b/test/e2e/kubectl.go @@ -40,11 +40,11 @@ import ( "github.com/ghodss/yaml" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/annotations" apierrs "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/unversioned" client "k8s.io/kubernetes/pkg/client/unversioned" "k8s.io/kubernetes/pkg/controller" - "k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/labels" utilnet "k8s.io/kubernetes/pkg/util/net" @@ -1432,7 +1432,7 @@ func forEachReplicationController(c *client.Client, ns, selectorKey, selectorVal func validateReplicationControllerConfiguration(rc api.ReplicationController) { if rc.Name == "redis-master" { - if _, ok := rc.Annotations[kubectl.LastAppliedConfigAnnotation]; !ok { + if _, ok := rc.Annotations[annotations.LastAppliedConfigAnnotation]; !ok { framework.Failf("Annotation not found in modified configuration:\n%v\n", rc) }