github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argoutil/resource.go (about) 1 // Copyright 2019 ArgoCD Operator Developers 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 argoutil 16 17 import ( 18 "context" 19 "fmt" 20 "strings" 21 22 corev1 "k8s.io/api/core/v1" 23 apierrors "k8s.io/apimachinery/pkg/api/errors" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/types" 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 argoprojv1alpha1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1" 29 argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1" 30 "github.com/argoproj-labs/argocd-operator/common" 31 ) 32 33 // AppendStringMap will append the map `add` to the given map `src` and return the result. 34 func AppendStringMap(src map[string]string, add map[string]string) map[string]string { 35 res := src 36 if len(src) <= 0 { 37 res = make(map[string]string, len(add)) 38 } 39 for key, val := range add { 40 res[key] = val 41 } 42 return res 43 } 44 45 // CombineImageTag will return the combined image and tag in the proper format for tags and digests. 46 func CombineImageTag(img string, tag string) string { 47 if strings.Contains(tag, ":") { 48 return fmt.Sprintf("%s@%s", img, tag) // Digest 49 } else if len(tag) > 0 { 50 return fmt.Sprintf("%s:%s", img, tag) // Tag 51 } 52 return img // No tag, use default 53 } 54 55 // CreateEvent will create a new Kubernetes Event with the given action, message, reason and involved uid. 56 func CreateEvent(client client.Client, eventType, action, message, reason string, objectMeta metav1.ObjectMeta, typeMeta metav1.TypeMeta) error { 57 event := newEvent(objectMeta) 58 event.Action = action 59 event.Type = eventType 60 event.InvolvedObject = corev1.ObjectReference{ 61 Name: objectMeta.Name, 62 Namespace: objectMeta.Namespace, 63 UID: objectMeta.UID, 64 ResourceVersion: objectMeta.ResourceVersion, 65 Kind: typeMeta.Kind, 66 APIVersion: typeMeta.APIVersion, 67 } 68 event.Message = message 69 event.Reason = reason 70 event.CreationTimestamp = metav1.Now() 71 event.FirstTimestamp = event.CreationTimestamp 72 event.LastTimestamp = event.CreationTimestamp 73 return client.Create(context.TODO(), event) 74 } 75 76 // FetchObject will retrieve the object with the given namespace and name using the Kubernetes API. 77 // The result will be stored in the given object. 78 func FetchObject(client client.Client, namespace string, name string, obj client.Object) error { 79 return client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, obj) 80 } 81 82 // FetchStorageSecretName will return the name of the Secret to use for the export process. 83 func FetchStorageSecretName(export *argoprojv1alpha1.ArgoCDExport) string { 84 name := NameWithSuffix(export.ObjectMeta, "export") 85 if export.Spec.Storage != nil && len(export.Spec.Storage.SecretName) > 0 { 86 name = export.Spec.Storage.SecretName 87 } 88 return name 89 } 90 91 // IsObjectFound will perform a basic check that the given object exists via the Kubernetes API. 92 // If an error occurs as part of the check, the function will return false. 93 func IsObjectFound(client client.Client, namespace string, name string, obj client.Object) bool { 94 return !apierrors.IsNotFound(FetchObject(client, namespace, name, obj)) 95 } 96 97 // NameWithSuffix will return a string using the Name from the given ObjectMeta with the provded suffix appended. 98 // Example: If ObjectMeta.Name is "test" and suffix is "object", the value of "test-object" will be returned. 99 func NameWithSuffix(meta metav1.ObjectMeta, suffix string) string { 100 return fmt.Sprintf("%s-%s", meta.Name, suffix) 101 } 102 103 func newEvent(meta metav1.ObjectMeta) *corev1.Event { 104 event := &corev1.Event{} 105 event.ObjectMeta.GenerateName = fmt.Sprintf("%s-", meta.Name) 106 event.ObjectMeta.Labels = meta.Labels 107 event.ObjectMeta.Namespace = meta.Namespace 108 return event 109 } 110 111 // LabelsForCluster returns the labels for all cluster resources. 112 func LabelsForCluster(cr *argoproj.ArgoCD) map[string]string { 113 labels := common.DefaultLabels(cr.Name) 114 return labels 115 } 116 117 // annotationsForCluster returns the annotations for all cluster resources. 118 func AnnotationsForCluster(cr *argoproj.ArgoCD) map[string]string { 119 annotations := common.DefaultAnnotations(cr.Name, cr.Namespace) 120 for key, val := range cr.ObjectMeta.Annotations { 121 annotations[key] = val 122 } 123 return annotations 124 }