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-03-30 10:13:14 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/kubernetes"
|
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/edgelesssys/constellation/v2/internal/kubernetes/kubectl"
|
|
|
|
"github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
|
|
|
"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
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
kubeClient := kubectl.New()
|
|
|
|
|
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())
|
|
|
|
kubeConfig, err := fileHandler.Read(constants.AdminConfFilename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading admin.conf: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// need kubectl client to fetch nodes.
|
|
|
|
if err := kubeClient.Initialize(kubeConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating k8s client config from kubeconfig: %w", err)
|
|
|
|
}
|
|
|
|
// need unstructed client to fetch NodeVersion CRD.
|
|
|
|
unstructuredClient, err := dynamic.NewForConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up custom resource client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// need helm client to fetch service versions.
|
2023-08-04 07:53:51 -04:00
|
|
|
// The client used here, doesn't need to know the current workspace.
|
|
|
|
// It may be refactored in the future for easier usage.
|
|
|
|
helmClient, err := helm.NewUpgradeClient(kubectl.New(), constants.UpgradeDir, constants.AdminConfFilename, constants.HelmNamespace, 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-08 07:03:23 -04:00
|
|
|
stableClient, err := kubernetes.NewStableClient(constants.AdminConfFilename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("setting up stable client: %w", err)
|
|
|
|
}
|
2023-07-27 10:14:36 -04:00
|
|
|
output, err := status(cmd.Context(), kubeClient, stableClient, helmVersionGetter, kubernetes.NewNodeVersionClient(unstructuredClient), 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-07-27 10:14:36 -04:00
|
|
|
func status(
|
|
|
|
ctx context.Context, kubeClient kubeClient, cmClient configMapClient, getHelmVersions func() (fmt.Stringer, error),
|
|
|
|
dynamicInterface kubernetes.DynamicInterface, attestVariant variant.Variant,
|
|
|
|
) (string, error) {
|
2023-03-30 10:13:14 -04:00
|
|
|
nodeVersion, err := kubernetes.GetConstellationVersion(ctx, dynamicInterface)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting constellation version: %w", err)
|
|
|
|
}
|
|
|
|
if len(nodeVersion.Status.Conditions) != 1 {
|
|
|
|
return "", fmt.Errorf("expected exactly one condition, got %d", len(nodeVersion.Status.Conditions))
|
|
|
|
}
|
|
|
|
|
2023-08-08 07:03:23 -04:00
|
|
|
attestationConfig, err := getAttestationConfig(ctx, cmClient, 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-03-30 10:13:14 -04:00
|
|
|
targetVersions, err := kubernetes.NewTargetVersions(nodeVersion)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting configured versions: %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-03-30 10:13:14 -04:00
|
|
|
status, err := kubernetes.ClusterStatus(ctx, kubeClient)
|
2023-03-24 06:51:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting cluster status: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:02:01 -04:00
|
|
|
return statusOutput(targetVersions, serviceVersions, status, nodeVersion, string(prettyYAML)), nil
|
2023-03-24 06:51:18 -04:00
|
|
|
}
|
|
|
|
|
2023-08-08 07:03:23 -04:00
|
|
|
func getAttestationConfig(ctx context.Context, cmClient configMapClient, attestVariant variant.Variant) (config.AttestationCfg, error) {
|
|
|
|
joinConfig, err := cmClient.GetCurrentConfigMap(ctx, constants.JoinConfigMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting current config map: %w", err)
|
|
|
|
}
|
|
|
|
rawAttestationConfig, ok := joinConfig.Data[constants.AttestationConfigFilename]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("attestationConfig not found in %s", constants.JoinConfigMap)
|
|
|
|
}
|
|
|
|
attestationConfig, err := config.UnmarshalAttestationConfig([]byte(rawAttestationConfig), attestVariant)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshalling attestation config: %w", err)
|
|
|
|
}
|
|
|
|
return attestationConfig, nil
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
targetVersions kubernetes.TargetVersions, serviceVersions fmt.Stringer,
|
|
|
|
status map[string]kubernetes.NodeStatus, nodeVersion v1alpha1.NodeVersion, rawAttestationConfig string,
|
|
|
|
) string {
|
2023-03-24 06:51:18 -04:00
|
|
|
builder := strings.Builder{}
|
|
|
|
|
|
|
|
builder.WriteString(targetVersionsString(targetVersions))
|
2023-07-27 10:14:36 -04:00
|
|
|
builder.WriteString(serviceVersions.String())
|
2023-03-24 06:51:18 -04:00
|
|
|
builder.WriteString(fmt.Sprintf("Cluster status: %s\n", nodeVersion.Status.Conditions[0].Message))
|
|
|
|
builder.WriteString(nodeStatusString(status, targetVersions))
|
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-03-30 10:13:14 -04:00
|
|
|
func nodeStatusString(status map[string]kubernetes.NodeStatus, targetVersions kubernetes.TargetVersions) string {
|
2023-03-24 06:51:18 -04:00
|
|
|
var upToDateImages int
|
|
|
|
var upToDateK8s int
|
|
|
|
for _, node := range status {
|
|
|
|
if node.KubeletVersion() == targetVersions.Kubernetes() {
|
|
|
|
upToDateK8s++
|
|
|
|
}
|
|
|
|
if node.ImageVersion() == targetVersions.ImagePath() {
|
|
|
|
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-03-30 10:13:14 -04:00
|
|
|
func targetVersionsString(target kubernetes.TargetVersions) string {
|
2023-03-24 06:51:18 -04:00
|
|
|
builder := strings.Builder{}
|
|
|
|
builder.WriteString("Target versions:\n")
|
|
|
|
builder.WriteString(fmt.Sprintf("\tImage: %s\n", target.Image()))
|
|
|
|
builder.WriteString(fmt.Sprintf("\tKubernetes: %s\n", target.Kubernetes()))
|
|
|
|
|
|
|
|
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-03-24 06:51:18 -04:00
|
|
|
type kubeClient interface {
|
|
|
|
GetNodes(ctx context.Context) ([]corev1.Node, error)
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:02:01 -04:00
|
|
|
type configMapClient interface {
|
|
|
|
GetCurrentConfigMap(ctx context.Context, name string) (*corev1.ConfigMap, error)
|
|
|
|
}
|