Skip to content

Commit

Permalink
Add tink-agent binary
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Doherty <[email protected]>
  • Loading branch information
chrisdoherty4 committed Jun 29, 2023
1 parent f20a793 commit 2946cc3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ help: ## Print this help
VERSION ?= $(shell git rev-parse --short HEAD)

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

.PHONY: build
build: $(BINARIES) ## Build all tink binaries. Cross build by setting GOOS and GOARCH.
Expand Down
10 changes: 10 additions & 0 deletions cmd/tink-agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM alpine:3.15

ARG TARGETOS
ARG TARGETARCH

RUN apk add --no-cache --update --upgrade ca-certificates

COPY bin/tink-agent-${TARGETOS}-${TARGETARCH} /usr/bin/tink-agent

ENTRYPOINT ["/usr/bin/tink-agent"]
13 changes: 13 additions & 0 deletions cmd/tink-agent/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"os"

"github.com/tinkerbell/tink/internal/cli"
)

func main() {
if err := cli.NewAgent().Execute(); err != nil {
os.Exit(-1)
}
}
8 changes: 4 additions & 4 deletions internal/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func (agent *Agent) run(ctx context.Context, wflw workflow.Workflow, events even
log := agent.Log.WithValues("workflow_id", wflw.ID)

workflowStart := time.Now()
log.Info("Start workflow")
log.Info("Starting workflow")

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

actionStart := time.Now()
log.Info("Start action")
log.Info("Starting action")

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

log.Info("Finish action", "duration", time.Since(actionStart).String())
log.Info("Finished action", "duration", time.Since(actionStart).String())
}

log.Info("Finish workflow", "duration", time.Since(workflowStart).String())
log.Info("Finished workflow", "duration", time.Since(workflowStart).String())
}

func extractReason(log logr.Logger, err error) string {
Expand Down
59 changes: 59 additions & 0 deletions internal/cli/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cli

import (
"fmt"

"github.com/go-logr/zapr"
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/internal/agent"
"github.com/tinkerbell/tink/internal/agent/runtime"
"github.com/tinkerbell/tink/internal/agent/transport"
"github.com/tinkerbell/tink/internal/proto/workflow/v2"
"go.uber.org/zap"
"google.golang.org/grpc"
)

// NewAgent builds a command that launches the agent component.
func NewAgent() *cobra.Command {
var opts struct {
AgentID string
TinkServerAddr string
}

// TODO(chrisdoherty4) Handle signals
cmd := cobra.Command{
Use: "tink-agent",
RunE: func(cmd *cobra.Command, args []string) error {
zl, err := zap.NewProduction()
if err != nil {
return fmt.Errorf("init logger: %w", err)
}
logger := zapr.NewLogger(zl)

rntime, err := runtime.NewDocker()
if err != nil {
return fmt.Errorf("create runtime: %w", err)
}

conn, err := grpc.DialContext(cmd.Context(), opts.TinkServerAddr)
if err != nil {
return fmt.Errorf("dial tink server: %w", err)
}
defer conn.Close()
trnport := transport.NewGRPC(logger, workflow.NewWorkflowServiceClient(conn))

return (&agent.Agent{
Log: logger,
ID: opts.AgentID,
Transport: trnport,
Runtime: rntime,
}).Start(cmd.Context())
},
}

flgs := cmd.Flags()
flgs.StringVar(&opts.AgentID, "agent-id", "", "An ID that uniquely identifies the agent instance")
flgs.StringVar(&opts.TinkServerAddr, "tink-server-addr", "127.0.0.1:42113", "Tink server address")

return &cmd
}

0 comments on commit 2946cc3

Please sign in to comment.