Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apiextensions-apiserver / pkg / apis / apiextensions / helpers.go
1 /*
2 Copyright 2017 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 apiextensions
18
19 import (
20         "fmt"
21         "time"
22
23         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 )
25
26 // SetCRDCondition sets the status condition.  It either overwrites the existing one or
27 // creates a new one
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)
33                 return
34         }
35
36         if existingCondition.Status != newCondition.Status {
37                 existingCondition.Status = newCondition.Status
38                 existingCondition.LastTransitionTime = newCondition.LastTransitionTime
39         }
40
41         existingCondition.Reason = newCondition.Reason
42         existingCondition.Message = newCondition.Message
43 }
44
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)
51                 }
52         }
53         crd.Status.Conditions = newConditions
54 }
55
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]
61                 }
62         }
63
64         return nil
65 }
66
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)
70 }
71
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)
75 }
76
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
82                 }
83         }
84         return false
85 }
86
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 {
90                 return true
91         }
92         if lhs == nil || rhs == nil {
93                 return false
94         }
95
96         return lhs.Message == rhs.Message && lhs.Reason == rhs.Reason && lhs.Status == rhs.Status && lhs.Type == rhs.Type
97 }
98
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 {
103                         return true
104                 }
105         }
106
107         return false
108 }
109
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)
116                 }
117         }
118         crd.Finalizers = newFinalizers
119 }
120
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 {
125                         return v.Served
126                 }
127         }
128         return false
129 }
130
131 // GetCRDStorageVersion returns the storage version for given CRD.
132 func GetCRDStorageVersion(crd *CustomResourceDefinition) (string, error) {
133         for _, v := range crd.Spec.Versions {
134                 if v.Storage {
135                         return v.Name, nil
136                 }
137         }
138         // This should not happened if crd is valid
139         return "", fmt.Errorf("invalid CustomResourceDefinition, no storage version")
140 }
141
142 func IsStoredVersion(crd *CustomResourceDefinition, version string) bool {
143         for _, v := range crd.Status.StoredVersions {
144                 if version == v {
145                         return true
146                 }
147         }
148         return false
149 }