forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seedresourcesuptodatecondition_controller.go
183 lines (156 loc) · 5.39 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
178
179
180
181
182
183
/*
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"
"reflect"
"go.uber.org/zap"
controllerutil "github.com/kubermatic/kubermatic/api/pkg/controller/util"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kubermaticv1helper "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1/helper"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"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 = "seed_resources_up_to_date_condition_controller"
func Add(
ctx context.Context,
log *zap.SugaredLogger,
mgr manager.Manager,
numWorkers int,
workerName string,
) error {
r := &reconciler{
ctx: ctx,
log: log.Named(ControllerName),
client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor(ControllerName),
workerName: workerName,
}
ctrlOptions := controller.Options{
Reconciler: r,
MaxConcurrentReconciles: numWorkers,
}
c, err := controller.New(ControllerName, mgr, ctrlOptions)
if err != nil {
return err
}
typesToWatch := []runtime.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: %v", t, err)
}
}
return c.Watch(&source.Kind{Type: &kubermaticv1.Cluster{}}, &handler.EnqueueRequestForObject{})
}
type reconciler struct {
ctx context.Context
log *zap.SugaredLogger
client ctrlruntimeclient.Client
recorder record.EventRecorder
workerName string
}
func (r *reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
cluster := &kubermaticv1.Cluster{}
if err := r.client.Get(r.ctx, request.NamespacedName, cluster); err != nil {
if kerrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("failed to get cluster %q: %v", request.Name, err)
}
// Add a wrapping here so we can emit an event on error
err := r.reconcile(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(cluster *kubermaticv1.Cluster) error {
if r.workerName != cluster.Labels[kubermaticv1.WorkerNameLabelKey] {
return nil
}
if cluster.Spec.Pause {
return nil
}
upToDate, err := r.seedResourcesUpToDate(cluster)
if err != nil {
return err
}
oldCluster := cluster.DeepCopy()
if !upToDate {
kubermaticv1helper.SetClusterCondition(
cluster,
kubermaticv1.ClusterConditionSeedResourcesUpToDate,
corev1.ConditionFalse,
kubermaticv1.ReasonClusterUpdateSuccessful,
"Some control plane components did not finish updating",
)
} else {
kubermaticv1helper.SetClusterCondition(
cluster,
kubermaticv1.ClusterConditionSeedResourcesUpToDate,
corev1.ConditionTrue,
kubermaticv1.ReasonClusterUpdateSuccessful,
"All control plane components are up to date",
)
}
if reflect.DeepEqual(oldCluster, cluster) {
return nil
}
return r.client.Patch(r.ctx, cluster, ctrlruntimeclient.MergeFrom(oldCluster))
}
func (r *reconciler) seedResourcesUpToDate(cluster *kubermaticv1.Cluster) (bool, error) {
listOpts := &ctrlruntimeclient.ListOptions{Namespace: cluster.Status.NamespaceName}
statefulSets := &appsv1.StatefulSetList{}
if err := r.client.List(r.ctx, statefulSets, listOpts); err != nil {
return false, fmt.Errorf("failed to list statefulSets: %v", 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(r.ctx, deployments, listOpts); err != nil {
return false, fmt.Errorf("failed to list deployments: %v", 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
}
}
return true, nil
}