-
Notifications
You must be signed in to change notification settings - Fork 9
/
describe.go
157 lines (128 loc) · 3.56 KB
/
describe.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cluster
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
)
var kubernetesResources = []string{
"pods",
"namespaces",
"services",
"replicasets",
"deployments",
"statefulsets",
"daemonsets",
}
var resources = append(kubernetesResources,
"nsc/containers",
"nsc/ingress",
)
func NewDescribeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "describe",
Short: "Outputs a description of the specified cluster.",
Args: cobra.ExactArgs(1),
}
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
cluster, _, err := selectRunningCluster(ctx, args)
if err != nil {
return err
}
if cluster == nil {
return nil
}
response, err := api.GetClusterSummary(ctx, api.Endpoint, cluster.ClusterId, resources)
if err != nil {
return err
}
switch *output {
case "json":
enc := json.NewEncoder(console.Stdout(ctx))
enc.SetIndent("", " ")
return enc.Encode(response.Summary)
default:
if *output != "plain" {
fmt.Fprintf(console.Warnings(ctx), "defaulting output to plain\n")
}
count := 0
for _, r := range response.Summary {
if len(r.PerResource) > 0 && slices.Contains(kubernetesResources, r.Resource) {
count++
}
}
if count > 0 {
fmt.Fprintf(console.Warnings(ctx), "The cluster contains Kubernetes resources which are not being output.\n")
}
var containers, ingresses []api.Resource
for _, r := range response.Summary {
if r.Resource == "nsc/containers" {
containers = maps.Values(r.PerResource)
} else if r.Resource == "nsc/ingress" {
ingresses = maps.Values(r.PerResource)
}
}
out := console.Stdout(ctx)
fmt.Fprintf(out, "%s:\n", cluster.ClusterId)
for k, ctr := range containers {
if k > 0 {
fmt.Fprintln(out)
}
fmt.Fprintf(out, " Managed Container ID: %s\n", ctr.Uid)
fmt.Fprintf(out, " Name: %s\n", ctr.Name)
fmt.Fprintf(out, " Created: %s\n", ctr.CreationTime)
if ctr.Tombstone != nil {
fmt.Fprintf(out, " Stopped: %s\n", ctr.Tombstone)
}
for _, x := range ctr.Container {
fmt.Fprintf(out, " Container %s\n", x.Id)
for _, port := range x.ExportedPort {
fmt.Fprintf(out, " Exported port: %s/%d -> %d\n", port.Proto, port.ContainerPort, port.ExportedPort)
}
}
for _, ingress := range ingresses {
for _, x := range ctr.Container {
if hasPort(x.ExportedPort, ingress.TargetExportedPort) {
fmt.Fprintf(out, " Ingress: https://%s%s\n", ingress.Name, ingressSuffix(ingress))
break
}
}
}
}
return nil
}
})
return cmd
}
func ingressSuffix(ingress api.Resource) string {
bits := map[string]struct{}{}
for _, x := range ingress.HttpMatchRule {
if x.DoesNotRequireAuth {
bits["does not require auth"] = struct{}{}
}
}
g := maps.Keys(bits)
if len(g) > 0 {
slices.Sort(g)
return " (" + strings.Join(g, "; ") + ")"
}
return ""
}
func hasPort(ports []*api.Container_ExportedContainerPort, port int32) bool {
for _, p := range ports {
if p.ExportedPort == port {
return true
}
}
return false
}