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 skaffold filter to handle multiple yaml documents #4829

Merged
merged 3 commits into from
Sep 29, 2020
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
13 changes: 6 additions & 7 deletions cmd/skaffold/app/cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package cmd

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,28 +60,27 @@ func NewCmdFilter() *cobra.Command {
// Unlike `skaffold debug`, this filtering affects all images and not just the built artifacts.
func runFilter(ctx context.Context, out io.Writer, debuggingFilters bool, buildArtifacts []build.Artifact) error {
return withRunner(ctx, func(r runner.Runner, cfg *latest.SkaffoldConfig) error {
bytes, err := ioutil.ReadAll(os.Stdin)
manifestList, err := manifest.Load(os.Stdin)
if err != nil {
return err
return fmt.Errorf("loading manifests: %w", err)
}
manifestList := manifest.ManifestList([][]byte{bytes})
if debuggingFilters {
// TODO(bdealwis): refactor this code
debugHelpersRegistry, err := config.GetDebugHelpersRegistry(opts.GlobalConfig)
if err != nil {
return err
return fmt.Errorf("resolving debug helpers: %w", err)
}
insecureRegistries, err := getInsecureRegistries(opts, cfg)
if err != nil {
return err
return fmt.Errorf("retrieving insecure registries: %w", err)
}

manifestList, err = debugging.ApplyDebuggingTransforms(manifestList, buildArtifacts, manifest.Registries{
DebugHelpersRegistry: debugHelpersRegistry,
InsecureRegistries: insecureRegistries,
})
if err != nil {
return err
return fmt.Errorf("transforming manifests: %w", err)
}
}
out.Write([]byte(manifestList.String()))
Expand Down
87 changes: 86 additions & 1 deletion pkg/skaffold/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package debug

import (
"bytes"
"strings"
"testing"

Expand Down Expand Up @@ -502,14 +503,98 @@ spec:
name: myfunction
image: myfunction`,
},
{
"multiple objects", false,
`apiVersion: v1
kind: Pod
metadata:
name: pod
spec:
containers:
- image: gcr.io/k8s-debug/debug-example:latest
name: example
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 10
selector:
matchLabels:
app: debug-app
template:
metadata:
labels:
app: debug-app
name: debug-pod
spec:
containers:
- image: gcr.io/k8s-debug/debug-example:latest
name: example
`,
`apiVersion: v1
kind: Pod
metadata:
annotations:
debug.cloud.google.com/config: '{"example":{"runtime":"test"}}'
creationTimestamp: null
name: pod
spec:
containers:
- env:
- name: KEY
value: value
image: gcr.io/k8s-debug/debug-example:latest
name: example
ports:
- containerPort: 9999
name: test
resources: {}
status: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: debug-app
strategy: {}
template:
metadata:
annotations:
debug.cloud.google.com/config: '{"example":{"runtime":"test"}}'
creationTimestamp: null
labels:
app: debug-app
name: debug-pod
spec:
containers:
- env:
- name: KEY
value: value
image: gcr.io/k8s-debug/debug-example:latest
name: example
ports:
- containerPort: 9999
name: test
resources: {}
status: {}`,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
retriever := func(image string) (imageConfiguration, error) {
return imageConfiguration{}, nil
}

result, err := applyDebuggingTransforms(manifest.ManifestList{[]byte(test.in)}, retriever, "HELPERS")
l, err := manifest.Load(bytes.NewReader([]byte(test.in)))
t.CheckError(false, err)
result, err := applyDebuggingTransforms(l, retriever, "HELPERS")

t.CheckErrorAndDeepEqual(test.shouldErr, err, test.out, result.String())
})
Expand Down
20 changes: 20 additions & 0 deletions pkg/skaffold/kubernetes/manifest/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,36 @@ limitations under the License.
package manifest

import (
"bufio"
"bytes"
"io"
"regexp"
"strings"

k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)

// ManifestList is a list of yaml manifests.
//nolint:golint
type ManifestList [][]byte

// Load uses the Kubernetes `apimachinery` to split YAML content into a set of YAML documents.
func Load(in io.Reader) (ManifestList, error) {
r := k8syaml.NewYAMLReader(bufio.NewReader(in))
var docs [][]byte
for {
doc, err := r.Read()
switch {
case err == io.EOF:
return ManifestList(docs), nil
case err != nil:
return nil, err
default:
docs = append(docs, doc)
}
}
}

func (l *ManifestList) String() string {
var str string
for i, manifest := range *l {
Expand Down
24 changes: 24 additions & 0 deletions pkg/skaffold/kubernetes/manifest/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,35 @@ limitations under the License.
package manifest

import (
"bytes"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestLoad(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{name: "empty", input: "", expected: []string{}},
{name: "single doc", input: "a: b", expected: []string{"a: b\n"}}, // note lf introduced
{name: "multiple docs", input: "a: b\n---\nc: d", expected: []string{"a: b\n", "c: d\n"}},
}
for _, test := range tests {
testutil.Run(t, test.name, func(t *testutil.T) {
result, err := Load(bytes.NewReader([]byte(test.input)))

t.CheckError(false, err)
t.CheckDeepEqual(len(test.expected), len(result))
for i := range test.expected {
t.CheckDeepEqual(test.expected[i], string(result[i]))
}
})
}
}

const pod1 = `apiVersion: v1
kind: Pod
metadata:
Expand Down