Skip to content

Commit

Permalink
disables dispatch for memory datastore
Browse files Browse the repository at this point in the history
fixes #111

users running the repository example where stuck
because the SpiceDB instance spun up had Dispatch API
server disabled, but actual dispatching enabled, so
Check requests errored out.

This adds tests to verify the untested method
ToEnvVarApplyConfiguration() and makes sure
memory datastore does not set dispatchUpstreamAddr
  • Loading branch information
vroldanbet committed Dec 1, 2022
1 parent f98d9ac commit 68a5069
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,16 @@ func (c *Config) ToEnvVarApplyConfiguration() []*applycorev1.EnvVarApplyConfigur
// (tls), or lives in an external secret (preshared key).
envVars := []*applycorev1.EnvVarApplyConfiguration{
applycorev1.EnvVar().WithName(c.SpiceConfig.EnvPrefix + "_LOG_LEVEL").WithValue(c.LogLevel),
applycorev1.EnvVar().WithName(c.SpiceConfig.EnvPrefix + "_DISPATCH_UPSTREAM_ADDR").
WithValue(fmt.Sprintf("kubernetes:///%s.%s:dispatch", c.Name, c.Namespace)),
applycorev1.EnvVar().WithName(c.SpiceConfig.EnvPrefix + "_GRPC_PRESHARED_KEY").
WithValueFrom(applycorev1.EnvVarSource().WithSecretKeyRef(
applycorev1.SecretKeySelector().WithName(c.SecretName).WithKey("preshared_key"))),
}
if c.DatastoreEngine != "memory" {
envVars = append(envVars, applycorev1.EnvVar().WithName(c.SpiceConfig.EnvPrefix+"_DATASTORE_CONN_URI").
WithValueFrom(applycorev1.EnvVarSource().WithSecretKeyRef(
applycorev1.SecretKeySelector().WithName(c.SecretName).WithKey("datastore_uri"))))
envVars = append(envVars,
applycorev1.EnvVar().WithName(c.SpiceConfig.EnvPrefix+"_DATASTORE_CONN_URI").WithValueFrom(applycorev1.EnvVarSource().WithSecretKeyRef(
applycorev1.SecretKeySelector().WithName(c.SecretName).WithKey("datastore_uri"))),
applycorev1.EnvVar().WithName(c.SpiceConfig.EnvPrefix+"_DISPATCH_UPSTREAM_ADDR").
WithValue(fmt.Sprintf("kubernetes:///%s.%s:dispatch", c.Name, c.Namespace)))
}

// Passthrough config is user-provided and only affects spicedb runtime.
Expand Down
176 changes: 176 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package config
import (
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
v1 "k8s.io/client-go/applyconfigurations/core/v1"

"github.com/authzed/spicedb-operator/pkg/apis/authzed/v1alpha1"
"github.com/authzed/spicedb-operator/pkg/updates"
Expand Down Expand Up @@ -50,6 +52,7 @@ func TestNewConfig(t *testing.T) {
name string
args args
want *Config
wantEnvs []string
wantWarnings []error
wantErrs []error
}{
Expand Down Expand Up @@ -136,6 +139,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "memory",
Expand Down Expand Up @@ -197,6 +208,12 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_ENGINE=memory",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=false",
},
},
{
name: "set image with tag explicitly",
Expand Down Expand Up @@ -244,6 +261,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set image with digest explicitly",
Expand Down Expand Up @@ -291,6 +316,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set replicas as int",
Expand Down Expand Up @@ -354,6 +387,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set replicas as string",
Expand Down Expand Up @@ -417,6 +458,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set extra labels as string",
Expand Down Expand Up @@ -484,6 +533,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set extra labels as map",
Expand Down Expand Up @@ -554,6 +611,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "skip migrations bool",
Expand Down Expand Up @@ -619,6 +684,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "skip migrations string",
Expand Down Expand Up @@ -684,6 +757,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set extra annotations as string",
Expand Down Expand Up @@ -751,6 +832,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set extra annotations as map",
Expand Down Expand Up @@ -821,6 +910,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=info",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "set different migration and spicedb log level",
Expand Down Expand Up @@ -888,6 +985,14 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=debug",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "update graph pushes the current version forward",
Expand Down Expand Up @@ -957,6 +1062,15 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=debug",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DATASTORE_MIGRATION_PHASE=phase1",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "explicit channel and version, updates to the next in the channel",
Expand Down Expand Up @@ -1028,6 +1142,15 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=debug",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DATASTORE_MIGRATION_PHASE=phase1",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
{
name: "explicit channel and version, doesn't update past the explicit version",
Expand Down Expand Up @@ -1104,6 +1227,15 @@ func TestNewConfig(t *testing.T) {
},
},
},
wantEnvs: []string{
"SPICEDB_LOG_LEVEL=debug",
"SPICEDB_GRPC_PRESHARED_KEY=preshared_key",
"SPICEDB_DATASTORE_CONN_URI=datastore_uri",
"SPICEDB_DISPATCH_UPSTREAM_ADDR=kubernetes:///test.test:dispatch",
"SPICEDB_DATASTORE_ENGINE=cockroachdb",
"SPICEDB_DATASTORE_MIGRATION_PHASE=phase1",
"SPICEDB_DISPATCH_CLUSTER_ENABLED=true",
},
},
}
for _, tt := range tests {
Expand All @@ -1114,6 +1246,50 @@ func TestNewConfig(t *testing.T) {
require.EqualValues(t, errors.NewAggregate(tt.wantErrs), err)
require.EqualValues(t, errors.NewAggregate(tt.wantWarnings), gotWarning)
require.Equal(t, tt.want, got)

if got != nil {
gotEnvs := got.ToEnvVarApplyConfiguration()
wantEnvs := envVarFromStrings(tt.wantEnvs)
require.Equal(t, wantEnvs, gotEnvs)
}
})
}
}

