Repository: m3db/m3db-operator
Pull request #216 (2020-05-20 21:10:20)

[spec] Break ext coord into separate config
We plan on adding more ways of looking up an external coordinator than
just a selector. Allow the external config to be expanded later on.

codecov[bot]: # [Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/216?src=pr&el=h1) Report
> Merging [#216](https://codecov.io/gh/m3db/m3db-operator/pull/216?src=pr&el=desc) into [master](https://codecov.io/gh/m3db/m3db-operator/commit/f2a93be545807c8cbd1e20069904f62606b33fb8&el=desc) will **decrease** coverage by `0.18%`.
> The diff coverage is `100.00%`.


[![Impacted file tree graph](https://codecov.io/gh/m3db/m3db-operator/pull/216/graphs/tree.svg?width=650&height=150&src=pr&token=jtHDyWhsto)](https://codecov.io/gh/m3db/m3db-operator/pull/216?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #216      +/-   ##
==========================================
- Coverage   75.52%   75.33%   -0.19%     
==========================================
  Files          30       30              
  Lines        2112     2112              
==========================================
- Hits         1595     1591       -4     
- Misses        391      393       +2     
- Partials      126      128       +2     
```


------

[Continue to review full report at Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/216?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/216?src=pr&el=footer). Last update [f2a93be...df694ea](https://codecov.io/gh/m3db/m3db-operator/pull/216?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/api.md b/docs/api.md
index a9f5403..2581edf 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -6,6 +6,7 @@ This document enumerates the Custom Resource Definitions used by the M3DB Operat
 ## Table of Contents
 * [ClusterCondition](#clustercondition)
 * [ClusterSpec](#clusterspec)
+* [ExternalCoordinatorConfig](#externalcoordinatorconfig)
 * [IsolationGroup](#isolationgroup)
 * [M3DBCluster](#m3dbcluster)
 * [M3DBClusterList](#m3dbclusterlist)
@@ -62,7 +63,7 @@ ClusterSpec defines the desired state for a M3 cluster to be converge to.
 | nodeEndpointFormat | NodeEndpointFormat allows overriding of the endpoint used for a node in the M3DB placement. Defaults to \"{{ .PodName }}.{{ .M3DBService }}:{{ .Port }}\". Useful if access to the cluster from other namespaces is desired. See \"Node Endpoint\" docs for full variables available. | string | false |
 | hostNetwork | HostNetwork indicates whether M3DB pods should run in the same network namespace as the node its on. This option should be used sparingly due to security concerns outlined in the linked documentation. https://kubernetes.io/docs/concepts/policy/pod-security-policy/#host-namespaces | bool | false |
 | dnsPolicy | DNSPolicy allows the user to set the pod's DNSPolicy. This is often used in conjunction with HostNetwork.+optional | *corev1.DNSPolicy | false |
-| externalCoordinatorSelector | Specify a \"controlling\" coordinator for the cluster It is expected that there is a separate standalone coordinator cluster It is externally managed - not managed by this operator It is expected to have a service endpoint Setup this db cluster, but do not assume a co-located coordinator Instead provide a selector here so we can point to a separate coordinator service Specify here the labels required for the selector | map[string]string | false |
+| externalCoordinator | Specify a \"controlling\" coordinator for the cluster. | *[ExternalCoordinatorConfig](#externalcoordinatorconfig) | false |
 | initContainers | Custom setup for db nodes can be done via initContainers Provide the complete spec for the initContainer here If any storage volumes are needed in the initContainer see InitVolumes below | []corev1.Container | false |
 | initVolumes | If the InitContainers require any storage volumes Provide the complete specification for the required Volumes here | []corev1.Volume | false |
 | podMetadata | PodMetadata is for any Metadata that is unique to the pods, and does not belong on any other objects, such as Prometheus scrape tags | [metav1.ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#objectmeta-v1-meta) | false |
@@ -70,6 +71,16 @@ ClusterSpec defines the desired state for a M3 cluster to be converge to.
 
 [Back to TOC](#table-of-contents)
 
+## ExternalCoordinatorConfig
+
+ExternalCoordinatorConfig defines parameters for using an external coordinator to control the cluster.\n\n- It is expected that there is a separate standalone coordinator cluster. - It is externally managed - not managed by this operator. - It is expected to have a service endpoint.\n\nSetup this db cluster, but do not assume a co-located coordinator. Instead provide a selector here so we can point to a separate coordinator service.
+
+| Field | Description | Scheme | Required |
+| ----- | ----------- | ------ | -------- |
+| selector |  | map[string]string | true |
+
+[Back to TOC](#table-of-contents)
+
 ## IsolationGroup
 
 IsolationGroup defines the name of zone as well attributes for the zone configuration
diff --git a/pkg/apis/m3dboperator/v1alpha1/cluster.go b/pkg/apis/m3dboperator/v1alpha1/cluster.go
index 800d6a5..2344e56 100644
--- a/pkg/apis/m3dboperator/v1alpha1/cluster.go
+++ b/pkg/apis/m3dboperator/v1alpha1/cluster.go
@@ -278,14 +278,8 @@ type ClusterSpec struct {
 	// +optional
 	DNSPolicy *corev1.DNSPolicy `json:"dnsPolicy,omitEmpty"`
 
-	// Specify a "controlling" coordinator for the cluster
-	// It is expected that there is a separate standalone coordinator cluster
-	// It is externally managed - not managed by this operator
-	// It is expected to have a service endpoint
-	// Setup this db cluster, but do not assume a co-located coordinator
-	// Instead provide a selector here so we can point to a separate coordinator service
-	// Specify here the labels required for the selector
-	ExternalCoordinatorSelector map[string]string `json:"externalCoordinatorSelector,omitempty"`
+	// Specify a "controlling" coordinator for the cluster.
+	ExternalCoordinator *ExternalCoordinatorConfig `json:"externalCoordinator,omitempty"`
 
 	// Custom setup for db nodes can be done via initContainers
 	// Provide the complete spec for the initContainer here
@@ -308,6 +302,19 @@ type ClusterSpec struct {
 	ParallelPodManagement bool `json:"parallelPodManagement,omitEmpty"`
 }
 
+// ExternalCoordinatorConfig defines parameters for using an external
+// coordinator to control the cluster.
+//
+// - It is expected that there is a separate standalone coordinator cluster.
+// - It is externally managed - not managed by this operator.
+// - It is expected to have a service endpoint.
+//
+// Setup this db cluster, but do not assume a co-located coordinator. Instead
+// provide a selector here so we can point to a separate coordinator service.
+type ExternalCoordinatorConfig struct {
+	Selector map[string]string `json:"selector,omityempty"`
+}
+
 // NodeAffinityTerm represents a node label and a set of label values, any of
 // which can be matched to assign a pod to a node.
 // +k8s:openapi-gen=true
diff --git a/pkg/k8sops/m3db/generators.go b/pkg/k8sops/m3db/generators.go
index 331e518..3e3bd06 100644
--- a/pkg/k8sops/m3db/generators.go
+++ b/pkg/k8sops/m3db/generators.go
@@ -246,8 +246,8 @@ func GenerateCoordinatorService(cluster *myspec.M3DBCluster) (*v1.Service, error
 
 	selectorLabels := labels.BaseLabels(cluster)
 	selectorLabels[labels.Component] = labels.ComponentM3DBNode
-	if len(cluster.Spec.ExternalCoordinatorSelector) > 0 {
-		selectorLabels = cluster.Spec.ExternalCoordinatorSelector
+	if cluster.Spec.ExternalCoordinator != nil && len(cluster.Spec.ExternalCoordinator.Selector) > 0 {
+		selectorLabels = cluster.Spec.ExternalCoordinator.Selector
 	}
 
 	serviceLabels := labels.BaseLabels(cluster)


Test patch:
diff --git a/pkg/k8sops/m3db/generators_test.go b/pkg/k8sops/m3db/generators_test.go
index fe46a2d..bdb26b9 100644
--- a/pkg/k8sops/m3db/generators_test.go
+++ b/pkg/k8sops/m3db/generators_test.go
@@ -579,7 +579,7 @@ func TestGenerateCoordinatorService(t *testing.T) {
 
 	assert.Equal(t, expSvc, svc)
 
-	cluster.Spec.ExternalCoordinatorSelector = map[string]string{"foo": "bar"}
+	cluster.Spec.ExternalCoordinator = &myspec.ExternalCoordinatorConfig{Selector: map[string]string{"foo": "bar"}}
 	expSvc.Spec.Selector = map[string]string{"foo": "bar"}
 	svc, err = GenerateCoordinatorService(cluster)
 	assert.NoError(t, err)