diff --git a/pkg/controller/mysqlcluster/internal/syncer/headless_service.go b/pkg/controller/mysqlcluster/internal/syncer/headless_service.go index 2944d7295..1b453180c 100644 --- a/pkg/controller/mysqlcluster/internal/syncer/headless_service.go +++ b/pkg/controller/mysqlcluster/internal/syncer/headless_service.go @@ -19,6 +19,7 @@ package mysqlcluster import ( core "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -35,11 +36,14 @@ func NewHeadlessSVCSyncer(c client.Client, scheme *runtime.Scheme, cluster *mysq }, } - return syncer.NewObjectSyncer("HeadlessSVC", cluster.Unwrap(), obj, c, scheme, func(in runtime.Object) error { + return syncer.NewObjectSyncer("HeadlessSVC", nil, obj, c, scheme, func(in runtime.Object) error { out := in.(*core.Service) out.Spec.ClusterIP = "None" - out.Spec.Selector = cluster.GetSelectorLabels() + out.Spec.Selector = labels.Set{ + "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/managed-by": "mysql.presslabs.org", + } if len(out.Spec.Ports) != 2 { out.Spec.Ports = make([]core.ServicePort, 2) } diff --git a/pkg/controller/mysqlcluster/mysqlcluster_controller_test.go b/pkg/controller/mysqlcluster/mysqlcluster_controller_test.go index e66011470..c65a75a13 100644 --- a/pkg/controller/mysqlcluster/mysqlcluster_controller_test.go +++ b/pkg/controller/mysqlcluster/mysqlcluster_controller_test.go @@ -125,12 +125,6 @@ var _ = Describe("MysqlCluster controller", func() { Namespace: cluster.Namespace, }, }, - &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("%s-mysql-nodes", name), - Namespace: cluster.Namespace, - }, - }, &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-mysql-master", name), @@ -202,7 +196,7 @@ var _ = Describe("MysqlCluster controller", func() { Eventually(func() error { return c.Get(context.TODO(), key, obj) }, timeout).Should(Succeed()) }, Entry("reconciles the statefulset", "%s-mysql", &appsv1.StatefulSet{}), - Entry("reconciles the headless service", "%s-mysql-nodes", &corev1.Service{}), + //Entry("reconciles the headless service", "mysql", &corev1.Service{}), Entry("reconciles the master service", "%s-mysql-master", &corev1.Service{}), Entry("reconciles the config map", "%s-mysql", &corev1.ConfigMap{}), Entry("reconciles the pod disruption budget", "%s-mysql", &policyv1beta1.PodDisruptionBudget{}), diff --git a/pkg/internal/mysqlcluster/mysqlcluster.go b/pkg/internal/mysqlcluster/mysqlcluster.go index 4c862ba7a..f2dd80bc0 100644 --- a/pkg/internal/mysqlcluster/mysqlcluster.go +++ b/pkg/internal/mysqlcluster/mysqlcluster.go @@ -26,6 +26,11 @@ import ( "github.com/presslabs/mysql-operator/pkg/util/constants" ) +const ( + // HeadlessSVCName is the name of the headless service that is commonly used for all clusters + HeadlessSVCName = "mysql" +) + // MysqlCluster is the wrapper for api.MysqlCluster type type MysqlCluster struct { *api.MysqlCluster @@ -62,9 +67,7 @@ func (c *MysqlCluster) GetLabels() labels.Set { } labels := labels.Set{ - // TODO: remove those labels at major release and update selector labels - "app": "mysql-operator", - "mysql_cluster": c.Name, + "mysql.presslabs.org/cluster": c.Name, "app.kubernetes.io/name": "mysql", "app.kubernetes.io/instance": instance, @@ -83,8 +86,10 @@ func (c *MysqlCluster) GetLabels() labels.Set { // GetSelectorLabels returns the labels that will be used as selector func (c *MysqlCluster) GetSelectorLabels() labels.Set { return labels.Set{ - "app": "mysql-operator", - "mysql_cluster": c.Name, + "mysql.presslabs.org/cluster": c.Name, + + "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/managed-by": "mysql.presslabs.org", } } @@ -119,7 +124,7 @@ func GetNameForResource(name ResourceName, clusterName string) string { case MasterService: return fmt.Sprintf("%s-mysql-master", clusterName) case HeadlessSVC: - return fmt.Sprintf("%s-mysql-nodes", clusterName) + return HeadlessSVCName default: return fmt.Sprintf("%s-mysql", clusterName) } diff --git a/pkg/orchestrator/fake/client.go b/pkg/orchestrator/fake/client.go index 59d8fac85..ccb018494 100644 --- a/pkg/orchestrator/fake/client.go +++ b/pkg/orchestrator/fake/client.go @@ -19,6 +19,7 @@ package fake import ( "database/sql" "fmt" + "regexp" "strings" "sync" @@ -173,12 +174,11 @@ func (o *OrcFakeClient) CheckDiscovered(key string) bool { } func (o *OrcFakeClient) getHostClusterAlias(host string) string { - // input: cluster-1943285891-mysql-0.cluster-1943285891-mysql-nodes.default + // input: cluster-1943285891-mysql-0.mysql.default // output: cluster-1943285891.default - s := strings.Split(host, ".") - ns := s[2] - cluster := strings.Replace(s[1], "-mysql-nodes", "", 1) - return fmt.Sprintf("%s.%s", cluster, ns) + re := regexp.MustCompile(`^([\w-]+)-mysql-\d*.mysql.([\w-]+)$`) + values := re.FindStringSubmatch(host) + return fmt.Sprintf("%s.%s", values[1], values[2]) } // Discover register a host into orchestrator diff --git a/pkg/sidecar/configs_test.go b/pkg/sidecar/configs_test.go index a3cfe6fbe..030b3b3b6 100644 --- a/pkg/sidecar/configs_test.go +++ b/pkg/sidecar/configs_test.go @@ -31,7 +31,7 @@ var _ = Describe("Test sidecar configs", func() { Hostname: "cluster-mysql-0", ClusterName: "cluster", Namespace: "default", - ServiceName: "cluster-mysql-nodes", + ServiceName: "mysql", BackupUser: "backup-user", BackupPassword: "backup-password", } @@ -45,7 +45,7 @@ var _ = Describe("Test sidecar configs", func() { }) It("should get the default master", func() { - Expect(cfg.MasterFQDN()).To(Equal("cluster-mysql-0.cluster-mysql-nodes.default")) + Expect(cfg.MasterFQDN()).To(Equal("cluster-mysql-0.mysql.default")) }) It("should determine the node role", func() {