move server client shared annotations to new api/annotations package

pull/6/head
jianhuiz 2016-05-04 17:37:03 -07:00
parent e973b5d27a
commit 441e206671
7 changed files with 81 additions and 42 deletions

View File

@ -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"

View File

@ -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

View File

@ -25,6 +25,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/annotations"
"k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions" "k8s.io/kubernetes/pkg/apis/extensions"
@ -34,7 +35,6 @@ import (
"k8s.io/kubernetes/pkg/client/record" "k8s.io/kubernetes/pkg/client/record"
"k8s.io/kubernetes/pkg/controller" "k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/framework" "k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
deploymentutil "k8s.io/kubernetes/pkg/util/deployment" deploymentutil "k8s.io/kubernetes/pkg/util/deployment"
utilerrors "k8s.io/kubernetes/pkg/util/errors" 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 // See https://github.com/kubernetes/kubernetes/pull/20035#issuecomment-179558615
func skipCopyAnnotation(key string) bool { func skipCopyAnnotation(key string) bool {
// Skip apply annotations and revision annotations. // 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 { func getSkippedAnnotations(annotations map[string]string) map[string]string {

View File

@ -19,6 +19,7 @@ package kubectl
import ( import (
"encoding/json" "encoding/json"
"k8s.io/kubernetes/pkg/api/annotations"
"k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
@ -28,23 +29,19 @@ type debugError interface {
DebugError() (msg string, args []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 // GetOriginalConfiguration retrieves the original configuration of the object
// from the annotation, or nil if no annotation was found. // from the annotation, or nil if no annotation was found.
func GetOriginalConfiguration(info *resource.Info) ([]byte, error) { 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 { if err != nil {
return nil, err return nil, err
} }
if annotations == nil { if annots == nil {
return nil, nil return nil, nil
} }
original, ok := annotations[LastAppliedConfigAnnotation] original, ok := annots[annotations.LastAppliedConfigAnnotation]
if !ok { if !ok {
return nil, nil return nil, nil
} }
@ -60,17 +57,17 @@ func SetOriginalConfiguration(info *resource.Info, original []byte) error {
} }
accessor := info.Mapping.MetadataAccessor accessor := info.Mapping.MetadataAccessor
annotations, err := accessor.Annotations(info.Object) annots, err := accessor.Annotations(info.Object)
if err != nil { if err != nil {
return err return err
} }
if annotations == nil { if annots == nil {
annotations = map[string]string{} annots = map[string]string{}
} }
annotations[LastAppliedConfigAnnotation] = string(original) annots[annotations.LastAppliedConfigAnnotation] = string(original)
if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annotations); err != nil { if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
return err return err
} }
@ -93,14 +90,14 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime.
} }
// Get the current annotations from the object. // Get the current annotations from the object.
annotations := accessor.GetAnnotations() annots := accessor.GetAnnotations()
if annotations == nil { if annots == nil {
annotations = map[string]string{} annots = map[string]string{}
} }
original := annotations[LastAppliedConfigAnnotation] original := annots[annotations.LastAppliedConfigAnnotation]
delete(annotations, LastAppliedConfigAnnotation) delete(annots, annotations.LastAppliedConfigAnnotation)
accessor.SetAnnotations(annotations) accessor.SetAnnotations(annots)
// TODO: this needs to be abstracted - there should be no assumption that versioned object // TODO: this needs to be abstracted - there should be no assumption that versioned object
// can be marshalled to JSON. // can be marshalled to JSON.
modified, err = json.Marshal(info.VersionedObject) modified, err = json.Marshal(info.VersionedObject)
@ -109,8 +106,8 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime.
} }
if annotate { if annotate {
annotations[LastAppliedConfigAnnotation] = string(modified) annots[annotations.LastAppliedConfigAnnotation] = string(modified)
accessor.SetAnnotations(annotations) accessor.SetAnnotations(annots)
// TODO: this needs to be abstracted - there should be no assumption that versioned object // TODO: this needs to be abstracted - there should be no assumption that versioned object
// can be marshalled to JSON. // can be marshalled to JSON.
modified, err = json.Marshal(info.VersionedObject) 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. // Restore the object to its original condition.
annotations[LastAppliedConfigAnnotation] = original annots[annotations.LastAppliedConfigAnnotation] = original
accessor.SetAnnotations(annotations) accessor.SetAnnotations(annots)
} else { } else {
// Otherwise, use the server side version of the object. // Otherwise, use the server side version of the object.
accessor := info.Mapping.MetadataAccessor accessor := info.Mapping.MetadataAccessor
// Get the current annotations from the object. // Get the current annotations from the object.
annotations, err := accessor.Annotations(info.Object) annots, err := accessor.Annotations(info.Object)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if annotations == nil { if annots == nil {
annotations = map[string]string{} annots = map[string]string{}
} }
original := annotations[LastAppliedConfigAnnotation] original := annots[annotations.LastAppliedConfigAnnotation]
delete(annotations, LastAppliedConfigAnnotation) delete(annots, annotations.LastAppliedConfigAnnotation)
if err := accessor.SetAnnotations(info.Object, annotations); err != nil { if err := accessor.SetAnnotations(info.Object, annots); err != nil {
return nil, err return nil, err
} }
@ -147,8 +144,8 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime.
} }
if annotate { if annotate {
annotations[LastAppliedConfigAnnotation] = string(modified) annots[annotations.LastAppliedConfigAnnotation] = string(modified)
if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annotations); err != nil { if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
return nil, err return nil, err
} }
@ -159,8 +156,8 @@ func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime.
} }
// Restore the object to its original condition. // Restore the object to its original condition.
annotations[LastAppliedConfigAnnotation] = original annots[annotations.LastAppliedConfigAnnotation] = original
if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annotations); err != nil { if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
return nil, err return nil, err
} }
} }

