This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
cli_status.go
185 lines (166 loc) · 4.84 KB
/
cli_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
package appstatus
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/acorn-io/baaah/pkg/router"
"github.com/acorn-io/baaah/pkg/typed"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/labels"
"github.com/acorn-io/runtime/pkg/publicname"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
func CLIStatus(req router.Request, resp router.Response) (err error) {
app := req.Object.(*v1.AppInstance)
app.Status.Columns.UpToDate = uptodate(app)
app.Status.Columns.Healthy = healthy(app)
app.Status.Columns.Message = message(app)
app.Status.Columns.Endpoints, err = endpoints(req, app)
// There's clearly a better way to do this, but it works and I'm lazy. The intention is that we want
// to detect that the acorn doesn't have any running containers (or needs to run containers) and has
// produced whatever it needs to and the status is not really helpful anymore, because it's done.
app.Status.AppStatus.Completed = strings.Contains(publicname.Get(app), ".") &&
app.Status.Ready &&
app.Status.Columns.Healthy == "0" &&
app.Status.Columns.UpToDate == "0" &&
app.Status.Columns.Message == "OK" &&
app.Status.Columns.Endpoints == ""
if app.Status.AppStatus.Completed {
if parentName := app.Labels[labels.AcornParentAcornName]; parentName != "" {
var parent v1.AppInstance
if err := req.Get(&parent, app.Namespace, parentName); apierrors.IsNotFound(err) {
app.Status.AppStatus.Completed = false
} else if err != nil {
return err
}
}
}
resp.Objects(app)
return
}
func message(app *v1.AppInstance) string {
buf := &bytes.Buffer{}
if !app.DeletionTimestamp.IsZero() {
buf.WriteString("[removing]")
} else if app.GetStopped() && !app.Status.AppStatus.Stopped {
buf.WriteString("[stopping]")
}
for _, cond := range app.Status.Conditions {
if cond.Type == v1.AppInstanceConditionReady {
continue
}
if !cond.Success && (cond.Error || cond.Transitioning) && cond.Message != "" {
if buf.Len() > 0 {
buf.WriteString(" ")
}
buf.WriteString("[")
buf.WriteString(cond.Type)
buf.WriteString(": ")
buf.WriteString(cond.Message)
buf.WriteString("]")
}
}
if buf.Len() != 0 {
return buf.String()
}
if app.Status.ConfirmUpgradeAppImage != "" {
return "Upgrade available: " + app.Status.ConfirmUpgradeAppImage
}
if app.Status.Ready {
return "OK"
}
return "pending"
}
func uptodate(app *v1.AppInstance) string {
if app.Status.Namespace == "" {
return "-"
}
if app.Status.AppStatus.Stopped {
return "-"
}
var (
desired, uptodate int32
)
for _, status := range app.Status.AppStatus.Containers {
uptodate += status.UpToDateReplicaCount
desired += status.RunningReplicaCount
}
if uptodate != desired {
return fmt.Sprintf("%d/%d", uptodate, desired)
}
return strconv.Itoa(int(uptodate))
}
func healthy(app *v1.AppInstance) string {
if app.Status.AppStatus.Stopped {
return "stopped"
}
if app.Status.Namespace == "" {
return "creating"
}
var (
ready, desired int32
)
for _, status := range app.Status.AppStatus.Containers {
desired += status.DesiredReplicaCount
ready += status.ReadyReplicaCount
}
if ready != desired {
return fmt.Sprintf("%d/%d", ready, desired)
}
return strconv.Itoa(int(ready))
}
func endpoints(req router.Request, app *v1.AppInstance) (string, error) {
endpointTarget := map[string][]v1.Endpoint{}
for _, endpoint := range app.Status.AppStatus.Endpoints {
target := fmt.Sprintf("%s:%d", endpoint.Target, endpoint.TargetPort)
endpointTarget[target] = append(endpointTarget[target], endpoint)
}
ingressTLSHosts, err := ingressTLSHosts(req.Ctx, req.Client, app)
if err != nil {
return "", err
}
var endpointStrings []string
for _, entry := range typed.Sorted(endpointTarget) {
var (
target, endpoints = entry.Key, entry.Value
publicStrings []string
)
for _, endpoint := range endpoints {
buf := &strings.Builder{}
switch endpoint.Protocol {
case v1.ProtocolHTTP:
if !strings.HasPrefix(endpoint.Address, "http") {
var host string
a, b, ok := strings.Cut(endpoint.Address, "://")
if ok {
host, _, _ = strings.Cut(b, ":")
} else {
host, _, _ = strings.Cut(a, ":")
}
if _, ok := ingressTLSHosts[host]; ok {
buf.WriteString("https://")
} else {
buf.WriteString("http://")
}
}
default:
buf.WriteString(strings.ToLower(string(endpoint.Protocol)))
buf.WriteString("://")
}
if endpoint.Pending {
if endpoint.Protocol == "http" {
buf.WriteString("<Pending Ingress>")
} else {
buf.WriteString("<Pending Load Balancer>")
}
} else {
buf.WriteString(endpoint.Address)
}
publicStrings = append(publicStrings, buf.String())
}
endpointStrings = append(endpointStrings,
fmt.Sprintf("%s => %s", strings.Join(publicStrings, " | "), target))
}
return strings.Join(endpointStrings, ", "), nil
}