diff --git a/controllers/libs/base/base.go b/controllers/libs/base/base.go index 2c79637f..ceb82386 100644 --- a/controllers/libs/base/base.go +++ b/controllers/libs/base/base.go @@ -195,8 +195,8 @@ func MkJob(name string, ns string, container apiv1.Container) batchv1.Job { }} } -// MkService produces a Service -func MkService(name string, ns string, selector string, ports []int32, portName string) apiv1.Service { +// mkServicePorts produces a ServicePort array +func mkServicePorts(ports []int32, portName string) []apiv1.ServicePort { servicePorts := []apiv1.ServicePort{} for _, p := range ports { servicePorts = append( @@ -207,13 +207,18 @@ func MkService(name string, ns string, selector string, ports []int32, portName Port: p, }) } + return servicePorts +} + +// MkService produces a Service +func MkService(name string, ns string, selector string, ports []int32, portName string) apiv1.Service { return apiv1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: ns, }, Spec: apiv1.ServiceSpec{ - Ports: servicePorts, + Ports: mkServicePorts(ports, portName), Selector: map[string]string{ "app": "sf", "run": selector, @@ -221,6 +226,22 @@ func MkService(name string, ns string, selector string, ports []int32, portName }} } +// MkServicePod produces a Service that target a single Pod by name +func MkServicePod(name string, ns string, podName string, ports []int32, portName string) apiv1.Service { + return apiv1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: ns, + }, + Spec: apiv1.ServiceSpec{ + Ports: mkServicePorts(ports, portName), + Selector: map[string]string{ + "statefulset.kubernetes.io/pod-name": podName, + }, + }} + +} + // MkHeadlessService produces a headless service. func MkHeadlessService(name string, ns string, selector string, ports []int32, portName string) apiv1.Service { service := MkService(name, ns, selector, ports, portName) diff --git a/controllers/mariadb.go b/controllers/mariadb.go index 8de7cdc7..4770ab32 100644 --- a/controllers/mariadb.go +++ b/controllers/mariadb.go @@ -18,10 +18,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const mariadbPort = 3306 -const mariaDBPortName = "mariadb-port" - -const zuulDBConfigSecret = "zuul-db-connection" +const ( + mariadbIdent = "mariadb" + mariadbPort = 3306 + mariaDBPortName = "mariadb-port" + zuulDBConfigSecret = "zuul-db-connection" +) type ZuulDBOpts struct { Username string @@ -37,7 +39,7 @@ func (r *SFController) CreateDBInitContainer(username string, password string, d g := "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + username + "'@'%' IDENTIFIED BY '${USER_PASSWORD}' WITH GRANT OPTION; FLUSH PRIVILEGES;" container := base.MkContainer("mariadb-client", base.MariabDBImage) container.Command = []string{"sh", "-c", ` - echo 'Running: mysql --host=mariadb --user=root --password="$MYSQL_ROOT_PASSWORD" -e "` + c + g + `"' + echo 'Running: mysql --host=" ` + mariadbIdent + `" --user=root --password="$MYSQL_ROOT_PASSWORD" -e "` + c + g + `"' ATTEMPT=0 while ! mysql --host=mariadb --user=root --password="$MYSQL_ROOT_PASSWORD" -e "` + c + g + `"; do ATTEMPT=$[ $ATTEMPT + 1 ] @@ -87,7 +89,7 @@ func (r *SFController) DBPostInit(configSecret apiv1.Secret) apiv1.Secret { zuulOpts := ZuulDBOpts{ Username: "zuul", Password: utils.NewUUIDString(), - Host: "mariadb", + Host: mariadbIdent, Port: mariadbPort, Database: "zuul", Params: map[string]string{}, @@ -124,15 +126,15 @@ func (r *SFController) DeployMariadb() bool { r.EnsureSecretUUID(passName) replicas := int32(1) - dep := r.mkStatefulSet("mariadb", base.MariabDBImage, r.getStorageConfOrDefault(r.cr.Spec.MariaDB.DBStorage), replicas, apiv1.ReadWriteOnce) + sts := r.mkStatefulSet(mariadbIdent, base.MariabDBImage, r.getStorageConfOrDefault(r.cr.Spec.MariaDB.DBStorage), replicas, apiv1.ReadWriteOnce) - dep.Spec.VolumeClaimTemplates = append( - dep.Spec.VolumeClaimTemplates, + sts.Spec.VolumeClaimTemplates = append( + sts.Spec.VolumeClaimTemplates, // TODO redirect logs to stdout so we don't need a volume base.MkPVC("mariadb-logs", r.ns, r.getStorageConfOrDefault(r.cr.Spec.MariaDB.LogStorage), apiv1.ReadWriteOnce)) - dep.Spec.Template.Spec.Containers[0].VolumeMounts = []apiv1.VolumeMount{ + sts.Spec.Template.Spec.Containers[0].VolumeMounts = []apiv1.VolumeMount{ { - Name: "mariadb", + Name: mariadbIdent, MountPath: "/var/lib/mysql", }, { @@ -144,28 +146,29 @@ func (r *SFController) DeployMariadb() bool { MountPath: "/run/mariadb", }, } - dep.Spec.Template.Spec.Containers[0].Env = []apiv1.EnvVar{ + sts.Spec.Template.Spec.Containers[0].Env = []apiv1.EnvVar{ base.MkEnvVar("HOME", "/var/lib/mysql"), base.MkSecretEnvVar("MYSQL_ROOT_PASSWORD", "mariadb-root-password", "mariadb-root-password"), } - dep.Spec.Template.Spec.Containers[0].Ports = []apiv1.ContainerPort{ + sts.Spec.Template.Spec.Containers[0].Ports = []apiv1.ContainerPort{ base.MkContainerPort(mariadbPort, mariaDBPortName), } - dep.Spec.Template.Spec.Containers[0].ReadinessProbe = base.MkReadinessTCPProbe(mariadbPort) - dep.Spec.Template.Spec.Containers[0].LivenessProbe = base.MkReadinessTCPProbe(mariadbPort) - dep.Spec.Template.Spec.Volumes = []apiv1.Volume{ + sts.Spec.Template.Spec.Containers[0].ReadinessProbe = base.MkReadinessTCPProbe(mariadbPort) + sts.Spec.Template.Spec.Containers[0].LivenessProbe = base.MkReadinessTCPProbe(mariadbPort) + sts.Spec.Template.Spec.Volumes = []apiv1.Volume{ base.MkEmptyDirVolume("mariadb-run"), } - r.GetOrCreate(&dep) + r.GetOrCreate(&sts) + servicePorts := []int32{mariadbPort} - srv := base.MkService("mariadb", r.ns, "mariadb", servicePorts, mariaDBPortName) + srv := base.MkServicePod("mariadb", r.ns, "mariadb-0", servicePorts, mariaDBPortName) r.GetOrCreate(&srv) var zuulDBSecret apiv1.Secret - if r.IsStatefulSetReady(&dep) { + if r.IsStatefulSetReady(&sts) { zuulDBSecret = apiv1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: zuulDBConfigSecret, @@ -179,8 +182,9 @@ func (r *SFController) DeployMariadb() bool { } } - isStatefulSet := r.IsStatefulSetReady(&dep) - conds.UpdateConditions(&r.cr.Status.Conditions, "mariadb", isStatefulSet) + isReady := r.IsStatefulSetReady(&sts) && zuulDBSecret.Data != nil + + conds.UpdateConditions(&r.cr.Status.Conditions, mariadbIdent, isReady) - return isStatefulSet && zuulDBSecret.Data != nil + return isReady } diff --git a/controllers/zuul.go b/controllers/zuul.go index ee1ad3b0..738b15ef 100644 --- a/controllers/zuul.go +++ b/controllers/zuul.go @@ -861,8 +861,8 @@ func (r *SFController) DeployZuulSecrets() { func (r *SFController) DeployZuul() bool { initContainers := []apiv1.Container{} - dbPassword := apiv1.Secret{} - if !r.GetM(zuulDBConfigSecret, &dbPassword) { + dbSettings := apiv1.Secret{} + if !r.GetM(zuulDBConfigSecret, &dbSettings) { r.log.Info("Waiting for db connection secret") return false } @@ -928,7 +928,8 @@ func (r *SFController) DeployZuul() bool { } // Set Database DB URI - cfgINI.Section("database").NewKey("dburi", fmt.Sprintf("mysql+pymysql://zuul:%s@mariadb/zuul", dbPassword.Data["password"])) + cfgINI.Section("database").NewKey("dburi", fmt.Sprintf( + "mysql+pymysql://%s:%s@%s/%s", dbSettings.Data["username"], dbSettings.Data["password"], dbSettings.Data["host"], dbSettings.Data["database"])) // Set Zookeeper hosts cfgINI.Section("zookeeper").NewKey("hosts", "zookeeper."+r.ns+":2281")