Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tink-agent binary #762

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the option here. It might be better to use grpc-addr complimenting the idea that we could have multiple transports. Introducing the file transport may inform us better.


return &cmd
}
Loading