2 Copyright 2017 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 // SetCRDCondition sets the status condition. It either overwrites the existing one or
28 func SetCRDCondition(crd *CustomResourceDefinition, newCondition CustomResourceDefinitionCondition) {
29 existingCondition := FindCRDCondition(crd, newCondition.Type)
30 if existingCondition == nil {
31 newCondition.LastTransitionTime = metav1.NewTime(time.Now())
32 crd.Status.Conditions = append(crd.Status.Conditions, newCondition)
36 if existingCondition.Status != newCondition.Status {
37 existingCondition.Status = newCondition.Status
38 existingCondition.LastTransitionTime = newCondition.LastTransitionTime
41 existingCondition.Reason = newCondition.Reason
42 existingCondition.Message = newCondition.Message
45 // RemoveCRDCondition removes the status condition.
46 func RemoveCRDCondition(crd *CustomResourceDefinition, conditionType CustomResourceDefinitionConditionType) {
47 newConditions := []CustomResourceDefinitionCondition{}
48 for _, condition := range crd.Status.Conditions {
49 if condition.Type != conditionType {
50 newConditions = append(newConditions, condition)
53 crd.Status.Conditions = newConditions
56 // FindCRDCondition returns the condition you're looking for or nil
57 func FindCRDCondition(crd *CustomResourceDefinition, conditionType CustomResourceDefinitionConditionType) *CustomResourceDefinitionCondition {
58 for i := range crd.Status.Conditions {
59 if crd.Status.Conditions[i].Type == conditionType {
60 return &crd.Status.Conditions[i]
67 // IsCRDConditionTrue indicates if the condition is present and strictly true
68 func IsCRDConditionTrue(crd *CustomResourceDefinition, conditionType CustomResourceDefinitionConditionType) bool {
69 return IsCRDConditionPresentAndEqual(crd, conditionType, ConditionTrue)
72 // IsCRDConditionFalse indicates if the condition is present and false true
73 func IsCRDConditionFalse(crd *CustomResourceDefinition, conditionType CustomResourceDefinitionConditionType) bool {
74 return IsCRDConditionPresentAndEqual(crd, conditionType, ConditionFalse)
77 // IsCRDConditionPresentAndEqual indicates if the condition is present and equal to the arg
78 func IsCRDConditionPresentAndEqual(crd *CustomResourceDefinition, conditionType CustomResourceDefinitionConditionType, status ConditionStatus) bool {
79 for _, condition := range crd.Status.Conditions {
80 if condition.Type == conditionType {
81 return condition.Status == status
87 // IsCRDConditionEquivalent returns true if the lhs and rhs are equivalent except for times
88 func IsCRDConditionEquivalent(lhs, rhs *CustomResourceDefinitionCondition) bool {
89 if lhs == nil && rhs == nil {
92 if lhs == nil || rhs == nil {
96 return lhs.Message == rhs.Message && lhs.Reason == rhs.Reason && lhs.Status == rhs.Status && lhs.Type == rhs.Type
99 // CRDHasFinalizer returns true if the finalizer is in the list
100 func CRDHasFinalizer(crd *CustomResourceDefinition, needle string) bool {
101 for _, finalizer := range crd.Finalizers {
102 if finalizer == needle {
110 // CRDRemoveFinalizer removes the finalizer if present
111 func CRDRemoveFinalizer(crd *CustomResourceDefinition, needle string) {
112 newFinalizers := []string{}
113 for _, finalizer := range crd.Finalizers {
114 if finalizer != needle {
115 newFinalizers = append(newFinalizers, finalizer)
118 crd.Finalizers = newFinalizers
121 // HasServedCRDVersion returns true if `version` is in the list of CRD's versions and the Served flag is set.
122 func HasServedCRDVersion(crd *CustomResourceDefinition, version string) bool {
123 for _, v := range crd.Spec.Versions {
124 if v.Name == version {
131 // GetCRDStorageVersion returns the storage version for given CRD.
132 func GetCRDStorageVersion(crd *CustomResourceDefinition) (string, error) {
133 for _, v := range crd.Spec.Versions {
138 // This should not happened if crd is valid
139 return "", fmt.Errorf("invalid CustomResourceDefinition, no storage version")
142 func IsStoredVersion(crd *CustomResourceDefinition, version string) bool {
143 for _, v := range crd.Status.StoredVersions {