This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
/
configmap_controller.go
166 lines (138 loc) · 5.41 KB
/
configmap_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
/*
Copyright 2022 The KCP Authors.
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 controllers
import (
"context"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/kcp-dev/logicalcluster/v3"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/kontext"
"sigs.k8s.io/controller-runtime/pkg/log"
)
type ConfigMapReconciler struct {
client.Client
}
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=secrets/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=secrets/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=configmaps/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=configmaps/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=namespaces/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=namespaces/finalizers,verbs=update
func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)
ctx = kontext.WithCluster(ctx, logicalcluster.Name(req.ClusterName))
// Test get
var configMap corev1.ConfigMap
if err := r.Get(ctx, req.NamespacedName, &configMap); err != nil {
log.Error(err, "unable to get configmap")
return ctrl.Result{}, nil
}
log.Info("Get: retrieved configMap")
labels := configMap.Labels
if labels["name"] != "" {
response := fmt.Sprintf("hello-%s", labels["name"])
if labels["response"] != response {
labels["response"] = response
// Test Update
if err := r.Update(ctx, &configMap); err != nil {
return ctrl.Result{}, err
}
log.Info("Update: updated configMap")
return ctrl.Result{}, nil
}
}
// Test list
var configMapList corev1.ConfigMapList
if err := r.List(ctx, &configMapList); err != nil {
log.Error(err, "unable to list configmaps")
return ctrl.Result{}, nil
}
log.Info("List: got", "itemCount", len(configMapList.Items))
found := false
for _, cm := range configMapList.Items {
if !logicalcluster.From(&cm).Empty() {
log.Info("List: got", "clusterName", logicalcluster.From(&cm).String(), "namespace", cm.Namespace, "name", cm.Name)
} else {
if cm.Name == configMap.Name && cm.Namespace == configMap.Namespace {
if found {
return ctrl.Result{}, fmt.Errorf("there should be listed only one configmap with the given name '%s' for the given namespace '%s' when the clusterName is not available", cm.Name, cm.Namespace)
}
found = true
log.Info("Found in listed configmaps", "namespace", cm.Namespace, "name", cm.Name)
}
}
}
// If the configmap has a namespace field, create the corresponding namespace
nsName, exists := configMap.Data["namespace"]
if exists {
var namespace corev1.Namespace
nsKey := types.NamespacedName{Name: nsName}
if err := r.Get(ctx, nsKey, &namespace); err != nil {
if !apierrors.IsNotFound(err) {
log.Error(err, "unable to get namespace")
return ctrl.Result{}, err
}
// Need to create ns
namespace.SetName(nsName)
if err = r.Create(ctx, &namespace); err != nil {
log.Error(err, "unable to create namespace")
return ctrl.Result{}, err
}
log.Info("Create: created ", "namespace", nsName)
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
log.Info("Exists", "namespace", nsName)
}
// If the configmap has a secretData field, create a secret in the same namespace
// If the secret already exists but is out of sync, it will be non-destructively patched
secretData, exists := configMap.Data["secretData"]
if exists {
var secret corev1.Secret
secret.SetName(configMap.GetName())
secret.SetNamespace(configMap.GetNamespace())
secret.SetOwnerReferences([]metav1.OwnerReference{{
Name: configMap.GetName(),
UID: configMap.GetUID(),
APIVersion: "v1",
Kind: "ConfigMap",
Controller: func() *bool { x := true; return &x }(),
}})
secret.Data = map[string][]byte{"dataFromCM": []byte(secretData)}
operationResult, err := controllerutil.CreateOrPatch(ctx, r, &secret, func() error {
secret.Data["dataFromCM"] = []byte(secretData)
return nil
})
if err != nil {
log.Error(err, "unable to create or patch secret")
return ctrl.Result{}, err
}
log.Info(string(operationResult), "secret", secret.GetName())
}
return ctrl.Result{}, nil
}
func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Complete(r)
}