2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-21 11:59:12 -04:00
|
|
|
"context"
|
2022-11-25 04:02:12 -05:00
|
|
|
"errors"
|
2022-03-22 11:03:15 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-02-10 08:59:44 -05:00
|
|
|
"os"
|
2023-03-03 03:38:57 -05:00
|
|
|
"sync"
|
2022-06-21 11:59:12 -04:00
|
|
|
"time"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-05-02 03:35:52 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
clientcodec "k8s.io/client-go/tools/clientcmd/api/latest"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/initproto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/helm"
|
2023-09-25 10:19:43 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/state"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2023-06-12 07:45:34 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/grpclog"
|
2023-03-02 09:08:31 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/uri"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
// NewInitCmd returns a new cobra.Command for the init command.
|
|
|
|
func NewInitCmd() *cobra.Command {
|
2022-03-22 11:03:15 -04:00
|
|
|
cmd := &cobra.Command{
|
2022-09-07 09:38:29 -04:00
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize the Constellation cluster",
|
2023-01-17 08:01:56 -05:00
|
|
|
Long: "Initialize the Constellation cluster.\n\n" +
|
|
|
|
"Start your confidential Kubernetes.",
|
|
|
|
Args: cobra.ExactArgs(0),
|
2023-10-24 09:39:18 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
// Define flags for apply backend that are not set by init
|
|
|
|
cmd.Flags().Bool("yes", false, "")
|
|
|
|
// Don't skip any phases
|
|
|
|
// The apply backend should handle init calls correctly
|
|
|
|
cmd.Flags().StringSlice("skip-phases", []string{}, "")
|
|
|
|
cmd.Flags().Duration("timeout", time.Hour, "")
|
|
|
|
return runApply(cmd, args)
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-09-20 04:07:55 -04:00
|
|
|
cmd.Flags().Bool("conformance", false, "enable conformance mode")
|
2023-07-07 11:09:45 -04:00
|
|
|
cmd.Flags().Bool("skip-helm-wait", false, "install helm charts without waiting for deployments to be ready")
|
2023-02-10 08:59:44 -05:00
|
|
|
cmd.Flags().Bool("merge-kubeconfig", false, "merge Constellation kubeconfig file with default kubeconfig file in $HOME/.kube/config")
|
2022-03-22 11:03:15 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
type initDoer struct {
|
2023-03-20 07:44:12 -04:00
|
|
|
dialer grpcDialer
|
|
|
|
endpoint string
|
|
|
|
req *initproto.InitRequest
|
2023-05-30 07:47:36 -04:00
|
|
|
resp *initproto.InitSuccessResponse
|
2023-03-20 07:44:12 -04:00
|
|
|
log debugLog
|
|
|
|
spinner spinnerInterf
|
|
|
|
connectedOnce bool
|
2023-05-30 07:47:36 -04:00
|
|
|
fh file.Handler
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
func (d *initDoer) Do(ctx context.Context) error {
|
2023-03-20 07:44:12 -04:00
|
|
|
// connectedOnce is set in handleGRPCStateChanges when a connection was established in one retry attempt.
|
|
|
|
// This should cancel any other retry attempts when the connection is lost since the bootstrapper likely won't accept any new attempts anymore.
|
|
|
|
if d.connectedOnce {
|
2023-09-25 06:10:07 -04:00
|
|
|
return &nonRetriableError{
|
|
|
|
logCollectionErr: errors.New("init already connected to the remote server in a previous attempt - resumption is not supported"),
|
|
|
|
err: errors.New("init already connected to the remote server in a previous attempt - resumption is not supported"),
|
|
|
|
}
|
2023-03-20 07:44:12 -04:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
conn, err := d.dialer.Dial(ctx, d.endpoint)
|
|
|
|
if err != nil {
|
2023-05-30 07:47:36 -04:00
|
|
|
d.log.Debugf("Dialing init server failed: %s. Retrying...", err)
|
2022-06-21 11:59:12 -04:00
|
|
|
return fmt.Errorf("dialing init server: %w", err)
|
|
|
|
}
|
2022-07-05 08:14:11 -04:00
|
|
|
defer conn.Close()
|
2023-03-03 03:38:57 -05:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
grpcStateLogCtx, grpcStateLogCancel := context.WithCancel(ctx)
|
|
|
|
defer grpcStateLogCancel()
|
2023-03-20 07:42:48 -04:00
|
|
|
d.handleGRPCStateChanges(grpcStateLogCtx, &wg, conn)
|
2023-03-03 03:38:57 -05:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
protoClient := initproto.NewAPIClient(conn)
|
2023-01-04 04:46:29 -05:00
|
|
|
d.log.Debugf("Created protoClient")
|
2022-06-21 11:59:12 -04:00
|
|
|
resp, err := protoClient.Init(ctx, d.req)
|
2022-03-29 05:38:14 -04:00
|
|
|
if err != nil {
|
2023-09-25 06:10:07 -04:00
|
|
|
return &nonRetriableError{
|
|
|
|
logCollectionErr: errors.New("rpc failed before first response was received - no logs available"),
|
|
|
|
err: fmt.Errorf("init call: %w", err),
|
|
|
|
}
|
2022-03-29 05:38:14 -04:00
|
|
|
}
|
2023-05-30 07:47:36 -04:00
|
|
|
|
|
|
|
res, err := resp.Recv() // get first response, either success or failure
|
|
|
|
if err != nil {
|
|
|
|
if e := d.getLogs(resp); e != nil {
|
|
|
|
d.log.Debugf("Failed to collect logs: %s", e)
|
2023-09-25 06:10:07 -04:00
|
|
|
return &nonRetriableError{
|
|
|
|
logCollectionErr: e,
|
|
|
|
err: err,
|
|
|
|
}
|
2023-05-30 07:47:36 -04:00
|
|
|
}
|
2023-09-25 06:10:07 -04:00
|
|
|
return &nonRetriableError{err: err}
|
2023-05-30 07:47:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch res.Kind.(type) {
|
|
|
|
case *initproto.InitResponse_InitFailure:
|
|
|
|
if e := d.getLogs(resp); e != nil {
|
|
|
|
d.log.Debugf("Failed to get logs from cluster: %s", e)
|
2023-09-25 06:10:07 -04:00
|
|
|
return &nonRetriableError{
|
|
|
|
logCollectionErr: e,
|
|
|
|
err: errors.New(res.GetInitFailure().GetError()),
|
|
|
|
}
|
2023-05-30 07:47:36 -04:00
|
|
|
}
|
2023-09-25 06:10:07 -04:00
|
|
|
return &nonRetriableError{err: errors.New(res.GetInitFailure().GetError())}
|
2023-05-30 07:47:36 -04:00
|
|
|
case *initproto.InitResponse_InitSuccess:
|
|
|
|
d.resp = res.GetInitSuccess()
|
|
|
|
case nil:
|
|
|
|
d.log.Debugf("Cluster returned nil response type")
|
2023-09-25 06:10:07 -04:00
|
|
|
err = errors.New("empty response from cluster")
|
|
|
|
if e := d.getLogs(resp); e != nil {
|
|
|
|
d.log.Debugf("Failed to collect logs: %s", e)
|
|
|
|
return &nonRetriableError{
|
|
|
|
logCollectionErr: e,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &nonRetriableError{err: err}
|
2023-05-30 07:47:36 -04:00
|
|
|
default:
|
|
|
|
d.log.Debugf("Cluster returned unknown response type")
|
2023-09-25 06:10:07 -04:00
|
|
|
err = errors.New("unknown response from cluster")
|
|
|
|
if e := d.getLogs(resp); e != nil {
|
|
|
|
d.log.Debugf("Failed to collect logs: %s", e)
|
|
|
|
return &nonRetriableError{
|
|
|
|
logCollectionErr: e,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &nonRetriableError{err: err}
|
2023-05-30 07:47:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *initDoer) getLogs(resp initproto.API_InitClient) error {
|
|
|
|
d.log.Debugf("Attempting to collect cluster logs")
|
|
|
|
for {
|
|
|
|
res, err := resp.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-25 06:10:07 -04:00
|
|
|
switch res.Kind.(type) {
|
|
|
|
case *initproto.InitResponse_InitFailure:
|
|
|
|
return errors.New("trying to collect logs: received init failure response, expected log response")
|
|
|
|
case *initproto.InitResponse_InitSuccess:
|
|
|
|
return errors.New("trying to collect logs: received init success response, expected log response")
|
|
|
|
case nil:
|
|
|
|
return errors.New("trying to collect logs: received nil response, expected log response")
|
|
|
|
}
|
|
|
|
|
2023-05-30 07:47:36 -04:00
|
|
|
log := res.GetLog().GetLog()
|
|
|
|
if log == nil {
|
2023-09-25 06:10:07 -04:00
|
|
|
return errors.New("received empty logs")
|
2023-05-30 07:47:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.fh.Write(constants.ErrorLog, log, file.OptAppend); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 11:59:12 -04:00
|
|
|
return nil
|
2022-03-29 05:38:14 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 07:42:48 -04:00
|
|
|
func (d *initDoer) handleGRPCStateChanges(ctx context.Context, wg *sync.WaitGroup, conn *grpc.ClientConn) {
|
2023-06-12 07:45:34 -04:00
|
|
|
grpclog.LogStateChangesUntilReady(ctx, conn, d.log, wg, func() {
|
|
|
|
d.connectedOnce = true
|
|
|
|
d.spinner.Stop()
|
|
|
|
d.spinner.Start("Initializing cluster ", false)
|
|
|
|
})
|
2023-03-03 03:38:57 -05:00
|
|
|
}
|
|
|
|
|
2022-04-05 03:13:09 -04:00
|
|
|
func writeRow(wr io.Writer, col1 string, col2 string) {
|
|
|
|
fmt.Fprint(wr, col1, "\t", col2, "\n")
|
|
|
|
}
|
|
|
|
|
2023-02-10 08:59:44 -05:00
|
|
|
type configMerger interface {
|
|
|
|
mergeConfigs(configPath string, fileHandler file.Handler) error
|
|
|
|
kubeconfigEnvVar() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type kubeconfigMerger struct {
|
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *kubeconfigMerger) mergeConfigs(configPath string, fileHandler file.Handler) error {
|
|
|
|
constellConfig, err := clientcmd.LoadFromFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading admin kubeconfig: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
|
|
|
|
loadingRules.Precedence = []string{
|
2023-03-08 09:48:36 -05:00
|
|
|
clientcmd.RecommendedHomeFile,
|
|
|
|
configPath, // our config should overwrite the default config
|
2023-02-10 08:59:44 -05:00
|
|
|
}
|
|
|
|
c.log.Debugf("Kubeconfig file loading precedence: %v", loadingRules.Precedence)
|
|
|
|
|
2023-03-08 09:48:36 -05:00
|
|
|
// merge the kubeconfigs
|
2023-02-10 08:59:44 -05:00
|
|
|
cfg, err := loadingRules.Load()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading merged kubeconfig: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current context to the cluster we just created
|
|
|
|
cfg.CurrentContext = constellConfig.CurrentContext
|
|
|
|
c.log.Debugf("Set current context to %s", cfg.CurrentContext)
|
|
|
|
|
|
|
|
json, err := runtime.Encode(clientcodec.Codec, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encoding merged kubeconfig: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mergedKubeconfig, err := yaml.JSONToYAML(json)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("converting merged kubeconfig to YAML: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fileHandler.Write(clientcmd.RecommendedHomeFile, mergedKubeconfig, file.OptOverwrite); err != nil {
|
|
|
|
return fmt.Errorf("writing merged kubeconfig to file: %w", err)
|
|
|
|
}
|
|
|
|
c.log.Debugf("Merged kubeconfig into default config file: %s", clientcmd.RecommendedHomeFile)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *kubeconfigMerger) kubeconfigEnvVar() string {
|
|
|
|
return os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
type grpcDialer interface {
|
|
|
|
Dial(ctx context.Context, target string) (*grpc.ClientConn, error)
|
|
|
|
}
|
2022-11-25 04:02:12 -05:00
|
|
|
|
|
|
|
type nonRetriableError struct {
|
2023-09-25 06:10:07 -04:00
|
|
|
logCollectionErr error
|
|
|
|
err error
|
2022-11-25 04:02:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the error message.
|
|
|
|
func (e *nonRetriableError) Error() string {
|
|
|
|
return e.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap returns the wrapped error.
|
|
|
|
func (e *nonRetriableError) Unwrap() error {
|
|
|
|
return e.err
|
|
|
|
}
|
2023-08-04 07:53:51 -04:00
|
|
|
|
2023-08-24 10:40:47 -04:00
|
|
|
type helmApplier interface {
|
2023-10-09 07:04:29 -04:00
|
|
|
PrepareApply(conf *config.Config, stateFile *state.State,
|
|
|
|
flags helm.Options, serviceAccURI string, masterSecret uri.MasterSecret) (
|
2023-09-25 10:19:43 -04:00
|
|
|
helm.Applier, bool, error)
|
2023-08-24 10:40:47 -04:00
|
|
|
}
|