Skip to content

Commit

Permalink
Use a common headless svc for all clusters, fixes #246
Browse files Browse the repository at this point in the history
  • Loading branch information
AMecea committed Mar 29, 2019
1 parent 0f33e91 commit 7362737
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/controller/mysqlcluster/mysqlcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,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),
Expand Down Expand Up @@ -201,7 +195,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{}),
Expand Down
13 changes: 10 additions & 3 deletions pkg/internal/mysqlcluster/mysqlcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +67,8 @@ func (c *MysqlCluster) GetLabels() labels.Set {
}

labels := labels.Set{
"mysql.presslabs.org/cluster": c.Name,
"mysql.presslabs.org/cluster": c.Name,

"app.kubernetes.io/name": "mysql",
"app.kubernetes.io/instance": instance,
"app.kubernetes.io/version": version,
Expand All @@ -80,8 +86,9 @@ 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{
"mysql.presslabs.org/cluster": c.Name,

"app.kubernetes.io/name": "mysql",
"mysql.presslabs.org/cluster": c.Name,
"app.kubernetes.io/managed-by": "mysql.presslabs.org",
}
}
Expand Down Expand Up @@ -117,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)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/orchestrator/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fake
import (
"database/sql"
"fmt"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/sidecar/configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand All @@ -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() {
Expand Down

0 comments on commit 7362737

Please sign in to comment.