Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(labelling): hotfix for issues related to deploy erroring due to nested yaml field overriding in StatefulSet w/ PVC #7011

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 15 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,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(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)
// this case covers lists so we don't update the navpath
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