...

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

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

     1  /*
     2  Copyright 2022 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  	apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  )
    26  
    27  // LastAppliedConfigAnnotation is the annotation used to store the previous
    28  // configuration of a resource for use in a three way diff by UpdateApplyAnnotation.
    29  //
    30  // This is a copy of the corev1 annotation since we don't want to depend on the whole package.
    31  const LastAppliedConfigAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
    32  
    33  // SetLastApplied sets the last-applied annotation the given value in
    34  // the object.
    35  func SetLastApplied(obj runtime.Object, value string) error {
    36  	accessor, err := meta.Accessor(obj)
    37  	if err != nil {
    38  		panic(fmt.Sprintf("couldn't get accessor: %v", err))
    39  	}
    40  	var annotations = accessor.GetAnnotations()
    41  	if annotations == nil {
    42  		annotations = map[string]string{}
    43  	}
    44  	annotations[LastAppliedConfigAnnotation] = value
    45  	if err := apimachineryvalidation.ValidateAnnotationsSize(annotations); err != nil {
    46  		delete(annotations, LastAppliedConfigAnnotation)
    47  	}
    48  	accessor.SetAnnotations(annotations)
    49  	return nil
    50  }
    51  

View as plain text