Skip to content

Commit 4c12a1f

Browse files
committed
sync-tags: add support for v0.x.y tags
Implements changes to the go modules KEP made in kubernetes/enhancements#1350. This commit updates sync-tags to publish `v0.x.y` tags for corresponding `v1.x.y` Kubernetes tag. The old `kubernetes-1.x.y` tags are still also published. Both `kubernetes-1.x.y` and `v0.x.y` point to the same sha. For `v0.x.y`: - At this tag, `go.mod` uses `v0.x.y` tags for dependencies. - When this tag is manually specified in a `go.mod` file, it will be retained as `v0.x.y`. For `kubernetes-1.x.y`: - At this tag, `go.mod` uses `v0.x.y` tags for dependencies. Note: this differs from the previous way, where pseudoversions were used. - When this tag is manually specified in a `go.mod` file, go will resolve it to the appropriate pseudoversion.
1 parent dc65f29 commit 4c12a1f

File tree

3 files changed

+222
-71
lines changed

3 files changed

+222
-71
lines changed

artifacts/scripts/construct.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ if [ -z "${SKIP_TAGS}" ]; then
150150
--dependencies "${DEPS}" \
151151
--mapping-output-file "../tag-${REPO}-{{.Tag}}-mapping" \
152152
--generate-godeps=${PUBLISHER_BOT_GENERATE_GODEPS:-false} \
153+
--publish-v0-semver \
153154
-alsologtostderr \
154155
"${EXTRA_ARGS[@]-}"
155156
if [ "${LAST_HEAD}" != "$(git rev-parse ${LAST_BRANCH})" ]; then

cmd/sync-tags/gomod.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333

3434
// updateGomodWithTaggedDependencies gets the dependencies at the given tag and fills go.mod and go.sum.
3535
// If anything is changed, it commits the changes. Returns true if go.mod changed.
36-
func updateGomodWithTaggedDependencies(tag string, depsRepo []string) (bool, error) {
36+
func updateGomodWithTaggedDependencies(tag string, depsRepo []string, semverTag bool) (bool, error) {
3737
found := map[string]bool{}
3838
changed := false
3939

@@ -59,10 +59,14 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string) (bool, err
5959
return changed, fmt.Errorf("failed to get tag %s for %q: %v", tag, depPkg, err)
6060
}
6161
rev := commit.String()
62-
pseudoVersion := fmt.Sprintf("v0.0.0-%s-%s", commitTime.UTC().Format("20060102150405"), rev[:12])
62+
pseudoVersionOrTag := fmt.Sprintf("v0.0.0-%s-%s", commitTime.UTC().Format("20060102150405"), rev[:12])
6363

64-
// in case the pseudoVersion has not changed, running go mod download will help
65-
// in avoiding packaging it up if the pseudoVersion has been published already
64+
if semverTag {
65+
pseudoVersionOrTag = tag
66+
}
67+
68+
// in case the pseudoVersion/tag has not changed, running go mod download will help
69+
// in avoiding packaging it up if the pseudoVersion/tag has been published already
6670
downloadCommand := exec.Command("go", "mod", "download")
6771
downloadCommand.Env = append(os.Environ(), "GO111MODULE=on", fmt.Sprintf("GOPRIVATE=%s", depPackages), "GOPROXY=https://proxy.golang.org")
6872
downloadCommand.Stdout = os.Stdout
@@ -71,46 +75,46 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string) (bool, err
7175
return changed, fmt.Errorf("error running go mod download for %s: %v", depPkg, err)
7276
}
7377

