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

Add version subcomand to wsl-helper #8270

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ fscache
fstrim
gabcdef
gazornaanplatt
gcflags
gcs
gdk
GENERALIZEDTIME
Expand Down
76 changes: 69 additions & 7 deletions scripts/dependencies/go-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ type GoDependencyOptions = {
* Additional environment for the go compiler; e.g. for GOARCH overrides.
*/
env?: NodeJS.ProcessEnv;

/**
* The version string to be stamped into the binary at build time.
* This is typically used with `-ldflags="-X ..."` to embed version information.
* Example: `1.18.1`.
*/
version?: string;

/**
* The Go module path, typically as defined in `go.mod`. This should match the
* import path of the module (e.g., `github.com/rancher-sandbox/rancher-desktop/src/go/wsl-helper`).
*/
modulePath?: string;

/**
* Custom linker flags (`-ldflags`) for the Go build process.
* The default value includes `-s -w`, which disables symbol table and debug information
* to reduce binary size. Override this to add or modify linker flags.
* Example: `['-s', '-w']`
*/
ldFlags?: string[]

/**
* Custom compiler flags (`-gcflags`) for the Go build process.
* By default, no additional compiler flags are included. Use this to pass
* specific flags to the Go compiler, such as optimizations or debugging options.
* Example: `['all=-N', '-l']` to disable optimizations and inlining for all
* packages, which is useful for debugging.
*/
gcFlags?: string[]
};

/**
Expand Down Expand Up @@ -49,10 +79,33 @@ export class GoDependency implements Dependency {
const sourceDir = path.join(process.cwd(), 'src', 'go', this.sourcePath);
const outFile = this.outFile(context);

console.log(`Building go utility \x1B[1;33;40m${ this.name }\x1B[0m from ${ sourceDir } to ${ outFile }...`);
await simpleSpawn('go', ['build', '-ldflags', '-s -w', '-o', outFile, '.'], {
const buildArgs: string[] = ['build'];

// Handle `-ldflags`: use custom flags if provided (default: `-s -w`),
// append version stamp if `modulePath` and `version` exist, and skip if no flags.
const ldFlags: string[] = this.options.ldFlags ? [...this.options.ldFlags] : ['-s', '-w'];

if (this.options.version && this.options.modulePath) {
ldFlags.push(`-X ${ this.options.modulePath }/pkg/version.Version=${ this.options.version }`);
}
if (ldFlags.length > 0) {
buildArgs.push('-ldflags', ldFlags.join(' '));
}

// Handle compiler flags (`-gcflags`). Only include `-gcflags` if explicitly provided and not empty.
if (this.options.gcFlags && this.options.gcFlags.length > 0) {
buildArgs.push('-gcflags', this.options.gcFlags.join(' '));
}

// common args
buildArgs.push('-o', outFile, '.');

const env = this.environment(context);

console.log(`Building go utility \x1B[1;33;40m${ this.name }\x1B[0m [${ env.GOOS }/${ env.GOARCH }] from ${ sourceDir } to ${ outFile }...`);
await simpleSpawn('go', buildArgs, {
cwd: sourceDir,
env: this.environment(context),
env,
});
}

Expand Down Expand Up @@ -86,8 +139,12 @@ export class GoDependency implements Dependency {
}

export class RDCtl extends GoDependency {
constructor() {
super('rdctl');
constructor(version: string) {
super('rdctl', {
outputPath: 'bin',
modulePath: 'github.com/rancher-sandbox/rancher-desktop/src/go/rdctl',
version,
});
}

dependencies(context: DownloadContext): string[] {
Expand All @@ -112,8 +169,13 @@ export class RDCtl extends GoDependency {
}

export class WSLHelper extends GoDependency {
constructor() {
super('wsl-helper', { outputPath: 'internal', env: { CGO_ENABLED: '0' } });
constructor(version: string) {
super('wsl-helper', {
outputPath: 'internal',
env: { CGO_ENABLED: '0' },
modulePath: 'github.com/rancher-sandbox/rancher-desktop/src/go/wsl-helper',
version,
});
}

dependencies(context: DownloadContext): string[] {
Expand Down
29 changes: 25 additions & 4 deletions scripts/postinstall.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import childProcess from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -25,6 +26,14 @@ type DependencyWithContext = {
*/
const InstallTimeout = 10 * 60 * 1_000; // Ten minutes.

