Skip to content

Commit c9e2d2d

Browse files
vcalasanshHarness
authored andcommitted
feat: [PIPE-29213]: Changes for local runner tasks being able to opt out of docker setup (#403)
* 7e34d5 Changes for local runner tasks being able to opt out of docker setup
1 parent fbcf372 commit c9e2d2d

File tree

3 files changed

+19
-60
lines changed

3 files changed

+19
-60
lines changed

engine/docker/docker.go

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,12 @@ const (
5555
running = "running"
5656
)
5757

58-
var (
59-
globalDockerClient *client.Client
60-
globalDockerClientLock sync.Mutex
61-
)
62-
6358
// Opts configures the Docker engine.
6459
type Opts struct {
65-
HidePull bool
66-
ReuseDockerClient bool
60+
HidePull bool
61+
// Callers can pass a non-nil client.APIClient here, and it
62+
// will be used instead of creating a new docker client
63+
DockerClient client.APIClient
6764
}
6865

6966
// Docker implements a Docker pipeline engine.
@@ -95,20 +92,14 @@ func New(client client.APIClient, opts Opts) *Docker {
9592
// NewEnv returns a new Engine from the environment.
9693
func NewEnv(opts Opts) (*Docker, error) {
9794
var cli client.APIClient
98-
var err error
99-
if opts.ReuseDockerClient {
100-
globalDockerClientLock.Lock()
101-
defer globalDockerClientLock.Unlock()
102-
// Try to (re)initialize if not set
103-
if globalDockerClient == nil {
104-
globalDockerClient, err = client.NewClientWithOpts(client.FromEnv)
105-
}
106-
cli = globalDockerClient
95+
if opts.DockerClient != nil {
96+
cli = opts.DockerClient
10797
} else {
98+
var err error
10899
cli, err = client.NewClientWithOpts(client.FromEnv)
109-
}
110-
if err != nil {
111-
return nil, err
100+
if err != nil {
101+
return nil, err
102+
}
112103
}
113104
return New(cli, opts), nil
114105
}
@@ -143,36 +134,6 @@ func (e *Docker) Setup(ctx context.Context, pipelineConfig *spec.PipelineConfig)
143134
}
144135

145136
err := e.createNetworkWithRetries(ctx, pipelineConfig)
146-
147-
// launches the inernal setup steps
148-
// for _, step := range pipelineConfig.Internal {
149-
// if err := e.create(ctx, spec, step, ioutil.Discard); err != nil {
150-
// logger.FromContext(ctx).
151-
// WithError(err).
152-
// WithField("container", step.ID).
153-
// Errorln("cannot create tmate container")
154-
// return err
155-
// }
156-
// if err := e.start(ctx, step.ID); err != nil {
157-
// logger.FromContext(ctx).
158-
// WithError(err).
159-
// WithField("container", step.ID).
160-
// Errorln("cannot start tmate container")
161-
// return err
162-
// }
163-
// if !step.Detach {
164-
// // the internal containers perform short-lived tasks
165-
// // and should not require > 1 minute to execute.
166-
// //
167-
// // just to be on the safe side we apply a timeout to
168-
// // ensure we never block pipeline execution because we
169-
// // are waiting on an internal task.
170-
// ctx, cancel := context.WithTimeout(ctx, time.Minute)
171-
// defer cancel()
172-
// e.wait(ctx, step.ID)
173-
// }
174-
// }
175-
176137
return errors.TrimExtraInfo(err)
177138
}
178139

engine/pids/pidfile.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,13 @@ func AppendPIDToFile(pid int, path string) error {
150150
return nil
151151
}
152152

153-
// KillProcessesFromFile forcefully terminates all processes with PIDs found in a file.
154-
// It takes a path to the file containing the PIDs and a timeout duration.
153+
// KillProcessesFromFile forcefully terminates all processes with the passed PIDs.
154+
// It takes a list of PIDs and a timeout duration.
155155
// It will return an error if it fails to kill any processes.
156-
// The file can be empty, and the function will return nil in that case.
156+
// The PID list can be empty, and the function will return nil in that case.
157157
// The function is goroutine-safe and will wait for all goroutines to finish
158158
// before returning.
159-
func KillProcessesFromFile(ctx context.Context, path string, timeout time.Duration) error {
160-
pids, err := ReadPIDsFromFile(path)
161-
if err != nil {
162-
return fmt.Errorf("failed to read PIDs from file: %w", err)
163-
}
164-
159+
func KillProcesses(ctx context.Context, pids []int, timeout time.Duration) error {
165160
if len(pids) == 0 {
166161
fmt.Println("No valid PIDs found in file")
167162
return nil

pipeline/runtime/step_executor_stateless.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"io"
1010

11+
"github.com/docker/docker/client"
1112
"github.com/harness/lite-engine/api"
1213
"github.com/harness/lite-engine/engine"
1314
"github.com/harness/lite-engine/engine/docker"
@@ -39,6 +40,7 @@ func (e *StepExecutorStateless) Run(
3940
ctx context.Context,
4041
r *api.StartStepRequest,
4142
cfg *spec.PipelineConfig,
43+
dockerClient client.APIClient,
4244
writer logstream.Writer,
4345
) (api.VMTaskExecutionResponse, error) {
4446
if r.ID == "" {
@@ -47,7 +49,7 @@ func (e *StepExecutorStateless) Run(
4749

4850
e.stepStatus = StepStatus{Status: Running}
4951

50-
state, outputs, envs, artifact, outputV2, telemetryData, optimizationState, stepErr := e.executeStep(ctx, r, cfg, writer)
52+
state, outputs, envs, artifact, outputV2, telemetryData, optimizationState, stepErr := e.executeStep(ctx, r, cfg, dockerClient, writer)
5153
e.stepStatus = StepStatus{Status: Complete, State: state, StepErr: stepErr, Outputs: outputs, Envs: envs,
5254
Artifact: artifact, OutputV2: outputV2, OptimizationState: optimizationState, TelemetryData: telemetryData}
5355
pollResponse := convertStatus(e.stepStatus)
@@ -58,11 +60,12 @@ func (e *StepExecutorStateless) executeStep( //nolint:gocritic
5860
ctx context.Context,
5961
r *api.StartStepRequest,
6062
cfg *spec.PipelineConfig,
63+
dockerClient client.APIClient,
6164
writer logstream.Writer,
6265
) (*runtime.State, map[string]string,
6366
map[string]string, []byte, []*api.OutputV2, *types.TelemetryData, string, error) {
6467
runFunc := func(ctx context.Context, step *spec.Step, output io.Writer, isDrone bool, isHosted bool) (*runtime.State, error) {
65-
return engine.RunStep(ctx, engine.Opts{Opts: docker.Opts{ReuseDockerClient: true}}, step, output, cfg, isDrone, isHosted)
68+
return engine.RunStep(ctx, engine.Opts{Opts: docker.Opts{DockerClient: dockerClient}}, step, output, cfg, isDrone, isHosted)
6669
}
6770
// Temporary: this should be removed once we have a better way of handling test intelligence.
6871
tiConfig := getTiCfg(&r.TIConfig, &r.MtlsConfig)

0 commit comments

Comments
 (0)