Skip to content

Commit

Permalink
Merge "mariadb - use a Service Pod Selector to always stick to mariad…
Browse files Browse the repository at this point in the history
…b-0 Pod."
  • Loading branch information
Microzuul CI authored and Gerrit Code Review committed Nov 10, 2023
2 parents 2143945 + 2d819b5 commit 10414ff
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
27 changes: 24 additions & 3 deletions controllers/libs/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -207,20 +207,41 @@ 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,
},
}}
}

// 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)
Expand Down
48 changes: 26 additions & 22 deletions controllers/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ]
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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",
},
{
Expand All @@ -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,
Expand All @@ -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
}
7 changes: 4 additions & 3 deletions controllers/zuul.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 10414ff

Please sign in to comment.