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/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/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/pkg/internal/mysqlcluster/mysqlcluster.go b/pkg/internal/mysqlcluster/mysqlcluster.go index 5f0529b07..7519f12a3 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 @@ -72,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, @@ -91,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", } } @@ -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: diff --git a/pkg/orchestrator/fake/client.go b/pkg/orchestrator/fake/client.go index e87fb3477..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*.mysql.([\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 d85b3ecfd..0e593a547 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) } @@ -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