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

Allow multiple configs in single skaffold.yaml #5199

Merged
merged 4 commits into from
Jan 11, 2021
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
124 changes: 89 additions & 35 deletions pkg/skaffold/schema/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"bytes"
"errors"
"fmt"
"io"
"strings"

"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/apiversion"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
Expand Down Expand Up @@ -66,7 +68,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v2beta8"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v2beta9"
misc "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml"
)

type APIVersion struct {
Expand Down Expand Up @@ -146,48 +147,19 @@ func IsSkaffoldConfig(file string) bool {

// ParseConfig reads a configuration file.
func ParseConfig(filename string) ([]util.VersionedConfig, error) {
// TODO: update this function to parse the input as multiple configs
buf, err := misc.ReadConfiguration(filename)
if err != nil {
return nil, fmt.Errorf("read skaffold config: %w", err)
}
// This is to quickly check that it's possibly a skaffold.yaml,
// without parsing the whole file.
if !bytes.Contains(buf, []byte("apiVersion")) {
return nil, errors.New("missing apiVersion")
}

apiVersion := &APIVersion{}
if err := yaml.Unmarshal(buf, apiVersion); err != nil {
return nil, fmt.Errorf("parsing api version: %w", err)
}

factory, present := SchemaVersions.Find(apiVersion.Version)
if !present {
return nil, fmt.Errorf("unknown api version: %q", apiVersion.Version)
}

// Remove all top-level keys starting with `.` so they can be used as YAML anchors
parsed := make(map[string]interface{})
if err := yaml.UnmarshalStrict(buf, parsed); err != nil {
return nil, fmt.Errorf("unable to parse YAML: %w", err)
}
for field := range parsed {
if strings.HasPrefix(field, ".") {
delete(parsed, field)
}
factories, err := configFactoryFromAPIVersion(buf)
if err != nil {
return nil, err
}
buf, err = yaml.Marshal(parsed)
buf, err = removeYamlAnchors(buf)
if err != nil {
return nil, fmt.Errorf("unable to re-marshal YAML without dotted keys: %w", err)
}

cfg := factory()
if err := yaml.UnmarshalStrict(buf, cfg); err != nil {
return nil, fmt.Errorf("unable to parse config: %w", err)
}

return []util.VersionedConfig{cfg}, nil
return parseConfig(buf, factories)
}

// ParseConfigAndUpgrade reads a configuration file and upgrades it to a given version.
Expand Down Expand Up @@ -237,3 +209,85 @@ func ParseConfigAndUpgrade(filename, toVersion string) ([]util.VersionedConfig,
}
return upgraded, nil
}

// configFactoryFromAPIVersion checks that all configs in the input stream have the same API version, and returns a function to create a config with that API version.
func configFactoryFromAPIVersion(buf []byte) ([]func() util.VersionedConfig, error) {
// This is to quickly check that it's possibly a skaffold.yaml,
// without parsing the whole file.
if !bytes.Contains(buf, []byte("apiVersion")) {
nkubala marked this conversation as resolved.
Show resolved Hide resolved
return nil, errors.New("missing apiVersion")
}

var factories []func() util.VersionedConfig
b := bytes.NewReader(buf)
decoder := yaml.NewDecoder(b)
for {
var v APIVersion
err := decoder.Decode(&v)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("parsing api version: %w", err)
}
factory, present := SchemaVersions.Find(v.Version)
if !present {
return nil, fmt.Errorf("unknown api version: %q", v.Version)
}
factories = append(factories, factory)
}
return factories, nil
}

// removeYamlAnchors removes all top-level keys starting with `.` from the input stream so they can be used as YAML anchors
func removeYamlAnchors(buf []byte) ([]byte, error) {
in := bytes.NewReader(buf)
var out bytes.Buffer

decoder := yaml.NewDecoder(in)
decoder.KnownFields(true)
encoder := yaml.NewEncoder(&out)
for {
parsed := make(map[string]interface{})
err := decoder.Decode(parsed)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("unable to parse YAML: %w", err)
}
for field := range parsed {
if strings.HasPrefix(field, ".") {
delete(parsed, field)
}
}
err = encoder.Encode(parsed)
if err != nil {
return nil, err
}
}
err := encoder.Close()
if err != nil {
return nil, err
}
return out.Bytes(), nil
}

func parseConfig(buf []byte, factories []func() util.VersionedConfig) ([]util.VersionedConfig, error) {
b := bytes.NewReader(buf)
decoder := yaml.NewDecoder(b)
decoder.KnownFields(true)
var cfgs []util.VersionedConfig
for index := 0; index < len(factories); index++ {
cfg := factories[index]()
err := decoder.Decode(cfg)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("unable to parse config: %w", err)
}
cfgs = append(cfgs, cfg)
}
return cfgs, nil
}
Loading