diff --git a/hack/new_config_version/version.go b/hack/new_config_version/version.go index 86d4c6c7a9a..0ddb9d2a78a 100644 --- a/hack/new_config_version/version.go +++ b/hack/new_config_version/version.go @@ -17,12 +17,123 @@ limitations under the License. package main import ( - "fmt" + "bufio" + "io/ioutil" + "os" + "path/filepath" + "regexp" "strings" - "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema" ) +// Before: prev -> current (latest) +// After: prev -> current -> new (latest) func main() { - fmt.Println(strings.TrimPrefix(latest.Version, "skaffold/")) + new := os.Args[1] + current := strings.TrimPrefix(schema.SchemaVersions[len(schema.SchemaVersions)-1].APIVersion, "skaffold/") + prev := strings.TrimPrefix(schema.SchemaVersions[len(schema.SchemaVersions)-2].APIVersion, "skaffold/") + + // Create a package for current version + walk(path("latest"), func(file string, info os.FileInfo) { + if !info.IsDir() { + cp(file, path(current, info.Name())) + sed(path(current, info.Name()), "package latest", "package "+current) + } + }) + + // Create code to upgrade from current to new + cp(path(prev, "upgrade.go"), path(current, "upgrade.go")) + sed(path(current, "upgrade.go"), current, new) + sed(path(current, "upgrade.go"), prev, current) + + // Create a test for the upgrade from current to new + cp(path(prev, "upgrade_test.go"), path(current, "upgrade_test.go")) + sed(path(current, "upgrade_test.go"), current, new) + sed(path(current, "upgrade_test.go"), prev, current) + + // Previous version now upgrades to current instead of latest + sed(path(prev, "upgrade.go"), "latest", current) + sed(path(prev, "upgrade_test.go"), "latest", current) + + // Latest uses the new version + sed(path("latest", "config.go"), current, new) + + // Update skaffold.yaml in integration tests + walk("integration", func(path string, info os.FileInfo) { + if info.Name() == "skaffold.yaml" { + sed(path, current, new) + } + }) + + // Add the new version to the list of versions + lines := lines(path("versions.go")) + var content string + for _, line := range lines { + content += line + "\n" + if strings.Contains(line, prev) { + content += strings.ReplaceAll(line, prev, current) + "\n" + } + } + write(path("versions.go"), []byte(content)) + + // Update the docs with the new version + sed("docs/config.toml", current, new) +} + +func path(elem ...string) string { + return filepath.Join(append([]string{"pkg", "skaffold", "schema"}, elem...)...) +} + +func read(path string) []byte { + buf, err := ioutil.ReadFile(path) + if err != nil { + panic("unable to read " + path) + } + return buf +} + +func write(path string, buf []byte) { + if err := ioutil.WriteFile(path, buf, os.ModePerm); err != nil { + panic("unable to write " + path) + } +} + +func sed(path string, old, new string) { + buf := read(path) + replaced := regexp.MustCompile(old).ReplaceAll(buf, []byte(new)) + write(path, replaced) +} + +func cp(path string, dest string) { + buf := read(path) + os.Mkdir(filepath.Dir(dest), os.ModePerm) + write(dest, buf) +} + +func lines(path string) []string { + file, err := os.Open(path) + if err != nil { + panic("unable to open " + path) + } + defer file.Close() + + var lines []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + return lines +} + +func walk(root string, fn func(path string, info os.FileInfo)) { + if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + fn(path, info) + return nil + }); err != nil { + panic("unable to list files") + } } diff --git a/hack/new_version.sh b/hack/new_version.sh index b024fc068a9..16d02791d64 100755 --- a/hack/new_version.sh +++ b/hack/new_version.sh @@ -14,50 +14,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -CURRENT_VERSION=`go run hack/new_config_version/version.go` -echo "Current config version: $CURRENT_VERSION" - echo "Please enter new config version:" read NEW_VERSION -echo "Please enter previous config version:" -read PREV_VERSION - -echo "Generating changes for new config version $NEW_VERSION..." - -sed -i docs/config.toml -e "s;$CURRENT_VERSION;$NEW_VERSION;g" - -cp -R pkg/skaffold/schema/latest pkg/skaffold/schema/${CURRENT_VERSION} - -sed -i pkg/skaffold/schema/${CURRENT_VERSION}/*.go -e "s;package latest;package $CURRENT_VERSION;g" - -sed pkg/skaffold/schema/${PREV_VERSION}/upgrade_test.go -e "s;$CURRENT_VERSION;$NEW_VERSION;g" > pkg/skaffold/schema/${CURRENT_VERSION}/upgrade_test.go -sed -i pkg/skaffold/schema/${CURRENT_VERSION}/upgrade_test.go -e "s;$PREV_VERSION;$CURRENT_VERSION;g" - -sed pkg/skaffold/schema/${PREV_VERSION}/upgrade.go -e "s;$CURRENT_VERSION;$NEW_VERSION;g" > pkg/skaffold/schema/${CURRENT_VERSION}/upgrade.go -sed -i pkg/skaffold/schema/${CURRENT_VERSION}/upgrade.go -e "s;$PREV_VERSION;$CURRENT_VERSION;g" - -sed -i pkg/skaffold/schema/${PREV_VERSION}/upgrade*.go -e "s;latest;$CURRENT_VERSION;g" -goimports -w pkg/skaffold/schema/${PREV_VERSION}/upgrade*.go - -sed -i pkg/skaffold/schema/latest/config.go -e "s;$CURRENT_VERSION;$NEW_VERSION;g" - -find integration -name "skaffold.yaml" -print0 | xargs -0 -I xx sed -i xx -e "s;$CURRENT_VERSION;$NEW_VERSION;g" - -sed pkg/skaffold/schema/versions.go -i -e "s;\(.*\)$PREV_VERSION.Version\(.*\)$PREV_VERSION\(.*\);&\n\1$CURRENT_VERSION.Version\2$CURRENT_VERSION\3;g" -sed pkg/skaffold/schema/versions.go -i -e "s;\(.*\)/$PREV_VERSION\(.*\);&\n\1/$CURRENT_VERSION\2;g" +go run ./hack/new_config_version/version.go ${NEW_VERSION} +goimports -w ./pkg/skaffold/schema make generate-schemas - git --no-pager diff --minimal - make test echo echo "---------------------------------------" echo -echo "Files generated for $NEW_VERSION. Don't worry about the hack/check-docs change failure, it is expected!" -echo "Other tests should have passed. For the docs change, commit the results and rerun 'make test'." +echo "Files generated for $NEW_VERSION." +echo "All tests should have passed. For the docs change, commit the results and rerun 'make test'." echo "Please double check manually the generated files as well: the upgrade functionality, and all the examples:" echo git status -s diff --git a/pkg/skaffold/schema/util/util.go b/pkg/skaffold/schema/util/util.go index de973156f61..2f58eeccf4d 100644 --- a/pkg/skaffold/schema/util/util.go +++ b/pkg/skaffold/schema/util/util.go @@ -22,7 +22,7 @@ import ( "strings" yamlpatch "github.com/krishicks/yaml-patch" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) type VersionedConfig interface { diff --git a/pkg/skaffold/schema/v1beta10/upgrade_test.go b/pkg/skaffold/schema/v1beta10/upgrade_test.go index 395b158642b..3b7a80c7c00 100644 --- a/pkg/skaffold/schema/v1beta10/upgrade_test.go +++ b/pkg/skaffold/schema/v1beta10/upgrade_test.go @@ -21,7 +21,7 @@ import ( next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1beta11" "github.com/GoogleContainerTools/skaffold/testutil" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) func TestUpgrade(t *testing.T) { diff --git a/pkg/skaffold/schema/v1beta11/upgrade_test.go b/pkg/skaffold/schema/v1beta11/upgrade_test.go index 1ed80c28efa..b591820fa4e 100644 --- a/pkg/skaffold/schema/v1beta11/upgrade_test.go +++ b/pkg/skaffold/schema/v1beta11/upgrade_test.go @@ -21,7 +21,7 @@ import ( next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/testutil" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) func TestUpgrade(t *testing.T) { diff --git a/pkg/skaffold/schema/v1beta9/upgrade_test.go b/pkg/skaffold/schema/v1beta9/upgrade_test.go index d23f4b759fb..525702c3a94 100644 --- a/pkg/skaffold/schema/v1beta9/upgrade_test.go +++ b/pkg/skaffold/schema/v1beta9/upgrade_test.go @@ -21,7 +21,7 @@ import ( next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1beta10" "github.com/GoogleContainerTools/skaffold/testutil" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) func TestUpgrade(t *testing.T) { diff --git a/pkg/skaffold/schema/versions.go b/pkg/skaffold/schema/versions.go index 0d2e18a0079..fbca23381d2 100644 --- a/pkg/skaffold/schema/versions.go +++ b/pkg/skaffold/schema/versions.go @@ -41,7 +41,7 @@ import ( misc "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) type APIVersion struct {