Skip to content

Commit

Permalink
Merge pull request #198 from authzed/container-name-backcompat
Browse files Browse the repository at this point in the history
support patches with the old container name
  • Loading branch information
ecordell authored May 17, 2023
2 parents 5bd61a8 + 4bc7556 commit 7d9498f
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 12 deletions.
3 changes: 1 addition & 2 deletions e2e/databases/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ func CreateFromManifests(ctx context.Context, namespace, engine string, restConf
err := decoder.Decode(&u.Object)
if errors.Is(err, io.EOF) {
break
} else {
Expect(err).To(Succeed())
}
Expect(err).To(Succeed())
u.SetNamespace(namespace)
objs = append(objs, u)
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/databases/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *SpannerProvider) New(ctx context.Context) *LogicalDatabase {
}
}

func (p *SpannerProvider) Cleanup(ctx context.Context, db *LogicalDatabase) {
func (p *SpannerProvider) Cleanup(_ context.Context, _ *LogicalDatabase) {
// TODO: figure out how to cleanup a spanner emulator db
}

Expand Down
63 changes: 62 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
"encoding/json"
"fmt"
"path/filepath"
Expand All @@ -9,12 +10,14 @@ import (
"strings"

"github.com/authzed/controller-idioms/hash"
jsonpatch "github.com/evanphx/json-patch"
"github.com/fatih/camelcase"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
applyappsv1 "k8s.io/client-go/applyconfigurations/apps/v1"
applybatchv1 "k8s.io/client-go/applyconfigurations/batch/v1"
applycorev1 "k8s.io/client-go/applyconfigurations/core/v1"
Expand Down Expand Up @@ -366,8 +369,8 @@ func NewConfig(cluster *v1alpha1.SpiceDBCluster, globalConfig *OperatorConfig, s
out := &Config{
MigrationConfig: migrationConfig,
SpiceConfig: spiceConfig,
Patches: cluster.Spec.Patches,
}
out.Patches = fixDeploymentPatches(out.Name, cluster.Spec.Patches)

// Validate that patches apply cleanly ahead of time
totalAppliedPatches := 0
Expand Down Expand Up @@ -750,6 +753,64 @@ func (c *Config) Deployment(migrationHash, secretHash string) *applyappsv1.Deplo
return d
}

// fixDeploymentPatches modifies any patches that could apply to the deployment
// referencing the old container names and rewrites them to use the new
// stable name
func fixDeploymentPatches(name string, in []v1alpha1.Patch) []v1alpha1.Patch {
if in == nil {
return nil
}
patches := make([]v1alpha1.Patch, 0, len(in))
patches = append(patches, in...)
for i, p := range patches {
// not a deployment patch
if !(p.Kind == "Deployment" || p.Kind == wildcard) {
continue
}

// patch doesn't contain the old name
if !strings.Contains(string(p.Patch), name+"-spicedb") {
continue
}

// determine what kind of patch it is
decoder := utilyaml.NewYAMLOrJSONDecoder(bytes.NewReader(p.Patch), 100)
var json6902op jsonpatch.Operation
err := decoder.Decode(&json6902op)
if err != nil {
continue
}

// if there's an operation, it's a json6902 patch, and can't have
// used the old names
if json6902op.Kind() != "unknown" {
continue
}

// parse the patch
smpPatch, err := utilyaml.ToJSON(p.Patch)
if err != nil {
continue
}

// patch the patch
patchPatch, err := jsonpatch.DecodePatch([]byte(`[
{"op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "spicedb"}
]`))
if err != nil {
continue
}
modified, err := patchPatch.Apply(smpPatch)
if err != nil {
continue
}

// update the stored patch
patches[i].Patch = modified
}
return patches
}

// toEnvVarName converts a key from the api object into an env var name.
// the key isCamelCased will be converted to PREFIX_IS_CAMEL_CASED
func toEnvVarName(prefix string, key string) string {
Expand Down
Loading

0 comments on commit 7d9498f

Please sign in to comment.