74-
// check if we have the pseudoVersion published already. if we don't, package it up
78+
// check if we have the pseudoVersion/tag published already. if we don't, package it up
7579
// and save to local mod download cache.
76-
if err := packageDepToGoModCache(depPath, depPkg, rev, pseudoVersion, commitTime); err != nil {
80+
if err := packageDepToGoModCache(depPath, depPkg, rev, pseudoVersionOrTag, commitTime); err != nil {
7781
return changed, fmt.Errorf("failed to package %s dependency: %v", depPkg, err)
7882
}
7983

80-
requireCommand := exec.Command("go", "mod", "edit", "-fmt", "-require", fmt.Sprintf("%s@%s", depPkg, pseudoVersion))
84+
requireCommand := exec.Command("go", "mod", "edit", "-fmt", "-require", fmt.Sprintf("%s@%s", depPkg, pseudoVersionOrTag))
8185
requireCommand.Env = append(os.Environ(), "GO111MODULE=on")
8286
requireCommand.Stdout = os.Stdout
8387
requireCommand.Stderr = os.Stderr
8488
if err := requireCommand.Run(); err != nil {
85-
return changed, fmt.Errorf("unable to pin %s in the require section of go.mod to %s: %v", depPkg, pseudoVersion, err)
89+
return changed, fmt.Errorf("unable to pin %s in the require section of go.mod to %s: %v", depPkg, pseudoVersionOrTag, err)
8690
}
8791

88-
replaceCommand := exec.Command("go", "mod", "edit", "-fmt", "-replace", fmt.Sprintf("%s=%s@%s", depPkg, depPkg, pseudoVersion))
92+
replaceCommand := exec.Command("go", "mod", "edit", "-fmt", "-replace", fmt.Sprintf("%s=%s@%s", depPkg, depPkg, pseudoVersionOrTag))
8993
replaceCommand.Env = append(os.Environ(), "GO111MODULE=on")
9094
replaceCommand.Stdout = os.Stdout
9195
replaceCommand.Stderr = os.Stderr
9296
if err := replaceCommand.Run(); err != nil {
93-
return changed, fmt.Errorf("unable to pin %s in the replace section of go.mod to %s: %v", depPkg, pseudoVersion, err)
97+
return changed, fmt.Errorf("unable to pin %s in the replace section of go.mod to %s: %v", depPkg, pseudoVersionOrTag, err)
9498
}
9599

96100
downloadCommand2 := exec.Command("go", "mod", "download")
97101
downloadCommand2.Env = append(os.Environ(), "GO111MODULE=on", fmt.Sprintf("GOPRIVATE=%s", depPackages), "GOPROXY=https://proxy.golang.org")
98102
downloadCommand2.Stdout = os.Stdout
99103
downloadCommand2.Stderr = os.Stderr
100104
if err := downloadCommand2.Run(); err != nil {
101-
return changed, fmt.Errorf("error running go mod download for pseudo-version %s for %s: %v", pseudoVersion, depPkg, err)
105+
return changed, fmt.Errorf("error running go mod download for pseudo-version %s for %s: %v", pseudoVersionOrTag, depPkg, err)
102106
}
103107

104108
tidyCommand := exec.Command("go", "mod", "tidy")
105-
tidyCommand.Env = append(os.Environ(), "GO111MODULE=on", "GOPOXY=file://${GOPATH}/pkg/mod/cache/download")
109+
tidyCommand.Env = append(os.Environ(), "GO111MODULE=on", fmt.Sprintf("GOPROXY=file://%s/pkg/mod/cache/download", os.Getenv("GOPATH")))
106110
tidyCommand.Stdout = os.Stdout
107111
tidyCommand.Stderr = os.Stderr
108112
if err := tidyCommand.Run(); err != nil {
109113
return changed, fmt.Errorf("unable to run go mod tidy for %s at %s: %v", depPkg, rev, err)
110114
}
111115

112116
found[dep] = true
113-
fmt.Printf("Bumping %s in go.mod to %s\n.", depPkg, rev)
117+
fmt.Printf("Bumping %s in go.mod to %s.\n", depPkg, rev)
114118
changed = true
115119
}
116120

@@ -146,18 +150,18 @@ type ModuleInfo struct {
146150
Time string
147151
}
148152

