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 overriding in StatefulSet w/ PVC
  • Loading branch information
aaron-prindle committed Jan 13, 2022
1 parent 8cd8b69 commit 9115b39
Show file tree
Hide file tree
Showing 5 changed files with 22 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(jsonpath 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(jsonpath 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(jsonpath 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(jsonpath string, o map[string]interface{}, k string, v interface{}) bool {
if k != "metadata" {
return true
}
Expand Down
25 changes: 17 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(jsonpath 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,9 @@ func traverseManifestFields(manifest map[string]interface{}, visitor FieldVisito
if shouldTransformManifest(manifest) {
visitor = &recursiveVisitorDecorator{visitor}
}
visitFields(manifest, visitor)
// TODO(aaron-prindle) "/"" vs ""?
// visitFields("/", manifest, visitor)
visitFields("", manifest, visitor)
}

func shouldTransformManifest(manifest map[string]interface{}) bool {
Expand Down Expand Up @@ -123,23 +126,29 @@ 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(jsonpath string, o map[string]interface{}, k string, v interface{}) bool {
if d.delegate.Visit(jsonpath, o, k, v) {
visitFields(jsonpath, v, d)
}
return false
}

// visitFields traverses all fields and calls the visitor for each.
func visitFields(o interface{}, visitor FieldVisitor) {
func visitFields(jsonpath string, o interface{}, visitor FieldVisitor) {
switch entries := o.(type) {
case []interface{}:
for _, v := range entries {
visitFields(v, visitor)
visitFields(jsonpath, v, visitor)
}
case map[string]interface{}:
for k, v := range entries {
visitor.Visit(entries, k, v)
jsonpath = path.Join(k)
// TODO(6416) temporary fix for StatefulSet + PVC use case, need to do something similar to the proposal in #6236 for full fix
if jsonpath == "/spec/volumeClaimTemplates" {

continue
}
visitor.Visit(jsonpath, 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(jsonpath 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 9115b39

Please sign in to comment.