Skip to content

Automatically fix old configs by default #1259

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

Merged
merged 7 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 1 deletion cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func newRunner(opts *config.SkaffoldOptions) (*runner.SkaffoldRunner, *latest.Sk
return nil, nil, errors.Wrap(err, "parsing skaffold config")
}

if err := schema.CheckVersionIsLatest(parsed.GetVersion()); err != nil {
// automatically upgrade older config
parsed, err = schema.UpgradeToLatest(parsed)
if err != nil {
return nil, nil, errors.Wrap(err, "invalid config")
}

Expand Down
29 changes: 13 additions & 16 deletions pkg/skaffold/schema/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package schema

import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

apiversion "github.com/GoogleContainerTools/skaffold/pkg/skaffold/apiversion"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -90,28 +91,24 @@ func ParseConfig(filename string, applyDefaults bool) (util.VersionedConfig, err
return cfg, nil
}

// CheckVersionIsLatest checks that a given version is the most recent.
func CheckVersionIsLatest(apiVersion string) error {
parsedVersion, err := apiversion.Parse(apiVersion)
// UpgradeToLatest upgrades a configuration to the latest version.
func UpgradeToLatest(vc util.VersionedConfig) (util.VersionedConfig, error) {
var err error

// first, check to make sure config version isn't too new
version, err := apiversion.Parse(vc.GetVersion())
if err != nil {
return errors.Wrap(err, "parsing api version")
return nil, errors.Wrap(err, "parsing api version")
}

if parsedVersion.LT(apiversion.MustParse(latest.Version)) {
return errors.New("config version out of date: run `skaffold fix`")
latestVersion := apiversion.MustParse(latest.Version)
if version.GT(latestVersion) {
return nil, errors.New("config version is too new for this version of skaffold: upgrade skaffold")
}

if parsedVersion.GT(apiversion.MustParse(latest.Version)) {
return errors.New("config version is too new for this version of skaffold: upgrade skaffold")
if version.LT(latestVersion) {
logrus.Infof("config version %s out of date: upgrading to latest (%s)", vc.GetVersion(), latest.Version)
}

return nil
}

// UpgradeToLatest upgrades a configuration to the latest version.
func UpgradeToLatest(vc util.VersionedConfig) (util.VersionedConfig, error) {
var err error

for vc.GetVersion() != latest.Version {
vc, err = vc.Upgrade()
if err != nil {
Expand Down
33 changes: 1 addition & 32 deletions pkg/skaffold/schema/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha1"
"github.com/GoogleContainerTools/skaffold/testutil"
"k8s.io/client-go/tools/clientcmd/api"
)
Expand Down Expand Up @@ -315,36 +314,6 @@ func withProfiles(profiles ...latest.Profile) func(*latest.SkaffoldPipeline) {
}
}

func TestCheckVersionIsLatest(t *testing.T) {
tests := []struct {
name string
version string
shouldErr bool
}{
{
name: "latest api version",
version: latest.Version,
},
{
name: "old api version",
version: v1alpha1.Version,
shouldErr: true,
},
{
name: "new api version",
version: "skaffold/v9",
shouldErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := CheckVersionIsLatest(test.version)

testutil.CheckError(t, test.shouldErr, err)
})
}
}

func TestUpgradeToNextVersion(t *testing.T) {
for i, schemaVersion := range schemaVersions[0 : len(schemaVersions)-2] {
from := schemaVersion
Expand All @@ -360,7 +329,7 @@ func TestUpgradeToNextVersion(t *testing.T) {
}
}

func TestCantUpgradeFromLastestVersion(t *testing.T) {
func TestCantUpgradeFromLatestVersion(t *testing.T) {
factory, present := schemaVersions.Find(latest.Version)
testutil.CheckDeepEqual(t, true, present)

Expand Down