k8s.io/kubernetes@v1.29.3/pkg/registry/rbac/helpers.go (about)

     1  /*
     2  Copyright 2017 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 rbac
    18  
    19  import (
    20  	"reflect"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/apimachinery/pkg/conversion"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  )
    26  
    27  // IsOnlyMutatingGCFields checks finalizers and ownerrefs which GC manipulates
    28  // and indicates that only those fields are changing
    29  func IsOnlyMutatingGCFields(obj, old runtime.Object, equalities conversion.Equalities) bool {
    30  	if old == nil || reflect.ValueOf(old).IsNil() {
    31  		return false
    32  	}
    33  
    34  	// make a copy of the newObj so that we can stomp for comparison
    35  	copied := obj.DeepCopyObject()
    36  	copiedMeta, err := meta.Accessor(copied)
    37  	if err != nil {
    38  		return false
    39  	}
    40  	oldMeta, err := meta.Accessor(old)
    41  	if err != nil {
    42  		return false
    43  	}
    44  	copiedMeta.SetOwnerReferences(oldMeta.GetOwnerReferences())
    45  	copiedMeta.SetFinalizers(oldMeta.GetFinalizers())
    46  	copiedMeta.SetSelfLink(oldMeta.GetSelfLink())
    47  	copiedMeta.SetManagedFields(oldMeta.GetManagedFields())
    48  
    49  	return equalities.DeepEqual(copied, old)
    50  }