Skip to content
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
36 changes: 26 additions & 10 deletions cmd/platform/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/devsy-org/api/pkg/auth"
devsyclientset "github.com/devsy-org/api/pkg/clientset/versioned"
platformflags "github.com/devsy-org/devsy/cmd/platform/flags"
"github.com/devsy-org/devsy/internal/diagnosticredact"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/log"
devsyopen "github.com/devsy-org/devsy/pkg/open"
Expand Down Expand Up @@ -59,6 +60,8 @@ const (

var defaultReleaseName = config.PlatformReleaseName

var platformToolCommand = exec.CommandContext

var defaultDeploymentName = "loft" // Need to update helm chart if we change this!

// StartCmd holds the login cmd flags.
Expand Down Expand Up @@ -691,9 +694,10 @@ func checkCLITools(ctx context.Context, kubeContext string) error {
)
}

output, err := exec.Command("helm", "version").CombinedOutput()
output, err := platformToolCommand(ctx, "helm", "version").CombinedOutput()
if err != nil {
return fmt.Errorf("seems like there are issues with your helm client: \n\n%s", output)
detail := diagnosticredact.EnvironmentRecord(string(output))
return fmt.Errorf("seems like there are issues with your helm client: \n\n%s", detail)
}

_, err = exec.LookPath("kubectl")
Expand All @@ -704,7 +708,7 @@ func checkCLITools(ctx context.Context, kubeContext string) error {
)
}

