@@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package controllers
17
+ package configmap
18
18
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "time"
23
22
24
23
corev1 "k8s.io/api/core/v1"
25
24
apierrors "k8s.io/apimachinery/pkg/api/errors"
26
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
26
"k8s.io/apimachinery/pkg/types"
27
+ "sigs.k8s.io/controller-runtime/pkg/kcp"
28
28
29
29
ctrl "sigs.k8s.io/controller-runtime"
30
30
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -33,68 +33,63 @@ import (
33
33
"sigs.k8s.io/controller-runtime/pkg/log"
34
34
)
35
35
36
- type ConfigMapReconciler struct {
37
- client.Client
36
+ type Reconciler struct {
37
+ Client client.Client
38
38
}
39
39
40
- func (r * ConfigMapReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
40
+ func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
41
41
log := log .FromContext (ctx ).WithValues ("cluster" , req .ClusterName )
42
42
43
43
// Test get
44
- var configMap corev1.ConfigMap
45
- if err := r .Client .Get (ctx , req .NamespacedName , & configMap ); err != nil {
44
+ var cm corev1.ConfigMap
45
+ if err := r .Client .Get (ctx , req .NamespacedName , & cm ); err != nil {
46
46
log .Error (err , "unable to get configmap" )
47
47
return ctrl.Result {}, nil
48
48
}
49
49
50
50
log .Info ("Get: retrieved configMap" )
51
- labels := configMap .Labels
51
+ if cm .Labels ["name" ] != "" {
52
+ response := fmt .Sprintf ("hello-%s" , cm .Labels ["name" ])
52
53
53
- if labels ["name" ] != "" {
54
- response := fmt .Sprintf ("hello-%s" , labels ["name" ])
55
-
56
- if labels ["response" ] != response {
57
- labels ["response" ] = response
54
+ if cm .Labels ["response" ] != response {
55
+ cm .Labels ["response" ] = response
58
56
59
57
// Test Update
60
- if err := r .Client .Update (ctx , & configMap ); err != nil {
58
+ if err := r .Client .Update (ctx , & cm ); err != nil {
61
59
return ctrl.Result {}, err
62
60
}
61
+
63
62
log .Info ("Update: updated configMap" )
64
63
return ctrl.Result {}, nil
65
64
}
66
65
}
67
66
68
67
// Test list
69
- var configMapList corev1.ConfigMapList
70
- if err := r .Client .List (ctx , & configMapList ); err != nil {
68
+ var cms corev1.ConfigMapList
69
+ if err := r .Client .List (ctx , & cms ); err != nil {
71
70
log .Error (err , "unable to list configmaps" )
72
71
return ctrl.Result {}, nil
73
72
}
74
- log .Info ("List: got" , "itemCount" , len (configMapList .Items ))
73
+ log .Info ("List: got" , "itemCount" , len (cms .Items ))
75
74
found := false
76
- for _ , cm := range configMapList .Items {
75
+ for _ , other := range cms .Items {
77
76
cluster , ok := kontext .ClusterFrom (ctx )
78
77
if ! ok {
79
- log .Info ("List: got" , "clusterName" , cluster .String (), "namespace" , cm .Namespace , "name" , cm .Name )
80
- } else {
81
- if cm .Name == configMap .Name && cm .Namespace == configMap .Namespace {
82
- if found {
83
- 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 )
84
- }
85
- found = true
86
- log .Info ("Found in listed configmaps" , "namespace" , cm .Namespace , "name" , cm .Name )
78
+ log .Info ("List: got" , "clusterName" , cluster .String (), "namespace" , other .Namespace , "name" , other .Name )
79
+ } else if other .Name == cm .Name && other .Namespace == cm .Namespace {
80
+ if found {
81
+ 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 )
87
82
}
83
+ found = true
84
+ log .Info ("Found in listed configmaps" , "namespace" , cm .Namespace , "name" , cm .Name )
88
85
}
89
86
}
90
87
91
88
// If the configmap has a namespace field, create the corresponding namespace
92
- nsName , exists := configMap .Data ["namespace" ]
89
+ nsName , exists := cm .Data ["namespace" ]
93
90
if exists {
94
91
var namespace corev1.Namespace
95
- nsKey := types.NamespacedName {Name : nsName }
96
-
97
- if err := r .Client .Get (ctx , nsKey , & namespace ); err != nil {
92
+ if err := r .Client .Get (ctx , types.NamespacedName {Name : nsName }, & namespace ); err != nil {
98
93
if ! apierrors .IsNotFound (err ) {
99
94
log .Error (err , "unable to get namespace" )
100
95
return ctrl.Result {}, err
@@ -107,29 +102,28 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
107
102
return ctrl.Result {}, err
108
103
}
109
104
log .Info ("Create: created " , "namespace" , nsName )
110
- return ctrl.Result {RequeueAfter : time . Second * 5 }, nil
105
+ return ctrl.Result {Requeue : true }, nil
111
106
}
112
107
log .Info ("Exists" , "namespace" , nsName )
113
108
}
114
109
115
110
// If the configmap has a secretData field, create a secret in the same namespace
116
111
// If the secret already exists but is out of sync, it will be non-destructively patched
117
- secretData , exists := configMap .Data ["secretData" ]
112
+ secretData , exists := cm .Data ["secretData" ]
118
113
if exists {
119
114
var secret corev1.Secret
120
-
121
- secret .SetName (configMap .GetName ())
122
- secret .SetNamespace (configMap .GetNamespace ())
123
- secret .SetOwnerReferences ([]metav1.OwnerReference {metav1.OwnerReference {
124
- Name : configMap .GetName (),
125
- UID : configMap .GetUID (),
115
+ secret .SetName (cm .GetName ())
116
+ secret .SetNamespace (cm .GetNamespace ())
117
+ secret .SetOwnerReferences ([]metav1.OwnerReference {{
118
+ Name : cm .GetName (),
119
+ UID : cm .GetUID (),
126
120
APIVersion : "v1" ,
127
121
Kind : "ConfigMap" ,
128
122
Controller : func () * bool { x := true ; return & x }(),
129
123
}})
130
124
secret .Data = map [string ][]byte {"dataFromCM" : []byte (secretData )}
131
125
132
- operationResult , err := controllerutil .CreateOrPatch (ctx , r , & secret , func () error {
126
+ operationResult , err := controllerutil .CreateOrPatch (ctx , r . Client , & secret , func () error {
133
127
secret .Data ["dataFromCM" ] = []byte (secretData )
134
128
return nil
135
129
})
@@ -143,9 +137,9 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
143
137
return ctrl.Result {}, nil
144
138
}
145
139
146
- func (r * ConfigMapReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
140
+ func (r * Reconciler ) SetupWithManager (mgr ctrl.Manager ) error {
147
141
return ctrl .NewControllerManagedBy (mgr ).
148
142
For (& corev1.ConfigMap {}).
149
143
Owns (& corev1.Secret {}).
150
- Complete (WithClusterInContext (r ))
144
+ Complete (kcp . WithClusterInContext (r ))
151
145
}
0 commit comments