Skip to content

Commit

Permalink
feat(worker): add ability to manage workers (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3rw3rk authored Apr 14, 2023
1 parent 4612e32 commit 878f13c
Show file tree
Hide file tree
Showing 31 changed files with 1,868 additions and 5 deletions.
2 changes: 1 addition & 1 deletion action/repo/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/sirupsen/logrus"
)

// Get captures a list of deployments based off the provided configuration.
// Get captures a list of repositories based off the provided configuration.
func (c *Config) Get(client *vela.Client) error {
logrus.Debug("executing get for repo configuration")

Expand Down
69 changes: 69 additions & 0 deletions action/worker/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package worker

import (
"fmt"

"github.com/go-vela/cli/internal/output"

"github.com/go-vela/sdk-go/vela"

"github.com/sirupsen/logrus"
)

// Add creates a worker based off the provided configuration.
func (c *Config) Add(client *vela.Client) error {
logrus.Debug("executing add for worker configuration")

// send API call to get a registration token for the given worker
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela#AdminWorkerService.RegisterToken
registerToken, _, err := client.Admin.Worker.RegisterToken(c.Hostname)
if err != nil || registerToken == nil {
return fmt.Errorf("unable to retrieve registration token: %w", err)
}

logrus.Tracef("got registration token, adding worker %q", c.Hostname)

// send API call to register a worker
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela#AdminWorkerService.Register
msg, _, err := client.Admin.Worker.Register(c.Address, registerToken.GetToken())
if err != nil {
return fmt.Errorf("unable to register worker at %q: %w", c.Address, err)
}

logrus.Tracef("worker %q registered", c.Hostname)

// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the worker in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(msg)
case output.DriverJSON:
// output the worker in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(msg)
case output.DriverSpew:
// output the worker in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(msg)
case output.DriverYAML:
// output the worker in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(msg)
default:
// output the worker in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(*msg)
}
}
120 changes: 120 additions & 0 deletions action/worker/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package worker

import (
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/mock/server"
"github.com/go-vela/worker/mock/worker"

"github.com/go-vela/sdk-go/vela"
)

func TestWorker_Config_Add(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

// create mock server
s := httptest.NewServer(server.FakeHandler())

// create a vela client
client, err := vela.NewClient(s.URL, "vela", nil)
if err != nil {
t.Errorf("unable to create client: %v", err)
}

// create mock worker server
w := httptest.NewServer(worker.FakeHandler())

// setup tests
tests := []struct {
failure bool
config *Config
}{
{
failure: false,
config: &Config{
Action: "add",
Hostname: "MyWorker",
Address: w.URL,
Output: "",
},
},
{
failure: false,
config: &Config{
Action: "add",
Hostname: "MyWorker",
Address: w.URL,
Output: "dump",
},
},
{
failure: false,
config: &Config{
Action: "add",
Hostname: "MyWorker",
Address: w.URL,
Output: "json",
},
},
{
failure: false,
config: &Config{
Action: "add",
Hostname: "MyWorker",
Address: w.URL,
Output: "spew",
},
},
{
failure: false,
config: &Config{
Action: "add",
Hostname: "MyWorker",
Address: w.URL,
Output: "yaml",
},
},
{
failure: true,
config: &Config{
Action: "add",
Hostname: "",
Address: w.URL,
Output: "yaml",
},
},
{
failure: true,
config: &Config{
Action: "add",
Hostname: "MyWorker",
Address: "",
Output: "yaml",
},
},
}

// run tests
for _, test := range tests {
err := test.config.Add(client)

if test.failure {
if err == nil {
t.Errorf("Add should have returned err")
}

continue
}

if err != nil {
t.Errorf("Add returned err: %v", err)
}
}
}
10 changes: 10 additions & 0 deletions action/worker/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package worker provides the defined CLI worker actions for Vela.
//
// Usage:
//
// import "github.com/go-vela/cli/action/worker"
package worker
58 changes: 58 additions & 0 deletions action/worker/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package worker

import (
"github.com/go-vela/cli/internal/output"

"github.com/go-vela/sdk-go/vela"

"github.com/sirupsen/logrus"
)

// Get captures a list of workers based off the provided configuration.
func (c *Config) Get(client *vela.Client) error {
logrus.Debug("executing get for worker configuration")

logrus.Tracef("capturing workers")

// send API call to capture a list of workers
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#WorkerService.GetAll
workers, _, err := client.Worker.GetAll()
if err != nil {
return err
}

// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the workers in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(workers)
case output.DriverJSON:
// output the workers in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(workers)
case output.DriverSpew:
// output the workers in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(workers)
case "wide":
// output the workers in wide table format
return wideTable(workers)
case output.DriverYAML:
// output the workers in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(workers)
default:
// output the workers in table format
return table(workers)
}
}
92 changes: 92 additions & 0 deletions action/worker/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package worker

import (
"net/http/httptest"
"testing"

"github.com/go-vela/server/mock/server"

"github.com/go-vela/sdk-go/vela"
)

func TestWorker_Config_Get(t *testing.T) {
// setup test server
s := httptest.NewServer(server.FakeHandler())

// create a vela client
client, err := vela.NewClient(s.URL, "vela", nil)
if err != nil {
t.Errorf("unable to create client: %v", err)
}

// setup tests
tests := []struct {
failure bool
config *Config
}{
{
failure: false,
config: &Config{
Action: "get",
Output: "",
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "dump",
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "json",
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "spew",
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "wide",
},
},
{
failure: false,
config: &Config{
Action: "get",
Output: "yaml",
},
},
// TODO: mock doesn't have failure for listing workers
}

// run tests
for _, test := range tests {
err := test.config.Get(client)

if test.failure {
if err == nil {
t.Errorf("Get should have returned err")
}

continue
}

if err != nil {
t.Errorf("Get returned err: %v", err)
}
}
}
Loading

0 comments on commit 878f13c

Please sign in to comment.