...

Source file src/github.com/cert-manager/issuer-lib/conditions/issuer.go

Documentation: github.com/cert-manager/issuer-lib/conditions

     1  /*
     2  Copyright 2023 The cert-manager 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 conditions
    18  
    19  import (
    20  	cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
    21  	cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/utils/clock"
    24  )
    25  
    26  // Update the status with the provided condition details & return
    27  // the added condition.
    28  func SetIssuerStatusCondition(
    29  	clock clock.PassiveClock,
    30  	existingConditions []cmapi.IssuerCondition,
    31  	patchConditions *[]cmapi.IssuerCondition,
    32  	observedGeneration int64,
    33  	conditionType cmapi.IssuerConditionType,
    34  	status cmmeta.ConditionStatus,
    35  	reason, message string,
    36  ) (*cmapi.IssuerCondition, *metav1.Time) {
    37  	newCondition := cmapi.IssuerCondition{
    38  		Type:               conditionType,
    39  		Status:             status,
    40  		Reason:             reason,
    41  		Message:            message,
    42  		ObservedGeneration: observedGeneration,
    43  	}
    44  
    45  	nowTime := metav1.NewTime(clock.Now())
    46  	newCondition.LastTransitionTime = &nowTime
    47  
    48  	// Reset the LastTransitionTime if the status hasn't changed
    49  	for _, cond := range existingConditions {
    50  		if cond.Type != conditionType {
    51  			continue
    52  		}
    53  
    54  		// If this update doesn't contain a state transition, we don't update
    55  		// the conditions LastTransitionTime to Now()
    56  		if cond.Status == status {
    57  			newCondition.LastTransitionTime = cond.LastTransitionTime
    58  		}
    59  	}
    60  
    61  	// Search through existing conditions
    62  	for idx, cond := range *patchConditions {
    63  		// Skip unrelated conditions
    64  		if cond.Type != conditionType {
    65  			continue
    66  		}
    67  
    68  		// Overwrite the existing condition
    69  		(*patchConditions)[idx] = newCondition
    70  
    71  		return &newCondition, &nowTime
    72  	}
    73  
    74  	// If we've not found an existing condition of this type, we simply insert
    75  	// the new condition into the slice.
    76  	*patchConditions = append(*patchConditions, newCondition)
    77  
    78  	return &newCondition, &nowTime
    79  }
    80  
    81  func GetIssuerStatusCondition(
    82  	conditions []cmapi.IssuerCondition,
    83  	conditionType cmapi.IssuerConditionType,
    84  ) *cmapi.IssuerCondition {
    85  	for _, cond := range conditions {
    86  		if cond.Type == conditionType {
    87  			return &cond
    88  		}
    89  	}
    90  	return nil
    91  }
    92  

View as plain text