Skip to content

Commit

Permalink
fix(labelling): hotfix for issues related to deploy erroring due to n…
Browse files Browse the repository at this point in the history
…ested yaml field overriding in StatefulSet w/ PVC
  • Loading branch information
aaron-prindle committed Jan 13, 2022
1 parent 8cd8b69 commit 23335e8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/skaffold/kubernetes/manifest/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type imageSaver struct {
Images []graph.Artifact
}

func (is *imageSaver) Visit(o map[string]interface{}, k string, v interface{}) bool {
func (is *imageSaver) Visit(navpath string, o map[string]interface{}, k string, v interface{}) bool {
if k != "image" {
return true
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func newImageReplacer(builds []graph.Artifact, selector imageSelector) *imageRep
}
}

func (r *imageReplacer) Visit(o map[string]interface{}, k string, v interface{}) bool {
func (r *imageReplacer) Visit(navpath string, o map[string]interface{}, k string, v interface{}) bool {
if k != "image" {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/kubernetes/manifest/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func newLabelsSetter(labels map[string]string) *labelsSetter {
}
}

func (r *labelsSetter) Visit(o map[string]interface{}, k string, v interface{}) bool {
func (r *labelsSetter) Visit(navpath string, o map[string]interface{}, k string, v interface{}) bool {
if k != "metadata" {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/kubernetes/manifest/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newNamespaceCollector() *namespaceCollector {
}
}

func (r *namespaceCollector) Visit(o map[string]interface{}, k string, v interface{}) bool {
func (r *namespaceCollector) Visit(navpath string, o map[string]interface{}, k string, v interface{}) bool {
if k != "metadata" {
return true
}
Expand Down
22 changes: 14 additions & 8 deletions pkg/skaffold/kubernetes/manifest/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package manifest

import (
"fmt"
"path"

apimachinery "k8s.io/apimachinery/pkg/runtime/schema"

Expand Down Expand Up @@ -46,7 +47,7 @@ var transformableAllowlist = map[apimachinery.GroupKind]bool{
type FieldVisitor interface {
// Visit is called for each transformable key contained in the object and may apply transformations/aggregations on it.
// It should return true to allow recursive traversal or false when the entry was transformed.
Visit(object map[string]interface{}, key string, value interface{}) bool
Visit(path string, object map[string]interface{}, key string, value interface{}) bool
}

// Visit recursively visits all transformable object fields within the manifests and lets the visitor apply transformations/aggregations on them.
Expand Down Expand Up @@ -81,7 +82,7 @@ func traverseManifestFields(manifest map[string]interface{}, visitor FieldVisito
if shouldTransformManifest(manifest) {
visitor = &recursiveVisitorDecorator{visitor}
}
visitFields(manifest, visitor)
visitFields("/", manifest, visitor)
}

func shouldTransformManifest(manifest map[string]interface{}) bool {
Expand Down Expand Up @@ -123,23 +124,28 @@ type recursiveVisitorDecorator struct {
delegate FieldVisitor
}

func (d *recursiveVisitorDecorator) Visit(o map[string]interface{}, k string, v interface{}) bool {
if d.delegate.Visit(o, k, v) {
visitFields(v, d)
func (d *recursiveVisitorDecorator) Visit(path string, o map[string]interface{}, k string, v interface{}) bool {
if d.delegate.Visit(path, o, k, v) {
visitFields(path, v, d)
}
return false
}

// visitFields traverses all fields and calls the visitor for each.
func visitFields(o interface{}, visitor FieldVisitor) {
// navpath: a '/' delimited path representing the fields navigated to this point
func visitFields(navpath string, o interface{}, visitor FieldVisitor) {
switch entries := o.(type) {
case []interface{}:
for _, v := range entries {
visitFields(v, visitor)
visitFields(navpath, v, visitor)
}
case map[string]interface{}:
for k, v := range entries {
visitor.Visit(entries, k, v)
// TODO(6416) temporary fix for StatefulSet + PVC use case, need to do something similar to the proposal in #6236 for full fix
if navpath == "/spec/volumeClaimTemplates" {
continue
}
visitor.Visit(path.Join(navpath, k), entries, k, v)
}
}
}
2 changes: 1 addition & 1 deletion pkg/skaffold/kubernetes/manifest/visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type mockVisitor struct {
replaceWith interface{}
}

func (m *mockVisitor) Visit(o map[string]interface{}, k string, v interface{}) bool {
func (m *mockVisitor) Visit(navpath string, o map[string]interface{}, k string, v interface{}) bool {
s := fmt.Sprintf("%+v", v)
if len(s) > 4 {
s = s[:4] + "..."
Expand Down

0 comments on commit 23335e8

Please sign in to comment.