View File

@ -28,9 +28,9 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/annotations"
"k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/client/unversioned/fake" "k8s.io/kubernetes/pkg/client/unversioned/fake"
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
) )
@ -117,7 +117,7 @@ func annotateRuntimeObject(t *testing.T, originalObj, currentObj runtime.Object,
if currentAnnotations == nil { if currentAnnotations == nil {
currentAnnotations = make(map[string]string) currentAnnotations = make(map[string]string)
} }
currentAnnotations[kubectl.LastAppliedConfigAnnotation] = string(original) currentAnnotations[annotations.LastAppliedConfigAnnotation] = string(original)
currentAccessor.SetAnnotations(currentAnnotations) currentAccessor.SetAnnotations(currentAnnotations)
current, err := json.Marshal(currentObj) current, err := json.Marshal(currentObj)
if err != nil { if err != nil {
@ -151,7 +151,7 @@ func validatePatchApplication(t *testing.T, req *http.Request) {
} }
annotationsMap := walkMapPath(t, patchMap, []string{"metadata", "annotations"}) 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) t.Fatalf("patch does not contain annotation:\n%s\n", patch)
} }

View File

@ -38,6 +38,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/api/annotations"
) )
const ( const (
@ -220,7 +221,7 @@ func testNewDeployment(f *framework.Framework) {
replicas := int32(1) replicas := int32(1)
framework.Logf("Creating simple deployment %s", deploymentName) framework.Logf("Creating simple deployment %s", deploymentName)
d := newDeployment(deploymentName, replicas, podLabels, nginxImageName, nginxImage, extensions.RollingUpdateDeploymentStrategyType, nil) 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) _, err := c.Extensions().Deployments(ns).Create(d)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
defer stopDeployment(c, f.Client, ns, deploymentName) defer stopDeployment(c, f.Client, ns, deploymentName)
@ -238,9 +239,9 @@ func testNewDeployment(f *framework.Framework) {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
// Check new RS annotations // Check new RS annotations
Expect(newRS.Annotations["test"]).Should(Equal("should-copy-to-replica-set")) 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["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) { func testRollingUpdateDeployment(f *framework.Framework) {

View File

@ -40,11 +40,11 @@ import (
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/annotations"
apierrs "k8s.io/kubernetes/pkg/api/errors" apierrs "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned" client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/controller" "k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/labels" "k8s.io/kubernetes/pkg/labels"
utilnet "k8s.io/kubernetes/pkg/util/net" utilnet "k8s.io/kubernetes/pkg/util/net"
@ -1432,7 +1432,7 @@ func forEachReplicationController(c *client.Client, ns, selectorKey, selectorVal
func validateReplicationControllerConfiguration(rc api.ReplicationController) { func validateReplicationControllerConfiguration(rc api.ReplicationController) {
if rc.Name == "redis-master" { 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) framework.Failf("Annotation not found in modified configuration:\n%v\n", rc)
} }