Skip to content

Commit cf5167a

Browse files
committed
Merge remote-tracking branch 'origin/master' into variable-sets
2 parents ff3a077 + c63722e commit cf5167a

File tree

7 files changed

+114
-165
lines changed

7 files changed

+114
-165
lines changed

internal/agent/agent.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/go-logr/logr"
1515
"github.com/leg100/otf/internal"
16-
"github.com/leg100/otf/internal/client"
1716
"golang.org/x/sync/errgroup"
1817
)
1918

@@ -33,9 +32,9 @@ var (
3332
// agent processes runs.
3433
type agent struct {
3534
Config
36-
client.Client
3735
logr.Logger
3836

37+
client
3938
spooler // spools new run events
4039
*terminator // terminates runs
4140
Downloader // terraform cli downloader
@@ -45,7 +44,7 @@ type agent struct {
4544
}
4645

4746
// NewAgent is the constructor for an agent
48-
func NewAgent(logger logr.Logger, app client.Client, cfg Config) (*agent, error) {
47+
func NewAgent(logger logr.Logger, app client, cfg Config) (*agent, error) {
4948
if cfg.Concurrency == 0 {
5049
cfg.Concurrency = DefaultConcurrency
5150
}
@@ -65,7 +64,7 @@ func NewAgent(logger logr.Logger, app client.Client, cfg Config) (*agent, error)
6564
pathFinder := newTerraformPathFinder(cfg.TerraformBinDir)
6665

6766
agent := &agent{
68-
Client: app,
67+
client: app,
6968
Config: cfg,
7069
Logger: logger,
7170
envs: DefaultEnvs,
@@ -90,7 +89,7 @@ func NewAgent(logger logr.Logger, app client.Client, cfg Config) (*agent, error)
9089
// via http
9190
func NewExternalAgent(ctx context.Context, logger logr.Logger, cfg ExternalConfig) (*agent, error) {
9291
// Sends unauthenticated ping to server
93-
app, err := client.New(cfg.HTTPConfig)
92+
app, err := newClient(cfg.HTTPConfig)
9493
if err != nil {
9594
return nil, err
9695
}

internal/agent/client.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package agent
2+
3+
import (
4+
"context"
5+
6+
"github.com/leg100/otf/internal"
7+
"github.com/leg100/otf/internal/configversion"
8+
"github.com/leg100/otf/internal/http"
9+
"github.com/leg100/otf/internal/logs"
10+
"github.com/leg100/otf/internal/pubsub"
11+
"github.com/leg100/otf/internal/resource"
12+
"github.com/leg100/otf/internal/run"
13+
"github.com/leg100/otf/internal/state"
14+
"github.com/leg100/otf/internal/tokens"
15+
"github.com/leg100/otf/internal/variable"
16+
"github.com/leg100/otf/internal/workspace"
17+
)
18+
19+
var (
20+
_ client = (*LocalClient)(nil)
21+
_ client = (*remoteClient)(nil)
22+
)
23+
24+
type (
25+
// client allows the agent to communicate with the server endpoints.
26+
client interface {
27+
GetWorkspace(ctx context.Context, workspaceID string) (*workspace.Workspace, error)
28+
ListVariables(ctx context.Context, workspaceID string) ([]*variable.Variable, error)
29+
GetPlanFile(ctx context.Context, id string, format run.PlanFormat) ([]byte, error)
30+
UploadPlanFile(ctx context.Context, id string, plan []byte, format run.PlanFormat) error
31+
GetLockFile(ctx context.Context, id string) ([]byte, error)
32+
UploadLockFile(ctx context.Context, id string, lockFile []byte) error
33+
ListRuns(ctx context.Context, opts run.ListOptions) (*resource.Page[*run.Run], error)
34+
StartPhase(ctx context.Context, id string, phase internal.PhaseType, opts run.PhaseStartOptions) (*run.Run, error)
35+
FinishPhase(ctx context.Context, id string, phase internal.PhaseType, opts run.PhaseFinishOptions) (*run.Run, error)
36+
DownloadConfig(ctx context.Context, id string) ([]byte, error)
37+
Watch(context.Context, run.WatchOptions) (<-chan pubsub.Event, error)
38+
CreateStateVersion(ctx context.Context, opts state.CreateStateVersionOptions) (*state.Version, error)
39+
DownloadCurrentState(ctx context.Context, workspaceID string) ([]byte, error)
40+
DeleteStateVersion(ctx context.Context, svID string) error
41+
DownloadState(ctx context.Context, svID string) ([]byte, error)
42+
Hostname() string
43+
44+
tokens.RunTokenService
45+
internal.PutChunkService
46+
}
47+
48+
// LocalClient is the client for an internal agent.
49+
LocalClient struct {
50+
tokens.TokensService
51+
variable.VariableService
52+
state.StateService
53+
workspace.WorkspaceService
54+
internal.HostnameService
55+
configversion.ConfigurationVersionService
56+
run.RunService
57+
logs.LogsService
58+
}
59+
60+
// remoteClient is the client for an external agent.
61+
remoteClient struct {
62+
*http.Client
63+
http.Config
64+
65+
*stateClient
66+
*configClient
67+
*variableClient
68+
*tokensClient
69+
*workspaceClient
70+
*runClient
71+
*logsClient
72+
}
73+
74+
stateClient = state.Client
75+
configClient = configversion.Client
76+
variableClient = variable.Client
77+
tokensClient = tokens.Client
78+
workspaceClient = workspace.Client
79+
runClient = run.Client
80+
logsClient = logs.Client
81+
)
82+
83+
// New constructs a client that uses http to remotely invoke OTF
84+
// services.
85+
func newClient(config http.Config) (*remoteClient, error) {
86+
httpClient, err := http.NewClient(config)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
return &remoteClient{
92+
Client: httpClient,
93+
stateClient: &stateClient{JSONAPIClient: httpClient},
94+
configClient: &configClient{JSONAPIClient: httpClient},
95+
variableClient: &variableClient{JSONAPIClient: httpClient},
96+
tokensClient: &tokensClient{JSONAPIClient: httpClient},
97+
workspaceClient: &workspaceClient{JSONAPIClient: httpClient},
98+
runClient: &runClient{JSONAPIClient: httpClient, Config: config},
99+
logsClient: &logsClient{JSONAPIClient: httpClient},
100+
}, nil
101+
}

internal/agent/environment.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/go-logr/logr"
1010
"github.com/hashicorp/go-multierror"
1111
"github.com/leg100/otf/internal"
12-
"github.com/leg100/otf/internal/client"
1312
"github.com/leg100/otf/internal/logs"
1413
"github.com/leg100/otf/internal/run"
1514
"github.com/leg100/otf/internal/tokens"
@@ -23,7 +22,7 @@ import (
2322
// TODO: this is a pointless abstraction; refactor it out into worker's
2423
// handle() func.
2524
type environment struct {
26-
client.Client
25+
client
2726
logr.Logger
2827
Downloader // Downloader for workers to download terraform cli on demand
2928

@@ -88,7 +87,7 @@ func newEnvironment(
8887

8988
env := &environment{
9089
Logger: logger,
91-
Client: agent,
90+
client: agent,
9291
Downloader: agent,
9392
out: writer,
9493
workdir: wd,

internal/agent/spooler.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/go-logr/logr"
99
"github.com/leg100/otf/internal"
10-
"github.com/leg100/otf/internal/client"
1110
"github.com/leg100/otf/internal/pubsub"
1211
"github.com/leg100/otf/internal/resource"
1312
"github.com/leg100/otf/internal/run"
@@ -34,9 +33,9 @@ type (
3433
// spoolerDaemon implements Spooler, receiving runs with either a queued plan or
3534
// apply, and converting them into spooled jobs.
3635
spoolerDaemon struct {
37-
queue chan *run.Run // Queue of queued jobs
38-
cancelations chan cancelation // Queue of cancelation requests
39-
client.Client // Application for retrieving queued runs
36+
queue chan *run.Run // Queue of queued jobs
37+
cancelations chan cancelation // Queue of cancelation requests
38+
client // Application for retrieving queued runs
4039
logr.Logger
4140
Config
4241
}
@@ -48,11 +47,11 @@ type (
4847
)
4948

5049
// newSpooler populates a Spooler with queued runs
51-
func newSpooler(app client.Client, logger logr.Logger, cfg Config) *spoolerDaemon {
50+
func newSpooler(app client, logger logr.Logger, cfg Config) *spoolerDaemon {
5251
return &spoolerDaemon{
5352
queue: make(chan *run.Run, spoolerCapacity),
5453
cancelations: make(chan cancelation, spoolerCapacity),
55-
Client: app,
54+
client: app,
5655
Logger: logger,
5756
Config: cfg,
5857
}

internal/agent/spooler_test_helper.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package agent
33
import (
44
"context"
55

6-
"github.com/leg100/otf/internal/client"
76
"github.com/leg100/otf/internal/pubsub"
87
"github.com/leg100/otf/internal/resource"
98
"github.com/leg100/otf/internal/run"
@@ -13,7 +12,7 @@ type fakeSpoolerApp struct {
1312
runs []*run.Run
1413
events chan pubsub.Event
1514

16-
client.Client
15+
client
1716
}
1817

1918
func (a *fakeSpoolerApp) ListRuns(ctx context.Context, opts run.ListOptions) (*resource.Page[*run.Run], error) {

internal/client/client.go

Lines changed: 0 additions & 145 deletions
This file was deleted.

internal/daemon/daemon.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/leg100/otf/internal/agent"
1414
"github.com/leg100/otf/internal/auth"
1515
"github.com/leg100/otf/internal/authenticator"
16-
"github.com/leg100/otf/internal/client"
1716
"github.com/leg100/otf/internal/cloud"
1817
"github.com/leg100/otf/internal/configversion"
1918
"github.com/leg100/otf/internal/disco"
@@ -240,11 +239,9 @@ func New(ctx context.Context, logger logr.Logger, cfg Config) (*Daemon, error) {
240239

241240
agent, err := agent.NewAgent(
242241
logger.WithValues("component", "agent"),
243-
client.LocalClient{
244-
AuthService: authService,
242+
agent.LocalClient{
245243
TokensService: tokensService,
246244
WorkspaceService: workspaceService,
247-
OrganizationService: orgService,
248245
VariableService: variableService,
249246
StateService: stateService,
250247
HostnameService: hostnameService,

0 commit comments

Comments
 (0)