123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /*
- Copyright 2014 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 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"
- )
- type debugError interface {
- DebugError() (msg string, args []interface{})
- }
- // GetOriginalConfiguration retrieves the original configuration of the object
- // from the annotation, or nil if no annotation was found.
- func GetOriginalConfiguration(mapping *meta.RESTMapping, obj runtime.Object) ([]byte, error) {
- annots, err := mapping.MetadataAccessor.Annotations(obj)
- if err != nil {
- return nil, err
- }
- if annots == nil {
- return nil, nil
- }
- original, ok := annots[annotations.LastAppliedConfigAnnotation]
- if !ok {
- return nil, nil
- }
- return []byte(original), nil
- }
- // SetOriginalConfiguration sets the original configuration of the object
- // as the annotation on the object for later use in computing a three way patch.
- func SetOriginalConfiguration(info *resource.Info, original []byte) error {
- if len(original) < 1 {
- return nil
- }
- accessor := info.Mapping.MetadataAccessor
- annots, err := accessor.Annotations(info.Object)
- if err != nil {
- return err
- }
- if annots == nil {
- annots = map[string]string{}
- }
- annots[annotations.LastAppliedConfigAnnotation] = string(original)
- if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
- return err
- }
- return nil
- }
- // GetModifiedConfiguration retrieves the modified configuration of the object.
- // If annotate is true, it embeds the result as an anotation in the modified
- // configuration. If an object was read from the command input, it will use that
- // version of the object. Otherwise, it will use the version from the server.
- func GetModifiedConfiguration(info *resource.Info, annotate bool, codec runtime.Encoder) ([]byte, error) {
- // First serialize the object without the annotation to prevent recursion,
- // then add that serialization to it as the annotation and serialize it again.
- var modified []byte
- if info.VersionedObject != nil {
- // If an object was read from input, use that version.
- accessor, err := meta.Accessor(info.VersionedObject)
- if err != nil {
- return nil, err
- }
- // Get the current annotations from the object.
- annots := accessor.GetAnnotations()
- if annots == nil {
- annots = map[string]string{}
- }
- 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)
- if err != nil {
- return nil, err
- }
- if annotate {
- 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)
- if err != nil {
- return nil, err
- }
- }
- // Restore the object to its original condition.
- 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.
- annots, err := accessor.Annotations(info.Object)
- if err != nil {
- return nil, err
- }
- if annots == nil {
- annots = map[string]string{}
- }
- original := annots[annotations.LastAppliedConfigAnnotation]
- delete(annots, annotations.LastAppliedConfigAnnotation)
- if err := accessor.SetAnnotations(info.Object, annots); err != nil {
- return nil, err
- }
- modified, err = runtime.Encode(codec, info.Object)
- if err != nil {
- return nil, err
- }
- if annotate {
- annots[annotations.LastAppliedConfigAnnotation] = string(modified)
- if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
- return nil, err
- }
- modified, err = runtime.Encode(codec, info.Object)
- if err != nil {
- return nil, err
- }
- }
- // Restore the object to its original condition.
- annots[annotations.LastAppliedConfigAnnotation] = original
- if err := info.Mapping.MetadataAccessor.SetAnnotations(info.Object, annots); err != nil {
- return nil, err
- }
- }
- return modified, nil
- }
- // UpdateApplyAnnotation calls CreateApplyAnnotation if the last applied
- // configuration annotation is already present. Otherwise, it does nothing.
- func UpdateApplyAnnotation(info *resource.Info, codec runtime.Encoder) error {
- if original, err := GetOriginalConfiguration(info.Mapping, info.Object); err != nil || len(original) <= 0 {
- return err
- }
- return CreateApplyAnnotation(info, codec)
- }
- // CreateApplyAnnotation gets the modified configuration of the object,
- // without embedding it again, and then sets it on the object as the annotation.
- func CreateApplyAnnotation(info *resource.Info, codec runtime.Encoder) error {
- modified, err := GetModifiedConfiguration(info, false, codec)
- if err != nil {
- return err
- }
- return SetOriginalConfiguration(info, modified)
- }
- // Create the annotation used by kubectl apply only when createAnnotation is true
- // Otherwise, only update the annotation when it already exists
- func CreateOrUpdateAnnotation(createAnnotation bool, info *resource.Info, codec runtime.Encoder) error {
- if createAnnotation {
- return CreateApplyAnnotation(info, codec)
- }
- return UpdateApplyAnnotation(info, codec)
- }
|