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
/
services.go
227 lines (201 loc) · 7.07 KB
/
services.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package appstatus
import (
"fmt"
"strconv"
"strings"
name2 "github.com/acorn-io/baaah/pkg/name"
"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"
client2 "github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/config"
"github.com/acorn-io/runtime/pkg/labels"
"github.com/acorn-io/runtime/pkg/ports"
"github.com/acorn-io/runtime/pkg/publicname"
"github.com/acorn-io/runtime/pkg/ref"
"github.com/acorn-io/z"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (a *appStatusRenderer) readServices() error {
existingStatus := a.app.Status.AppStatus.Services
// reset state
a.app.Status.AppStatus.Services = make(map[string]v1.ServiceStatus, len(a.app.Status.AppSpec.Services))
for _, entry := range typed.Sorted(a.app.Status.AppSpec.Services) {
serviceName, serviceDef := entry.Key, entry.Value
s := v1.ServiceStatus{
CommonStatus: v1.CommonStatus{
LinkOverride: ports.LinkService(a.app, serviceName),
},
ExpressionErrors: existingStatus[serviceName].ExpressionErrors,
MissingConsumerPermissions: existingStatus[serviceName].MissingConsumerPermissions,
}
var err error
s.Ready, s.Defined, err = a.isServiceReady(serviceName)
if err != nil {
return err
}
if s.LinkOverride == "" && serviceDef.Image != "" {
s.ServiceAcornName = publicname.ForChild(a.app, serviceName)
serviceAcorn := &v1.AppInstance{}
err := a.c.Get(a.ctx, router.Key(a.app.Namespace, name2.SafeHashConcatName(a.app.Name, serviceName)), serviceAcorn)
if apierrors.IsNotFound(err) || err == nil {
s.ServiceAcornName = publicname.Get(serviceAcorn)
s.ServiceAcornReady = serviceAcorn.Status.Ready
} else {
return err
}
}
if len(s.MissingConsumerPermissions) > 0 {
s.ErrorMessages = append(s.ErrorMessages, "invalid service is trying to grant permissions to consumer not granted"+
" to this service. The acorn containing this service must be updated to request the following permissions, "+z.Pointer(client2.ErrRulesNeeded{
Permissions: s.MissingConsumerPermissions,
}).Error())
}
service := &v1.ServiceInstance{}
if err := ref.Lookup(a.ctx, a.c, service, a.app.Status.Namespace, serviceName); apierrors.IsNotFound(err) {
s.Ready = false
s.UpToDate = s.ServiceAcornName != ""
s.Defined = s.ServiceAcornName != ""
a.app.Status.AppStatus.Services[serviceName] = s
} else if err != nil {
return err
} else {
s.Defined = s.Defined || !service.Status.HasService
s.UpToDate = service.Namespace != a.app.Status.Namespace ||
service.Annotations[labels.AcornAppGeneration] == strconv.Itoa(int(a.app.Generation))
s.UpToDate = s.Defined && s.UpToDate
s.Ready = (s.Ready || !service.Status.HasService) && s.UpToDate
if s.ServiceAcornName != "" {
s.Ready = s.Ready && s.ServiceAcornReady
}
s.Default = service.Spec.Default
s.Ports = service.Spec.Ports
s.Data = service.Spec.Data
s.Consumer = service.Spec.Consumer
s.Secrets = service.Spec.Secrets
s.Address = service.Spec.Address
if len(service.Status.Endpoints) > 0 {
s.Endpoint = service.Status.Endpoints[0].Address
}
var (
failed bool
failedName string
failedMessage string
waiting bool
waitingName string
waitingMessage string
)
for _, condition := range service.Status.Conditions {
if condition.Error {
failed = true
failedName = service.Name
failedMessage = condition.Message
} else if condition.Transitioning || !condition.Success {
waiting = true
waitingName = service.Name
waitingMessage = condition.Message
}
}
switch {
case failed:
s.ErrorMessages = append(s.ErrorMessages, fmt.Sprintf("%s: failed [%s]", failedName, failedMessage))
case waiting:
s.TransitioningMessages = append(s.TransitioningMessages, fmt.Sprintf("%s: waiting [%s]", waitingName, waitingMessage))
default:
}
}
// The ref.Lookup call above will find what the service resolves to, but not the actual local service object.
// This is because the ref.Lookup will traverse spec.external references until it finds the destination.
// Here we lookup the local object so we can determine if it's the default or not
localService := &v1.ServiceInstance{}
if err := a.c.Get(a.ctx, router.Key(a.app.Status.Namespace, serviceName), localService); err == nil {
s.Default = localService.Spec.Default
} else if !apierrors.IsNotFound(err) {
return err
}
addExpressionErrors(&s.CommonStatus, s.ExpressionErrors)
// Not ready if we have any error messages
if len(s.ErrorMessages) > 0 {
s.Ready = false
}
if s.Ready {
s.State = "ready"
} else if s.UpToDate {
if len(s.ErrorMessages) > 0 {
s.State = "failing"
} else if s.ServiceAcornName != "" && !s.ServiceAcornReady {
s.State = "not ready"
s.TransitioningMessages = append(s.TransitioningMessages, fmt.Sprintf("acorn [%s] is not ready", s.ServiceAcornName))
} else {
s.State = "not ready"
}
} else if s.Defined {
if len(s.ErrorMessages) > 0 {
s.State = "error"
} else {
s.State = "updating"
}
} else {
if len(s.ErrorMessages) > 0 {
s.State = "error"
} else {
s.State = "pending"
}
}
a.app.Status.AppStatus.Services[serviceName] = s
}
return nil
}
func (a *appStatusRenderer) isServiceReady(svc string) (ready bool, found bool, err error) {
return a.isServiceReadyByNamespace(nil, a.app.Status.Namespace, svc)
}
func (a *appStatusRenderer) isServiceReadyByNamespace(seen []client.ObjectKey, namespace, svc string) (ready bool, found bool, err error) {
if slices.Contains(seen, router.Key(namespace, svc)) {
return false, false, fmt.Errorf("circular service dependency on %s/%s: %v", namespace, svc, seen)
}
seen = append(seen, router.Key(namespace, svc))
var svcDep corev1.Service
err = a.c.Get(a.ctx, router.Key(namespace, svc), &svcDep)
if apierrors.IsNotFound(err) {
return false, false, nil
}
if err != nil {
// if err just return it as not ready
return false, true, err
}
if svcDep.Labels[labels.AcornManaged] != "true" {
// for services we don't manage, just return ready always
return true, true, nil
}
if svcDep.Spec.ExternalName != "" {
cfg, err := config.Get(a.ctx, a.c)
if err != nil {
// if err just return it as not ready
return false, true, nil
}
if strings.HasSuffix(svcDep.Spec.ExternalName, cfg.InternalClusterDomain) {
parts := strings.Split(svcDep.Spec.ExternalName, ".")
if len(parts) > 2 {
return a.isServiceReadyByNamespace(seen, parts[1], parts[0])
}
}
// for unknown external names we just assume they are always ready
return true, true, nil
}
var endpoints corev1.Endpoints
err = a.c.Get(a.ctx, router.Key(namespace, svc), &endpoints)
if apierrors.IsNotFound(err) {
return false, false, nil
} else if err != nil {
return false, true, err
}
for _, subset := range endpoints.Subsets {
if len(subset.Addresses) > 0 {
return true, true, nil
}
}
return false, true, nil
}