From f5a9c5cedcf778d562b300cc67615827933ab829 Mon Sep 17 00:00:00 2001 From: xiezhang Date: Thu, 8 Sep 2022 13:09:09 -0400 Subject: [PATCH] DBAAS-831: update DBaaSConnection status to conform to the Provisioned Service ducktype defined in the Service Binding Specification for Kubernetes --- ...s.redhat.com_crunchybridgeconnections.yaml | 28 +++---- ...s.redhat.com_crunchybridgeconnections.yaml | 28 +++---- controllers/dbaas.redhat.com/connection.go | 81 +++++-------------- go.mod | 2 + go.sum | 4 +- 5 files changed, 45 insertions(+), 98 deletions(-) diff --git a/bundle/manifests/dbaas.redhat.com_crunchybridgeconnections.yaml b/bundle/manifests/dbaas.redhat.com_crunchybridgeconnections.yaml index 9a466c8..060f72c 100644 --- a/bundle/manifests/dbaas.redhat.com_crunchybridgeconnections.yaml +++ b/bundle/manifests/dbaas.redhat.com_crunchybridgeconnections.yaml @@ -58,6 +58,16 @@ spec: status: description: DBaaSConnectionStatus defines the observed state of DBaaSConnection properties: + binding: + description: 'Binding exposes a secret containing the binding information + for the instance. It implements the service binding Provisioned + Service duck type. See: https://github.com/servicebinding/spec#provisioned-service' + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + type: object conditions: items: description: "Condition contains details for one aspect of the current @@ -127,24 +137,6 @@ spec: - type type: object type: array - connectionInfoRef: - description: A ConfigMap holding non-sensitive information needed - for connecting to the DB instance - properties: - name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, uid?' - type: string - type: object - credentialsRef: - description: Secret holding the credentials needed for accessing the - DB instance - properties: - name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, uid?' - type: string - type: object type: object type: object served: true diff --git a/config/crd/bases/dbaas.redhat.com_crunchybridgeconnections.yaml b/config/crd/bases/dbaas.redhat.com_crunchybridgeconnections.yaml index 925bf87..0dcfc09 100644 --- a/config/crd/bases/dbaas.redhat.com_crunchybridgeconnections.yaml +++ b/config/crd/bases/dbaas.redhat.com_crunchybridgeconnections.yaml @@ -60,6 +60,16 @@ spec: status: description: DBaaSConnectionStatus defines the observed state of DBaaSConnection properties: + binding: + description: 'Binding exposes a secret containing the binding information + for the instance. It implements the service binding Provisioned + Service duck type. See: https://github.com/servicebinding/spec#provisioned-service' + properties: + name: + description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names + TODO: Add other useful fields. apiVersion, kind, uid?' + type: string + type: object conditions: items: description: "Condition contains details for one aspect of the current @@ -129,24 +139,6 @@ spec: - type type: object type: array - connectionInfoRef: - description: A ConfigMap holding non-sensitive information needed - for connecting to the DB instance - properties: - name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, uid?' - type: string - type: object - credentialsRef: - description: Secret holding the credentials needed for accessing the - DB instance - properties: - name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, uid?' - type: string - type: object type: object type: object served: true diff --git a/controllers/dbaas.redhat.com/connection.go b/controllers/dbaas.redhat.com/connection.go index 8b1a1bc..1a58eee 100644 --- a/controllers/dbaas.redhat.com/connection.go +++ b/controllers/dbaas.redhat.com/connection.go @@ -17,6 +17,7 @@ package dbaasredhatcom import ( "context" + "fmt" "net" "net/url" "strings" @@ -38,15 +39,17 @@ const ( PORTKEYNAME string = "port" DBKEYNAME string = "database" TYPEKEYNAME string = "type" + USERNAMEKEYNAME string = "username" + PASSWORDKEYNAME string = "password" DATABASESERVICETYPE string = "postgresql" - PROVIDERVALUE = "Red Hat DBaaS / Crunchy Bridge" + PROVIDERVALUE = "rhoda/crunchy bridge" PROVIDERKEY = "provider" ) // connectionDetails func (r *CrunchyBridgeConnectionReconciler) connectionDetails(instanceID string, connection *dbaasredhatcomv1alpha1.CrunchyBridgeConnection, bridgeapi *bridgeapi.Client, req ctrl.Request, logger logr.Logger) error { - if r.isBindingExist(connection) { + if r.isBindingExist(connection) && connection.Status.Binding != nil { return nil } @@ -57,31 +60,21 @@ func (r *CrunchyBridgeConnectionReconciler) connectionDetails(instanceID string, return err } - if connection.Status.CredentialsRef == nil { - secret := getOwnedSecret(connection, connectionRole.Name, connectionRole.Password) + if connection.Status.Binding == nil { + secret := getOwnedSecret(connection, connectionRole.URI, connectionRole.Name, connectionRole.Password) err := r.Client.Create(context.Background(), secret, &client.CreateOptions{}) if err != nil { logger.Error(err, "Error in creating the secret") return err } - connection.Status.CredentialsRef = &corev1.LocalObjectReference{Name: secret.Name} - } - if connection.Status.ConnectionInfoRef == nil { - configMap := getOwnedConfigMap(connection, connectionRole.URI) - configMapCreated, err := r.Clientset.CoreV1().ConfigMaps(req.Namespace).Create(context.Background(), configMap, metav1.CreateOptions{}) - if err != nil { - logger.Error(err, "Error in creating the configMap") - return err - } - - connection.Status.ConnectionInfoRef = &corev1.LocalObjectReference{Name: configMapCreated.Name} + connection.Status.Binding = &corev1.LocalObjectReference{Name: secret.Name} } return nil } // getOwnedSecret returns a secret object for database credentials with ownership set -func getOwnedSecret(connection *dbaasredhatcomv1alpha1.CrunchyBridgeConnection, username, password string) *corev1.Secret { +func getOwnedSecret(connection *dbaasredhatcomv1alpha1.CrunchyBridgeConnection, connectionString, username, password string) *corev1.Secret { return &corev1.Secret{ TypeMeta: metav1.TypeMeta{ Kind: "Opaque", @@ -107,58 +100,26 @@ func getOwnedSecret(connection *dbaasredhatcomv1alpha1.CrunchyBridgeConnection, }, }, }, - Data: map[string][]byte{ - "username": []byte(username), - "password": []byte(password), - }, - } -} - -// getOwnedConfigMap returns a configmap object for database name, host , port with ownership set -func getOwnedConfigMap(connection *dbaasredhatcomv1alpha1.CrunchyBridgeConnection, connectionString string) *corev1.ConfigMap { - - return &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1.ObjectMeta{ - GenerateName: "crunchy-bridge-db-conn-cm-", - Namespace: connection.Namespace, - Labels: map[string]string{ - "managed-by": "crunchy-bridge-operator", - "owner": connection.Name, - "owner.kind": connection.Kind, - "owner.namespace": connection.Namespace, - }, - OwnerReferences: []metav1.OwnerReference{ - { - UID: connection.GetUID(), - APIVersion: connection.APIVersion, - BlockOwnerDeletion: ptr.BoolPtr(false), - Controller: ptr.BoolPtr(true), - Kind: connection.Kind, - Name: connection.Name, - }, - }, - }, - Data: connectionCMData(connectionString), + Type: corev1.SecretType(fmt.Sprintf("servicebinding.io/%s", DATABASESERVICETYPE)), + Data: connectionSecretData(connectionString, username, password), } } -// connectionCMData -func connectionCMData(connectionString string) map[string]string { - bindingParamsMap := make(map[string]string) +// connectionSecretData +func connectionSecretData(connectionString, username, password string) map[string][]byte { + bindingParamsMap := make(map[string][]byte) u, err := url.Parse(connectionString) if err != nil { return bindingParamsMap } host, port, _ := net.SplitHostPort(u.Host) - bindingParamsMap[TYPEKEYNAME] = DATABASESERVICETYPE - bindingParamsMap[PROVIDERKEY] = PROVIDERVALUE - bindingParamsMap[HOSTKEYNAME] = host - bindingParamsMap[PORTKEYNAME] = port - bindingParamsMap[DBKEYNAME] = strings.TrimLeft(u.Path, "/") + bindingParamsMap[TYPEKEYNAME] = []byte(DATABASESERVICETYPE) + bindingParamsMap[PROVIDERKEY] = []byte(PROVIDERVALUE) + bindingParamsMap[HOSTKEYNAME] = []byte(host) + bindingParamsMap[PORTKEYNAME] = []byte(port) + bindingParamsMap[DBKEYNAME] = []byte(strings.TrimLeft(u.Path, "/")) + bindingParamsMap[USERNAMEKEYNAME] = []byte(username) + bindingParamsMap[PASSWORDKEYNAME] = []byte(password) return bindingParamsMap } diff --git a/go.mod b/go.mod index 285506b..fd71f13 100644 --- a/go.mod +++ b/go.mod @@ -17,3 +17,5 @@ require ( sigs.k8s.io/controller-runtime v0.11.2 ) + +replace github.com/RHEcosystemAppEng/dbaas-operator => github.com/xieshenzh/dbaas-operator v1.0.1-0.20220907182015-12def45fad1d diff --git a/go.sum b/go.sum index cc2a02a..61cb93e 100644 --- a/go.sum +++ b/go.sum @@ -135,8 +135,6 @@ github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= -github.com/RHEcosystemAppEng/dbaas-operator v1.0.1-0.20220829191729-018de64ac56f h1:+WPUdf/Q2JjhgcsZYvkutn9gRqV6B/AW5MShjNOyQE4= -github.com/RHEcosystemAppEng/dbaas-operator v1.0.1-0.20220829191729-018de64ac56f/go.mod h1:5yvviv5SMNVM9Ns9Y3xMaBrzb7EgMfq5uROehQ+R7/Y= github.com/RHsyseng/operator-utils v1.4.9/go.mod h1:LjFIMqr7OOliHrRz1sqqFvHbdqsNlxZkSbK1cfOWinQ= github.com/SAP/go-hdb v0.14.1/go.mod h1:7fdQLVC2lER3urZLjZCm0AuMQfApof92n3aylBPEkMo= github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ= @@ -1319,6 +1317,8 @@ github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2 github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/xieshenzh/dbaas-operator v1.0.1-0.20220907182015-12def45fad1d h1:tcEA7bSl8tdgl7JBmpCWCBoQKA7f2ajEj42Regxu5oQ= +github.com/xieshenzh/dbaas-operator v1.0.1-0.20220907182015-12def45fad1d/go.mod h1:5yvviv5SMNVM9Ns9Y3xMaBrzb7EgMfq5uROehQ+R7/Y= github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=