...

Source file src/k8s.io/apimachinery/pkg/util/managedfields/internal/lastappliedupdater.go

Documentation: k8s.io/apimachinery/pkg/util/managedfields/internal

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package internal
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  )
    26  
    27  type lastAppliedUpdater struct {
    28  	fieldManager Manager
    29  }
    30  
    31  var _ Manager = &lastAppliedUpdater{}
    32  
    33  // NewLastAppliedUpdater sets the client-side apply annotation up to date with
    34  // server-side apply managed fields
    35  func NewLastAppliedUpdater(fieldManager Manager) Manager {
    36  	return &lastAppliedUpdater{
    37  		fieldManager: fieldManager,
    38  	}
    39  }
    40  
    41  // Update implements Manager.
    42  func (f *lastAppliedUpdater) Update(liveObj, newObj runtime.Object, managed Managed, manager string) (runtime.Object, Managed, error) {
    43  	return f.fieldManager.Update(liveObj, newObj, managed, manager)
    44  }
    45  
    46  // server-side apply managed fields
    47  func (f *lastAppliedUpdater) Apply(liveObj, newObj runtime.Object, managed Managed, manager string, force bool) (runtime.Object, Managed, error) {
    48  	liveObj, managed, err := f.fieldManager.Apply(liveObj, newObj, managed, manager, force)
    49  	if err != nil {
    50  		return liveObj, managed, err
    51  	}
    52  
    53  	// Sync the client-side apply annotation only from kubectl server-side apply.
    54  	// To opt-out of this behavior, users may specify a different field manager.
    55  	//
    56  	// If the client-side apply annotation doesn't exist,
    57  	// then continue because we have no annotation to update
    58  	if manager == "kubectl" && hasLastApplied(liveObj) {
    59  		lastAppliedValue, err := buildLastApplied(newObj)
    60  		if err != nil {
    61  			return nil, nil, fmt.Errorf("failed to build last-applied annotation: %v", err)
    62  		}
    63  		err = SetLastApplied(liveObj, lastAppliedValue)
    64  		if err != nil {
    65  			return nil, nil, fmt.Errorf("failed to set last-applied annotation: %v", err)
    66  		}
    67  	}
    68  	return liveObj, managed, err
    69  }
    70  
    71  func hasLastApplied(obj runtime.Object) bool {
    72  	var accessor, err = meta.Accessor(obj)
    73  	if err != nil {
    74  		panic(fmt.Sprintf("couldn't get accessor: %v", err))
    75  	}
    76  	var annotations = accessor.GetAnnotations()
    77  	if annotations == nil {
    78  		return false
    79  	}
    80  	lastApplied, ok := annotations[LastAppliedConfigAnnotation]
    81  	return ok && len(lastApplied) > 0
    82  }
    83  
    84  func buildLastApplied(obj runtime.Object) (string, error) {
    85  	obj = obj.DeepCopyObject()
    86  
    87  	var accessor, err = meta.Accessor(obj)
    88  	if err != nil {
    89  		panic(fmt.Sprintf("couldn't get accessor: %v", err))
    90  	}
    91  
    92  	// Remove the annotation from the object before encoding the object
    93  	var annotations = accessor.GetAnnotations()
    94  	delete(annotations, LastAppliedConfigAnnotation)
    95  	accessor.SetAnnotations(annotations)
    96  
    97  	lastApplied, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
    98  	if err != nil {
    99  		return "", fmt.Errorf("couldn't encode object into last applied annotation: %v", err)
   100  	}
   101  	return string(lastApplied), nil
   102  }
   103  

View as plain text