Skip to content

Commit 82b1adb

Browse files
authored
Fix the version stamp set during build (#124)
The ldflags passed to the build expect to be the root package for the repository, and should contain the pkg directory with the version variables so that the build can set them. The package passed was to the plugin entry point (./cmd/kubernetes) which caused the version stamp to be empty. Signed-off-by: Carolyn Van Slyck <[email protected]>
2 parents b0ddade + 25fcb21 commit 82b1adb

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

magefile.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
package main
55

66
import (
7+
"encoding/json"
78
"fmt"
8-
99
"io/ioutil"
1010
"log"
1111
"os"
1212
"path/filepath"
13+
"runtime"
1314
"strings"
1415
"time"
1516

@@ -61,13 +62,15 @@ const (
6162
// Docker registry for porter client container
6263
porterRegistry = "ghcr.io/getporter"
6364
porterConfigFile = "./tests/integration/operator/testdata/operator_porter_config.yaml"
65+
66+
// The root package name of the plugin repository
67+
pluginPkg = "get.porter.sh/plugin/kubernetes"
6468
)
6569

6670
// Dirs to watch recursively for build target
6771
var (
6872
srcDirs = []string{"cmd/", "pkg/", "go.mod", "magefile.go"}
6973
binDir = "bin/plugins/kubernetes/"
70-
pluginPkg = fmt.Sprintf("./cmd/%s", pluginName)
7174
supportedClientGOOS = []string{"linux", "darwin", "windows"}
7275
// number of nodes for ginkgo parallel test execution (1=sequential)
7376
ginkgoNodes = "1"
@@ -165,6 +168,25 @@ func XBuildAll() {
165168
}
166169

167170
releases.PreparePluginForPublish(pluginName)
171+
verifyVersionStamp()
172+
}
173+
174+
// verifyVersionStamp checks that the version was set on the cross-compiled binaries
175+
func verifyVersionStamp() {
176+
// When this test fails, pluginPkg is set incorrectly or not passed to the releases functions properly
177+
pluginBinaryPath := filepath.Join(binDir, fmt.Sprintf("dev/kubernetes-%s-amd64", runtime.GOOS))
178+
versionOutput, _ := must.OutputV(pluginBinaryPath, "version", "-o=json")
179+
180+
var raw map[string]interface{}
181+
if err := json.Unmarshal([]byte(versionOutput), &raw); err != nil {
182+
mgx.Must(fmt.Errorf("error parsing the version command output as json: %w", err))
183+
}
184+
185+
meta := releases.LoadMetadata()
186+
gotVersion := raw["version"].(string)
187+
if gotVersion != meta.Version {
188+
mgx.Must(fmt.Errorf("the version was not set correctly on the kubernetes plugin binary %s: expected %q got %q", pluginBinaryPath, meta.Version, gotVersion))
189+
}
168190
}
169191

170192
// Run local integration tests against the test cluster.

0 commit comments

Comments
 (0)