|
| 1 | +// Copyright Red Hat, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package v1 |
| 16 | + |
| 17 | +import ( |
| 18 | + "time" |
| 19 | + |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +type StatusConditions struct { |
| 25 | + // Represents the latest available observations of a federation's current state. |
| 26 | + // +optional |
| 27 | + // +patchMergeKey=type |
| 28 | + // +patchStrategy=merge |
| 29 | + Conditions []Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,10,rep,name=conditions"` |
| 30 | +} |
| 31 | + |
| 32 | +type ConditionType string |
| 33 | + |
| 34 | +const ( |
| 35 | + // Connected indicates that one or more instances of istiod are currently |
| 36 | + // connected to the remote mesh. |
| 37 | + ConnectedServiceMeshPeerCondition ConditionType = "Connected" |
| 38 | + // Degraded indicates that one or more instances of istiod are currently |
| 39 | + // not connected to the remote mesh. |
| 40 | + DegradedServiceMeshPeerCondition ConditionType = "Degraded" |
| 41 | + // Serving indicates that one or more instances of istiod is currently |
| 42 | + // serving discovery information to a remote mesh. |
| 43 | + ServingServiceMeshPeerCondition ConditionType = "Degraded" |
| 44 | + // Ready indicates that all instances of istiod are connected to the remote |
| 45 | + // mesh. |
| 46 | + ReadyServiceMeshPeerCondition ConditionType = "Ready" |
| 47 | + // Exporting indicates that the mesh is exporting services to the remote |
| 48 | + // mesh. |
| 49 | + ExportingExportedServiceSetCondition ConditionType = "Exporting" |
| 50 | + // Importing indicates that the mesh is importing services from the remote |
| 51 | + // mesh. |
| 52 | + ImportingImportedServiceSetCondition ConditionType = "Importing" |
| 53 | +) |
| 54 | + |
| 55 | +const ( |
| 56 | + ConnectedConditionReason = "Connected" |
| 57 | + NotConnectedConditionReason = "NotConnected" |
| 58 | + DegradedConditionReason = "Degraded" |
| 59 | + NotDegradedConditionReason = "NotDegraded" |
| 60 | + ServingConditionReason = "Serving" |
| 61 | + NotServingConditionReason = "NotServing" |
| 62 | + ReadyConditionReason = "Ready" |
| 63 | + NotReadyConditionReason = "NotReady" |
| 64 | + ExportingConditionReason = "Exporting" |
| 65 | + NoRulesMatchedConditionReason = "NoRulesMatched" |
| 66 | + NoRulesDefinedConditionReason = "NoRulesDefined" |
| 67 | + ImportingConditionReason = "Importing" |
| 68 | + NoExportedServicesConditionReason = "NoExportedServices" |
| 69 | +) |
| 70 | + |
| 71 | +// Condition describes the state of a federation at a certain point. |
| 72 | +type Condition struct { |
| 73 | + // Type of federation condition. |
| 74 | + Type ConditionType `json:"type" protobuf:"bytes,1,opt,name=type,casttype=ConditionType"` |
| 75 | + // Status of the condition, one of True, False, Unknown. |
| 76 | + Status corev1.ConditionStatus `json:"status" protobuf:"bytes,2,opt,name=status,casttype=k8s.io/api/core/v1.ConditionStatus"` |
| 77 | + // Last time the condition transitioned from one status to another. |
| 78 | + // +optional |
| 79 | + LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty" protobuf:"bytes,3,opt,name=lastTransitionTime"` |
| 80 | + // The reason for the condition's last transition. |
| 81 | + // +optional |
| 82 | + Reason string `json:"reason,omitempty" protobuf:"bytes,4,opt,name=reason"` |
| 83 | + // A human readable message indicating details about the transition. |
| 84 | + // +optional |
| 85 | + Message string `json:"message,omitempty" protobuf:"bytes,5,opt,name=message"` |
| 86 | +} |
| 87 | + |
| 88 | +// GetCondition removes a condition for the list of conditions |
| 89 | +func (s *StatusConditions) GetCondition(conditionType ConditionType) Condition { |
| 90 | + if s == nil { |
| 91 | + return Condition{Type: conditionType, Status: corev1.ConditionUnknown} |
| 92 | + } |
| 93 | + for i := range s.Conditions { |
| 94 | + if s.Conditions[i].Type == conditionType { |
| 95 | + return s.Conditions[i] |
| 96 | + } |
| 97 | + } |
| 98 | + return Condition{Type: conditionType, Status: corev1.ConditionUnknown} |
| 99 | +} |
| 100 | + |
| 101 | +// SetCondition sets a specific condition in the list of conditions |
| 102 | +func (s *StatusConditions) SetCondition(condition Condition) *StatusConditions { |
| 103 | + if s == nil { |
| 104 | + return nil |
| 105 | + } |
| 106 | + // These only get serialized out to the second. This can break update |
| 107 | + // skipping, as the time in the resource returned from the client may not |
| 108 | + // match the time in our cached status during a reconcile. We truncate here |
| 109 | + // to save any problems down the line. |
| 110 | + now := metav1.NewTime(time.Now().Truncate(time.Second)) |
| 111 | + for i, prevCondition := range s.Conditions { |
| 112 | + if prevCondition.Type == condition.Type { |
| 113 | + if prevCondition.Status != condition.Status { |
| 114 | + condition.LastTransitionTime = now |
| 115 | + } else { |
| 116 | + condition.LastTransitionTime = prevCondition.LastTransitionTime |
| 117 | + } |
| 118 | + s.Conditions[i] = condition |
| 119 | + return s |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // If the condition does not exist, |
| 124 | + // initialize the lastTransitionTime |
| 125 | + condition.LastTransitionTime = now |
| 126 | + s.Conditions = append(s.Conditions, condition) |
| 127 | + return s |
| 128 | +} |
| 129 | + |
| 130 | +// RemoveCondition removes a condition for the list of conditions |
| 131 | +func (s *StatusConditions) RemoveCondition(conditionType ConditionType) *StatusConditions { |
| 132 | + if s == nil { |
| 133 | + return nil |
| 134 | + } |
| 135 | + for i := range s.Conditions { |
| 136 | + if s.Conditions[i].Type == conditionType { |
| 137 | + s.Conditions = append(s.Conditions[:i], s.Conditions[i+1:]...) |
| 138 | + return s |
| 139 | + } |
| 140 | + } |
| 141 | + return s |
| 142 | +} |
0 commit comments