|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 KusionStack 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 api |
| 18 | + |
| 19 | +import appsv1alpha1 "kusionstack.io/kube-api/apps/v1alpha1" |
| 20 | + |
| 21 | +type XSetLabelAnnotationEnum int |
| 22 | + |
| 23 | +const ( |
| 24 | + // OperatingLabelPrefix indicates a target is under operation |
| 25 | + // set by xset controller |
| 26 | + OperatingLabelPrefix XSetLabelAnnotationEnum = iota |
| 27 | + |
| 28 | + // OperationTypeLabelPrefix indicates the type of operation |
| 29 | + // set by xset controller |
| 30 | + OperationTypeLabelPrefix |
| 31 | + |
| 32 | + // OperateLabelPrefix indicates a target could start operation |
| 33 | + // set by related opsLifecycle controller. |
| 34 | + // xset controller will start operation only after this label is set |
| 35 | + OperateLabelPrefix |
| 36 | + |
| 37 | + // UndoOperationTypeLabelPrefix indicates a type of operation has been canceled. |
| 38 | + // need to be handled by related opsLifecycle controller |
| 39 | + UndoOperationTypeLabelPrefix |
| 40 | + |
| 41 | + // ServiceAvailableLabel indicates a target is available for service. |
| 42 | + // set by related opsLifecycle controller. |
| 43 | + ServiceAvailableLabel |
| 44 | + |
| 45 | + // PreparingDeleteLabel indicates a target is preparing to be deleted. |
| 46 | + // set by xset controller, |
| 47 | + // handle by related opsLifecycle controller if needed. |
| 48 | + PreparingDeleteLabel |
| 49 | + |
| 50 | + // ControlledByXSetLabel indicates a target is controlled by xset. |
| 51 | + // set by xset controller |
| 52 | + ControlledByXSetLabel |
| 53 | + |
| 54 | + // XInstanceIdLabelKey is used to attach instance ID on x |
| 55 | + XInstanceIdLabelKey |
| 56 | + |
| 57 | + // XSetUpdateIndicationLabelKey is used to indicate a target is updated by xset |
| 58 | + XSetUpdateIndicationLabelKey |
| 59 | + |
| 60 | + // XDeletionIndicationLabelKey is used to indicate a target is deleted by xset |
| 61 | + XDeletionIndicationLabelKey |
| 62 | + |
| 63 | + // XReplaceIndicationLabelKey is used to indicate a target is replaced by xset |
| 64 | + XReplaceIndicationLabelKey |
| 65 | + |
| 66 | + // XReplacePairNewId is used to indicate the new created target on replace origin target |
| 67 | + XReplacePairNewId |
| 68 | + |
| 69 | + // XReplacePairOriginName is used to indicate replace origin target name on the new created target |
| 70 | + XReplacePairOriginName |
| 71 | + |
| 72 | + // XReplaceByReplaceUpdateLabelKey indicates a target is replaced by update by xset |
| 73 | + XReplaceByReplaceUpdateLabelKey |
| 74 | + |
| 75 | + // XOrphanedIndicationLabelKey is used to indicate a target is orphaned by xset |
| 76 | + XOrphanedIndicationLabelKey |
| 77 | + |
| 78 | + // XCreatingLabel indicates a target is creating by xset |
| 79 | + XCreatingLabel |
| 80 | + |
| 81 | + // XCompletingLabel indicates a target is completing by xset |
| 82 | + XCompletingLabel |
| 83 | + |
| 84 | + // XExcludeIndicationLabelKey is used to indicate a target is excluded by xset |
| 85 | + XExcludeIndicationLabelKey |
| 86 | + |
| 87 | + // LastXStatusAnnotationKey is used to record the last status of a target by xset |
| 88 | + LastXStatusAnnotationKey |
| 89 | +) |
| 90 | + |
| 91 | +type XSetLabelAnnotationManager interface { |
| 92 | + Get(labels map[string]string, labelType XSetLabelAnnotationEnum) (string, bool) |
| 93 | + Set(labels map[string]string, labelType XSetLabelAnnotationEnum, value string) |
| 94 | + Delete(labels map[string]string, labelType XSetLabelAnnotationEnum) |
| 95 | + Value(labelType XSetLabelAnnotationEnum) string |
| 96 | + GetWellKnownLabelPrefixesWithID() []string |
| 97 | +} |
| 98 | + |
| 99 | +var defaultXSetLabelAnnotationManager = map[XSetLabelAnnotationEnum]string{ |
| 100 | + OperatingLabelPrefix: appsv1alpha1.PodOperatingLabelPrefix, |
| 101 | + OperationTypeLabelPrefix: appsv1alpha1.PodOperationTypeLabelPrefix, |
| 102 | + OperateLabelPrefix: appsv1alpha1.PodOperateLabelPrefix, |
| 103 | + UndoOperationTypeLabelPrefix: appsv1alpha1.PodUndoOperationTypeLabelPrefix, |
| 104 | + ServiceAvailableLabel: appsv1alpha1.PodServiceAvailableLabel, |
| 105 | + PreparingDeleteLabel: appsv1alpha1.PodPreparingDeleteLabel, |
| 106 | + |
| 107 | + ControlledByXSetLabel: appsv1alpha1.ControlledByKusionStackLabelKey, |
| 108 | + XInstanceIdLabelKey: appsv1alpha1.PodInstanceIDLabelKey, |
| 109 | + XSetUpdateIndicationLabelKey: appsv1alpha1.CollaSetUpdateIndicateLabelKey, |
| 110 | + XDeletionIndicationLabelKey: appsv1alpha1.PodDeletionIndicationLabelKey, |
| 111 | + XReplaceIndicationLabelKey: appsv1alpha1.PodReplaceIndicationLabelKey, |
| 112 | + XReplacePairNewId: appsv1alpha1.PodReplacePairNewId, |
| 113 | + XReplacePairOriginName: appsv1alpha1.PodReplacePairOriginName, |
| 114 | + XReplaceByReplaceUpdateLabelKey: appsv1alpha1.PodReplaceByReplaceUpdateLabelKey, |
| 115 | + XOrphanedIndicationLabelKey: appsv1alpha1.PodOrphanedIndicateLabelKey, |
| 116 | + XCreatingLabel: appsv1alpha1.PodCreatingLabel, |
| 117 | + XCompletingLabel: appsv1alpha1.PodCompletingLabel, |
| 118 | + XExcludeIndicationLabelKey: appsv1alpha1.PodExcludeIndicationLabelKey, |
| 119 | + |
| 120 | + LastXStatusAnnotationKey: appsv1alpha1.LastPodStatusAnnotationKey, |
| 121 | +} |
| 122 | + |
| 123 | +func NewXSetLabelAnnotationManager() XSetLabelAnnotationManager { |
| 124 | + return &xSetLabelAnnotationManager{ |
| 125 | + labelManager: defaultXSetLabelAnnotationManager, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +type xSetLabelAnnotationManager struct { |
| 130 | + labelManager map[XSetLabelAnnotationEnum]string |
| 131 | +} |
| 132 | + |
| 133 | +func (m *xSetLabelAnnotationManager) Get(labels map[string]string, key XSetLabelAnnotationEnum) (string, bool) { |
| 134 | + if labels == nil { |
| 135 | + return "", false |
| 136 | + } |
| 137 | + labelKey := m.labelManager[key] |
| 138 | + val, exist := labels[labelKey] |
| 139 | + return val, exist |
| 140 | +} |
| 141 | + |
| 142 | +func (m *xSetLabelAnnotationManager) Set(labels map[string]string, key XSetLabelAnnotationEnum, val string) { |
| 143 | + if labels == nil { |
| 144 | + labels = make(map[string]string) |
| 145 | + } |
| 146 | + labelKey := m.labelManager[key] |
| 147 | + labels[labelKey] = val |
| 148 | +} |
| 149 | + |
| 150 | +func (m *xSetLabelAnnotationManager) Delete(labels map[string]string, key XSetLabelAnnotationEnum) { |
| 151 | + if labels == nil { |
| 152 | + return |
| 153 | + } |
| 154 | + labelKey := m.labelManager[key] |
| 155 | + delete(labels, labelKey) |
| 156 | +} |
| 157 | + |
| 158 | +func (m *xSetLabelAnnotationManager) GetWellKnownLabelPrefixesWithID() []string { |
| 159 | + return []string{ |
| 160 | + m.labelManager[OperatingLabelPrefix], |
| 161 | + m.labelManager[OperationTypeLabelPrefix], |
| 162 | + m.labelManager[UndoOperationTypeLabelPrefix], |
| 163 | + m.labelManager[OperatingLabelPrefix], |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func (m *xSetLabelAnnotationManager) Value(key XSetLabelAnnotationEnum) string { |
| 168 | + return m.labelManager[key] |
| 169 | +} |
0 commit comments