-
Notifications
You must be signed in to change notification settings - Fork 0
/
microservices.go
149 lines (135 loc) · 3.75 KB
/
microservices.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
/*
* *******************************************************************************
* * Copyright (c) 2023 Datasance Teknoloji A.S.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package get
import (
"fmt"
"math"
"strings"
rsc "github.com/datasance/potctl/internal/resource"
clientutil "github.com/datasance/potctl/internal/util/client"
"github.com/datasance/potctl/pkg/util"
"github.com/datasance/iofog-go-sdk/v3/pkg/client"
)
type microserviceExecutor struct {
namespace string
client *client.Client
msvcPerID map[string]*client.MicroserviceInfo
agentPerID map[string]*client.AgentInfo
}
func newMicroserviceExecutor(namespace string) *microserviceExecutor {
a := µserviceExecutor{}
a.namespace = namespace
a.msvcPerID = make(map[string]*client.MicroserviceInfo)
a.agentPerID = make(map[string]*client.AgentInfo)
return a
}
func (exe *microserviceExecutor) init() (err error) {
exe.client, err = clientutil.NewControllerClient(exe.namespace)
if err != nil {
if rsc.IsNoControlPlaneError(err) {
return nil
}
return
}
listMsvcs, err := exe.client.GetAllMicroservices()
if err != nil {
return err
}
for i := 0; i < len(listMsvcs.Microservices); i++ {
exe.msvcPerID[listMsvcs.Microservices[i].UUID] = &listMsvcs.Microservices[i]
}
listAgents, err := exe.client.ListAgents(client.ListAgentsRequest{})
if err != nil {
return err
}
for i := 0; i < len(listAgents.Agents); i++ {
exe.agentPerID[listAgents.Agents[i].UUID] = &listAgents.Agents[i]
}
return
}
func (exe *microserviceExecutor) GetName() string {
return ""
}
func (exe *microserviceExecutor) Execute() error {
// Fetch data
if err := exe.init(); err != nil {
return err
}
printNamespace(exe.namespace)
table := exe.generateMicroserviceOutput()
return print(table)
}
func (exe *microserviceExecutor) generateMicroserviceOutput() (table [][]string) {
// Generate table and headers
table = make([][]string, len(exe.msvcPerID)+1)
headers := []string{"MICROSERVICE", "STATUS", "AGENT", "VOLUMES", "PORTS"}
table[0] = append(table[0], headers...)
// Populate rows
count := 0
for _, ms := range exe.msvcPerID {
if util.IsSystemMsvc(ms) {
continue
}
volumes := ""
for idx, volume := range ms.Volumes {
if idx == 0 {
volumes += fmt.Sprintf("%s:%s", volume.HostDestination, volume.ContainerDestination)
} else {
volumes += fmt.Sprintf(", %s:%s", volume.HostDestination, volume.ContainerDestination)
}
}
ports := ""
for idx, port := range ms.Ports {
if idx == 0 {
ports += fmt.Sprintf("%v:%v", port.External, port.Internal)
} else {
ports += fmt.Sprintf(", %v:%v", port.External, port.Internal)
}
}
agent, ok := exe.agentPerID[ms.AgentUUID]
var agentName string
if !ok {
agentName = "-"
} else {
agentName = agent.Name
}
status := ms.Status.Status
switch status {
case "":
status = "-"
case "PULLING":
if ms.Status.Percentage > 0 {
status = fmt.Sprintf("%s (%d%s)", ms.Status.Status, int(math.Round(ms.Status.Percentage)), "%")
}
}
if ms.Status.ErrorMessage != "" {
msg := ms.Status.ErrorMessage
if strings.Contains(msg, "invalid mount config for type \"bind\"") {
msg = "Volume missing"
} else if strings.Contains(msg, "runtime create failed") {
msg = "Error starting container"
}
status = fmt.Sprintf("%s (%s)", ms.Status.Status, msg)
}
row := []string{
ms.Name,
status,
agentName,
volumes,
ports,
}
table[count+1] = append(table[count+1], row...)
count++
}
return table
}