/**
* Retrieves the application version from package.json to stamp Go binaries.
* This version number ensures Go utilities like WSL helpers are tagged with
* the same version as the main application, maintaining consistency across
* all components of Rancher Desktop.
*/
const versionToStamp = getStampVersion();

// Dependencies that should be installed into places that users touch
// (so users' WSL distros and hosts as of the time of writing).
const userTouchedDependencies = [
Expand All @@ -36,7 +45,7 @@ const userTouchedDependencies = [
new tools.DockerProvidedCredHelpers(),
new tools.ECRCredHelper(),
new tools.SpinCLI(),
new goUtils.RDCtl(),
new goUtils.RDCtl(versionToStamp),
new goUtils.GoDependency('docker-credential-none'),
];

Expand All @@ -59,20 +68,20 @@ const windowsDependencies = [
new WSLDistroImage(),
new Wix(),
new goUtils.GoDependency('networking/cmd/host', 'internal/host-switch'),
new goUtils.WSLHelper(),
new goUtils.WSLHelper(versionToStamp),
new goUtils.NerdctlStub(),
new goUtils.SpinStub(),
];

// Dependencies that are specific to WSL.
const wslDependencies = [
new Moproxy(),
new goUtils.RDCtl(),
new goUtils.RDCtl(versionToStamp),
new goUtils.GoDependency('guestagent', 'staging'),
new goUtils.GoDependency('networking/cmd/vm', 'staging/vm-switch'),
new goUtils.GoDependency('networking/cmd/network', 'staging/network-setup'),
new goUtils.GoDependency('networking/cmd/proxy', 'staging/wsl-proxy'),
new goUtils.WSLHelper(),
new goUtils.WSLHelper(versionToStamp),
new goUtils.NerdctlStub(),
];

Expand Down Expand Up @@ -254,3 +263,15 @@ const keepScriptAlive = setTimeout(() => { }, 24 * 3600 * 1000);
process.exit(exitCode);
}
})();

/**
* Gets the version string for Go tools from git.
* Format: {tag}-{commits}-{hash}{dirty}
* Examples: v1.18.0, v1.18.0-39-gf46609959, v1.18.0-39-gf46609959.m
*/
function getStampVersion(): string {
const gitCommand = 'git describe --match v[0-9]* --dirty=.m --always --tags';
const stdout = childProcess.execSync(gitCommand, { encoding: 'utf-8' });

return stdout;
}
3 changes: 2 additions & 1 deletion src/go/rdctl/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/client"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/version"
"github.com/spf13/cobra"
)

Expand All @@ -29,7 +30,7 @@ var showVersionCmd = &cobra.Command{
Short: "Shows the CLI version.",
Long: `Shows the CLI version.`,
RunE: func(cmd *cobra.Command, args []string) error {
_, err := fmt.Printf("rdctl client version: %s, targeting server version: %s\n", client.Version, client.ApiVersion)
_, err := fmt.Printf("rdctl client version: %s, targeting server version: %s\n", version.Version, client.ApiVersion)
return err
},
}
Expand Down
1 change: 0 additions & 1 deletion src/go/rdctl/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

const (
Version = "1.1.0"
ApiVersion = "v1"
)

Expand Down
3 changes: 3 additions & 0 deletions src/go/rdctl/pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package version

var Version = "0.0.0"
39 changes: 39 additions & 0 deletions src/go/wsl-helper/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2022 SUSE LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"

"github.com/rancher-sandbox/rancher-desktop/src/go/wsl-helper/pkg/version"
"github.com/spf13/cobra"
)

// showVersionCmd represents the showVersion command
var showVersionCmd = &cobra.Command{
Use: "version",
Short: "Shows the wsl-helper version.",
Long: `Shows the wsl-helper version.`,
RunE: func(cmd *cobra.Command, args []string) error {
_, err := fmt.Printf("wsl-helper version: %s\n", version.Version)
return err
},
}

func init() {
rootCmd.AddCommand(showVersionCmd)
}
3 changes: 3 additions & 0 deletions src/go/wsl-helper/pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package version

var Version = "0.0.0"