Skip to content

Commit

Permalink
[m3admin-client] Set zone header based on cluster spec (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyk authored Apr 27, 2021
1 parent 41fcc6e commit 29e99f3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions integration/harness/m3admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (h *Harness) NewPlacementClient(cluster *myspec.M3DBCluster) (placement.Cli
env := k8sops.DefaultM3ClusterEnvironmentName(cluster)
adminCl := m3admin.NewClient(
m3admin.WithEnvironment(env),
m3admin.WithZone(cluster.Spec.Zone),
)
url := fmt.Sprintf(proxyBaseURLFmt, h.Namespace, svc.Name, "7201")
h.Logger.Sugar().Infof("calling url '%s' with env '%s'", url, env)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/m3admin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func newMultiAdminClient(adminOpts []m3admin.Option, logger *zap.Logger) *multiA

func (m *multiAdminClient) adminClientForCluster(cluster *myspec.M3DBCluster) m3admin.Client {
env := k8sops.DefaultM3ClusterEnvironmentName(cluster)
opts := append(m.adminOpts, m3admin.WithEnvironment(env))
opts := append(m.adminOpts, m3admin.WithEnvironment(env), m3admin.WithZone(cluster.Spec.Zone))
return m.adminClientFn(opts...)
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/m3admin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (

const (
m3EnvironmentHeader = "Cluster-Environment-Name"
// m3HeaderClusterZoneName is the header used to specify the zone name.
m3HeaderClusterZoneName = "Cluster-Zone-Name"
)

var (
Expand All @@ -61,6 +63,7 @@ type client struct {
client *retryhttp.Client
logger *zap.Logger
environment string
zone string
}

type nullLogger struct{}
Expand All @@ -78,6 +81,7 @@ func NewClient(clientOpts ...Option) Client {
client: opts.client,
logger: opts.logger,
environment: opts.environment,
zone: opts.zone,
}

if client.client == nil {
Expand Down Expand Up @@ -133,6 +137,9 @@ func (c *client) DoHTTPRequest(
if c.environment != "" {
request.Header.Add(m3EnvironmentHeader, c.environment)
}
if c.zone != "" {
request.Header.Add(m3HeaderClusterZoneName, c.zone)
}

if l.Core().Enabled(zapcore.DebugLevel) {
dump, err := httputil.DumpRequest(request.Request, true)
Expand Down
12 changes: 10 additions & 2 deletions pkg/m3admin/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func TestClient_DoHTTPRequest_Header(t *testing.T) {
w.WriteHeader(500)
return
}
if r.Header.Get(m3HeaderClusterZoneName) != "bar-zone" {
w.WriteHeader(500)
return
}
w.Write([]byte("hello"))
}))
defer s.Close()
Expand All @@ -102,11 +106,15 @@ func TestClient_DoHTTPRequest_Header(t *testing.T) {
return data
}

cl := newTestClient(WithEnvironment("fooz-env"))
cl := newTestClient(WithEnvironment("fooz-env"), WithZone("bar-zone"))
_, err := cl.DoHTTPRequest("GET", s.URL, nil)
assert.Error(t, err)

cl = newTestClient(WithEnvironment("foo-env"))
cl = newTestClient(WithEnvironment("foo-env"), WithZone("barz-zone"))
_, err = cl.DoHTTPRequest("GET", s.URL, nil) //nolint:bodyclose
assert.Error(t, err)

cl = newTestClient(WithEnvironment("foo-env"), WithZone("bar-zone"))
resp, err := cl.DoHTTPRequest("GET", s.URL, nil)
require.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusOK)
Expand Down
8 changes: 8 additions & 0 deletions pkg/m3admin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type options struct {
logger *zap.Logger
client *retryhttp.Client
environment string
zone string
}

// WithLogger configures a logger for the client. If not set a noop logger will
Expand Down Expand Up @@ -67,6 +68,13 @@ func WithEnvironment(e string) Option {
})
}

// WithZone controls the Cluster-Zone-Name header to m3coordinator.
func WithZone(z string) Option {
return optionFn(func(o *options) {
o.zone = z
})
}

// RequestOption defines a per-request option.
type RequestOption interface {
execute(*reqOptions)
Expand Down

0 comments on commit 29e99f3

Please sign in to comment.