-
Notifications
You must be signed in to change notification settings - Fork 404
/
fsm.go
273 lines (228 loc) · 8.47 KB
/
fsm.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package serverless
import (
"context"
"fmt"
"reflect"
"runtime"
"strings"
"time"
"go.uber.org/zap"
"github.com/kyma-project/kyma/components/function-controller/internal/git"
"github.com/kyma-project/kyma/components/function-controller/internal/resource"
serverlessv1alpha2 "github.com/kyma-project/kyma/components/function-controller/pkg/apis/serverless/v1alpha2"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type stateFn func(context.Context, *reconciler, *systemState) (stateFn, error)
type k8s struct {
client resource.Client
recorder record.EventRecorder
statsCollector StatsCollector
}
type cfg struct {
docker DockerConfig
fn FunctionConfig
}
// nolint
type out struct {
result ctrl.Result
}
type reconciler struct {
cfg cfg
fn stateFn
log *zap.SugaredLogger
gitClient GitClient
k8s
out
}
const (
continuousGitCheckoutAnnotation = "serverless.kyma-project.io/continuousGitCheckout"
)
func (m *reconciler) reconcile(ctx context.Context, f serverlessv1alpha2.Function) (ctrl.Result, error) {
state := systemState{instance: f}
var err error
loop:
for m.fn != nil {
select {
case <-ctx.Done():
err = ctx.Err()
break loop
default:
m.log.With("stateFn", m.stateFnName()).Info("next state")
m.fn, err = m.fn(ctx, m, &state)
}
}
m.log.With("requeueAfter", m.result.RequeueAfter).
With("requeue", m.result.Requeue).
With("error", err).
Info("reconciliation result")
return m.result, err
}
func (m *reconciler) stateFnName() string {
fullName := runtime.FuncForPC(reflect.ValueOf(m.fn).Pointer()).Name()
splitFullName := strings.Split(fullName, ".")
if len(splitFullName) < 3 {
return fullName
}
shortName := splitFullName[2]
return shortName
}
var (
// ErrBuildReconcilerFailed is returned if it is impossible to create reconciler build function
ErrBuildReconcilerFailed = errors.New("build reconciler failed")
)
// this function is a terminator
func buildGenericStatusUpdateStateFn(condition serverlessv1alpha2.Condition, repo *serverlessv1alpha2.Repository, commit string) stateFn {
return func(ctx context.Context, r *reconciler, s *systemState) (stateFn, error) {
if condition.LastTransitionTime.IsZero() {
return nil, fmt.Errorf("LastTransitionTime for condition %s is not set", condition.Type)
}
existingFunction := &serverlessv1alpha2.Function{}
err := r.client.Get(ctx, types.NamespacedName{Namespace: s.instance.Namespace, Name: s.instance.Name}, existingFunction)
if err != nil {
return nil, errors.Wrap(client.IgnoreNotFound(err), "while getting function instance")
}
updatedStatus := existingFunction.Status.DeepCopy()
updatedStatus.Conditions = updateCondition(existingFunction.Status.Conditions, condition)
if err := r.populateStatusFromSystemState(updatedStatus, s); err != nil {
return nil, errors.Wrap(err, "while setting up Status")
}
isGitType := s.instance.TypeOf(serverlessv1alpha2.FunctionTypeGit)
if isGitType && repo != nil {
updatedStatus.Repository = *repo
updatedStatus.Commit = commit
}
if err := r.updateFunctionStatusWithEvent(ctx, existingFunction, updatedStatus, condition); err != nil {
r.log.Warnf("while updating function status: %s", err)
return nil, errors.Wrap(err, "while updating function status")
}
r.statsCollector.UpdateReconcileStats(&s.instance, condition)
return nil, nil
}
}
func (m *reconciler) populateStatusFromSystemState(status *serverlessv1alpha2.FunctionStatus, s *systemState) error {
status.Runtime = s.instance.Spec.Runtime
status.RuntimeImage = s.instance.Status.RuntimeImage
if s.instance.Spec.RuntimeImageOverride != "" {
status.RuntimeImage = s.instance.Spec.RuntimeImageOverride
}
status.RuntimeImageOverride = s.instance.Spec.RuntimeImageOverride
// set scale sub-resource
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: s.podLabels()})
if err != nil {
m.log.Warnf("failed to get selector for labelSelector: %w", err)
return errors.Wrap(err, "while getting selectors")
}
status.PodSelector = selector.String()
if len(s.deployments.Items) > 0 {
status.Replicas = s.deployments.Items[0].Status.Replicas
}
return nil
}
func (m *reconciler) updateFunctionStatusWithEvent(ctx context.Context, f *serverlessv1alpha2.Function, s *serverlessv1alpha2.FunctionStatus, condition serverlessv1alpha2.Condition) error {
if reflect.DeepEqual(f.Status, s) {
return nil
}
f.Status = *s
if err := m.client.Status().Update(ctx, f); err != nil {
return errors.Wrap(err, "while updating function status")
}
eventType := "Normal"
if condition.Status == corev1.ConditionFalse {
eventType = "Warning"
}
m.recorder.Event(f, eventType, string(condition.Reason), condition.Message)
return nil
}
func buildStatusUpdateStateFnWithCondition(condition serverlessv1alpha2.Condition) stateFn {
return buildGenericStatusUpdateStateFn(condition, nil, "")
}
func stateFnGitCheckSources(ctx context.Context, r *reconciler, s *systemState) (stateFn, error) {
var auth *git.AuthOptions
if s.instance.Spec.Source.GitRepository.Auth != nil {
var secret corev1.Secret
key := client.ObjectKey{
Namespace: s.instance.Namespace,
Name: s.instance.Spec.Source.GitRepository.Auth.SecretName,
}
if err := r.client.Get(ctx, key, &secret); err != nil {
return nil, errors.Wrap(err, "while getting secret")
}
auth = &git.AuthOptions{
Type: git.RepositoryAuthType(s.instance.Spec.Source.GitRepository.Auth.Type),
Credentials: readSecretData(secret.Data),
SecretName: s.instance.Spec.Source.GitRepository.Auth.SecretName,
}
}
options := git.Options{
URL: s.instance.Spec.Source.GitRepository.URL,
Reference: s.instance.Spec.Source.GitRepository.Reference,
Auth: auth,
}
if skipGitSourceCheck(s.instance, r.cfg) {
r.log.Info(fmt.Sprintf("skipping function [%s] source check", s.instance.Name))
expectedJob := s.buildGitJob(options, r.cfg)
return buildStateFnCheckImageJob(expectedJob), nil
}
var revision string
var err error
revision, err = r.gitClient.LastCommit(options)
if err != nil {
r.log.Error(err, " while fetching last commit")
var errMsg string
r.result, errMsg = NextRequeue(err)
// TODO: This return masks the error from r.syncRevision() and doesn't pass it to the controller. This should be fixed in a follow up PR.
condition := serverlessv1alpha2.Condition{
Type: serverlessv1alpha2.ConditionConfigurationReady,
Status: corev1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: serverlessv1alpha2.ConditionReasonSourceUpdateFailed,
Message: errMsg,
}
return buildStatusUpdateStateFnWithCondition(condition), nil
}
srcChanged := s.gitFnSrcChanged(revision)
if !srcChanged {
expectedJob := s.buildGitJob(options, r.cfg)
return buildStateFnCheckImageJob(expectedJob), nil
}
condition := serverlessv1alpha2.Condition{
Type: serverlessv1alpha2.ConditionConfigurationReady,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: serverlessv1alpha2.ConditionReasonSourceUpdated,
Message: fmt.Sprintf("Sources %s updated", s.instance.Name),
}
repository := serverlessv1alpha2.Repository{
Reference: s.instance.Spec.Source.GitRepository.Reference,
BaseDir: s.instance.Spec.Source.GitRepository.BaseDir,
}
return buildGenericStatusUpdateStateFn(condition, &repository, revision), nil
}
func stateFnInitialize(ctx context.Context, r *reconciler, s *systemState) (stateFn, error) {
if err := ctx.Err(); err != nil {
return nil, errors.Wrap(err, "context error")
}
isGitType := s.instance.TypeOf(serverlessv1alpha2.FunctionTypeGit)
if isGitType {
return stateFnGitCheckSources, nil
}
return stateFnInlineCheckSources, nil
}
func skipGitSourceCheck(f serverlessv1alpha2.Function, cfg cfg) bool {
if v, ok := f.Annotations[continuousGitCheckoutAnnotation]; ok && strings.ToLower(v) == "true" {
return false
}
// ConditionConfigurationReady is set to true for git functions when the source is updated.
// if not, this is a new function, we need to do git check.
configured := f.Status.Condition(serverlessv1alpha2.ConditionConfigurationReady)
if configured == nil || !configured.IsTrue() {
return false
}
return time.Since(configured.LastTransitionTime.Time) < cfg.fn.FunctionReadyRequeueDuration
}