-
Notifications
You must be signed in to change notification settings - Fork 55
/
list.go
190 lines (157 loc) · 4.47 KB
/
list.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
package cmd
import (
"fmt"
"math/rand"
"net/http"
"os"
"sort"
"strings"
"github.com/infrahq/infra/internal"
"github.com/infrahq/infra/internal/api"
"github.com/lensesio/tableprinter"
"k8s.io/client-go/tools/clientcmd"
)
type ListOptions struct {
internal.Options `mapstructure:",squash"`
}
type statusRow struct {
CurrentlySelected string `header:"CURRENT"` // * if selected
Name string `header:"NAME"`
Kind string `header:"KIND"`
Status string `header:"STATUS"`
Endpoint string // don't display in table
CertificateAuthorityData []byte // don't display in table
}
func list(options *ListOptions) error {
config, err := currentHostConfig()
if err != nil {
return err
}
client, err := apiClientFromConfig(options.Host)
if err != nil {
return err
}
ctx, err := apiContextFromConfig(options.Host)
if err != nil {
return err
}
users, res, err := client.UsersAPI.ListUsers(ctx).Email(config.Name).Execute()
if err != nil {
switch res.StatusCode {
case http.StatusForbidden:
fmt.Fprintln(os.Stderr, "Session has expired.")
if err = login(&LoginOptions{Current: true}); err != nil {
return err
}
return list(options)
default:
return errWithResponseContext(err, res)
}
}
// This shouldn't be possible but check nonetheless
switch {
case len(users) < 1:
return fmt.Errorf("user \"%s\" not found", config.Name)
case len(users) > 1:
return fmt.Errorf("found multiple users \"%s\"", config.Name)
}
user := users[0]
kubeConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), nil).RawConfig()
if err != nil {
println(err.Error())
}
// deduplicate rows
rows := make(map[string]statusRow)
for _, r := range user.Roles {
rows[r.Destination.Id] = newRow(r, kubeConfig.CurrentContext)
}
for _, g := range user.Groups {
for _, r := range g.Roles {
rows[r.Destination.Id] = newRow(r, kubeConfig.CurrentContext)
}
}
rowsList := make([]statusRow, 0)
for _, r := range rows {
rowsList = append(rowsList, r)
}
sort.Slice(rowsList, func(i, j int) bool {
// Sort by combined name, descending
return rowsList[i].Name < rowsList[j].Name
})
ok, err := canReachInternet()
if !ok {
for i := range rowsList {
rowsList[i].Status = fmt.Sprintf("💻 → %s → ❌ Can't reach network: (%s)", globe(), err)
}
}
if ok {
for i, row := range rowsList {
// check success case first for speed.
ok, lastErr := canGetEngineStatus(row)
if ok {
rowsList[i].Status = "✅ OK"
continue
}
// if we had a problem, check all the stops in order to figure out where it's getting stuck
if ok, err := canConnectToEndpoint(row.Endpoint); !ok {
rowsList[i].Status = fmt.Sprintf("💻 → %s → ❌ Can't reach endpoint %q (%s)", globe(), row.Endpoint, err)
continue
}
if ok, err := canConnectToTLSEndpoint(row); !ok {
rowsList[i].Status = fmt.Sprintf("💻 → %s → 🌥 → ❌ Can't negotiate TLS (%s)", globe(), err)
continue
}
// if we made it here, we must be talking to something that isn't the engine.
rowsList[i].Status = fmt.Sprintf("💻 → %s → 🌥 → 🔒 → ❌ Can't talk to infra engine (%s)", globe(), lastErr)
}
}
printTable(rowsList)
err = updateKubeconfig(user)
if err != nil {
return err
}
return nil
}
func newRow(role api.Role, currentContext string) statusRow {
row := statusRow{
Name: role.Destination.Name,
Status: "💻 → ❌ Can't reach internet",
}
if k8s, ok := role.Destination.GetKubernetesOk(); ok {
row.Endpoint = k8s.Endpoint
row.CertificateAuthorityData = []byte(k8s.Ca)
row.Kind = "Kubernetes"
}
parts := strings.Split(currentContext, ":")
if len(parts) >= 2 && parts[0] == "infra" && parts[1] == role.Destination.Name {
row.CurrentlySelected = "*"
}
return row
}
func globe() string {
//nolint:gosec // No need for crypto random
switch rand.Intn(3) {
case 1:
return "🌍"
case 2:
return "🌏"
default:
return "🌎"
}
}
func printTable(data interface{}) {
table := tableprinter.New(os.Stdout)
table.AutoFormatHeaders = true
table.HeaderAlignment = tableprinter.AlignLeft
table.AutoWrapText = false
table.DefaultAlignment = tableprinter.AlignLeft
table.CenterSeparator = ""
table.ColumnSeparator = ""
table.RowSeparator = ""
table.HeaderLine = false
table.BorderBottom = false
table.BorderLeft = false
table.BorderRight = false
table.BorderTop = false
table.Print(data)
}