Simplified bootstrapping of a cluster (#40)

* Simplified bootstrapping of a cluster

Introduced a new convention about a pre-defined `bootstrap-admin` account for `orchard controller run`. Providing `ORCHARD_BOOTSTRAP_ADMIN_TOKEN` will auto-create such user for easier configuration. `bootstrap-admin` can be used for creating other service accounts on the first run and after that can be disposed.

Also change `orchard worker run` to expect controller URL as the only parameter and a bootstrap token passed via an argument instead of using a context that might not be created.

* Missing error check
This commit is contained in:
Fedor Korotkov
2023-03-22 23:43:37 +04:00
committed by GitHub
parent 9b5ad09841
commit cdf5c5eb00
8 changed files with 100 additions and 39 deletions
+36 -3
View File
@@ -1,19 +1,52 @@
package worker
import (
"errors"
"github.com/cirruslabs/orchard/internal/bootstraptoken"
"github.com/cirruslabs/orchard/internal/netconstants"
"github.com/cirruslabs/orchard/internal/worker"
"github.com/cirruslabs/orchard/pkg/client"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var ErrBootstrapTokenNotProvided = errors.New("no bootstrap token provided")
var bootstrapTokenRaw string
func newRunCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "run",
RunE: runWorker,
Args: cobra.ExactArgs(1),
}
cmd.PersistentFlags().StringVar(&bootstrapTokenRaw, "bootstrap-token", "",
"a bootstrap token retrieved via `orchard get bootstrap-token <service-account-name-for-workers>`")
return cmd
}
func runWorker(cmd *cobra.Command, args []string) (err error) {
controllerURL, err := netconstants.NormalizeAddress(args[0])
if err != nil {
return err
}
if bootstrapTokenRaw == "" {
return ErrBootstrapTokenNotProvided
}
bootstrapToken, err := bootstraptoken.NewFromString(bootstrapTokenRaw)
if err != nil {
return err
}
controllerClient, err := client.New(
client.WithAddress(controllerURL.String()),
client.WithTrustedCertificate(bootstrapToken.Certificate()),
client.WithCredentials(bootstrapToken.ServiceAccountName(), bootstrapToken.ServiceAccountToken()),
)
if err != nil {
return err
}
// Initialize the logger
logger, err := zap.NewProduction()
if err != nil {
@@ -25,10 +58,10 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
}
}()
worker, err := worker.New(worker.WithDataDirPath(dataDirPath), worker.WithLogger(logger))
workerInstance, err := worker.New(worker.WithClient(controllerClient), worker.WithLogger(logger))
if err != nil {
return err
}
return worker.Run(cmd.Context())
return workerInstance.Run(cmd.Context())
}