/
controller.go
175 lines (144 loc) · 5.87 KB
/
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
/*
Copyright 2021 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 masterconstraintsynchronizer
import (
"context"
"fmt"
"go.uber.org/zap"
kubermaticapiv1 "k8c.io/kubermatic/v2/pkg/api/v1"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
"k8c.io/kubermatic/v2/pkg/controller/util/predicate"
kuberneteshelper "k8c.io/kubermatic/v2/pkg/kubernetes"
"k8c.io/kubermatic/v2/pkg/resources/reconciling"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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 (
// This controller syncs the kubermatic constraints on the master cluster to the seed clusters.
ControllerName = "master_constraint_syncing_controller"
)
type reconciler struct {
log *zap.SugaredLogger
masterClient ctrlruntimeclient.Client
namespace string
seedClients map[string]ctrlruntimeclient.Client
recorder record.EventRecorder
}
func Add(ctx context.Context,
masterMgr manager.Manager,
namespace string,
seedManagers map[string]manager.Manager,
log *zap.SugaredLogger,
) error {
log = log.Named(ControllerName)
r := &reconciler{
log: log,
masterClient: masterMgr.GetClient(),
namespace: namespace,
seedClients: map[string]ctrlruntimeclient.Client{},
recorder: masterMgr.GetEventRecorderFor(ControllerName),
}
c, err := controller.New(ControllerName, masterMgr, controller.Options{
Reconciler: r,
})
if err != nil {
return fmt.Errorf("failed to construct controller: %w", err)
}
for seedName, seedManager := range seedManagers {
r.seedClients[seedName] = seedManager.GetClient()
}
// Watch for changes to Constraints
if err := c.Watch(&source.Kind{Type: &kubermaticv1.Constraint{}}, &handler.EnqueueRequestForObject{}, predicate.ByNamespace(namespace)); err != nil {
return fmt.Errorf("failed to watch constraints: %w", err)
}
return nil
}
func constraintCreatorGetter(constraint *kubermaticv1.Constraint) reconciling.NamedKubermaticV1ConstraintCreatorGetter {
return func() (string, reconciling.KubermaticV1ConstraintCreator) {
return constraint.Name, func(c *kubermaticv1.Constraint) (*kubermaticv1.Constraint, error) {
c.Name = constraint.Name
c.Spec = constraint.Spec
c.Namespace = constraint.Namespace
return c, nil
}
}
}
// Reconcile reconciles the kubermatic constraints in the master cluster and syncs them to all seeds.
func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := r.log.With("resource", request.Name)
log.Debug("Processing")
err := r.reconcile(ctx, log, request)
if err != nil {
log.Errorw("ReconcilingError", zap.Error(err))
}
return reconcile.Result{}, err
}
func (r *reconciler) syncAllSeeds(ctx context.Context, log *zap.SugaredLogger, constraint *kubermaticv1.Constraint, action func(seedClient ctrlruntimeclient.Client, constraint *kubermaticv1.Constraint) error) error {
for seedName, seedClient := range r.seedClients {
log := log.With("seed", seedName)
log.Debug("Reconciling constraint with seed")
err := action(seedClient, constraint)
if err != nil {
return fmt.Errorf("failed syncing constraint %s for seed %s: %w", constraint.Name, seedName, err)
}
log.Debug("Reconciled constraint with seed")
}
return nil
}
func (r *reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, request reconcile.Request) error {
constraint := &kubermaticv1.Constraint{}
if err := r.masterClient.Get(ctx, request.NamespacedName, constraint); err != nil {
return ctrlruntimeclient.IgnoreNotFound(err)
}
// handling deletion
if !constraint.DeletionTimestamp.IsZero() {
if err := r.handleDeletion(ctx, log, constraint); err != nil {
return fmt.Errorf("handling deletion of constraint: %w", err)
}
return nil
}
if err := kuberneteshelper.TryAddFinalizer(ctx, r.masterClient, constraint, kubermaticapiv1.GatekeeperSeedConstraintCleanupFinalizer); err != nil {
return fmt.Errorf("failed to add finalizer: %w", err)
}
constraintCreatorGetters := []reconciling.NamedKubermaticV1ConstraintCreatorGetter{
constraintCreatorGetter(constraint),
}
return r.syncAllSeeds(ctx, log, constraint, func(seedClient ctrlruntimeclient.Client, constraint *kubermaticv1.Constraint) error {
return reconciling.ReconcileKubermaticV1Constraints(ctx, constraintCreatorGetters, r.namespace, seedClient)
})
}
func (r *reconciler) handleDeletion(ctx context.Context, log *zap.SugaredLogger, constraint *kubermaticv1.Constraint) error {
// if finalizer not set to master Constraint then return
if !kuberneteshelper.HasFinalizer(constraint, kubermaticapiv1.GatekeeperSeedConstraintCleanupFinalizer) {
return nil
}
err := r.syncAllSeeds(ctx, log, constraint, func(seedClient ctrlruntimeclient.Client, constraint *kubermaticv1.Constraint) error {
err := seedClient.Delete(ctx, &kubermaticv1.Constraint{
ObjectMeta: metav1.ObjectMeta{
Name: constraint.Name,
Namespace: constraint.Namespace,
},
})
return ctrlruntimeclient.IgnoreNotFound(err)
})
if err != nil {
return err
}
return kuberneteshelper.TryRemoveFinalizer(ctx, r.masterClient, constraint, kubermaticapiv1.GatekeeperSeedConstraintCleanupFinalizer)
}