...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/resourceactuation/resourceactuation.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/resourceactuation

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package resourceactuation
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/reconciliationinterval"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    22  
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  )
    25  
    26  // ShouldSkip skips a resource actuatation if the ReconcileIntervalInSecondsAnnotation = 0 and the KRM resource has not changed since its last UpToDate.
    27  // This will disable drift correction on corresponding GCP resources since the reconcileInterval is set to 0.
    28  func ShouldSkip(u *unstructured.Unstructured) (bool, error) {
    29  	generation, found, err := unstructured.NestedInt64(u.Object, "metadata", "generation")
    30  	if err != nil {
    31  		return false, fmt.Errorf("error getting the value for 'metadata.generation' %v", err)
    32  	}
    33  	if !found {
    34  		return false, nil
    35  	}
    36  	observedGeneration, found, err := unstructured.NestedInt64(u.Object, "status", "observedGeneration")
    37  	if err != nil {
    38  		return false, fmt.Errorf("error getting the value for 'status.observedGeneration': %v", err)
    39  	}
    40  	if !found {
    41  		return false, nil
    42  	}
    43  	if observedGeneration != generation {
    44  		return false, nil
    45  	}
    46  
    47  	if val, ok := k8s.GetAnnotation(k8s.ReconcileIntervalInSecondsAnnotation, u); ok {
    48  		reconcileInterval, err := reconciliationinterval.MeanReconcileReenqueuePeriodFromAnnotation(val)
    49  		if err != nil {
    50  			return false, err
    51  		}
    52  		if reconcileInterval == 0 {
    53  			conditions, found, err := unstructured.NestedSlice(u.Object, "status", "conditions")
    54  			if err != nil {
    55  				return false, fmt.Errorf("error getting object conditions: %v", err)
    56  			}
    57  			if !found {
    58  				return false, nil
    59  			}
    60  			for _, condition := range conditions {
    61  				conditionMap, ok := condition.(map[string]interface{})
    62  				if !ok {
    63  					return false, fmt.Errorf("error coverting condition %v to map", condition)
    64  				}
    65  				if status, foundStatus := conditionMap["status"].(string); foundStatus && status == "True" {
    66  					if reason, foundCondition := conditionMap["reason"].(string); foundCondition && reason == k8s.UpToDate {
    67  						return true, nil
    68  					}
    69  				}
    70  			}
    71  		}
    72  	}
    73  	return false, nil
    74  }
    75  

View as plain text