-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(worker): add ability to manage workers (#430)
- Loading branch information
1 parent
4612e32
commit 878f13c
Showing
31 changed files
with
1,868 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.