github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/component_input_helpers.go (about) 1 package internal 2 3 import ( 4 "context" 5 "fmt" 6 "reflect" 7 "strings" 8 9 "github.com/google/uuid" 10 "github.com/kyma-incubator/reconciler/pkg/keb" 11 reconcilerApi "github.com/kyma-incubator/reconciler/pkg/keb" 12 "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema" 13 "github.com/kyma-project/kyma-environment-broker/internal/ptr" 14 15 v1 "k8s.io/api/core/v1" 16 k8serrors "k8s.io/apimachinery/pkg/api/errors" 17 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 18 "k8s.io/client-go/kubernetes" 19 "k8s.io/client-go/tools/clientcmd" 20 ) 21 22 const ( 23 BTPOperatorComponentName = "btp-operator" 24 25 // BTP Operator overrides keys 26 BTPOperatorClientID = "manager.secret.clientid" 27 BTPOperatorClientSecret = "manager.secret.clientsecret" 28 BTPOperatorURL = "manager.secret.url" // deprecated, for btp-operator v0.2.0 29 BTPOperatorSMURL = "manager.secret.sm_url" // for btp-operator v0.2.3 30 BTPOperatorTokenURL = "manager.secret.tokenurl" 31 BTPOperatorClusterID = "cluster.id" 32 BTPOperatorPriorityClass = "manager.priorityClassName" 33 BTPOperatorPriorityClassValue = "kyma-system" 34 ) 35 36 var btpOperatorRequiredKeys = []string{BTPOperatorClientID, BTPOperatorClientSecret, BTPOperatorURL, BTPOperatorSMURL, BTPOperatorTokenURL, BTPOperatorClusterID, BTPOperatorPriorityClass} 37 38 type ClusterIDGetter func(string) (string, error) 39 40 func GetBTPOperatorProvisioningOverrides(creds *ServiceManagerOperatorCredentials, clusterId string) []*gqlschema.ConfigEntryInput { 41 return []*gqlschema.ConfigEntryInput{ 42 { 43 Key: BTPOperatorClientID, 44 Value: creds.ClientID, 45 Secret: ptr.Bool(true), 46 }, 47 { 48 Key: BTPOperatorClientSecret, 49 Value: creds.ClientSecret, 50 Secret: ptr.Bool(true), 51 }, 52 { 53 Key: BTPOperatorURL, 54 Value: creds.ServiceManagerURL, 55 }, 56 { 57 Key: BTPOperatorSMURL, 58 Value: creds.ServiceManagerURL, 59 }, 60 { 61 Key: BTPOperatorTokenURL, 62 Value: creds.URL, 63 }, 64 { 65 Key: BTPOperatorClusterID, 66 Value: clusterId, 67 }, 68 { 69 Key: BTPOperatorPriorityClass, 70 Value: BTPOperatorPriorityClassValue, 71 }, 72 } 73 } 74 75 func GetBTPOperatorReconcilerOverrides(creds *ServiceManagerOperatorCredentials, clusterID string) []reconcilerApi.Configuration { 76 overrides := GetBTPOperatorProvisioningOverrides(creds, clusterID) 77 var config []reconcilerApi.Configuration 78 for _, c := range overrides { 79 secret := false 80 if c.Secret != nil { 81 secret = *c.Secret 82 } 83 rc := reconcilerApi.Configuration{Key: c.Key, Value: c.Value, Secret: secret} 84 config = append(config, rc) 85 } 86 return config 87 } 88 89 func GetClusterIDWithKubeconfig(kubeconfig string) (string, error) { 90 cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfig)) 91 if err != nil { 92 return "", err 93 } 94 cs, err := kubernetes.NewForConfig(cfg) 95 if err != nil { 96 return "", err 97 } 98 cm, err := cs.CoreV1().ConfigMaps("kyma-system").Get(context.Background(), "cluster-info", metav1.GetOptions{}) 99 if k8serrors.IsNotFound(err) { 100 cm = &v1.ConfigMap{ 101 ObjectMeta: metav1.ObjectMeta{ 102 Name: "cluster-info", 103 Namespace: "kyma-system", 104 }, 105 Data: map[string]string{ 106 "id": uuid.NewString(), 107 }, 108 } 109 if cm, err = cs.CoreV1().ConfigMaps(cm.Namespace).Create(context.Background(), cm, metav1.CreateOptions{}); err != nil { 110 return "", err 111 } 112 return cm.Data["id"], nil 113 } 114 if err != nil { 115 return "", err 116 } 117 return cm.Data["id"], nil 118 } 119 120 func CheckBTPCredsValid(clusterConfiguration reconcilerApi.Cluster) error { 121 vals := make(map[string]bool) 122 hasBTPOperator := false 123 var errs []string 124 for _, c := range clusterConfiguration.KymaConfig.Components { 125 if c.Component == BTPOperatorComponentName { 126 hasBTPOperator = true 127 for _, cfg := range c.Configuration { 128 for _, key := range btpOperatorRequiredKeys { 129 if cfg.Key == key { 130 vals[key] = true 131 if cfg.Value == nil { 132 errs = append(errs, fmt.Sprintf("missing required value for %v", key)) 133 } 134 if val, ok := cfg.Value.(string); !ok || val == "" { 135 errs = append(errs, fmt.Sprintf("missing required value for %v", key)) 136 } 137 } 138 } 139 } 140 } 141 } 142 if hasBTPOperator { 143 for _, key := range btpOperatorRequiredKeys { 144 if !vals[key] { 145 errs = append(errs, fmt.Sprintf("missing required key %v", key)) 146 } 147 } 148 if len(errs) != 0 { 149 return fmt.Errorf("BTP Operator is about to be installed but is missing required configuration: %v", strings.Join(errs, ", ")) 150 } 151 } 152 return nil 153 } 154 155 func CheckBTPCredsMatching(a, b keb.Component) bool { 156 if a.URL != b.URL { 157 return false 158 } 159 if a.Version != b.Version { 160 return false 161 } 162 if a.Namespace != b.Namespace { 163 return false 164 } 165 ma := make(map[string]keb.Configuration) 166 for _, aa := range a.Configuration { 167 ma[aa.Key] = aa 168 } 169 mb := make(map[string]keb.Configuration) 170 for _, bb := range b.Configuration { 171 mb[bb.Key] = bb 172 } 173 for _, key := range btpOperatorRequiredKeys { 174 if !reflect.DeepEqual(ma[key], mb[key]) { 175 return false 176 } 177 } 178 return true 179 } 180 181 func IsEuAccess(platformRegion string) bool { 182 switch platformRegion { 183 case "cf-eu11": 184 return true 185 case "cf-ch20": 186 return true 187 } 188 return false 189 }