Skip to content

Commit

Permalink
fix: worker registration (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3rw3rk authored Apr 18, 2023
1 parent d77d1db commit 3bad979
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 92 deletions.
59 changes: 50 additions & 9 deletions action/worker/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
package worker

import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"

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

Expand All @@ -28,13 +33,49 @@ func (c *Config) Add(client *vela.Client) error {

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

// send API call to register a worker
// create a custom http client using the registration token received
// from .RegisterToken as a Bearer token.
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela#AdminWorkerService.Register
msg, _, err := client.Admin.Worker.Register(c.Address, registerToken.GetToken())
// we will make a call to the registration endpoint on the worker
// at the given c.Address for the worker.
workerRegistrationURL := strings.TrimSuffix(c.Address, "/")
workerRegistrationURL = fmt.Sprintf("%s/register", workerRegistrationURL)

// create a new request for the given URL (c.Address)
req, err := http.NewRequestWithContext(context.Background(), "POST", workerRegistrationURL, nil)
if err != nil {
return fmt.Errorf("unable to form request for worker registration endpoint")
}

// add the authorization header using the registration token
req.Header.Add("Authorization", "Bearer "+registerToken.GetToken())

// add the user agent for the request
req.Header.Add("User-Agent", client.UserAgent)

// create a new http client
httpClient := http.DefaultClient
httpClient.Timeout = time.Second * 15

// perform the request to the worker registration endpoint
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to register worker at %q: %w", c.Address, err)
return fmt.Errorf("error on response to worker registration endpoint")
}
defer resp.Body.Close()

// read the body response
_, err = io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unable to read response body for worker registration call")
}

// if the call was successful but didn't register successfully
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error while registering worker at %q, received: %d", workerRegistrationURL, resp.StatusCode)
}

out := fmt.Sprintf("worker %q registered successfully", c.Hostname)

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

Expand All @@ -44,26 +85,26 @@ func (c *Config) Add(client *vela.Client) error {
// output the worker in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(msg)
return output.Dump(out)
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)
return output.JSON(out)
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)
return output.Spew(out)
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)
return output.YAML(out)
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)
return output.Stdout(out)
}
}
53 changes: 26 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/gin-gonic/gin v1.9.0
github.com/go-git/go-git/v5 v5.6.1
github.com/go-vela/sdk-go v0.18.2-0.20230407134447-c34cd778f44a
github.com/go-vela/server v0.18.2-0.20230407143935-3335f6a0134a
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c
github.com/go-vela/worker v0.18.2-0.20230406165141-c76c2e460786
github.com/go-vela/sdk-go v0.19.0-rc1
github.com/go-vela/server v0.19.0-rc1
github.com/go-vela/types v0.19.0-rc1
github.com/go-vela/worker v0.19.0-rc1
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/gosuri/uitable v0.0.4
github.com/manifoldco/promptui v0.9.0
Expand Down Expand Up @@ -45,7 +45,7 @@ require (
github.com/docker/docker v20.10.24+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/drone/envsubst v1.0.3 // indirect
github.com/drone/envsubst/v2 v2.0.0-20210730161058-179042472c46 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
Expand All @@ -55,19 +55,18 @@ require (
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-github/v50 v50.1.0 // indirect
github.com/google/go-github/v51 v51.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand All @@ -84,7 +83,7 @@ require (
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand All @@ -110,27 +109,27 @@ require (
github.com/ugorji/go/codec v1.2.9 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.starlark.net v0.0.0-20230228032650-dded03209ead // indirect
go.starlark.net v0.0.0-20230302034142-4b1e35fe2254 // indirect
golang.org/x/arch v0.1.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
k8s.io/api v0.26.1 // indirect
k8s.io/apimachinery v0.26.2 // indirect
k8s.io/client-go v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
k8s.io/api v0.27.1 // indirect
k8s.io/apimachinery v0.27.1 // indirect
k8s.io/client-go v0.27.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 3bad979

Please sign in to comment.