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 100
/
functions.go
156 lines (136 loc) · 4.17 KB
/
functions.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
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"
apierror "k8s.io/apimachinery/pkg/api/errors"
)
func (a *appStatusRenderer) readFunctions() error {
var (
isTransitioning bool
existingStatus = a.app.Status.AppStatus.Functions
)
// reset state
a.app.Status.AppStatus.Functions = make(map[string]v1.ContainerStatus, len(a.app.Status.AppSpec.Functions))
summary, err := a.getReplicasSummary(labels.AcornFunctionName)
if err != nil {
return err
}
for functionName, functionDef := range a.app.Status.AppSpec.Functions {
var cs v1.ContainerStatus
summary := summary[functionName]
cs.Defined = ports.IsLinked(a.app, functionName)
cs.LinkOverride = ports.LinkService(a.app, functionName)
cs.ErrorMessages = append(cs.ErrorMessages, summary.ErrorMessages...)
cs.ExpressionErrors = existingStatus[functionName].ExpressionErrors
cs.Dependencies = existingStatus[functionName].Dependencies
cs.TransitioningMessages = append(cs.TransitioningMessages, summary.TransitioningMessages...)
cs.MaxReplicaRestartCount = summary.MaxReplicaRestartCount
hash, err := configHash(functionDef)
if err != nil {
return err
}
cs.ConfigHash = hash
dep := appsv1.Deployment{}
err = a.c.Get(a.ctx, router.Key(a.app.Status.Namespace, functionName), &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, labels.AcornFunctionName)
if err != nil {
return err
}
}
}
if cs.LinkOverride != "" {
var err error
cs.UpToDate = true
cs.Ready, cs.Defined, err = a.isServiceReady(functionName)
if err != nil {
return err
}
}
if len(cs.TransitioningMessages) > 0 {
isTransitioning = true
}
a.app.Status.AppStatus.Functions[functionName] = cs
}
a.app.Status.AppStatus.Stopped = false
if !isTransitioning && a.app.GetStopped() {
allZero := true
for _, v := range a.app.Status.AppStatus.Functions {
if v.DesiredReplicaCount != 0 {
allZero = false
break
}
}
if allZero {
a.app.Status.AppStatus.Stopped = true
}
}
return nil
}
func setFunctionMessages(app *v1.AppInstance) {
for functionName, cs := range app.Status.AppStatus.Functions {
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 function restarts", cs.MaxReplicaRestartCount))
}
}
app.Status.AppStatus.Functions[functionName] = cs
}
}