ceph-csi/vendor/k8s.io/kubernetes/pkg/kubectl/apply.go

147 lines
4.4 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
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 (
2018-03-06 22:33:18 +00:00
"k8s.io/api/core/v1"
2018-01-09 18:57:14 +00:00
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
)
2018-07-18 14:47:22 +00:00
var metadataAccessor = meta.NewAccessor()
2018-01-09 18:57:14 +00:00
// GetOriginalConfiguration retrieves the original configuration of the object
// from the annotation, or nil if no annotation was found.
2018-07-18 14:47:22 +00:00
func GetOriginalConfiguration(obj runtime.Object) ([]byte, error) {
annots, err := metadataAccessor.Annotations(obj)
2018-01-09 18:57:14 +00:00
if err != nil {
return nil, err
}
if annots == nil {
return nil, nil
}
2018-03-06 22:33:18 +00:00
original, ok := annots[v1.LastAppliedConfigAnnotation]
2018-01-09 18:57:14 +00:00
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.
2018-07-18 14:47:22 +00:00
func setOriginalConfiguration(obj runtime.Object, original []byte) error {
2018-01-09 18:57:14 +00:00
if len(original) < 1 {
return nil
}
2018-07-18 14:47:22 +00:00
annots, err := metadataAccessor.Annotations(obj)
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
if annots == nil {
annots = map[string]string{}
}
2018-03-06 22:33:18 +00:00
annots[v1.LastAppliedConfigAnnotation] = string(original)
2018-07-18 14:47:22 +00:00
return metadataAccessor.SetAnnotations(obj, annots)
2018-01-09 18:57:14 +00:00
}
// GetModifiedConfiguration retrieves the modified configuration of the object.
// If annotate is true, it embeds the result as an annotation 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.
2018-07-18 14:47:22 +00:00
func GetModifiedConfiguration(obj runtime.Object, annotate bool, codec runtime.Encoder) ([]byte, error) {
2018-01-09 18:57:14 +00:00
// 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
// Otherwise, use the server side version of the object.
// Get the current annotations from the object.
2018-07-18 14:47:22 +00:00
annots, err := metadataAccessor.Annotations(obj)
2018-01-09 18:57:14 +00:00
if err != nil {
return nil, err
}
if annots == nil {
annots = map[string]string{}
}
2018-03-06 22:33:18 +00:00
original := annots[v1.LastAppliedConfigAnnotation]
delete(annots, v1.LastAppliedConfigAnnotation)
2018-07-18 14:47:22 +00:00
if err := metadataAccessor.SetAnnotations(obj, annots); err != nil {
2018-01-09 18:57:14 +00:00
return nil, err
}
2018-07-18 14:47:22 +00:00
modified, err = runtime.Encode(codec, obj)
2018-01-09 18:57:14 +00:00
if err != nil {
return nil, err
}
if annotate {
2018-03-06 22:33:18 +00:00
annots[v1.LastAppliedConfigAnnotation] = string(modified)
2018-07-18 14:47:22 +00:00
if err := metadataAccessor.SetAnnotations(obj, annots); err != nil {
2018-01-09 18:57:14 +00:00
return nil, err
}
2018-07-18 14:47:22 +00:00
modified, err = runtime.Encode(codec, obj)
2018-01-09 18:57:14 +00:00
if err != nil {
return nil, err
}
}
// Restore the object to its original condition.
2018-03-06 22:33:18 +00:00
annots[v1.LastAppliedConfigAnnotation] = original
2018-07-18 14:47:22 +00:00
if err := metadataAccessor.SetAnnotations(obj, annots); err != nil {
2018-01-09 18:57:14 +00:00
return nil, err
}
return modified, nil
}
// UpdateApplyAnnotation calls CreateApplyAnnotation if the last applied
// configuration annotation is already present. Otherwise, it does nothing.
2018-07-18 14:47:22 +00:00
func UpdateApplyAnnotation(obj runtime.Object, codec runtime.Encoder) error {
if original, err := GetOriginalConfiguration(obj); err != nil || len(original) <= 0 {
2018-01-09 18:57:14 +00:00
return err
}
2018-07-18 14:47:22 +00:00
return CreateApplyAnnotation(obj, codec)
2018-01-09 18:57:14 +00:00
}
// CreateApplyAnnotation gets the modified configuration of the object,
// without embedding it again, and then sets it on the object as the annotation.
2018-07-18 14:47:22 +00:00
func CreateApplyAnnotation(obj runtime.Object, codec runtime.Encoder) error {
modified, err := GetModifiedConfiguration(obj, false, codec)
2018-01-09 18:57:14 +00:00
if err != nil {
return err
}
2018-07-18 14:47:22 +00:00
return setOriginalConfiguration(obj, modified)
2018-01-09 18:57:14 +00:00
}
2018-11-26 18:23:56 +00:00
// CreateOrUpdateAnnotation creates the annotation used by
// kubectl apply only when createAnnotation is true
2018-01-09 18:57:14 +00:00
// Otherwise, only update the annotation when it already exists
2018-07-18 14:47:22 +00:00
func CreateOrUpdateAnnotation(createAnnotation bool, obj runtime.Object, codec runtime.Encoder) error {
2018-01-09 18:57:14 +00:00
if createAnnotation {
2018-07-18 14:47:22 +00:00
return CreateApplyAnnotation(obj, codec)
2018-01-09 18:57:14 +00:00
}
2018-07-18 14:47:22 +00:00
return UpdateApplyAnnotation(obj, codec)
2018-01-09 18:57:14 +00:00
}