github.com/oam-dev/kubevela@v1.9.11/pkg/oam/auxiliary.go (about) 1 /* 2 Copyright 2021 The KubeVela 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 oam 18 19 import ( 20 "time" 21 22 "github.com/crossplane/crossplane-runtime/pkg/meta" 23 "sigs.k8s.io/controller-runtime/pkg/client" 24 ) 25 26 // SetCluster add cluster label to object 27 func SetCluster(o client.Object, clusterName string) { 28 meta.AddLabels(o, map[string]string{LabelAppCluster: clusterName}) 29 } 30 31 // SetClusterIfEmpty set cluster label to object if the label is empty 32 func SetClusterIfEmpty(o client.Object, clusterName string) { 33 if GetCluster(o) == "" { 34 SetCluster(o, clusterName) 35 } 36 } 37 38 // GetCluster get cluster from object 39 func GetCluster(o client.Object) string { 40 if labels := o.GetLabels(); labels != nil { 41 return labels[LabelAppCluster] 42 } 43 return "" 44 } 45 46 // GetPublishVersion get PublishVersion from object 47 func GetPublishVersion(o client.Object) string { 48 if annotations := o.GetAnnotations(); annotations != nil { 49 return annotations[AnnotationPublishVersion] 50 } 51 return "" 52 } 53 54 // GetDeployVersion get DeployVersion from object 55 func GetDeployVersion(o client.Object) string { 56 if annotations := o.GetAnnotations(); annotations != nil { 57 return annotations[AnnotationDeployVersion] 58 } 59 return "" 60 } 61 62 // GetLastAppliedTime . 63 func GetLastAppliedTime(o client.Object) time.Time { 64 if annotations := o.GetAnnotations(); annotations != nil { 65 s := annotations[AnnotationLastAppliedTime] 66 if t, err := time.Parse(time.RFC3339, s); err == nil { 67 return t 68 } 69 } 70 return o.GetCreationTimestamp().Time 71 } 72 73 // SetPublishVersion set PublishVersion for object 74 func SetPublishVersion(o client.Object, publishVersion string) { 75 annotations := o.GetAnnotations() 76 if annotations == nil { 77 annotations = map[string]string{} 78 } 79 annotations[AnnotationPublishVersion] = publishVersion 80 o.SetAnnotations(annotations) 81 } 82 83 // GetControllerRequirement get ControllerRequirement from object 84 func GetControllerRequirement(o client.Object) string { 85 if annotations := o.GetAnnotations(); annotations != nil { 86 return annotations[AnnotationControllerRequirement] 87 } 88 return "" 89 } 90 91 // SetControllerRequirement set ControllerRequirement for object 92 func SetControllerRequirement(o client.Object, controllerRequirement string) { 93 annotations := o.GetAnnotations() 94 if annotations == nil { 95 annotations = map[string]string{} 96 } 97 annotations[AnnotationControllerRequirement] = controllerRequirement 98 if controllerRequirement == "" { 99 delete(annotations, AnnotationControllerRequirement) 100 } 101 o.SetAnnotations(annotations) 102 }