2023-03-24 06:51:18 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-07 11:02:01 -04:00
|
|
|
"errors"
|
2023-03-24 06:51:18 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/helm"
|
2023-08-21 10:15:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/kubecmd"
|
2023-07-07 11:02:01 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2023-03-24 06:51:18 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
2023-07-07 11:02:01 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-03-24 06:51:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewStatusCmd returns a new cobra.Command for the statuus command.
|
|
|
|
func NewStatusCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "status",
|
2023-07-05 10:44:57 -04:00
|
|
|
Short: "Show status of a Constellation cluster",
|
|
|
|
Long: "Show the status of a constellation cluster.\n\n" +
|
|
|
|
"Shows microservice, image, and Kubernetes versions installed in the cluster. Also shows status of current version upgrades.",
|
2023-03-24 06:51:18 -04:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: runStatus,
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// runStatus runs the terminate command.
|
2023-04-05 12:40:35 -04:00
|
|
|
func runStatus(cmd *cobra.Command, _ []string) error {
|
2023-03-24 06:51:18 -04:00
|
|
|
log, err := newCLILogger(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating logger: %w", err)
|
|
|
|
}
|
|
|
|
defer log.Sync()
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
flags, err := parseStatusFlags(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:51:18 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
|
|
|
|
2023-08-24 10:40:47 -04:00
|
|
|
helmClient, err := helm.NewReleaseVersionClient(constants.AdminConfFilename, log)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up helm client: %w", err)
|
|
|
|
}
|
2023-07-27 10:14:36 -04:00
|
|
|
helmVersionGetter := func() (fmt.Stringer, error) {
|
|
|
|
return helmClient.Versions()
|
|
|
|
}
|
2023-03-24 06:51:18 -04:00
|
|
|
|
2023-07-07 11:02:01 -04:00
|
|
|
fetcher := attestationconfigapi.NewFetcher()
|
2023-08-04 07:53:51 -04:00
|
|
|
conf, err := config.New(fileHandler, constants.ConfigFilename, fetcher, flags.force)
|
2023-07-07 11:02:01 -04:00
|
|
|
var configValidationErr *config.ValidationError
|
|
|
|
if errors.As(err, &configValidationErr) {
|
|
|
|
cmd.PrintErrln(configValidationErr.LongMessage())
|
|
|
|
}
|
|
|
|
variant := conf.GetAttestationConfig().GetVariant()
|
|
|
|
|
2023-08-24 10:40:47 -04:00
|
|
|
kubeClient, err := kubecmd.New(cmd.OutOrStdout(), constants.AdminConfFilename, fileHandler, log)
|
2023-08-08 07:03:23 -04:00
|
|
|
if err != nil {
|
2023-08-21 10:15:32 -04:00
|
|
|
return fmt.Errorf("setting up kubernetes client: %w", err)
|
2023-08-08 07:03:23 -04:00
|
|
|
}
|
2023-08-21 10:15:32 -04:00
|
|
|
|
|
|
|
output, err := status(cmd.Context(), helmVersionGetter, kubeClient, variant)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Print(output)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// status queries the cluster for the relevant status information and returns the output string.
|
2023-08-21 10:15:32 -04:00
|
|
|
func status(ctx context.Context, getHelmVersions func() (fmt.Stringer, error), kubeClient kubeCmd, attestVariant variant.Variant,
|
2023-07-27 10:14:36 -04:00
|
|
|
) (string, error) {
|
2023-08-21 10:15:32 -04:00
|
|
|
nodeVersion, err := kubeClient.GetConstellationVersion(ctx)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting constellation version: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
attestationConfig, err := kubeClient.GetClusterAttestationConfig(ctx, attestVariant)
|
2023-07-07 11:02:01 -04:00
|
|
|
if err != nil {
|
2023-08-08 07:03:23 -04:00
|
|
|
return "", fmt.Errorf("getting attestation config: %w", err)
|
2023-07-07 11:02:01 -04:00
|
|
|
}
|
|
|
|
prettyYAML, err := yaml.Marshal(attestationConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("marshalling attestation config: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-27 10:14:36 -04:00
|
|
|
serviceVersions, err := getHelmVersions()
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting service versions: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
status, err := kubeClient.ClusterStatus(ctx)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting cluster status: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
return statusOutput(nodeVersion, serviceVersions, status, string(prettyYAML)), nil
|
2023-08-08 07:03:23 -04:00
|
|
|
}
|
|
|
|
|
2023-03-24 06:51:18 -04:00
|
|
|
// statusOutput creates the status cmd output string by formatting the received information.
|
2023-07-27 10:14:36 -04:00
|
|
|
func statusOutput(
|
2023-08-21 10:15:32 -04:00
|
|
|
nodeVersion kubecmd.NodeVersion, serviceVersions fmt.Stringer,
|
|
|
|
status map[string]kubecmd.NodeStatus, rawAttestationConfig string,
|
2023-07-27 10:14:36 -04:00
|
|
|
) string {
|
2023-03-24 06:51:18 -04:00
|
|
|
builder := strings.Builder{}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
builder.WriteString(targetVersionsString(nodeVersion))
|
2023-07-27 10:14:36 -04:00
|
|
|
builder.WriteString(serviceVersions.String())
|
2023-08-21 10:15:32 -04:00
|
|
|
builder.WriteString(fmt.Sprintf("Cluster status: %s\n", nodeVersion.ClusterStatus()))
|
|
|
|
builder.WriteString(nodeStatusString(status, nodeVersion))
|
2023-07-07 11:02:01 -04:00
|
|
|
builder.WriteString(fmt.Sprintf("Attestation config:\n%s", indentEntireStringWithTab(rawAttestationConfig)))
|
2023-03-24 06:51:18 -04:00
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:02:01 -04:00
|
|
|
func indentEntireStringWithTab(input string) string {
|
|
|
|
lines := strings.Split(input, "\n")
|
|
|
|
for i, line := range lines[:len(lines)-1] {
|
|
|
|
lines[i] = "\t" + line
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:51:18 -04:00
|
|
|
// nodeStatusString creates the node status part of the output string.
|
2023-08-21 10:15:32 -04:00
|
|
|
func nodeStatusString(status map[string]kubecmd.NodeStatus, targetVersions kubecmd.NodeVersion) string {
|
2023-03-24 06:51:18 -04:00
|
|
|
var upToDateImages int
|
|
|
|
var upToDateK8s int
|
|
|
|
for _, node := range status {
|
2023-08-21 10:15:32 -04:00
|
|
|
if node.KubeletVersion() == targetVersions.KubernetesVersion() {
|
2023-03-24 06:51:18 -04:00
|
|
|
upToDateK8s++
|
|
|
|
}
|
2023-08-21 10:15:32 -04:00
|
|
|
if node.ImageVersion() == targetVersions.ImageReference() {
|
2023-03-24 06:51:18 -04:00
|
|
|
upToDateImages++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := strings.Builder{}
|
|
|
|
if upToDateImages != len(status) || upToDateK8s != len(status) {
|
|
|
|
builder.WriteString(fmt.Sprintf("\tImage: %d/%d\n", upToDateImages, len(status)))
|
|
|
|
builder.WriteString(fmt.Sprintf("\tKubernetes: %d/%d\n", upToDateK8s, len(status)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// targetVersionsString creates the target versions part of the output string.
|
2023-08-21 10:15:32 -04:00
|
|
|
func targetVersionsString(target kubecmd.NodeVersion) string {
|
2023-03-24 06:51:18 -04:00
|
|
|
builder := strings.Builder{}
|
|
|
|
builder.WriteString("Target versions:\n")
|
2023-08-21 10:15:32 -04:00
|
|
|
builder.WriteString(fmt.Sprintf("\tImage: %s\n", target.ImageVersion()))
|
|
|
|
builder.WriteString(fmt.Sprintf("\tKubernetes: %s\n", target.KubernetesVersion()))
|
2023-03-24 06:51:18 -04:00
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
type statusFlags struct {
|
|
|
|
workspace string
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseStatusFlags(cmd *cobra.Command) (statusFlags, error) {
|
|
|
|
workspace, err := cmd.Flags().GetString("workspace")
|
|
|
|
if err != nil {
|
|
|
|
return statusFlags{}, fmt.Errorf("getting config flag: %w", err)
|
|
|
|
}
|
|
|
|
force, err := cmd.Flags().GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return statusFlags{}, fmt.Errorf("getting config flag: %w", err)
|
|
|
|
}
|
|
|
|
return statusFlags{
|
|
|
|
workspace: workspace,
|
|
|
|
force: force,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:15:32 -04:00
|
|
|
type kubeCmd interface {
|
|
|
|
ClusterStatus(ctx context.Context) (map[string]kubecmd.NodeStatus, error)
|
|
|
|
GetConstellationVersion(ctx context.Context) (kubecmd.NodeVersion, error)
|
|
|
|
GetClusterAttestationConfig(ctx context.Context, variant variant.Variant) (config.AttestationCfg, error)
|
2023-07-07 11:02:01 -04:00
|
|
|
}
|