From aaaa77f36b73dbaebe0a6d767c3a8ac6bae53706 Mon Sep 17 00:00:00 2001 From: Denis Slepov Date: Mon, 25 Sep 2023 14:57:17 +0300 Subject: [PATCH 1/5] Change headless svc Change the Headless Service Name to Template-Generated Name --- CHANGELOG.md | 2 ++ pkg/internal/mysqlcluster/mysqlcluster.go | 7 +------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c9ecf77..209704fab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). * Add `KeepAfterDelete` in `.Spec.VolumeSpec` to keep pvc after mysql cluster been deleted. ### Changed +* Changed headless svc name to a templated name that includes the mysql cluster name + ### Removed ### Fixed diff --git a/pkg/internal/mysqlcluster/mysqlcluster.go b/pkg/internal/mysqlcluster/mysqlcluster.go index 5f0529b07..58ce0ec73 100644 --- a/pkg/internal/mysqlcluster/mysqlcluster.go +++ b/pkg/internal/mysqlcluster/mysqlcluster.go @@ -30,11 +30,6 @@ import ( "github.com/bitpoke/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 @@ -137,7 +132,7 @@ func GetNameForResource(name ResourceName, clusterName string) string { case HealthyReplicasService: return fmt.Sprintf("%s-mysql-replicas", clusterName) case HeadlessSVC: - return HeadlessSVCName + return fmt.Sprintf("%s-mysql-headless", clusterName) case OldHeadlessSVC: return fmt.Sprintf("%s-mysql-nodes", clusterName) case Secret: From 96b3fae0fc3a6f727f0c832bd10c4f4536be072f Mon Sep 17 00:00:00 2001 From: Denis Slepov Date: Mon, 25 Sep 2023 17:08:44 +0300 Subject: [PATCH 2/5] Fix svc test Fix headless svc regexp --- pkg/orchestrator/fake/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/orchestrator/fake/client.go b/pkg/orchestrator/fake/client.go index e87fb3477..e8bf71be6 100644 --- a/pkg/orchestrator/fake/client.go +++ b/pkg/orchestrator/fake/client.go @@ -171,7 +171,7 @@ func (o *OrcFakeClient) CheckDiscovered(key string) bool { func (o *OrcFakeClient) getHostClusterAlias(host string) string { // input: cluster-1943285891-mysql-0.mysql.default // output: cluster-1943285891.default - re := regexp.MustCompile(`^([\w-]+)-mysql-\d*.mysql.([\w-]+)$`) + re := regexp.MustCompile(`^([\w-]+)-mysql-\d*.([\w-]+)-mysql-headless.([\w-]+)$`) values := re.FindStringSubmatch(host) return fmt.Sprintf("%s.%s", values[1], values[2]) } From adb4b535726fcea2b66bf9c2181dc6c4517e47b6 Mon Sep 17 00:00:00 2001 From: Denis Slepov Date: Tue, 26 Sep 2023 18:06:14 +0500 Subject: [PATCH 3/5] Fix svc e2e --- pkg/controller/orchestrator/orchestrator_reconcile.go | 2 +- test/e2e/framework/cluster_util.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/controller/orchestrator/orchestrator_reconcile.go b/pkg/controller/orchestrator/orchestrator_reconcile.go index d33fb1310..7397501cd 100644 --- a/pkg/controller/orchestrator/orchestrator_reconcile.go +++ b/pkg/controller/orchestrator/orchestrator_reconcile.go @@ -486,7 +486,7 @@ func (ou *orcUpdater) removeNodeConditionNotInOrc(insts InstancesSet) { // indexInSts is a helper function that returns the index of the pod in statefulset func indexInSts(name string) (int32, error) { - re := regexp.MustCompile(`^[\w-]+-mysql-(\d*)\.[\w-]*mysql(?:-nodes)?\.[\w-]+$`) + re := regexp.MustCompile(`^[\w-]+-mysql-(\d*)\.[\w-]*mysql(?:-nodes|-headless)?\.[\w-]+$`) values := re.FindStringSubmatch(name) if len(values) != 2 { return 0, fmt.Errorf("no match found") diff --git a/test/e2e/framework/cluster_util.go b/test/e2e/framework/cluster_util.go index d85b3ecfd..f0d67c6c4 100644 --- a/test/e2e/framework/cluster_util.go +++ b/test/e2e/framework/cluster_util.go @@ -143,7 +143,7 @@ func GetNameForResource(name string, cluster *api.MysqlCluster) string { case "svc-read": return fmt.Sprintf("%s-mysql", cluster.Name) case "svc-headless": - return "mysql" + return fmt.Sprintf("%s-mysql-headless", cluster.Name) default: return fmt.Sprintf("%s-mysql", cluster.Name) } From 248fb1b3199d5dc7c00da875684e52807114052e Mon Sep 17 00:00:00 2001 From: Denis Slepov Date: Tue, 26 Sep 2023 19:14:56 +0500 Subject: [PATCH 4/5] Fix selectors --- .../mysqlcluster/internal/syncer/headless_service.go | 6 ++++-- pkg/internal/mysqlcluster/mysqlcluster.go | 6 +++--- pkg/orchestrator/fake/client.go | 2 +- test/e2e/framework/cluster_util.go | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/controller/mysqlcluster/internal/syncer/headless_service.go b/pkg/controller/mysqlcluster/internal/syncer/headless_service.go index d7d806fe4..df9297abb 100644 --- a/pkg/controller/mysqlcluster/internal/syncer/headless_service.go +++ b/pkg/controller/mysqlcluster/internal/syncer/headless_service.go @@ -17,6 +17,8 @@ limitations under the License. package mysqlcluster import ( + "fmt" + core "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -40,14 +42,14 @@ func NewHeadlessSVCSyncer(c client.Client, scheme *runtime.Scheme, cluster *mysq return syncer.NewObjectSyncer("HeadlessSVC", nil, service, c, func() error { // add general labels to this service service.Labels = map[string]string{ - "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", cluster.Name), "app.kubernetes.io/managed-by": "mysql.presslabs.org", } service.Labels["mysql.presslabs.org/service-type"] = "namespace-nodes" service.Spec.ClusterIP = "None" service.Spec.Selector = labels.Set{ - "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", cluster.Name), "app.kubernetes.io/managed-by": "mysql.presslabs.org", } // we want to be able to access pods even if the pod is not ready because the operator should update diff --git a/pkg/internal/mysqlcluster/mysqlcluster.go b/pkg/internal/mysqlcluster/mysqlcluster.go index 58ce0ec73..d43de32ea 100644 --- a/pkg/internal/mysqlcluster/mysqlcluster.go +++ b/pkg/internal/mysqlcluster/mysqlcluster.go @@ -67,7 +67,7 @@ func (c *MysqlCluster) GetLabels() labels.Set { labels := labels.Set{ "mysql.presslabs.org/cluster": c.Name, - "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", c.Name), "app.kubernetes.io/instance": instance, "app.kubernetes.io/version": c.GetMySQLSemVer().String(), "app.kubernetes.io/component": component, @@ -86,7 +86,7 @@ func (c *MysqlCluster) GetSelectorLabels() labels.Set { return labels.Set{ "mysql.presslabs.org/cluster": c.Name, - "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", c.Name), "app.kubernetes.io/managed-by": "mysql.presslabs.org", } } @@ -160,7 +160,7 @@ func (c *MysqlCluster) GetMasterHost() string { for _, ns := range c.Status.Nodes { if cond := c.GetNodeCondition(ns.Name, api.NodeConditionMaster); cond != nil && - cond.Status == core.ConditionTrue { + cond.Status == core.ConditionTrue { masterHost = ns.Name } } diff --git a/pkg/orchestrator/fake/client.go b/pkg/orchestrator/fake/client.go index e8bf71be6..7d4de443b 100644 --- a/pkg/orchestrator/fake/client.go +++ b/pkg/orchestrator/fake/client.go @@ -171,7 +171,7 @@ func (o *OrcFakeClient) CheckDiscovered(key string) bool { func (o *OrcFakeClient) getHostClusterAlias(host string) string { // input: cluster-1943285891-mysql-0.mysql.default // output: cluster-1943285891.default - re := regexp.MustCompile(`^([\w-]+)-mysql-\d*.([\w-]+)-mysql-headless.([\w-]+)$`) + re := regexp.MustCompile(`^([\w-]+)-mysql-\d*(?:\.[\w-]+)?-mysql(?:-headless)?\.(.+)$`) values := re.FindStringSubmatch(host) return fmt.Sprintf("%s.%s", values[1], values[2]) } diff --git a/test/e2e/framework/cluster_util.go b/test/e2e/framework/cluster_util.go index f0d67c6c4..0e593a547 100644 --- a/test/e2e/framework/cluster_util.go +++ b/test/e2e/framework/cluster_util.go @@ -234,7 +234,7 @@ func (f *Framework) ReadSQLTest(cluster *api.MysqlCluster, pod int, pw string) s func GetClusterLabels(cluster *api.MysqlCluster) labels.Set { labels := labels.Set{ "mysql.presslabs.org/cluster": cluster.Name, - "app.kubernetes.io/name": "mysql", + "app.kubernetes.io/name": fmt.Sprintf("%s-mysql-headless", cluster.Name), } return labels From 1c2339f98fc543cfa947701c1e29d007bbd9f025 Mon Sep 17 00:00:00 2001 From: Denis Slepov Date: Fri, 6 Oct 2023 17:57:15 +0300 Subject: [PATCH 5/5] Fix format --- pkg/internal/mysqlcluster/mysqlcluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/internal/mysqlcluster/mysqlcluster.go b/pkg/internal/mysqlcluster/mysqlcluster.go index d43de32ea..7519f12a3 100644 --- a/pkg/internal/mysqlcluster/mysqlcluster.go +++ b/pkg/internal/mysqlcluster/mysqlcluster.go @@ -160,7 +160,7 @@ func (c *MysqlCluster) GetMasterHost() string { for _, ns := range c.Status.Nodes { if cond := c.GetNodeCondition(ns.Name, api.NodeConditionMaster); cond != nil && - cond.Status == core.ConditionTrue { + cond.Status == core.ConditionTrue { masterHost = ns.Name } }