diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 97de1b18ad..acd0d26358 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -1073,7 +1073,6 @@ jobs: - name: cache kind.exe (Windows) if: matrix.install-kind == true && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') - id: kind-cache-windows uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ runner.temp }}/kind.exe @@ -1081,27 +1080,16 @@ jobs: - name: setup kind (Windows) if: matrix.install-kind == true && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') - shell: bash + shell: pwsh env: KIND_EXPERIMENTAL_PROVIDER: podman DOCKER_HOST: npipe:////./pipe/podman-machine-default run: | - if [ "${{ steps.kind-cache-windows.outputs.cache-hit }}" != "true" ]; then - curl -Lo "$RUNNER_TEMP/kind.exe" "https://github.com/kubernetes-sigs/kind/releases/download/v0.33.0/kind-windows-amd64" - expected="4b22adaa135368c5a465d56bbd8e520cbea87272a06ca00b6078e7b81515c9fc" - actual=$(sha256sum "$RUNNER_TEMP/kind.exe" | awk '{print $1}' | sed 's/^[^a-f0-9]*//') - if [ "$actual" != "$expected" ]; then - echo "SHA256 mismatch for kind.exe! Expected: $expected, Got: $actual" - exit 1 - fi - echo "kind.exe checksum verified: $actual" - fi - - export PATH="$RUNNER_TEMP:$PATH" - echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - - CLUSTER_NAME=$(python -c "import uuid; print(uuid.uuid4().hex)") - kind create cluster --name "$CLUSTER_NAME" --image kindest/node:v1.36.4@sha256:099e049362a1526b2db71494e1947aae99bd16290d7c895f2b7ea312e3cbfaed + & "$env:RUNNER_TEMP\devsy-ci-podman\devsy-ci-podman.exe" install-kind ` + --url "https://github.com/kubernetes-sigs/kind/releases/download/v0.33.0/kind-windows-amd64" ` + --sha256 "4b22adaa135368c5a465d56bbd8e520cbea87272a06ca00b6078e7b81515c9fc" ` + --kind-path "$env:RUNNER_TEMP\kind.exe" ` + --image "kindest/node:v1.36.4@sha256:099e049362a1526b2db71494e1947aae99bd16290d7c895f2b7ea312e3cbfaed" # NOTE: skevetter/setup-kind does not work on Windows runners - name: setup kind diff --git a/hack/podman_ci/install.go b/hack/podman_ci/install.go index 98996f2e7c..f48c053831 100644 --- a/hack/podman_ci/install.go +++ b/hack/podman_ci/install.go @@ -83,7 +83,7 @@ func installPodmanMSI( if err != nil { return fmt.Errorf("read podman MSI: %w", err) } - if err := verifyMSIChecksum(msi, cfg.SHA256); err != nil { + if err := verifyChecksum("podman MSI", msi, cfg.SHA256); err != nil { return err } deps.Log.Printf("Podman MSI checksum verified: %s", installSum(msi)) @@ -114,10 +114,15 @@ func installSum(data []byte) string { return hex.EncodeToString(sum[:]) } -func verifyMSIChecksum(data []byte, expected string) error { +func verifyChecksum(artifact string, data []byte, expected string) error { actual := installSum(data) if !strings.EqualFold(actual, expected) { - return fmt.Errorf("SHA256 mismatch for podman MSI! Expected: %s, Got: %s", expected, actual) + return fmt.Errorf( + "SHA256 mismatch for %s! Expected: %s, Got: %s", + artifact, + expected, + actual, + ) } return nil } diff --git a/hack/podman_ci/kind.go b/hack/podman_ci/kind.go new file mode 100644 index 0000000000..9df45f9aad --- /dev/null +++ b/hack/podman_ci/kind.go @@ -0,0 +1,103 @@ +package main + +import ( + "context" + "crypto/rand" + "encoding/hex" + "fmt" + "os" + "os/exec" + "path/filepath" + + "github.com/spf13/cobra" +) + +const ( + commandInstallKind = "install-kind" + kindClusterNameSize = 16 +) + +type ClusterNamer func() (string, error) + +type ClusterCreator func(ctx context.Context, kindPath, name, image string) error + +type KindConfig struct { + URL string + SHA256 string + KindPath string + Image string + GitHubPath string +} + +func newInstallKindCommand() *cobra.Command { + var cfg KindConfig + cmd := &cobra.Command{ + Use: commandInstallKind, + Short: "Install a verified kind binary and create a cluster with a random name", + RunE: func(*cobra.Command, []string) error { + deps := Dependencies{ + Log: newLogger(), + Files: osFileSystem{}, + Download: downloadFile, + NewClusterName: newClusterName, + CreateCluster: createKindCluster, + } + return classifyOperationalError(installKind(context.Background(), deps, cfg)) + }, + } + cmd.Flags().StringVar(&cfg.URL, "url", "", "kind download URL") + cmd.Flags().StringVar(&cfg.SHA256, "sha256", "", "expected SHA256 of the kind binary") + cmd.Flags().StringVar(&cfg.KindPath, "kind-path", "", "local path for the kind binary") + cmd.Flags().StringVar(&cfg.Image, "image", "", "kind node image") + cmd.Flags(). + StringVar(&cfg.GitHubPath, "github-path", os.Getenv("GITHUB_PATH"), "file to append the kind directory to") + for _, name := range []string{"url", "sha256", "kind-path", "image"} { + _ = cmd.MarkFlagRequired(name) + } + return cmd +} + +func installKind(ctx context.Context, deps Dependencies, cfg KindConfig) error { + if _, statErr := deps.Files.Stat(cfg.KindPath); statErr != nil { + if err := deps.Download(ctx, cfg.URL, cfg.KindPath); err != nil { + return fmt.Errorf("download kind: %w", err) + } + } + data, err := deps.Files.ReadFile(cfg.KindPath) + if err != nil { + return fmt.Errorf("read kind: %w", err) + } + if err := verifyChecksum("kind.exe", data, cfg.SHA256); err != nil { + return err + } + deps.Log.Printf("kind.exe checksum verified: %s", installSum(data)) + + if err := appendGitHubPath(deps.Files, cfg.GitHubPath, filepath.Dir(cfg.KindPath)); err != nil { + return err + } + + name, err := deps.NewClusterName() + if err != nil { + return fmt.Errorf("generate kind cluster name: %w", err) + } + deps.Log.Printf("creating kind cluster %s", name) + if err := deps.CreateCluster(ctx, cfg.KindPath, name, cfg.Image); err != nil { + return fmt.Errorf("kind create cluster: %w", err) + } + return nil +} + +func newClusterName() (string, error) { + b := make([]byte, kindClusterNameSize) + if _, err := rand.Read(b); err != nil { + return "", err + } + return hex.EncodeToString(b), nil +} + +func createKindCluster(ctx context.Context, kindPath, name, image string) error { + cmd := exec.CommandContext(ctx, kindPath, "create", "cluster", "--name", name, "--image", image) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/hack/podman_ci/kind_test.go b/hack/podman_ci/kind_test.go new file mode 100644 index 0000000000..f718e5ad5e --- /dev/null +++ b/hack/podman_ci/kind_test.go @@ -0,0 +1,243 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "reflect" + "regexp" + "strconv" + "strings" + "testing" +) + +const ( + kindHelperEnv = "DEVSY_PODMAN_KIND_HELPER" + kindHelperExitEnv = "DEVSY_PODMAN_KIND_HELPER_EXIT" + testKindImage = "kindest/node:v1.36.4@sha256:099e049362a1526b2db71494e1947aae99bd16290d7c895f2b7ea312e3cbfaed" + testClusterName = "0123456789abcdef0123456789abcdef" + testDockerHost = "npipe:////./pipe/podman-machine-default" +) + +func TestMain(m *testing.M) { + if out := os.Getenv(kindHelperEnv); out != "" { + os.Exit(runKindHelper(out)) + } + os.Exit(m.Run()) +} + +type kindHelperReport struct { + Args []string `json:"args"` + KindProvider string `json:"kindExperimentalProvider"` + DockerHost string `json:"dockerHost"` +} + +func runKindHelper(out string) int { + report := kindHelperReport{ + Args: os.Args[1:], + KindProvider: os.Getenv("KIND_EXPERIMENTAL_PROVIDER"), + DockerHost: os.Getenv("DOCKER_HOST"), + } + data, err := json.Marshal(report) + if err != nil { + return 2 + } + if err := os.WriteFile(out, data, 0o600); err != nil { + return 2 + } + code, _ := strconv.Atoi(os.Getenv(kindHelperExitEnv)) + return code +} + +type kindClusterCall struct{ kindPath, name, image string } + +type kindFixture struct { + deps Dependencies + cfg KindConfig + files *installFS + out *bytes.Buffer + downloads int + clusters []kindClusterCall +} + +func newKindFixture(t *testing.T, kind []byte) *kindFixture { + t.Helper() + dir := t.TempDir() + fx := &kindFixture{ + files: &installFS{files: map[string][]byte{}, appends: map[string]*installWriter{}}, + out: &bytes.Buffer{}, + } + fx.cfg = KindConfig{ + URL: "https://example.test/kind-windows-amd64", + SHA256: installSum(kind), + KindPath: filepath.Join(dir, "kind.exe"), + Image: testKindImage, + GitHubPath: filepath.Join(dir, "github_path"), + } + fx.deps = Dependencies{ + Log: &Logger{Out: fx.out, Err: fx.out}, + Files: fx.files, + Download: func(_ context.Context, _, dest string) error { + fx.downloads++ + fx.files.files[dest] = kind + return nil + }, + NewClusterName: func() (string, error) { return testClusterName, nil }, + CreateCluster: func(_ context.Context, kindPath, name, image string) error { + fx.clusters = append(fx.clusters, kindClusterCall{kindPath, name, image}) + return nil + }, + } + return fx +} + +func (fx *kindFixture) gitHubPathContent(t *testing.T) string { + t.Helper() + w := fx.files.appends[fx.cfg.GitHubPath] + if w == nil { + t.Fatalf("no GITHUB_PATH append to %s", fx.cfg.GitHubPath) + } + return w.String() +} + +func TestInstallKindSkipsDownloadWhenBinaryPresent(t *testing.T) { + kind := []byte("kind binary") + fx := newKindFixture(t, kind) + fx.files.files[fx.cfg.KindPath] = kind + fx.cfg.SHA256 = strings.ToUpper(fx.cfg.SHA256) + + if err := installKind(context.Background(), fx.deps, fx.cfg); err != nil { + t.Fatal(err) + } + if fx.downloads != 0 { + t.Fatalf("downloads = %d, want 0", fx.downloads) + } + want := []kindClusterCall{{fx.cfg.KindPath, testClusterName, testKindImage}} + if !reflect.DeepEqual(fx.clusters, want) { + t.Fatalf("clusters = %+v, want %+v", fx.clusters, want) + } + if got, want := fx.gitHubPathContent(t), filepath.Dir(fx.cfg.KindPath)+"\n"; got != want { + t.Fatalf("GITHUB_PATH content = %q, want %q", got, want) + } +} + +func TestInstallKindDownloadsMissingBinary(t *testing.T) { + kind := []byte("kind binary") + fx := newKindFixture(t, kind) + + if err := installKind(context.Background(), fx.deps, fx.cfg); err != nil { + t.Fatal(err) + } + if fx.downloads != 1 { + t.Fatalf("downloads = %d, want 1", fx.downloads) + } + if got := string(fx.files.files[fx.cfg.KindPath]); got != string(kind) { + t.Fatalf("downloaded content = %q, want %q", got, kind) + } + if len(fx.clusters) != 1 { + t.Fatalf("cluster creations = %d, want 1", len(fx.clusters)) + } +} + +func TestInstallKindRejectsChecksumMismatchBeforeClusterCreate(t *testing.T) { + fx := newKindFixture(t, []byte("kind binary")) + fx.files.files[fx.cfg.KindPath] = []byte("tampered") + + err := installKind(context.Background(), fx.deps, fx.cfg) + want := fmt.Sprintf("SHA256 mismatch for kind.exe! Expected: %s, Got: %s", + fx.cfg.SHA256, installSum([]byte("tampered"))) + if err == nil || err.Error() != want { + t.Fatalf("error = %v, want %q", err, want) + } + if len(fx.clusters) != 0 { + t.Fatalf("cluster created despite checksum mismatch: %+v", fx.clusters) + } + if _, ok := fx.files.appends[fx.cfg.GitHubPath]; ok { + t.Fatal("GITHUB_PATH appended despite checksum mismatch") + } +} + +func TestInstallKindPropagatesClusterCreateError(t *testing.T) { + boom := errors.New("boom") + fx := newKindFixture(t, []byte("kind binary")) + fx.deps.CreateCluster = func(context.Context, string, string, string) error { return boom } + + err := installKind(context.Background(), fx.deps, fx.cfg) + if !errors.Is(err, boom) { + t.Fatalf("error = %v, want wrapped %v", err, boom) + } +} + +func TestNewClusterNameIsRandomHex(t *testing.T) { + pattern := regexp.MustCompile(`^[0-9a-f]{32}$`) + first, err := newClusterName() + if err != nil { + t.Fatal(err) + } + if !pattern.MatchString(first) { + t.Fatalf("cluster name %q is not 32 lowercase hex characters", first) + } + second, err := newClusterName() + if err != nil { + t.Fatal(err) + } + if first == second { + t.Fatalf("cluster name repeated: %q", first) + } +} + +func TestCreateKindClusterPassesArgsAndInheritedEnvironment(t *testing.T) { + report := filepath.Join(t.TempDir(), "report.json") + t.Setenv(kindHelperEnv, report) + t.Setenv(kindHelperExitEnv, "0") + t.Setenv("KIND_EXPERIMENTAL_PROVIDER", "podman") + t.Setenv("DOCKER_HOST", testDockerHost) + + if err := createKindCluster( + context.Background(), + os.Args[0], + testClusterName, + testKindImage, + ); err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(report) + if err != nil { + t.Fatal(err) + } + var got kindHelperReport + if err := json.Unmarshal(data, &got); err != nil { + t.Fatal(err) + } + want := kindHelperReport{ + Args: []string{ + "create", + "cluster", + "--name", + testClusterName, + "--image", + testKindImage, + }, + KindProvider: "podman", + DockerHost: testDockerHost, + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("kind invocation = %+v, want %+v", got, want) + } +} + +func TestCreateKindClusterReturnsExitErrorOnFailure(t *testing.T) { + t.Setenv(kindHelperEnv, filepath.Join(t.TempDir(), "report.json")) + t.Setenv(kindHelperExitEnv, "3") + + err := createKindCluster(context.Background(), os.Args[0], testClusterName, testKindImage) + var exitErr *exec.ExitError + if !errors.As(err, &exitErr) || exitErr.ExitCode() != 3 { + t.Fatalf("error = %v, want exit code 3", err) + } +} diff --git a/hack/podman_ci/setup.go b/hack/podman_ci/setup.go index a36cd9844f..c888486032 100644 --- a/hack/podman_ci/setup.go +++ b/hack/podman_ci/setup.go @@ -25,12 +25,14 @@ func (osFileSystem) OpenAppend(path string) (io.WriteCloser, error) { } type Dependencies struct { - Runner Runner - Clock Clock - Log *Logger - Files FileSystem - Budget *Budget - Download Downloader + Runner Runner + Clock Clock + Log *Logger + Files FileSystem + Budget *Budget + Download Downloader + NewClusterName ClusterNamer + CreateCluster ClusterCreator } func newRootCommand() *cobra.Command { @@ -42,6 +44,7 @@ func newRootCommand() *cobra.Command { } root.AddCommand(newSetupCommand()) root.AddCommand(newInstallCommand()) + root.AddCommand(newInstallKindCommand()) return root }