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
/
containers.go
212 lines (185 loc) · 5.71 KB
/
containers.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
package appstatus
import (
"fmt"
"strconv"
"github.com/acorn-io/baaah/pkg/router"
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/ports"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierror "k8s.io/apimachinery/pkg/api/errors"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/kubectl/pkg/util/deployment"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (a *appStatusRenderer) readContainers() error {
var (
isTransitioning bool
existingStatus = a.app.Status.AppStatus.Containers
)
// reset state
a.app.Status.AppStatus.Containers = make(map[string]v1.ContainerStatus, len(a.app.Status.AppSpec.Containers))
summary, err := a.getReplicasSummary(labels.AcornContainerName)
if err != nil {
return err
}
for containerName, containerDef := range a.app.Status.AppSpec.Containers {
var cs v1.ContainerStatus
summary := summary[containerName]
cs.Defined = ports.IsLinked(a.app, containerName)
cs.LinkOverride = ports.LinkService(a.app, containerName)
cs.ErrorMessages = append(cs.ErrorMessages, summary.ErrorMessages...)
cs.ExpressionErrors = existingStatus[containerName].ExpressionErrors
cs.Dependencies = existingStatus[containerName].Dependencies
cs.TransitioningMessages = append(cs.TransitioningMessages, summary.TransitioningMessages...)
cs.MaxReplicaRestartCount = summary.MaxReplicaRestartCount
hash, err := configHash(containerDef)
if err != nil {
return err
}
cs.ConfigHash = hash
dep := appsv1.Deployment{}
err = a.c.Get(a.ctx, router.Key(a.app.Status.Namespace, containerName), &dep)
if apierror.IsNotFound(err) {
// do nothing
} else if err != nil {
return err
} else {
cs.Defined = true
cs.UpToDate = dep.Annotations[labels.AcornAppGeneration] == strconv.Itoa(int(a.app.Generation)) && dep.Annotations[labels.AcornConfigHashAnnotation] == hash
cs.ReadyReplicaCount = dep.Status.ReadyReplicas
cs.RunningReplicaCount = dep.Status.Replicas
cs.DesiredReplicaCount = replicas(dep.Spec.Replicas)
cs.UpToDateReplicaCount = dep.Status.UpdatedReplicas
if cs.UpToDate && cs.ReadyReplicaCount == cs.DesiredReplicaCount && cs.UpToDateReplicaCount >= cs.DesiredReplicaCount {
cs.Ready, err = a.isDeploymentReady(&dep)
if err != nil {
return err
}
}
}
if cs.LinkOverride != "" {
var err error
cs.UpToDate = true
cs.Ready, cs.Defined, err = a.isServiceReady(containerName)
if err != nil {
return err
}
}
if len(cs.TransitioningMessages) > 0 {
isTransitioning = true
}
a.app.Status.AppStatus.Containers[containerName] = cs
}
a.app.Status.AppStatus.Stopped = false
if !isTransitioning && a.app.GetStopped() {
allZero := true
for _, v := range a.app.Status.AppStatus.Containers {
if v.DesiredReplicaCount != 0 {
allZero = false
break
}
}
if allZero {
a.app.Status.AppStatus.Stopped = true
}
}
return nil
}
func setContainerMessages(app *v1.AppInstance) {
for containerName, cs := range app.Status.AppStatus.Containers {
addExpressionErrors(&cs.CommonStatus, cs.ExpressionErrors)
// Not ready if we have any error messages
if len(cs.ErrorMessages) > 0 {
cs.Ready = false
}
if cs.Ready {
if app.GetStopped() {
cs.State = "stopped"
} else {
cs.State = "running"
}
} else if cs.UpToDate {
if len(cs.ErrorMessages) > 0 {
cs.State = "failing"
} else {
cs.State = "not ready"
}
} else if cs.Defined {
if len(cs.ErrorMessages) > 0 {
cs.State = "error"
} else {
cs.State = "updating"
}
} else {
if len(cs.ErrorMessages) > 0 {
cs.State = "error"
} else {
cs.State = "pending"
}
}
if !cs.Ready {
msg, blocked := isBlocked(cs.Dependencies, cs.ExpressionErrors)
if blocked {
cs.State = "waiting"
}
cs.TransitioningMessages = append(cs.TransitioningMessages, msg...)
}
// Add informative messages if all else is healthy
if len(cs.TransitioningMessages) == 0 && len(cs.ErrorMessages) == 0 {
if cs.RunningReplicaCount > 1 {
cs.Messages = append(cs.Messages, fmt.Sprintf("%d running replicas", cs.RunningReplicaCount))
}
if cs.MaxReplicaRestartCount > 0 {
cs.Messages = append(cs.Messages, fmt.Sprintf("%d container restarts", cs.MaxReplicaRestartCount))
}
}
app.Status.AppStatus.Containers[containerName] = cs
}
}
func (a *appStatusRenderer) isDeploymentReady(dep *appsv1.Deployment) (bool, error) {
available := false
for _, cond := range dep.Status.Conditions {
if cond.Type == "Available" && cond.Status == corev1.ConditionTrue {
available = true
break
}
}
if !available {
return false, nil
}
if dep.Annotations[labels.AcornAppGeneration] != strconv.Itoa(int(a.app.Generation)) ||
dep.Status.ObservedGeneration != dep.Generation ||
dep.Status.Replicas != dep.Status.ReadyReplicas ||
dep.Status.Replicas != dep.Status.UpdatedReplicas {
return false, nil
}
reps := &appsv1.ReplicaSetList{}
err := a.c.List(a.ctx, reps, &kclient.ListOptions{
LabelSelector: klabels.SelectorFromSet(map[string]string{
labels.AcornAppName: a.app.Name,
labels.AcornAppNamespace: a.app.Namespace,
labels.AcornContainerName: dep.Labels[labels.AcornContainerName],
}),
Namespace: a.app.Status.Namespace,
})
if err != nil {
return false, nil
}
for _, rep := range reps.Items {
if rep.Annotations[deployment.RevisionAnnotation] == dep.Annotations[deployment.RevisionAnnotation] &&
rep.Generation == rep.Status.ObservedGeneration &&
rep.Status.Replicas == rep.Status.ReadyReplicas &&
rep.Status.Replicas == rep.Status.AvailableReplicas {
return true, nil
}
}
return false, nil
}
func replicas(replicas *int32) int32 {
if replicas == nil {
return 1
}
return *replicas
}