github.com/verrazzano/verrazzano@v1.7.1/platform-operator/internal/k8s/namespace/namespace.go (about) 1 // Copyright (c) 2021, 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package namespace 4 5 import ( 6 "context" 7 8 globalconst "github.com/verrazzano/verrazzano/pkg/constants" 9 "github.com/verrazzano/verrazzano/platform-operator/constants" 10 corev1 "k8s.io/api/core/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 controllerruntime "sigs.k8s.io/controller-runtime" 13 "sigs.k8s.io/controller-runtime/pkg/client" 14 ) 15 16 // CreateAndLabelNamespace - Utility function to create a namespace and optionally add either the VZ managed and/or Istio injection labels 17 func CreateAndLabelNamespace(client client.Client, ns string, isVerrazzanoManaged bool, withIstioInjection bool) error { 18 nsObj := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}} 19 _, err := controllerruntime.CreateOrUpdate(context.TODO(), client, nsObj, 20 func() error { 21 nsObj.Labels, _ = MergeMaps(nsObj.Labels, createLabelsMap(ns, isVerrazzanoManaged, withIstioInjection)) 22 return nil 23 }, 24 ) 25 return err 26 } 27 28 // CreateCertManagerNamespace - Create/Update and label the cert-manager namespace 29 func CreateCertManagerNamespace(client client.Client) error { 30 return CreateAndLabelNamespace(client, globalconst.CertManagerNamespace, true, false) 31 } 32 33 // CreateIngressNginxNamespace - Create/Update and label the ingres-nginx namespace 34 func CreateIngressNginxNamespace(client client.Client, istioInjectionEnabled bool, namespace string) error { 35 return CreateAndLabelNamespace(client, namespace, true, istioInjectionEnabled) 36 } 37 38 // CreateIstioNamespace - Create/Update and label the Istio namespace 39 func CreateIstioNamespace(client client.Client) error { 40 return CreateAndLabelNamespace(client, globalconst.IstioSystemNamespace, true, false) 41 } 42 43 // CreateKeycloakNamespace - Create/Update and label the Keycloak namespace 44 func CreateKeycloakNamespace(client client.Client, istioInjectionEnabled bool) error { 45 return CreateAndLabelNamespace(client, globalconst.KeycloakNamespace, true, istioInjectionEnabled) 46 } 47 48 // CreateMysqlOperator - Create/Update and label the MySQL operator namespace 49 func CreateMysqlOperator(client client.Client, istioInjectionEnabled bool) error { 50 return CreateAndLabelNamespace(client, globalconst.MySQLOperatorNamespace, true, istioInjectionEnabled) 51 } 52 53 // CreateRancherNamespace - Create/Update and label the Rancher system namespace 54 func CreateRancherNamespace(client client.Client) error { 55 return CreateAndLabelNamespace(client, globalconst.RancherSystemNamespace, true, false) 56 } 57 58 // CreateVerrazzanoMonitoringNamespace - Create/Update and label the Verrazzano monitoring namespace 59 func CreateVerrazzanoMonitoringNamespace(client client.Client, istioInjectionEnabled bool) error { 60 return CreateAndLabelNamespace(client, constants.VerrazzanoMonitoringNamespace, true, istioInjectionEnabled) 61 } 62 63 // CreateVerrazzanoSystemNamespace - Create/Update and label the Verrazzano system namespace 64 func CreateVerrazzanoSystemNamespace(client client.Client, istioInjectionEnabled bool) error { 65 return CreateAndLabelNamespace(client, globalconst.VerrazzanoSystemNamespace, true, istioInjectionEnabled) 66 } 67 68 // CreateVerrazzanoCapiNamespace - Create/Update and label the verrazzano-capi namespace 69 func CreateVerrazzanoCapiNamespace(client client.Client) error { 70 return CreateAndLabelNamespace(client, globalconst.VerrazzanoCAPINamespace, true, false) 71 } 72 73 // CreateVerrazzanoMultiClusterNamespace - Create/Update and label the Verrazzano multi-cluster namespace 74 func CreateVerrazzanoMultiClusterNamespace(client client.Client) error { 75 return CreateAndLabelNamespace(client, globalconst.VerrazzanoMultiClusterNamespace, true, false) 76 } 77 78 // CreateVeleroNamespace - Create/Update and label the Velero namespace 79 func CreateVeleroNamespace(client client.Client) error { 80 return CreateAndLabelNamespace(client, constants.VeleroNameSpace, true, true) 81 } 82 83 // CreateArgoCDNamespace - Create/Update and label the Argo CD namespace 84 func CreateArgoCDNamespace(client client.Client, istioInjectionEnabled bool) error { 85 return CreateAndLabelNamespace(client, constants.ArgoCDNamespace, true, istioInjectionEnabled) 86 } 87 88 // CreateDexNamespace - Create/Update and label the Dex namespace 89 func CreateDexNamespace(client client.Client, istioInjectionEnabled bool) error { 90 return CreateAndLabelNamespace(client, constants.DexNamespace, true, istioInjectionEnabled) 91 } 92 93 // MergeMaps Merge one map into another, creating new one if necessary; returns the updated map and true if it was modified 94 func MergeMaps(to map[string]string, from map[string]string) (map[string]string, bool) { 95 mergedMap := to 96 if mergedMap == nil { 97 mergedMap = make(map[string]string) 98 } 99 var updated bool 100 for k, v := range from { 101 mergedMap[k] = v 102 } 103 return mergedMap, updated 104 } 105 106 // createLabelsMap - Create a map with the the Verrazzano-managed and/or Istio injection labels 107 func createLabelsMap(ns string, isVerrazzanoManaged bool, withIstioInjection bool) map[string]string { 108 annotations := map[string]string{} 109 if isVerrazzanoManaged { 110 annotations[globalconst.LabelVerrazzanoNamespace] = ns 111 } 112 if withIstioInjection { 113 annotations[globalconst.LabelIstioInjection] = "enabled" 114 } 115 return annotations 116 }