Repository: m3db/m3db-operator
Pull request #287 (2021-04-27 16:03:19)

[placement] Allow zone to be overridden
This allows us to direct placement writes
to an etcd cluster other than the hardcoded "embedded"
zone.

codecov[bot]: # [Codecov](https://codecov.io/gh/m3db/m3db-operator/pull/287?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=m3db) Report
> Merging [#287](https://codecov.io/gh/m3db/m3db-operator/pull/287?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=m3db) (d5d9a76) into [master](https://codecov.io/gh/m3db/m3db-operator/commit/3259a9c503178c823f38f9b395135c0a88f3dcae?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=m3db) (3259a9c) will **decrease** coverage by `0.12%`.
> The diff coverage is `100.00%`.


[![Impacted file tree graph](https://codecov.io/gh/m3db/m3db-operator/pull/287/graphs/tree.svg?width=650&height=150&src=pr&token=jtHDyWhsto&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=m3db)](https://codecov.io/gh/m3db/m3db-operator/pull/287?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=m3db)

```diff
@@            Coverage Diff             @@
##           master     #287      +/-   ##
==========================================
- Coverage   76.16%   76.03%   -0.13%     
==========================================
  Files          32       32              
  Lines        2538     2541       +3     
==========================================
- Hits         1933     1932       -1     
- Misses        427      430       +3     
- Partials      178      179       +1     
```


------

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


Patch:
diff --git a/pkg/apis/m3dboperator/v1alpha1/cluster.go b/pkg/apis/m3dboperator/v1alpha1/cluster.go
index a4ecbf0..973143f 100644
--- a/pkg/apis/m3dboperator/v1alpha1/cluster.go
+++ b/pkg/apis/m3dboperator/v1alpha1/cluster.go
@@ -323,6 +323,11 @@ type ClusterSpec struct {
 	// OnDeleteUpdateStrategy sets StatefulSets created by the operator to
 	// have OnDelete as the update strategy instead of RollingUpdate.
 	OnDeleteUpdateStrategy bool `json:"onDeleteUpdateStrategy,omitempty"`
+
+	// Zone defines the zone that placement instances will be written to if set.
+	// If not set, the default zone of "embedded" will be used.
+	// +optional
+	Zone string `json:"zone,omitempty"`
 }
 
 // ExternalCoordinatorConfig defines parameters for using an external
diff --git a/pkg/apis/m3dboperator/v1alpha1/openapi_generated.go b/pkg/apis/m3dboperator/v1alpha1/openapi_generated.go
index 5755f18..6e3c9cb 100644
--- a/pkg/apis/m3dboperator/v1alpha1/openapi_generated.go
+++ b/pkg/apis/m3dboperator/v1alpha1/openapi_generated.go
@@ -657,6 +657,13 @@ func schema_pkg_apis_m3dboperator_v1alpha1_ClusterSpec(ref common.ReferenceCallb
 							Format:      "",
 						},
 					},
+					"zone": {
+						SchemaProps: spec.SchemaProps{
+							Description: "Zone defines the zone that placement instances will be written to if set. If not set, the default zone of \"embedded\" will be used.",
+							Type:        []string{"string"},
+							Format:      "",
+						},
+					},
 				},
 				Required: []string{"parallelPodManagement"},
 			},
diff --git a/pkg/k8sops/m3db/placement.go b/pkg/k8sops/m3db/placement.go
index 93fc3dc..b99248c 100644
--- a/pkg/k8sops/m3db/placement.go
+++ b/pkg/k8sops/m3db/placement.go
@@ -88,10 +88,15 @@ func PlacementInstanceFromPod(cluster *myspec.M3DBCluster, pod *corev1.Pod, idPr
 		return nil, pkgerrors.WithMessage(err, "cannot execute node endpoint template")
 	}
 
+	zone := cluster.Spec.Zone
+	if zone == "" {
+		zone = _zoneEmbedded
+	}
+
 	instance := &placementpb.Instance{
 		Id:             idStr,
 		IsolationGroup: isoGroup,
-		Zone:           _zoneEmbedded,
+		Zone:           zone,
 		Weight:         100,
 		Hostname:       pod.Name + "." + epCtx.M3DBService,
 		Endpoint:       str.String(),


Test patch:
diff --git a/pkg/k8sops/m3db/placement_test.go b/pkg/k8sops/m3db/placement_test.go
index ae3d346..c0d8353 100644
--- a/pkg/k8sops/m3db/placement_test.go
+++ b/pkg/k8sops/m3db/placement_test.go
@@ -86,6 +86,22 @@ func TestPlacementInstanceFromPod(t *testing.T) {
 			Port:           9000,
 		}
 
+		inst, err := PlacementInstanceFromPod(cluster, pod, idProvider)
+		assert.NoError(t, err)
+		assert.Equal(t, expInst, inst)
+	})
+	t.Run("zone override", func(t *testing.T) {
+		cluster.Spec.Zone = "embedded-override"
+		expInst := &placementpb.Instance{
+			Id:             `{"name":"pod-a"}`,
+			IsolationGroup: "zone-a",
+			Zone:           "embedded-override",
+			Weight:         100,
+			Hostname:       "pod-a.m3dbnode-cluster-a",
+			Endpoint:       "pod-a.m3dbnode-cluster-a.my_ns:9000",
+			Port:           9000,
+		}
+
 		inst, err := PlacementInstanceFromPod(cluster, pod, idProvider)
 		assert.NoError(t, err)
 		assert.Equal(t, expInst, inst)