-
Notifications
You must be signed in to change notification settings - Fork 155
/
seedresourcesuptodatecondition_controller.go
177 lines (150 loc) · 5.79 KB
/
seedresourcesuptodatecondition_controller.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package seedresourcesuptodatecondition
import (
"context"
"fmt"
"go.uber.org/zap"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
kubermaticv1helper "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1/helper"
controllerutil "k8c.io/kubermatic/v2/pkg/controller/util"
"k8c.io/kubermatic/v2/pkg/version/kubermatic"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const ControllerName = "kkp-seed-up-to-date-synchronizer"
type reconciler struct {
log *zap.SugaredLogger
client ctrlruntimeclient.Client
recorder record.EventRecorder
workerName string
versions kubermatic.Versions
}
func Add(
ctx context.Context,
log *zap.SugaredLogger,
mgr manager.Manager,
numWorkers int,
workerName string,
versions kubermatic.Versions,
) error {
r := &reconciler{
log: log.Named(ControllerName),
client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor(ControllerName),
workerName: workerName,
versions: versions,
}
ctrlOptions := controller.Options{
Reconciler: r,
MaxConcurrentReconciles: numWorkers,
}
c, err := controller.New(ControllerName, mgr, ctrlOptions)
if err != nil {
return err
}
typesToWatch := []ctrlruntimeclient.Object{
&appsv1.Deployment{},
&appsv1.StatefulSet{},
}
for _, t := range typesToWatch {
if err := c.Watch(&source.Kind{Type: t}, controllerutil.EnqueueClusterForNamespacedObject(mgr.GetClient())); err != nil {
return fmt.Errorf("failed to create watch for %T: %w", t, err)
}
}
return c.Watch(&source.Kind{Type: &kubermaticv1.Cluster{}}, &handler.EnqueueRequestForObject{})
}
func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
cluster := &kubermaticv1.Cluster{}
if err := r.client.Get(ctx, request.NamespacedName, cluster); err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("failed to get cluster %q: %w", request.Name, err)
}
// Add a wrapping here so we can emit an event on error
err := r.reconcile(ctx, cluster)
if err != nil {
r.log.With("cluster", request.Name).Errorw("Failed to reconcile cluster", zap.Error(err))
r.recorder.Event(cluster, corev1.EventTypeWarning, "ReconcilingError", err.Error())
}
return reconcile.Result{}, err
}
func (r *reconciler) reconcile(ctx context.Context, cluster *kubermaticv1.Cluster) error {
if r.workerName != cluster.Labels[kubermaticv1.WorkerNameLabelKey] {
return nil
}
if cluster.Spec.Pause || cluster.Status.NamespaceName == "" {
return nil
}
upToDate, err := r.seedResourcesUpToDate(ctx, cluster)
if err != nil {
return err
}
return kubermaticv1helper.UpdateClusterStatus(ctx, r.client, cluster, func(c *kubermaticv1.Cluster) {
conditionType := kubermaticv1.ClusterConditionSeedResourcesUpToDate
value := corev1.ConditionFalse
message := "Some control plane components did not finish updating"
if upToDate {
value = corev1.ConditionTrue
message = "All control plane components are up to date"
}
kubermaticv1helper.SetClusterCondition(c, r.versions, conditionType, value, kubermaticv1.ReasonClusterUpdateSuccessful, message)
})
}
func (r *reconciler) seedResourcesUpToDate(ctx context.Context, cluster *kubermaticv1.Cluster) (bool, error) {
listOpts := &ctrlruntimeclient.ListOptions{Namespace: cluster.Status.NamespaceName}
statefulSets := &appsv1.StatefulSetList{}
if err := r.client.List(ctx, statefulSets, listOpts); err != nil {
return false, fmt.Errorf("failed to list statefulSets: %w", err)
}
for _, statefulSet := range statefulSets.Items {
if statefulSet.Spec.Replicas == nil {
return false, nil
}
if *statefulSet.Spec.Replicas != statefulSet.Status.UpdatedReplicas ||
*statefulSet.Spec.Replicas != statefulSet.Status.CurrentReplicas ||
*statefulSet.Spec.Replicas != statefulSet.Status.ReadyReplicas {
return false, nil
}
}
deployments := &appsv1.DeploymentList{}
if err := r.client.List(ctx, deployments, listOpts); err != nil {
return false, fmt.Errorf("failed to list deployments: %w", err)
}
for _, deployment := range deployments.Items {
if deployment.Spec.Replicas == nil {
return false, nil
}
if *deployment.Spec.Replicas != deployment.Status.UpdatedReplicas ||
*deployment.Spec.Replicas != deployment.Status.AvailableReplicas ||
*deployment.Spec.Replicas != deployment.Status.ReadyReplicas {
return false, nil
}
}
// This is to avoid setting the resource up-to-date condition in the
// initial stage when deploymens and statefulsets are not yet deployed.
// TODO This is not perfect as we may endup in a situation where
// the available control plane components are ready, but not all components have
// been deployed yet. This scenario is quite unlikely to happen though and
// the impact of having the condition set is not big.
return len(deployments.Items) > 0 || len(statefulSets.Items) > 0, nil
}