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

Fix another nil pointer panic in k8s mode #3075

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
15 changes: 9 additions & 6 deletions internal/job/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (e *Executor) Run(ctx context.Context) (exitCode int) {
// Start with stdout and stderr as their usual selves.
stdout, stderr := io.Writer(os.Stdout), io.Writer(os.Stderr)

// The shell environment is initially the current environment.
// It is mutated by kubernetesSetup and needed for setupRedactors.
environ := env.FromSlice(os.Environ())

// Create a logger to stderr that can be used for things prior to the
// redactor setup.
// Be careful not to log customer secrets here!
Expand All @@ -95,7 +99,7 @@ func (e *Executor) Run(ctx context.Context) (exitCode int) {
tempLog.Commentf("Using Kubernetes support")

socket := &kubernetes.Client{ID: e.KubernetesContainerID}
if err := e.kubernetesSetup(ctx, socket); err != nil {
if err := e.kubernetesSetup(ctx, environ, socket); err != nil {
e.shell.Errorf("Failed to start kubernetes socket client: %v", err)
return 1
}
Expand All @@ -113,7 +117,6 @@ func (e *Executor) Run(ctx context.Context) (exitCode int) {

// setup the redactors here once and for the life of the executor
// they will be flushed at the end of each hook
environ := env.FromSlice(os.Environ())
preRedactedStdout, preRedactedLogger := e.setupRedactors(tempLog, environ, stdout, stderr)

// Check if not nil to allow for tests to overwrite shell.
Expand Down Expand Up @@ -1201,7 +1204,7 @@ func (e *Executor) setupRedactors(log shell.Logger, environ *env.Environment, st
return stdoutRedactor, logger
}

func (e *Executor) kubernetesSetup(ctx context.Context, k8sAgentSocket *kubernetes.Client) error {
func (e *Executor) kubernetesSetup(ctx context.Context, environ *env.Environment, k8sAgentSocket *kubernetes.Client) error {
rtr := roko.NewRetrier(
roko.WithMaxAttempts(7),
roko.WithStrategy(roko.Exponential(2*time.Second, 0)),
Expand Down Expand Up @@ -1247,18 +1250,18 @@ func (e *Executor) kubernetesSetup(ctx context.Context, k8sAgentSocket *kubernet
// Just in case someone has tried to fiddle with this, set it
// unconditionally (to be compatible with pre-v3.74.1 / PR 2851
// behavior).
e.shell.Env.Set(n, v)
environ.Set(n, v)
if err := os.Setenv(n, v); err != nil {
return err
}
continue
}
// Skip any that are already set.
if e.shell.Env.Exists(n) {
if environ.Exists(n) {
continue
}
// Set it!
e.shell.Env.Set(n, v)
environ.Set(n, v)
if err := os.Setenv(n, v); err != nil {
return err
}
Expand Down