Skip to content

Commit 7c4c6a0

Browse files
authored
Add tink-agent binary (#762)
_Depends on #734_ This PR adds the tink-agent binary to that includes minimal configuration to enable launching an agent with connectivity over gRPC to a server.
2 parents c25eb48 + bc84381 commit 7c4c6a0

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ help: ## Print this help
4949
VERSION ?= $(shell git rev-parse --short HEAD)
5050

5151
# Define all the binaries we build for this project that get packaged into containers.
52-
BINARIES := tink-server tink-worker tink-controller virtual-worker
52+
BINARIES := tink-server tink-agent tink-worker tink-controller virtual-worker
5353

5454
.PHONY: build
5555
build: $(BINARIES) ## Build all tink binaries. Cross build by setting GOOS and GOARCH.

cmd/tink-agent/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM alpine:3.15
2+
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
RUN apk add --no-cache --update --upgrade ca-certificates
7+
8+
COPY bin/tink-agent-${TARGETOS}-${TARGETARCH} /usr/bin/tink-agent
9+
10+
ENTRYPOINT ["/usr/bin/tink-agent"]

cmd/tink-agent/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/tinkerbell/tink/internal/cli"
7+
)
8+
9+
func main() {
10+
if err := cli.NewAgent().Execute(); err != nil {
11+
os.Exit(-1)
12+
}
13+
}

internal/agent/run.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ func (agent *Agent) run(ctx context.Context, wflw workflow.Workflow, events even
2626
log := agent.Log.WithValues("workflow_id", wflw.ID)
2727

2828
workflowStart := time.Now()
29-
log.Info("Start workflow")
29+
log.Info("Starting workflow")
3030

3131
for _, action := range wflw.Actions {
3232
log := log.WithValues("action_id", action.ID, "action_name", action.Name)
3333

3434
actionStart := time.Now()
35-
log.Info("Start action")
35+
log.Info("Starting action")
3636

3737
started := event.ActionStarted{
3838
ActionID: action.ID,
@@ -79,10 +79,10 @@ func (agent *Agent) run(ctx context.Context, wflw workflow.Workflow, events even
7979
return
8080
}
8181

82-
log.Info("Finish action", "duration", time.Since(actionStart).String())
82+
log.Info("Finished action", "duration", time.Since(actionStart).String())
8383
}
8484

85-
log.Info("Finish workflow", "duration", time.Since(workflowStart).String())
85+
log.Info("Finished workflow", "duration", time.Since(workflowStart).String())
8686
}
8787

8888
func extractReason(log logr.Logger, err error) string {

internal/cli/agent.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-logr/zapr"
7+
"github.com/spf13/cobra"
8+
"github.com/tinkerbell/tink/internal/agent"
9+
"github.com/tinkerbell/tink/internal/agent/runtime"
10+
"github.com/tinkerbell/tink/internal/agent/transport"
11+
"github.com/tinkerbell/tink/internal/proto/workflow/v2"
12+
"go.uber.org/zap"
13+
"google.golang.org/grpc"
14+
)
15+
16+
// NewAgent builds a command that launches the agent component.
17+
func NewAgent() *cobra.Command {
18+
var opts struct {
19+
AgentID string
20+
TinkServerAddr string
21+
}
22+
23+
// TODO(chrisdoherty4) Handle signals
24+
cmd := cobra.Command{
25+
Use: "tink-agent",
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
zl, err := zap.NewProduction()
28+
if err != nil {
29+
return fmt.Errorf("init logger: %w", err)
30+
}
31+
logger := zapr.NewLogger(zl)
32+
33+
rntime, err := runtime.NewDocker()
34+
if err != nil {
35+
return fmt.Errorf("create runtime: %w", err)
36+
}
37+
38+
conn, err := grpc.DialContext(cmd.Context(), opts.TinkServerAddr)
39+
if err != nil {
40+
return fmt.Errorf("dial tink server: %w", err)
41+
}
42+
defer conn.Close()
43+
trnport := transport.NewGRPC(logger, workflow.NewWorkflowServiceClient(conn))
44+
45+
return (&agent.Agent{
46+
Log: logger,
47+
ID: opts.AgentID,
48+
Transport: trnport,
49+
Runtime: rntime,
50+
}).Start(cmd.Context())
51+
},
52+
}
53+
54+
flgs := cmd.Flags()
55+
flgs.StringVar(&opts.AgentID, "agent-id", "", "An ID that uniquely identifies the agent instance")
56+
flgs.StringVar(&opts.TinkServerAddr, "tink-server-addr", "127.0.0.1:42113", "Tink server address")
57+
58+
return &cmd
59+
}

0 commit comments

Comments
 (0)