kubectlCmd := exec.CommandContext(
kubectlCmd := platformToolCommand(
ctx,
"kubectl",
"version",
Expand All @@ -715,7 +719,7 @@ func checkCLITools(ctx context.Context, kubeContext string) error {
if err != nil {
return fmt.Errorf(
"seems like kubectl cannot connect to your Kubernetes cluster: \n\n%s",
output,
diagnosticredact.EnvironmentRecord(string(output)),
)
}

Expand Down Expand Up @@ -1232,13 +1236,17 @@ func installNginxIngress(
fmt.Fprint(os.Stderr, "\n")
log.Infof("Executing command: helm %s\n", strings.Join(args, " "))
log.Info("Waiting for ingress controller deployment, this can take several minutes")
helmCmd := exec.CommandContext(
helmCmd := platformToolCommand(
ctx,
"helm",
args...) // #nosec G204 -- helm args are constructed internally
output, err := helmCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error during helm command: %s (%w)", string(output), err)
return fmt.Errorf(
"error during helm command: %s (%w)",
diagnosticredact.EnvironmentRecord(string(output)),
err,
)
}

list, err := kubeClient.CoreV1().Secrets(ingressNginx).List(ctx, metav1.ListOptions{
Expand Down Expand Up @@ -1535,7 +1543,7 @@ func upgradeRelease(
fmt.Fprint(os.Stderr, "\n")
log.Infof("Executing command: helm %s\n", strings.Join(args, " "))
log.Info("Waiting for helm command, this can take up to several minutes")
helmCmd := exec.CommandContext(
helmCmd := platformToolCommand(
ctx,
"helm",
args...) // #nosec G204 -- helm args are constructed internally
Expand All @@ -1544,7 +1552,11 @@ func upgradeRelease(
}
output, err := helmCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error during helm command: %s (%w)", string(output), err)
return fmt.Errorf(
"error during helm command: %s (%w)",
diagnosticredact.EnvironmentRecord(string(output)),
err,
)
}

log.Infof("Devsy Platform has been deployed to your cluster!")
Expand All @@ -1564,7 +1576,7 @@ func getReleaseManifests(
)
args = helmArgs(args, target, extraArgs)

helmCmd := exec.CommandContext(
helmCmd := platformToolCommand(
ctx,
"helm",
args...) // #nosec G204 -- helm args are constructed internally
Expand All @@ -1573,7 +1585,11 @@ func getReleaseManifests(
}
output, err := helmCmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error during helm command: %s (%w)", string(output), err)
return "", fmt.Errorf(
"error during helm command: %s (%w)",
diagnosticredact.EnvironmentRecord(string(output)),
err,
)
}
return string(output), nil
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/platform/start_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/devsy-org/devsy/internal/diagnosticredact"
"github.com/devsy-org/devsy/pkg/config"
devcconfig "github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/devsy-org/devsy/pkg/hash"
Expand Down Expand Up @@ -257,9 +258,10 @@ func (cmd *StartCmd) prepareDocker() error {
)
}

output, err := exec.Command("docker", "ps").CombinedOutput()
output, err := platformToolCommand(context.Background(), "docker", "ps").CombinedOutput()
if err != nil {
return fmt.Errorf("seems like there are issues with your docker cli: \n\n%s", output)
detail := diagnosticredact.EnvironmentRecord(string(output))
return fmt.Errorf("seems like there are issues with your docker cli: \n\n%s", detail)
}

return nil
Expand Down Expand Up @@ -446,7 +448,7 @@ func (e *Error) Error() string {
message += string(exitError.Stderr) + "\n"
}

return message + e.err.Error()
return diagnosticredact.EnvironmentRecord(message + e.err.Error())
}

func (e *Error) Unwrap() error {
Expand Down
102 changes: 102 additions & 0 deletions cmd/platform/start_docker_diagnostic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package platform

import (
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"

"github.com/devsy-org/devsy/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

const platformFoldedValue = "synthetic-platform-continuation"

func TestDockerStartErrorMasksFoldedOutput(t *testing.T) {
const fixture = "synthetic-docker-start-continuation"
exit := &exec.ExitError{
Stderr: []byte("Authorization: Bearer\r\n " + fixture + "\r\npublic stderr"),
}
err := WrapCommandError([]byte("public stdout"), exit)
require.ErrorIs(t, err, exit)
assert.NotContains(t, err.Error(), fixture)
assert.Contains(t, err.Error(), "public stdout")
assert.Contains(t, err.Error(), "public stderr")
}

func TestPlatformToolFailuresMaskFoldedOutput(t *testing.T) {
dir := t.TempDir()
ctx := context.Background()
fixturePlatformTools(t, dir)
t.Setenv("PATH", dir)
target := helmTarget{chartName: "fixture"}
for _, fixture := range []struct {
name string
run func() error
}{
{"docker", (&StartCmd{}).prepareDocker},
{"helm version", func() error { return checkCLITools(ctx, "") }},
{"kubectl version", func() error { return checkCLITools(ctx, "fixture") }},
{"ingress", func() error { return installNginxIngress(ctx, nil, "") }},
{"upgrade", func() error { return upgradeRelease(ctx, target, nil) }},
{"manifests", func() error {
_, err := getReleaseManifests(ctx, target, nil)
return err
}},
} {
t.Run(fixture.name, func(t *testing.T) {
t.Setenv("DEVSY_FIXTURE_TOOL_SUCCESS", fixture.name)
err := fixture.run()
require.Error(t, err)
assert.Contains(t, err.Error(), "public tool failure")
assert.NotContains(t, err.Error(), platformFoldedValue)
})
}
}

func TestHelmUninstallFailureMasksLoggedOutput(t *testing.T) {
fixturePlatformTools(t, t.TempDir())
observed := log.InitTestObserved(t, zapcore.DebugLevel)
runHelmUninstallCommand(platformToolCommand(t.Context(), "helm"))
require.Positive(t, observed.FilterMessageSnippet("public tool failure").Len())
require.Zero(t, observed.FilterMessageSnippet(platformFoldedValue).Len())
}

func fixturePlatformTools(t *testing.T, dir string) {
t.Helper()
self, err := os.Executable()
require.NoError(t, err)
original := platformToolCommand
t.Cleanup(func() { platformToolCommand = original })
platformToolCommand = func(ctx context.Context, name string, _ ...string) *exec.Cmd {
command := exec.CommandContext(ctx, self, "-test.run=^TestSyntheticPlatformTool$")
command.Env = append(os.Environ(), "DEVSY_PLATFORM_DIAGNOSTIC_FIXTURE="+name)
return command
}
for _, name := range []string{"docker", "helm", "kubectl"} {
if runtime.GOOS == "windows" {
name += ".exe"
}
path := filepath.Join(dir, name)
require.NoError(t, os.WriteFile(path, nil, 0o600))
mode, err := os.Stat(path)
require.NoError(t, err)
require.NoError(t, os.Chmod(path, mode.Mode()|0o100))
}
}

func TestSyntheticPlatformTool(t *testing.T) {
tool := os.Getenv("DEVSY_PLATFORM_DIAGNOSTIC_FIXTURE")
if tool == "" ||
tool == "helm" && os.Getenv("DEVSY_FIXTURE_TOOL_SUCCESS") == "kubectl version" {
return
}
_, _ = os.Stderr.WriteString(
"public tool failure\nAuthorization: Bearer\n " + platformFoldedValue + "\n",
)
os.Exit(9)
}
13 changes: 9 additions & 4 deletions cmd/platform/start_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

devsyclientset "github.com/devsy-org/api/pkg/clientset/versioned"
"github.com/devsy-org/devsy/internal/diagnosticredact"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/log"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -70,12 +71,16 @@ func runHelmUninstall(
}
log.Infof("Executing command: helm %s", strings.Join(args, " "))
helmCmd := exec.CommandContext(ctx, "helm", args...) // #nosec G204 -- internally constructed
output, err := helmCmd.CombinedOutput()
runHelmUninstallCommand(helmCmd)
return nil
}

func runHelmUninstallCommand(cmd *exec.Cmd) {
output, err := cmd.CombinedOutput()
if err != nil {
log.Errorf("error during helm command: %s (%v)", string(output), err)
log.Errorf("error during helm command: %s (%v)",
diagnosticredact.RecordWithEnvironment(cmd.Environ(), string(output)), err)
}

return nil
}

func cleanupPlatformResources(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ require (
github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/dblohm7/wingoes v0.0.0-20240119213807-a09d6be7affa // indirect
github.com/devsy-org/admin-apis v1.2.0 // indirect
github.com/devsy-org/devsy/logging v0.0.0-20261121080943-b1a01fb92a98
github.com/devsy-org/devsy/logging v0.0.0-20261122052847-badb828ea923
github.com/dghubble/go-twitter v0.0.0-20211115160449-93a8679adecb // indirect
github.com/dghubble/oauth1 v0.7.3 // indirect
github.com/dghubble/sling v1.4.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ github.com/devsy-org/apiserver v1.5.4 h1:/bEPrSRSlfii2QHxc9qAiXlQawNJfIaPLraR3bY
github.com/devsy-org/apiserver v1.5.4/go.mod h1:sDFCTjCN13wAHhno4KX0Z756gCI8ttW/c7NwTzo16K0=
github.com/devsy-org/devsy-runtime-sdk v1.6.0 h1:iEV4Y0Sc3LJNkTLadJSKcWcAf/oCXvC/AkHilrg/KyM=
github.com/devsy-org/devsy-runtime-sdk v1.6.0/go.mod h1:MiBP/fiY83DAS0TueEiJZSHYZ1vq85UR4KJTMfHIM+E=
github.com/devsy-org/devsy/logging v0.0.0-20261121080943-b1a01fb92a98 h1:Z+uG77rbf+Msu7LSoWnAe4MylASfsr8gin37+DA4ikk=
github.com/devsy-org/devsy/logging v0.0.0-20261121080943-b1a01fb92a98/go.mod h1:kpwoMhSdrhnJfTJ014ht04BLw0DS7FyF33RqIIe8CKs=
github.com/devsy-org/devsy/logging v0.0.0-20261122052847-badb828ea923 h1:7H4skJl0aR623Sm4jtTai/TjQLnKplMkCkMsxIiz1e0=
github.com/devsy-org/devsy/logging v0.0.0-20261122052847-badb828ea923/go.mod h1:kpwoMhSdrhnJfTJ014ht04BLw0DS7FyF33RqIIe8CKs=
github.com/devsy-org/ssh v1.2.10 h1:fCxyqx8ti6qzKhVts55q0zDpx72q1YHNM7VK6TGkAps=
github.com/devsy-org/ssh v1.2.10/go.mod h1:IiIPdjt/ilk08Z+I6ofwUaH52I/1PmVnp+0pOVQXlwI=
github.com/devsy-org/tailscale v1.102.2 h1:9SB6htvO+HmG8alal8WGCshHapfHR7dUFRSMYiFIzIM=
Expand Down
Loading
Loading