// envs that will be mapped back to ENV vars
var secrets = map[string]struct{}{
"SPICEDB_GRPC_PRESHARED_KEY": {},
"SPICEDB_DATASTORE_CONN_URI": {},
}

func envVarFromStrings(envs []string) []*v1.EnvVarApplyConfiguration {
vars := make([]*v1.EnvVarApplyConfiguration, 0, len(envs))
for _, env := range envs {
name, value, _ := strings.Cut(env, "=")
var valueFrom *v1.EnvVarSourceApplyConfiguration
var valuePtr *string
if value != "" {
valuePtr = &value
}
// hack for the sake of simplifying test fixtures
// if it's lowercase key, we assume it's a secret
if _, ok := secrets[name]; ok {
localname := ""
valueFrom = &v1.EnvVarSourceApplyConfiguration{
SecretKeyRef: &v1.SecretKeySelectorApplyConfiguration{
LocalObjectReferenceApplyConfiguration: v1.LocalObjectReferenceApplyConfiguration{
Name: &localname,
},
Key: valuePtr,
},
}
valuePtr = nil
}
vars = append(vars, &v1.EnvVarApplyConfiguration{
Name: &name,
Value: valuePtr,
ValueFrom: valueFrom,
})
}
return vars
}
10 changes: 5 additions & 5 deletions pkg/controller/ensure_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestEnsureDeploymentHandler(t *testing.T) {
migrationHash: "testtesttesttest",
secretHash: "secret",
existingDeployments: []*appsv1.Deployment{{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
metadata.SpiceDBConfigKey: "n678h5dfh674h6fh66chbbh544h667q",
metadata.SpiceDBConfigKey: "nch66fh557h68dh555h688h8dhd4q",
}}}},
expectNext: nextKey,
},
Expand All @@ -67,7 +67,7 @@ func TestEnsureDeploymentHandler(t *testing.T) {
migrationHash: "testtesttesttest",
secretHash: "secret",
existingDeployments: []*appsv1.Deployment{{}, {ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
metadata.SpiceDBConfigKey: "n678h5dfh674h6fh66chbbh544h667q",
metadata.SpiceDBConfigKey: "nch66fh557h68dh555h688h8dhd4q",
}}}},
expectDelete: true,
expectNext: nextKey,
Expand All @@ -77,7 +77,7 @@ func TestEnsureDeploymentHandler(t *testing.T) {
migrationHash: "testtesttesttest",
secretHash: "secret1",
existingDeployments: []*appsv1.Deployment{{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
metadata.SpiceDBConfigKey: "n678h5dfh674h6fh66chbbh544h667q",
metadata.SpiceDBConfigKey: "nch66fh557h68dh555h688h8dhd4q",
}}}},
expectApply: true,
expectRequeueAfter: true,
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestEnsureDeploymentHandler(t *testing.T) {
}}}},
existingDeployments: []*appsv1.Deployment{{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
metadata.SpiceDBConfigKey: "n649h99h5cdh579h5cdh568hdh5f5q",
metadata.SpiceDBConfigKey: "n696h689hfdh647h5fbh565h57dh5c5q",
}},
Status: appsv1.DeploymentStatus{
Replicas: 2,
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestEnsureDeploymentHandler(t *testing.T) {
}}}},
existingDeployments: []*appsv1.Deployment{{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{
metadata.SpiceDBConfigKey: "n649h99h5cdh579h5cdh568hdh5f5q",
metadata.SpiceDBConfigKey: "n696h689hfdh647h5fbh565h57dh5c5q",
}},
Status: appsv1.DeploymentStatus{
Replicas: 2,
Expand Down

0 comments on commit 68a5069

Please sign in to comment.