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

Commit 42242e6

Browse files
committed
Expand CRUD testing
Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>
1 parent 8e2ec3e commit 42242e6

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

controllers/configmap_controller.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controllers
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
2324
corev1 "k8s.io/api/core/v1"
@@ -35,13 +36,47 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
3536

3637
ctx = kcpclient.WithCluster(ctx, req.ObjectKey.Cluster)
3738

38-
var configmap corev1.ConfigMap
39-
if err := r.Get(ctx, req.ObjectKey, &configmap); err != nil {
39+
// Test get
40+
var configMap corev1.ConfigMap
41+
42+
if err := r.Get(ctx, req.ObjectKey, &configMap); err != nil {
4043
log.Error(err, "unable to get configmap")
41-
return ctrl.Result{}, err
44+
return ctrl.Result{}, nil
45+
}
46+
47+
log.Info("Get: retrieved configMap")
48+
49+
// Test list
50+
var configMapList corev1.ConfigMapList
51+
if err := r.List(ctx, &configMapList); err != nil {
52+
log.Error(err, "unable to list configmaps")
53+
return ctrl.Result{}, nil
54+
}
55+
for _, cm := range configMapList.Items {
56+
log.Info("List: got", "namespace", cm.Namespace, "name", cm.Name)
57+
}
58+
59+
labels := configMap.Labels
60+
if labels == nil {
61+
return ctrl.Result{}, nil
4262
}
4363

44-
log.Info("Retrieved configmap")
64+
// Test Update
65+
if labels["name"] == "" {
66+
return ctrl.Result{}, nil
67+
}
68+
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
79+
}
4580

4681
return ctrl.Result{}, nil
4782
}

main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"flag"
2021
"os"
2122

2223
kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
@@ -26,6 +27,7 @@ import (
2627
corev1 "k8s.io/api/core/v1"
2728
"k8s.io/apimachinery/pkg/runtime"
2829
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
30+
"sigs.k8s.io/controller-runtime/pkg/client/config"
2931

3032
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3133
"k8s.io/client-go/rest"
@@ -38,18 +40,27 @@ import (
3840
)
3941

4042
var (
41-
scheme = runtime.NewScheme()
42-
setupLog = ctrl.Log.WithName("setup")
43+
scheme = runtime.NewScheme()
44+
setupLog = ctrl.Log.WithName("setup")
45+
kubeconfigContext string
4346
)
4447

4548
func init() {
4649
utilruntime.Must(corev1.AddToScheme(scheme))
50+
51+
flag.StringVar(&kubeconfigContext, "context", "", "kubeconfig context")
4752
}
4853

4954
func main() {
55+
flag.Parse()
56+
5057
ctrl.SetLogger(zap.New())
5158

52-
cfg := ctrl.GetConfigOrDie()
59+
cfg, err := config.GetConfigWithContext(kubeconfigContext)
60+
if err != nil {
61+
setupLog.Error(err, "unable to get rest config")
62+
os.Exit(1)
63+
}
5364

5465
httpClient, err := rest.HTTPClientFor(cfg)
5566
if err != nil {

0 commit comments

Comments
 (0)