149-
func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commitTime time.Time) error {
153+
func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersionOrTag string, commitTime time.Time) error {
150154
cacheDir := fmt.Sprintf("%s/pkg/mod/cache/download/%s/@v", os.Getenv("GOPATH"), depPkg)
151-
goModFile := fmt.Sprintf("%s/%s.mod", cacheDir, pseudoVersion)
155+
goModFile := fmt.Sprintf("%s/%s.mod", cacheDir, pseudoVersionOrTag)
152156

153157
if _, err := os.Stat(goModFile); err == nil {
154-
fmt.Printf("Pseudo version %s for %s is already packaged up.\n", pseudoVersion, depPkg)
158+
fmt.Printf("%s for %s is already packaged up.\n", pseudoVersionOrTag, depPkg)
155159
return nil
156160
} else if err != nil && !os.IsNotExist(err) {
157161
return fmt.Errorf("Could not check if %s exists: %v", goModFile, err)
158162
}
159163

160-
fmt.Printf("Packaging up pseudo version %s for %s into go mod cache.\n", pseudoVersion, depPkg)
164+
fmt.Printf("Packaging up %s for %s into go mod cache.\n", pseudoVersionOrTag, depPkg)
161165

162166
// create the cache if it doesn't exist
163167
if err := os.MkdirAll(filepath.Dir(goModFile), os.FileMode(755)); err != nil {
@@ -173,14 +177,14 @@ func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commi
173177
return fmt.Errorf("failed to checkout %s at %s: %v", depPkg, commit, err)
174178
}
175179

176-
// copy go.mod to pseudoVersion.mod in the cache dir
180+
// copy go.mod to the cache dir
177181
if err := copyFile(fmt.Sprintf("%s/go.mod", depPath), goModFile); err != nil {
178182
return fmt.Errorf("unable to copy %s file to %s to gomod cache for %s: %v", fmt.Sprintf("%s/go.mod", depPath), goModFile, depPkg, err)
179183
}
180184

181-
// create pseudoVersion.info file in the cache dir
185+
// create info file in the cache dir
182186
moduleInfo := ModuleInfo{
183-
Version: pseudoVersion,
187+
Version: pseudoVersionOrTag,
184188
Name: commit,
185189
Short: commit[:12],
186190
Time: commitTime.UTC().Format("2006-01-02T15:04:05Z"),
@@ -190,17 +194,17 @@ func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commi
190194
if err != nil {
191195
return fmt.Errorf("error marshaling .info file for %s: %v", depPkg, err)
192196
}
193-
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s.info", cacheDir, pseudoVersion), moduleFile, 0644); err != nil {
194-
return fmt.Errorf("failed to write %s file for %s: %v", fmt.Sprintf("%s/%s.info", cacheDir, pseudoVersion), depPkg, err)
197+
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s.info", cacheDir, pseudoVersionOrTag), moduleFile, 0644); err != nil {
198+
return fmt.Errorf("failed to write %s file for %s: %v", fmt.Sprintf("%s/%s.info", cacheDir, pseudoVersionOrTag), depPkg, err)
195199
}
196200

197-
// create the pseudoVersion.zip file in the cache dir. This zip file has the same hash
201+
// create the zip file in the cache dir. This zip file has the same hash
198202
// as of the zip file that would have been created by go mod download.
199-
zipCommand := exec.Command("/gomod-zip", "--package-name", depPkg, "--pseudo-version", pseudoVersion)
203+
zipCommand := exec.Command("/gomod-zip", "--package-name", depPkg, "--pseudo-version", pseudoVersionOrTag)
200204
zipCommand.Stdout = os.Stdout
201205
zipCommand.Stderr = os.Stderr
202206
if err := zipCommand.Run(); err != nil {
203-
return fmt.Errorf("failed to run gomod-zip for %s at %s: %v", depPkg, pseudoVersion, err)
207+
return fmt.Errorf("failed to run gomod-zip for %s at %s: %v", depPkg, pseudoVersionOrTag, err)
204208
}
205209

206210
// append the pseudoVersion to the list file in the cache dir
@@ -210,7 +214,7 @@ func packageDepToGoModCache(depPath, depPkg, commit, pseudoVersion string, commi
210214
}
211215
defer listFile.Close()
212216

213-
if _, err := listFile.WriteString(fmt.Sprintf("%s\n", pseudoVersion)); err != nil {
217+
if _, err := listFile.WriteString(fmt.Sprintf("%s\n", pseudoVersionOrTag)); err != nil {
214218
return fmt.Errorf("unable to write to list file in %s: %v", cacheDir, err)
215219
}
216220

0 commit comments

Comments
 (0)