Consolidate controller bootstrap login in run command (#38)

This commit is contained in:
Fedor Korotkov
2023-03-21 15:36:55 -04:00
committed by GitHub
parent 10f56bb5e3
commit 9b5ad09841
7 changed files with 68 additions and 95 deletions
+38 -15
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/cirruslabs/orchard/internal/controller"
"github.com/cirruslabs/orchard/internal/netconstants"
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
"github.com/spf13/cobra"
"go.uber.org/zap"
"os"
@@ -30,6 +31,20 @@ func newRunCommand() *cobra.Command {
cmd.PersistentFlags().StringVarP(&address, "listen", "l", fmt.Sprintf(":%s", port), "address to listen on")
// flags for auto-init if necessary
// this simplifies the user experience to run the controller in serverless environments
cmd.PersistentFlags().StringVar(&controllerCertPath, "controller-cert", "",
"use the controller certificate from the specified path instead of the auto-generated one"+
" (requires --controller-key)")
cmd.PersistentFlags().StringVar(&controllerKeyPath, "controller-key", "",
"use the controller certificate key from the specified path instead of the auto-generated one"+
" (requires --controller-cert)")
cmd.PersistentFlags().StringVar(&serviceAccountName, "superuser-account-name", "",
"optional name of a service account with maximum privileges to auto-create")
cmd.PersistentFlags().StringVar(&serviceAccountToken, "superuser-account-token", "",
"token to use when creating a service account with maximum privileges "+
"(required when --admin-account-name is specified)")
return cmd
}
@@ -51,22 +66,20 @@ func runController(cmd *cobra.Command, args []string) (err error) {
return err
}
initialized, err := dataDir.Initialized()
if err != nil {
return err
var controllerCert tls.Certificate
if dataDir.ControllerCertificateExists() {
controllerCert, err = dataDir.ControllerCertificate()
if err != nil {
return err
}
} else {
controllerCert, err = FindControllerCertificate(dataDir)
if err != nil {
return err
}
}
if !initialized {
return fmt.Errorf("%w: data directory is not initialized, please run \"orchard controller init\" first",
ErrRunFailed)
}
controllerCert, err := dataDir.ControllerCertificate()
if err != nil {
return err
}
controller, err := controller.New(
controllerInstance, err := controller.New(
controller.WithListenAddr(address),
controller.WithDataDir(dataDir),
controller.WithLogger(logger),
@@ -81,5 +94,15 @@ func runController(cmd *cobra.Command, args []string) (err error) {
return err
}
return controller.Run(cmd.Context())
if serviceAccountName != "" {
err = controllerInstance.EnsureServiceAccount(&v1.ServiceAccount{
Meta: v1.Meta{
Name: serviceAccountName,
},
Token: serviceAccountToken,
Roles: v1.AllServiceAccountRoles(),
})
}
return controllerInstance.Run(cmd.Context())
}