Skip to content

Commit

Permalink
refactor: rename agent pkg to remoteops (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 authored Oct 30, 2023
1 parent dc7c7cc commit dad555a
Show file tree
Hide file tree
Showing 36 changed files with 125 additions and 133 deletions.
8 changes: 4 additions & 4 deletions cmd/otf-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

cmdutil "github.com/leg100/otf/cmd"
"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/logr"
"github.com/leg100/otf/internal/remoteops"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -27,7 +27,7 @@ func main() {
func run(ctx context.Context, args []string) error {
var (
loggerCfg *logr.Config
cfg *agent.ExternalConfig
cfg *remoteops.AgentConfig
)

cmd := &cobra.Command{
Expand All @@ -41,7 +41,7 @@ func run(ctx context.Context, args []string) error {
return err
}

agent, err := agent.NewExternalAgent(cmd.Context(), logger, *cfg)
agent, err := remoteops.NewAgent(cmd.Context(), logger, *cfg)
if err != nil {
return fmt.Errorf("unable to start agent: %w", err)
}
Expand All @@ -54,7 +54,7 @@ func run(ctx context.Context, args []string) error {
cmd.SetArgs(args)

loggerCfg = logr.NewConfigFromFlags(cmd.Flags())
cfg = agent.NewExternalConfigFromFlags(cmd.Flags())
cfg = remoteops.NewAgentConfigFromFlags(cmd.Flags())

if err := cmdutil.SetFlagsFromEnvVariables(cmd.Flags()); err != nil {
return errors.Wrap(err, "failed to populate config from environment vars")
Expand Down
4 changes: 2 additions & 2 deletions cmd/otfd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

cmdutil "github.com/leg100/otf/cmd"
"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/authenticator"
"github.com/leg100/otf/internal/daemon"
"github.com/leg100/otf/internal/github"
"github.com/leg100/otf/internal/gitlab"
"github.com/leg100/otf/internal/logr"
"github.com/leg100/otf/internal/remoteops"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -103,7 +103,7 @@ func parseFlags(ctx context.Context, args []string, out io.Writer) error {
cmd.Flags().StringVar(&cfg.GoogleIAPConfig.Audience, "google-jwt-audience", "", "The Google JWT audience claim for validation. If unspecified then validation is skipped")

loggerConfig = logr.NewConfigFromFlags(cmd.Flags())
cfg.AgentConfig = agent.NewConfigFromFlags(cmd.Flags())
cfg.RemoteOpsConfig = remoteops.NewConfigFromFlags(cmd.Flags())

if err := cmdutil.SetFlagsFromEnvVariables(cmd.Flags()); err != nil {
return errors.Wrap(err, "failed to populate config from environment vars")
Expand Down
15 changes: 0 additions & 15 deletions internal/agent/agent_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions internal/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"errors"

"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/authenticator"
"github.com/leg100/otf/internal/configversion"
"github.com/leg100/otf/internal/inmem"
"github.com/leg100/otf/internal/remoteops"
"github.com/leg100/otf/internal/tokens"
)

Expand All @@ -16,7 +16,7 @@ var ErrInvalidSecretLength = errors.New("secret must be 16 bytes in size")
// Config configures the otfd daemon. Descriptions of each field can be found in
// the flag definitions in ./cmd/otfd
type Config struct {
AgentConfig *agent.Config
RemoteOpsConfig *remoteops.Config
CacheConfig *inmem.CacheConfig
GithubHostname string
GithubClientID string
Expand Down Expand Up @@ -46,9 +46,9 @@ type Config struct {
}

func ApplyDefaults(cfg *Config) {
if cfg.AgentConfig == nil {
cfg.AgentConfig = &agent.Config{
Concurrency: agent.DefaultConcurrency,
if cfg.RemoteOpsConfig == nil {
cfg.RemoteOpsConfig = &remoteops.Config{
Concurrency: remoteops.DefaultConcurrency,
}
}
if cfg.CacheConfig == nil {
Expand Down
24 changes: 12 additions & 12 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/go-logr/logr"
"github.com/gorilla/mux"
"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/api"
"github.com/leg100/otf/internal/auth"
"github.com/leg100/otf/internal/authenticator"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/leg100/otf/internal/organization"
"github.com/leg100/otf/internal/pubsub"
"github.com/leg100/otf/internal/releases"
"github.com/leg100/otf/internal/remoteops"
"github.com/leg100/otf/internal/repohooks"
"github.com/leg100/otf/internal/run"
"github.com/leg100/otf/internal/scheduler"
Expand Down Expand Up @@ -71,7 +71,7 @@ type (

Handlers []internal.Handlers

agent process
opsDaemon process
}

process interface {
Expand Down Expand Up @@ -277,9 +277,9 @@ func New(ctx context.Context, logger logr.Logger, cfg Config) (*Daemon, error) {
RunService: runService,
})

agent, err := agent.NewAgent(
logger.WithValues("component", "agent"),
agent.LocalClient{
remoteopsDaemon, err := remoteops.NewDaemon(
logger.WithValues("component", "remoteops"),
remoteops.InProcClient{
TokensService: tokensService,
WorkspaceService: workspaceService,
VariableService: variableService,
Expand All @@ -289,7 +289,7 @@ func New(ctx context.Context, logger logr.Logger, cfg Config) (*Daemon, error) {
RunService: runService,
LogsService: logsService,
},
*cfg.AgentConfig,
*cfg.RemoteOpsConfig,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -400,7 +400,7 @@ func New(ctx context.Context, logger logr.Logger, cfg Config) (*Daemon, error) {
ConnectionService: connectionService,
Broker: broker,
DB: db,
agent: agent,
opsDaemon: remoteopsDaemon,
}, nil
}

Expand Down Expand Up @@ -511,12 +511,12 @@ func (d *Daemon) Start(ctx context.Context, started chan struct{}) error {
case <-d.Broker.Started():
}

// Run local agent in background
// Run remote ops daemon in background
g.Go(func() error {
// give local agent unlimited access to services
agentCtx := internal.AddSubjectToContext(ctx, &internal.Superuser{Username: "local-agent"})
if err := d.agent.Start(agentCtx); err != nil {
return fmt.Errorf("agent terminated: %w", err)
// give daemon unlimited access to services
daemonCtx := internal.AddSubjectToContext(ctx, &internal.Superuser{Username: "remoteops-daemon"})
if err := d.opsDaemon.Start(daemonCtx); err != nil {
return fmt.Errorf("remote ops daemon terminated: %w", err)
}
return nil
})
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package integration
import (
"testing"

"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/daemon"
"github.com/leg100/otf/internal/remoteops"
"github.com/leg100/otf/internal/sql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -30,7 +30,7 @@ func TestCluster(t *testing.T) {

// start agent, instructing it to connect to otfd2,
// add --debug flag, which dumps info that this test relies upon
otfd2.startAgent(t, ctx, org.Name, agent.ExternalConfig{Config: agent.Config{Debug: true}})
otfd2.startAgent(t, ctx, org.Name, remoteops.AgentConfig{Config: remoteops.Config{Debug: true}})

// create root module, setting otfd1 as hostname
root := newRootModule(t, otfd1.Hostname(), org.Name, "dev")
Expand Down
6 changes: 3 additions & 3 deletions internal/integration/daemon_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/google/uuid"
"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/auth"
"github.com/leg100/otf/internal/cli"
"github.com/leg100/otf/internal/configversion"
Expand All @@ -23,6 +22,7 @@ import (
"github.com/leg100/otf/internal/organization"
"github.com/leg100/otf/internal/pubsub"
"github.com/leg100/otf/internal/releases"
"github.com/leg100/otf/internal/remoteops"
"github.com/leg100/otf/internal/run"
"github.com/leg100/otf/internal/sql"
"github.com/leg100/otf/internal/state"
Expand Down Expand Up @@ -419,7 +419,7 @@ func (s *testDaemon) createAgentToken(t *testing.T, ctx context.Context, organiz

// startAgent starts an external agent, configuring it with the given
// organization and configuring it to connect to the daemon.
func (s *testDaemon) startAgent(t *testing.T, ctx context.Context, organization string, cfg agent.ExternalConfig) {
func (s *testDaemon) startAgent(t *testing.T, ctx context.Context, organization string, cfg remoteops.AgentConfig) {
t.Helper()

// Configure logger; discard logs by default
Expand All @@ -436,7 +436,7 @@ func (s *testDaemon) startAgent(t *testing.T, ctx context.Context, organization
cfg.APIConfig.Token = string(token)
cfg.APIConfig.Address = s.Hostname()

agent, err := agent.NewExternalAgent(ctx, logger, cfg)
agent, err := remoteops.NewAgent(ctx, logger, cfg)
require.NoError(t, err)

ctx, cancel := context.WithCancel(ctx)
Expand Down
6 changes: 3 additions & 3 deletions internal/integration/run_cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"

"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/releases"
"github.com/leg100/otf/internal/remoteops"
"github.com/leg100/otf/internal/variable"
"github.com/leg100/otf/internal/workspace"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -49,8 +49,8 @@ func TestIntegration_RunCancel(t *testing.T) {

// start an external agent (it's the only way to specify a separate bin
// directory currently).
daemon.startAgent(t, ctx, org.Name, agent.ExternalConfig{
Config: agent.Config{TerraformBinDir: bins},
daemon.startAgent(t, ctx, org.Name, remoteops.AgentConfig{
Config: remoteops.Config{TerraformBinDir: bins},
})

// create workspace specifying that it use an external agent.
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/run_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/leg100/otf/internal"
"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/remoteops"
"github.com/leg100/otf/internal/run"
"github.com/leg100/otf/internal/workspace"
"github.com/stretchr/testify/require"
Expand All @@ -24,7 +24,7 @@ func TestRunError(t *testing.T) {

// create a daemon and start an agent
daemon, org, ctx := setup(t, nil)
daemon.startAgent(t, ctx, org.Name, agent.ExternalConfig{})
daemon.startAgent(t, ctx, org.Name, remoteops.AgentConfig{})

// two tests: one run on the daemon, one via the agent.
tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os/exec"
"testing"

"github.com/leg100/otf/internal/agent"
"github.com/leg100/otf/internal/daemon"
"github.com/leg100/otf/internal/remoteops"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -23,7 +23,7 @@ func TestSandbox(t *testing.T) {
require.NoError(t, err)

daemon, org, ctx := setup(t, &config{Config: daemon.Config{
AgentConfig: &agent.Config{
RemoteOpsConfig: &remoteops.Config{
Sandbox: true,
Debug: true,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/logs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (a *api) addHandlers(r *mux.Router) {
signed.Use(internal.VerifySignedURL(a.Verifier))
signed.HandleFunc("/runs/{run_id}/logs/{phase}", a.getLogs).Methods("GET")

// client is typically an external agent
// client is typically otf-agent
r = r.PathPrefix(otfapi.DefaultBasePath).Subrouter()
r.HandleFunc("/runs/{run_id}/logs/{phase}", a.putLogs).Methods("PUT")
}
Expand Down
24 changes: 11 additions & 13 deletions internal/agent/client.go → internal/remoteops/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agent
package remoteops

import (
"context"
Expand All @@ -17,12 +17,12 @@ import (
)

var (
_ client = (*LocalClient)(nil)
_ client = (*remoteClient)(nil)
_ client = (*InProcClient)(nil)
_ client = (*rpcClient)(nil)
)

type (
// client allows the agent to communicate with the server endpoints.
// client allows the daemon to communicate with the server endpoints.
client interface {
GetWorkspace(ctx context.Context, workspaceID string) (*workspace.Workspace, error)
ListEffectiveVariables(ctx context.Context, runID string) ([]*variable.Variable, error)
Expand All @@ -43,8 +43,8 @@ type (
internal.PutChunkService
}

// LocalClient is the client for an internal agent.
LocalClient struct {
// InProcClient is a client for in-process communication with the server.
InProcClient struct {
tokens.TokensService
variable.VariableService
state.StateService
Expand All @@ -55,8 +55,8 @@ type (
logs.LogsService
}

// remoteClient is the client for an external agent.
remoteClient struct {
// rpcClient is a client for communication via RPC with the server.
rpcClient struct {
*otfapi.Client
otfapi.Config

Expand All @@ -78,15 +78,13 @@ type (
logsClient = logs.Client
)

// New constructs a client that uses http to remotely invoke OTF
// services.
func newClient(config ExternalConfig) (*remoteClient, error) {
// New constructs a client that uses RPC to remotely invoke OTF services.
func newClient(config AgentConfig) (*rpcClient, error) {
api, err := otfapi.NewClient(config.APIConfig)
if err != nil {
return nil, err
}

return &remoteClient{
return &rpcClient{
Client: api,
stateClient: &stateClient{Client: api},
configClient: &configClient{Client: api},
Expand Down
Loading

0 comments on commit dad555a

Please sign in to comment.