Repository: scylladb/scylla-operator
Pull request #969 (2022-04-06 14:07:05)

Allow directly selecting member services for building CQL connections
<!-- Please take a look at our [Contributing](https://github.com/scylladb/scylla-operator/blob/master/docs/contributing.md)
documentation before submitting a Pull Request!
Thank you for contributing to the Scylla Operator! -->

**Description of your changes:**
This PR introduces `scylla-operator.scylladb.com/scylla-service-type` label to Scylla Services, which allows for distinguishing between `member` and `identity` (headless) Services.



Patch:
diff --git a/pkg/controller/scyllacluster/resource.go b/pkg/controller/scyllacluster/resource.go
index ffc6adb1b..cd12aea4e 100644
--- a/pkg/controller/scyllacluster/resource.go
+++ b/pkg/controller/scyllacluster/resource.go
@@ -31,11 +31,14 @@ const (
 )
 
 func HeadlessServiceForCluster(c *scyllav1.ScyllaCluster) *corev1.Service {
+	labels := naming.ClusterLabels(c)
+	labels[naming.ScyllaServiceTypeLabel] = string(naming.ScyllaServiceTypeIdentity)
+
 	return &corev1.Service{
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      naming.HeadlessServiceNameForCluster(c),
 			Namespace: c.Namespace,
-			Labels:    naming.ClusterLabels(c),
+			Labels:    labels,
 			OwnerReferences: []metav1.OwnerReference{
 				*metav1.NewControllerRef(c, controllerGVK),
 			},
@@ -62,6 +65,7 @@ func MemberService(sc *scyllav1.ScyllaCluster, rackName, name string, oldService
 	labels := naming.ClusterLabels(sc)
 	labels[naming.DatacenterNameLabel] = sc.Spec.Datacenter.Name
 	labels[naming.RackNameLabel] = rackName
+	labels[naming.ScyllaServiceTypeLabel] = string(naming.ScyllaServiceTypeMember)
 
 	// Copy the old replace label, if present.
 	var replaceAddr string
diff --git a/pkg/naming/constants.go b/pkg/naming/constants.go
index 3ae83d559..5345ab3b4 100644
--- a/pkg/naming/constants.go
+++ b/pkg/naming/constants.go
@@ -27,12 +27,20 @@ const (
 	LabelValueFalse = "false"
 )
 
+type ScyllaServiceType string
+
+const (
+	ScyllaServiceTypeIdentity ScyllaServiceType = "identity"
+	ScyllaServiceTypeMember   ScyllaServiceType = "member"
+)
+
 // Generic Labels used on objects created by the operator.
 const (
 	ClusterNameLabel             = "scylla/cluster"
 	DatacenterNameLabel          = "scylla/datacenter"
 	RackNameLabel                = "scylla/rack"
 	ScyllaVersionLabel           = "scylla/scylla-version"
+	ScyllaServiceTypeLabel       = "scylla-operator.scylladb.com/scylla-service-type"
 	ManagedHash                  = "scylla-operator.scylladb.com/managed-hash"
 	NodeConfigJobForNodeUIDLabel = "scylla-operator.scylladb.com/node-config-job-for-node-uid"
 	NodeConfigJobTypeLabel       = "scylla-operator.scylladb.com/node-config-job-type"


Test patch:
diff --git a/pkg/controller/scyllacluster/resource_test.go b/pkg/controller/scyllacluster/resource_test.go
index eb5430552..5fd0edf1a 100644
--- a/pkg/controller/scyllacluster/resource_test.go
+++ b/pkg/controller/scyllacluster/resource_test.go
@@ -55,6 +55,7 @@ func TestMemberService(t *testing.T) {
 			"scylla/cluster":               "basic",
 			"scylla/datacenter":            "dc",
 			"scylla/rack":                  "rack",
+			"scylla-operator.scylladb.com/scylla-service-type": "member",
 		}
 	}
 	basicPorts := []corev1.ServicePort{
diff --git a/test/e2e/utils/helpers.go b/test/e2e/utils/helpers.go
index 796b21cef..37b32e0d9 100644
--- a/test/e2e/utils/helpers.go
+++ b/test/e2e/utils/helpers.go
@@ -518,9 +518,7 @@ func GetScyllaClient(ctx context.Context, client corev1client.CoreV1Interface, s
 
 func GetHosts(ctx context.Context, client corev1client.CoreV1Interface, sc *scyllav1.ScyllaCluster) ([]string, error) {
 	serviceList, err := client.Services(sc.Namespace).List(ctx, metav1.ListOptions{
-		LabelSelector: labels.Set{
-			naming.ClusterNameLabel: sc.Name,
-		}.AsSelector().String(),
+		LabelSelector: GetMemberServiceSelector(sc.Name).String(),
 	})
 	if err != nil {
 		return nil, err
@@ -532,11 +530,6 @@ func GetHosts(ctx context.Context, client corev1client.CoreV1Interface, sc *scyl
 			return nil, fmt.Errorf("service %s/%s is of type %q instead of %q", s.Namespace, s.Name, s.Spec.Type, corev1.ServiceTypeClusterIP)
 		}
 
-		// TODO: Fix labeling in the operator so it can be selected.
-		if s.Name == naming.HeadlessServiceNameForCluster(sc) {
-			continue
-		}
-
 		if s.Spec.ClusterIP == corev1.ClusterIPNone {
 			return nil, fmt.Errorf("service %s/%s doesn't have a ClusterIP", s.Namespace, s.Name)
 		}
@@ -580,3 +573,10 @@ func GetNodeName(sc *scyllav1.ScyllaCluster, idx int) string {
 		idx,
 	)
 }
+
+func GetMemberServiceSelector(scyllaClusterName string) labels.Selector {
+	return labels.Set{
+		naming.ClusterNameLabel:       scyllaClusterName,
+		naming.ScyllaServiceTypeLabel: string(naming.ScyllaServiceTypeMember),
+	}.AsSelector()
+}