Skip to content

Commit

Permalink
Merge pull request #2863 from nkubala/crd-init
Browse files Browse the repository at this point in the history
Duck-type k8s manifests instead of actually parsing when running init
  • Loading branch information
nkubala authored Sep 12, 2019
2 parents 93b65da + e7b2603 commit c3b54fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
36 changes: 24 additions & 12 deletions pkg/skaffold/initializer/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/runtime"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/scheme"
)

// ValidSuffixes are the supported file formats for kubernetes manifests
var ValidSuffixes = []string{".yml", ".yaml", ".json"}

var requiredFields = []string{"apiVersion", "kind", "metadata"}

// Kubectl holds parameters to run kubectl.
type Kubectl struct {
configs []string
Expand Down Expand Up @@ -79,8 +79,10 @@ func (k *Kubectl) GetImages() []string {
return k.images
}

// parseImagesFromKubernetesYaml attempts to parse k8s objects from a yaml file
// if successful, it will return the images referenced in the k8s config
// parseImagesFromKubernetesYaml uses required fields from the k8s spec
// to determine if a provided yaml file is a valid k8s manifest, as detailed in
// https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/#required-fields.
// if so, it will return the images referenced in the k8s config
// so they can be built by the generated skaffold yaml
func parseImagesFromKubernetesYaml(filepath string) ([]string, error) {
f, err := os.Open(filepath)
Expand All @@ -89,7 +91,7 @@ func parseImagesFromKubernetesYaml(filepath string) ([]string, error) {
}
r := k8syaml.NewYAMLReader(bufio.NewReader(f))

objects := []runtime.Object{}
yamlsFound := 0
images := []string{}

for {
Expand All @@ -100,26 +102,36 @@ func parseImagesFromKubernetesYaml(filepath string) ([]string, error) {
if err != nil {
return nil, errors.Wrap(err, "reading config file")
}
d := scheme.Codecs.UniversalDeserializer()
obj, _, err := d.Decode(doc, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "decoding kubernetes yaml")
}

m := make(map[interface{}]interface{})
if err := yaml.Unmarshal(doc, &m); err != nil {
return nil, errors.Wrap(err, "reading kubernetes YAML")
}

if !isKubernetesYaml(m) {
continue
}

yamlsFound++

images = append(images, parseImagesFromYaml(m)...)
objects = append(objects, obj)
}
if len(objects) == 0 {
if yamlsFound == 0 {
return nil, errors.New("no valid kubernetes objects decoded")
}
return images, nil
}

func isKubernetesYaml(doc map[interface{}]interface{}) bool {
for _, field := range requiredFields {
if _, ok := doc[field]; !ok {
logrus.Debugf("%s not present in yaml, continuing", field)
return false
}
}
return true
}

// adapted from pkg/skaffold/deploy/kubectl/recursiveReplaceImage()
func parseImagesFromYaml(doc interface{}) []string {
images := []string{}
Expand Down
13 changes: 13 additions & 0 deletions pkg/skaffold/initializer/kubectl/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ subjects:
images: []string{},
shouldErr: false,
},
{
description: "crd",
contents: `apiVersion: my.crd.io/v1
kind: CustomType
metadata:
name: test crd
spec:
containers:
- name: container
image: gcr.io/my/image`,
images: []string{"gcr.io/my/image"},
shouldErr: false,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand Down

0 comments on commit c3b54fc

Please sign in to comment.