blob: 934790dcb16a0af2aa94a7d1c845a637b1ec227e [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2020 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package meta
18
19import (
20 "time"
21
22 metav1 "k8s.io/apimachinery/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)
30func 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}
55
56// RemoveStatusCondition removes the corresponding conditionType from conditions.
57// conditions must be non-nil.
58func RemoveStatusCondition(conditions *[]metav1.Condition, conditionType string) {
59 if conditions == nil {
60 return
61 }
62 newConditions := make([]metav1.Condition, 0, len(*conditions)-1)
63 for _, condition := range *conditions {
64 if condition.Type != conditionType {
65 newConditions = append(newConditions, condition)
66 }
67 }
68
69 *conditions = newConditions
70}
71
72// FindStatusCondition finds the conditionType in conditions.
73func FindStatusCondition(conditions []metav1.Condition, conditionType string) *metav1.Condition {
74 for i := range conditions {
75 if conditions[i].Type == conditionType {
76 return &conditions[i]
77 }
78 }
79
80 return nil
81}
82
83// IsStatusConditionTrue returns true when the conditionType is present and set to `metav1.ConditionTrue`
84func IsStatusConditionTrue(conditions []metav1.Condition, conditionType string) bool {
85 return IsStatusConditionPresentAndEqual(conditions, conditionType, metav1.ConditionTrue)
86}
87
88// IsStatusConditionFalse returns true when the conditionType is present and set to `metav1.ConditionFalse`
89func IsStatusConditionFalse(conditions []metav1.Condition, conditionType string) bool {
90 return IsStatusConditionPresentAndEqual(conditions, conditionType, metav1.ConditionFalse)
91}
92
93// IsStatusConditionPresentAndEqual returns true when conditionType is present and equal to status.
94func IsStatusConditionPresentAndEqual(conditions []metav1.Condition, conditionType string, status metav1.ConditionStatus) bool {
95 for _, condition := range conditions {
96 if condition.Type == conditionType {
97 return condition.Status == status
98 }
99 }
100 return false
101}