/
status.go
217 lines (187 loc) · 6.18 KB
/
status.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2023 Khulnasoft, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package system
import (
"fmt"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
"github.com/khulnasoft/meshplay/meshplayctl/internal/cli/root/config"
"github.com/khulnasoft/meshplay/meshplayctl/pkg/utils"
meshkitkube "github.com/khulnasoft/meshkit/utils/kubernetes"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var verboseStatus bool
var linkDocStatus = map[string]string{
"link": "![status-usage](/assets/img/meshplayctl/status.png)",
"caption": "Usage of meshplayctl system status",
}
// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Check Meshplay status",
Long: `Check status of Meshplay and Meshplay components.`,
Example: `
// Check status of Meshplay, Meshplay adapters, Meshplay Operator and its controllers.
meshplayctl system status
// (optional) Extra data in status table
meshplayctl system status --verbose
`,
Annotations: linkDocStatus,
PreRunE: func(cmd *cobra.Command, args []string) error {
//Check prerequisite
hcOptions := &HealthCheckOptions{
IsPreRunE: true,
PrintLogs: false,
Subcommand: cmd.Use,
}
hc, err := NewHealthChecker(hcOptions)
if err != nil {
utils.Log.Error(ErrHealthCheckFailed(err))
return nil
}
// execute healthchecks
err = hc.RunPreflightHealthChecks()
if err != nil {
cmd.SilenceUsage = true
}
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New(utils.SystemLifeCycleError(fmt.Sprintf("this command takes no arguments. See '%s --help' for more information.\n", cmd.CommandPath()), "status"))
}
// Get viper instance used for context
mctlCfg, err := config.GetMeshplayCtl(viper.GetViper())
if err != nil {
utils.Log.Error(err)
return nil
}
// get the platform, channel and the version of the current context
// if a temp context is set using the -c flag, use it as the current context
if tempContext != "" {
err = mctlCfg.SetCurrentContext(tempContext)
if err != nil {
utils.Log.Error(ErrSetCurrentContext(errors.Wrap(err, "failed to set temporary context")))
return nil
}
}
currCtx, err := mctlCfg.GetCurrentContext()
if err != nil {
utils.Log.Error(ErrGetCurrentContext(err))
return nil
}
currPlatform := currCtx.GetPlatform()
ok, err := utils.AreMeshplayComponentsRunning(currPlatform)
if err != nil {
return err
}
if !ok {
log.Error("Meshplay is not running. Run `meshplayctl system start` to start Meshplay.")
return nil
}
switch currPlatform {
case "docker":
// List the running Meshplay containers
start := exec.Command("docker-compose", "-f", utils.DockerComposeFile, "ps")
outputStd, err := start.Output()
if err != nil {
return errors.Wrap(err, utils.SystemError("failed to get Meshplay status"))
}
outputString := string(outputStd)
if strings.Contains(outputString, "meshplay") {
log.Info(outputString)
}
hcOptions := &HealthCheckOptions{
PrintLogs: false,
IsPreRunE: false,
Subcommand: "status",
RunKubernetesChecks: true,
}
hc, err := NewHealthChecker(hcOptions)
if err != nil {
utils.Log.Error(ErrHealthCheckFailed(err))
return nil
}
// If k8s is available print the status of pods in the MeshplayNamespace
if err = hc.Run(); err != nil {
return nil
}
fallthrough
case "kubernetes":
// if the platform is kubernetes, use kubernetes go-client to
// display pod status in the MeshplayNamespace
// create an kubernetes client
client, err := meshkitkube.New([]byte(""))
if err != nil {
return err
}
// List the pods in the MeshplayNamespace
podList, err := utils.GetPodList(client, utils.MeshplayNamespace)
if err != nil {
return err
}
var data [][]string
columnNames := []string{"Name", "Ready", "Status", "Restarts", "Age"}
// List all the pods similar to kubectl get pods -n MeshplayNamespace
for _, pod := range podList.Items {
// Calculate the age of the pod
podCreationTime := pod.GetCreationTimestamp()
age := time.Since(podCreationTime.Time).Round(time.Second)
// Get the status of each of the pods
podStatus := pod.Status
var containerRestarts int32
var containerReady int
var totalContainers int
if len(pod.Spec.Containers) > 0 && len(podStatus.ContainerStatuses) > 0 {
// If a pod has multiple containers, get the status from all
for container := range pod.Spec.Containers {
containerRestarts += podStatus.ContainerStatuses[container].RestartCount
if podStatus.ContainerStatuses[container].Ready {
containerReady++
}
totalContainers++
}
}
// Get the values from the pod status
name := utils.GetCleanPodName(pod.GetName())
ready := fmt.Sprintf("%v/%v", containerReady, containerReady)
status := fmt.Sprintf("%v", podStatus.Phase)
restarts := fmt.Sprintf("%v", containerRestarts)
ageS := age.String()
row := []string{name, ready, status, restarts, ageS}
// Append this to data to be printed in a table
if verboseStatus {
row = append(row, pod.Name)
row = append(row, podStatus.PodIP)
}
data = append(data, row)
}
if verboseStatus {
columnNames = append(columnNames, "Pod-Names")
columnNames = append(columnNames, "Pod-IP")
}
// Print the data to a table for readability
utils.PrintToTable(columnNames, data)
log.Info("\nMeshplay endpoint is " + currCtx.GetEndpoint())
}
return nil
},
}
func init() {
statusCmd.Flags().BoolVarP(&verboseStatus, "verbose", "v", false, "(optional) Extra data in status table")
}