Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit 74c6b76

Browse files
committed
Exercise all verbs in example, add an owned resource
1 parent 7b0e9cd commit 74c6b76

File tree

1 file changed

+78
-20
lines changed

1 file changed

+78
-20
lines changed

controllers/configmap_controller.go

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

23-
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
2424
corev1 "k8s.io/api/core/v1"
25+
apierrors "k8s.io/apimachinery/pkg/api/errors"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
28+
29+
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
30+
31+
"github.com/kcp-dev/logicalcluster"
32+
2533
ctrl "sigs.k8s.io/controller-runtime"
2634
"sigs.k8s.io/controller-runtime/pkg/client"
35+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2736
"sigs.k8s.io/controller-runtime/pkg/log"
2837
)
2938

@@ -45,6 +54,22 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
4554
}
4655

4756
log.Info("Get: retrieved configMap")
57+
labels := configMap.Labels
58+
59+
if labels["name"] != "" {
60+
response := fmt.Sprintf("hello-%s", labels["name"])
61+
62+
if labels["response"] != response {
63+
labels["response"] = response
64+
65+
// Test Update
66+
if err := r.Update(ctx, &configMap); err != nil {
67+
return ctrl.Result{}, err
68+
}
69+
log.Info("Update: updated configMap")
70+
return ctrl.Result{}, nil
71+
}
72+
}
4873

4974
// Test list
5075
var configMapList corev1.ConfigMapList
@@ -53,29 +78,61 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
5378
return ctrl.Result{}, nil
5479
}
5580
for _, cm := range configMapList.Items {
56-
log.Info("List: got", "namespace", cm.Namespace, "name", cm.Name)
81+
log.Info("List: got", "clusterName", logicalcluster.From(&cm).String(), "namespace", cm.Namespace, "name", cm.Name)
5782
}
5883

59-
labels := configMap.Labels
60-
if labels == nil {
61-
return ctrl.Result{}, nil
62-
}
63-
64-
// Test Update
65-
if labels["name"] == "" {
66-
return ctrl.Result{}, nil
84+
nsName, exists := configMap.Data["namespace"]
85+
if exists {
86+
var namespace corev1.Namespace
87+
nsKey := client.ObjectKey{
88+
NamespacedName: types.NamespacedName{Name: nsName},
89+
Cluster: req.ObjectKey.Cluster,
90+
}
91+
92+
if err := r.Get(ctx, nsKey, &namespace); err != nil {
93+
if !apierrors.IsNotFound(err) {
94+
log.Error(err, "unable to get namespace")
95+
return ctrl.Result{}, err
96+
}
97+
98+
// Need to create ns
99+
namespace.SetName(nsName)
100+
if err = r.Create(ctx, &namespace); err != nil {
101+
log.Error(err, "unable to create namespace")
102+
return ctrl.Result{}, err
103+
}
104+
log.Info("Create: created ", "namespace", nsName)
105+
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
106+
}
107+
log.Info("Exists", "namespace", nsName)
67108
}
68109

69-
response := fmt.Sprintf("hello-%s", labels["name"])
70-
71-
if labels["response"] == response {
72-
return ctrl.Result{}, nil
73-
}
74-
75-
labels["response"] = response
76-
77-
if err := r.Update(ctx, &configMap); err != nil {
78-
return ctrl.Result{}, err
110+
secretData, exists := configMap.Data["secretData"]
111+
112+
if exists {
113+
var secret corev1.Secret
114+
115+
secret.SetName(configMap.GetName())
116+
secret.SetNamespace(configMap.GetNamespace())
117+
secret.SetClusterName(logicalcluster.From(&configMap).String())
118+
secret.SetOwnerReferences([]metav1.OwnerReference{metav1.OwnerReference{
119+
Name: configMap.GetName(),
120+
UID: configMap.GetUID(),
121+
APIVersion: "v1",
122+
Kind: "ConfigMap",
123+
Controller: func() *bool { x := true; return &x }(),
124+
}})
125+
secret.Data = map[string][]byte{"dataFromCM": []byte(secretData)}
126+
127+
operationResult, err := controllerutil.CreateOrPatch(ctx, r, &secret, func() error {
128+
secret.Data["dataFromCM"] = []byte(secretData)
129+
return nil
130+
})
131+
if err != nil {
132+
log.Error(err, "unable to create or patch secret")
133+
return ctrl.Result{}, err
134+
}
135+
log.Info(string(operationResult), "secret", secret.GetName())
79136
}
80137

81138
return ctrl.Result{}, nil
@@ -84,5 +141,6 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
84141
func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
85142
return ctrl.NewControllerManagedBy(mgr).
86143
For(&corev1.ConfigMap{}).
144+
Owns(&corev1.Secret{}).
87145
Complete(r)
88146
}

0 commit comments

Comments
 (0)