...

Source file src/github.com/cert-manager/issuer-lib/conditions/certificaterequest.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 SetCertificateRequestStatusCondition(
    29  	clock clock.PassiveClock,
    30  	existingConditions []cmapi.CertificateRequestCondition,
    31  	patchConditions *[]cmapi.CertificateRequestCondition,
    32  	conditionType cmapi.CertificateRequestConditionType,
    33  	status cmmeta.ConditionStatus,
    34  	reason, message string,
    35  ) (*cmapi.CertificateRequestCondition, *metav1.Time) {
    36  	newCondition := cmapi.CertificateRequestCondition{
    37  		Type:    conditionType,
    38  		Status:  status,
    39  		Reason:  reason,
    40  		Message: message,
    41  	}
    42  
    43  	nowTime := metav1.NewTime(clock.Now())
    44  	newCondition.LastTransitionTime = &nowTime
    45  
    46  	// Reset the LastTransitionTime if the status hasn't changed
    47  	for _, cond := range existingConditions {
    48  		if cond.Type != conditionType {
    49  			continue
    50  		}
    51  
    52  		// If this update doesn't contain a state transition, we don't update
    53  		// the conditions LastTransitionTime to Now()
    54  		if cond.Status == status {
    55  			newCondition.LastTransitionTime = cond.LastTransitionTime
    56  		}
    57  	}
    58  
    59  	// Search through existing conditions
    60  	for idx, cond := range *patchConditions {
    61  		// Skip unrelated conditions
    62  		if cond.Type != conditionType {
    63  			continue
    64  		}
    65  
    66  		// Overwrite the existing condition
    67  		(*patchConditions)[idx] = newCondition
    68  
    69  		return &newCondition, &nowTime
    70  	}
    71  
    72  	// If we've not found an existing condition of this type, we simply insert
    73  	// the new condition into the slice.
    74  	*patchConditions = append(*patchConditions, newCondition)
    75  
    76  	return &newCondition, &nowTime
    77  }
    78  

View as plain text