github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/conditions.go (about)

     1  /*
     2  Copyright 2020 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 meta
    18  
    19  import (
    20  	"time"
    21  
    22  	metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1"
    23  )
    24  
    25  // SetStatusCondition sets the corresponding condition in conditions to newCondition.
    26  // conditions must be non-nil.
    27  //  1. if the condition of the specified type already exists (all fields of the existing condition are updated to
    28  //     newCondition, LastTransitionTime is set to now if the new status differs from the old status)
    29  //  2. if a condition of the specified type does not exist (LastTransitionTime is set to now() if unset, and newCondition is appended)
    30  func SetStatusCondition(conditions *[]metav1.Condition, newCondition metav1.Condition) {
    31  	if conditions == nil {
    32  		return
    33  	}
    34  	existingCondition := FindStatusCondition(*conditions, newCondition.Type)
    35  	if existingCondition == nil {
    36  		if newCondition.LastTransitionTime.IsZero() {
    37  			newCondition.LastTransitionTime = metav1.NewTime(time.Now())
    38  		}
    39  		*conditions = append(*conditions, newCondition)
    40  		return
    41  	}
    42  
    43  	if existingCondition.Status != newCondition.Status {
    44  		existingCondition.Status = newCondition.Status
    45  		if !newCondition.LastTransitionTime.IsZero() {
    46  			existingCondition.LastTransitionTime = newCondition.LastTransitionTime
    47  		} else {
    48  			existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
    49  		}
    50  	}
    51  
    52  	existingCondition.Reason = newCondition.Reason
    53  	existingCondition.Message = newCondition.Message
    54  	existingCondition.ObservedGeneration = newCondition.ObservedGeneration
    55  }
    56  
    57  // RemoveStatusCondition removes the corresponding conditionType from conditions.
    58  // conditions must be non-nil.
    59  func RemoveStatusCondition(conditions *[]metav1.Condition, conditionType string) {
    60  	if conditions == nil || len(*conditions) == 0 {
    61  		return
    62  	}
    63  	newConditions := make([]metav1.Condition, 0, len(*conditions)-1)
    64  	for _, condition := range *conditions {
    65  		if condition.Type != conditionType {
    66  			newConditions = append(newConditions, condition)
    67  		}
    68  	}
    69  
    70  	*conditions = newConditions
    71  }
    72  
    73  // FindStatusCondition finds the conditionType in conditions.
    74  func FindStatusCondition(conditions []metav1.Condition, conditionType string) *metav1.Condition {
    75  	for i := range conditions {
    76  		if conditions[i].Type == conditionType {
    77  			return &conditions[i]
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // IsStatusConditionTrue returns true when the conditionType is present and set to `metav1.ConditionTrue`
    85  func IsStatusConditionTrue(conditions []metav1.Condition, conditionType string) bool {
    86  	return IsStatusConditionPresentAndEqual(conditions, conditionType, metav1.ConditionTrue)
    87  }
    88  
    89  // IsStatusConditionFalse returns true when the conditionType is present and set to `metav1.ConditionFalse`
    90  func IsStatusConditionFalse(conditions []metav1.Condition, conditionType string) bool {
    91  	return IsStatusConditionPresentAndEqual(conditions, conditionType, metav1.ConditionFalse)
    92  }
    93  
    94  // IsStatusConditionPresentAndEqual returns true when conditionType is present and equal to status.
    95  func IsStatusConditionPresentAndEqual(conditions []metav1.Condition, conditionType string, status metav1.ConditionStatus) bool {
    96  	for _, condition := range conditions {
    97  		if condition.Type == conditionType {
    98  			return condition.Status == status
    99  		}
   100  	}
   101  	return false
   102  }