sigs.k8s.io/cluster-api@v1.7.1/util/topology/topology.go (about)

     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 topology implements topology utility functions.
    18  package topology
    19  
    20  import (
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    23  
    24  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    25  )
    26  
    27  // ShouldSkipImmutabilityChecks returns true if it is a dry-run request and the object has the
    28  // TopologyDryRunAnnotation annotation set, false otherwise.
    29  // This ensures that the immutability check is skipped only when dry-running and when the operations has been invoked by the topology controller.
    30  // Instead, kubectl dry-run behavior remains consistent with the one user gets when doing kubectl apply (immutability is enforced).
    31  func ShouldSkipImmutabilityChecks(req admission.Request, obj metav1.Object) bool {
    32  	// Check if the request is a dry-run
    33  	if req.DryRun == nil || !*req.DryRun {
    34  		return false
    35  	}
    36  
    37  	if obj == nil {
    38  		return false
    39  	}
    40  
    41  	// Check for the TopologyDryRunAnnotation
    42  	annotations := obj.GetAnnotations()
    43  	if annotations == nil {
    44  		return false
    45  	}
    46  	if _, ok := annotations[clusterv1.TopologyDryRunAnnotation]; ok {
    47  		return true
    48  	}
    49  	return false
    50  }