-
Notifications
You must be signed in to change notification settings - Fork 35
/
check.go
129 lines (109 loc) · 4.04 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Edgeless Systems GmbH.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/edgelesssys/marblerun/cli/internal/helm"
"github.com/edgelesssys/marblerun/cli/internal/kube"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// NewCheckCmd returns the check command.
func NewCheckCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check the status of MarbleRun's control plane",
Long: `Check the status of MarbleRun's control plane`,
Args: cobra.NoArgs,
RunE: runCheck,
}
cmd.Flags().Uint("timeout", 60, "Time to wait before aborting in seconds")
cmd.Flags().String("namespace", helm.Namespace, "Namespace MarbleRun is deployed to")
return cmd
}
func runCheck(cmd *cobra.Command, _ []string) error {
kubeClient, err := kube.NewClient()
if err != nil {
return err
}
timeout, err := cmd.Flags().GetUint("timeout")
if err != nil {
return err
}
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
return cliCheck(cmd, kubeClient, namespace, timeout)
}
// cliCheck if MarbleRun control-plane deployments are ready to use.
func cliCheck(cmd *cobra.Command, kubeClient kubernetes.Interface, namespace string, timeout uint) error {
if err := checkDeploymentStatus(cmd, kubeClient, helm.InjectorDeployment, namespace, timeout); err != nil {
return err
}
return checkDeploymentStatus(cmd, kubeClient, helm.CoordinatorDeployment, namespace, timeout)
}
// checkDeploymentStatus checks if a deployment is installed on the cluster.
// If it is, this function will wait until all replicas have the "available" status (ready for at least minReadySeconds).
// Current status is continuously printed on screen.
func checkDeploymentStatus(cmd *cobra.Command, kubeClient kubernetes.Interface, deploymentName string, namespace string, timeout uint) error {
_, err := kubeClient.AppsV1().Deployments(namespace).Get(cmd.Context(), deploymentName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}
if errors.IsNotFound(err) {
cmd.Printf("%s is not installed on this cluster\n", deploymentName)
} else {
deploymentReady := false
var tries uint
var podsReady string
// check if command is run from a terminal
isTTY := false
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
isTTY = true
// save current cursor position
cmd.Print("\033[s")
}
for !deploymentReady && tries < timeout {
deploymentReady, podsReady, err = deploymentIsReady(cmd.Context(), kubeClient, deploymentName, namespace)
if err != nil {
return err
}
// if command was run from a terminal we can print continuous updates on the same line
if isTTY {
// return cursor and clear line
cmd.Print("\033[u\033[K")
cmd.Printf("%s pods ready: %s", deploymentName, podsReady)
} else {
cmd.Printf("%s pods ready: %s\n", deploymentName, podsReady)
}
tries++
time.Sleep(1 * time.Second)
}
cmd.Println()
if tries == timeout {
return fmt.Errorf("deployment %s was not ready after %d seconds (%s pods available) ", deploymentName, timeout, podsReady)
}
}
return nil
}
// deploymentIsReady checks on the status of a single deployment and returns the number of available pods in the form "available/total".
func deploymentIsReady(ctx context.Context, kubeClient kubernetes.Interface, deploymentName string, namespace string) (bool, string, error) {
deployment, err := kubeClient.AppsV1().Deployments(namespace).Get(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
return false, "", err
}
status := fmt.Sprintf("%d/%d", deployment.Status.AvailableReplicas, deployment.Status.Replicas)
if deployment.Status.Replicas == deployment.Status.AvailableReplicas {
return true, status, nil
}
return false, status, nil
}