Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
update examples after review
  • Loading branch information
aerfio committed Sep 26, 2023
commit c183365c722a674ca3e91c75ba9935dec02f9b1e
27 changes: 21 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllerruntime_test

import (
"context"
"encoding/json"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -74,9 +75,23 @@ type ExampleCRDWithConfigMapRef struct {
ConfigMapRef corev1.LocalObjectReference `json:"configMapRef"`
}

func deepCopyObject(arg any) runtime.Object {
// DO NOT use this code in production code, this is only for presentation purposes.
// in real code you should generate DeepCopy methods by using controller-gen CLI tool.
argBytes, err := json.Marshal(arg)
if err != nil {
panic(err)
}
out := &ExampleCRDWithConfigMapRefList{}
if err := json.Unmarshal(argBytes, out); err != nil {
panic(err)
}
return out
}

// DeepCopyObject implements client.Object.
func (*ExampleCRDWithConfigMapRef) DeepCopyObject() runtime.Object {
panic("unimplemented")
func (in *ExampleCRDWithConfigMapRef) DeepCopyObject() runtime.Object {
return deepCopyObject(in)
}

type ExampleCRDWithConfigMapRefList struct {
Expand All @@ -86,14 +101,14 @@ type ExampleCRDWithConfigMapRefList struct {
}

// DeepCopyObject implements client.ObjectList.
func (*ExampleCRDWithConfigMapRefList) DeepCopyObject() runtime.Object {
panic("unimplemented")
func (in *ExampleCRDWithConfigMapRefList) DeepCopyObject() runtime.Object {
return deepCopyObject(in)
}

// This example creates a simple application Controller that is configured for ExampleCRDWithConfigMapRef CRD.
// Any change in the configMap referenced in this Custom Resource will cause the re-reconcile of the parent ExampleCRDWithConfigMapRef
// due to the implementation of the .Watches method of "sigs.k8s.io/controller-runtime/pkg/builder".Builder.
func Example_watches() {
func Example_customHandler() {
log := ctrl.Log.WithName("builder-examples")

manager, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
Expand All @@ -106,7 +121,7 @@ func Example_watches() {
NewControllerManagedBy(manager).
For(&ExampleCRDWithConfigMapRef{}).
Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, cm client.Object) []ctrl.Request {
// map a change to referenced configMap to ExampleCRDWithConfigMapRef, which causes its re-reconcile
// map a change from referenced configMap to ExampleCRDWithConfigMapRef, which causes its re-reconcile
crList := &ExampleCRDWithConfigMapRefList{}
if err := manager.GetClient().List(ctx, crList); err != nil {
manager.GetLogger().Error(err, "while listing ExampleCRDWithConfigMapRefs")
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func ExampleClient_patch() {
}

// This example shows how to use the client with unstructured objects to create/patch objects using Server Side Apply,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mention why the dance of converting to unstructured is needed

// "k8s.io/apimachinery/pkg/runtime".DefaultUnstructuredConverter.ToUnstructured is used to convert an object into map[string]any representation,
// which is then set as an "Object" field in *unstructured.Unstructured struct, which implements client.Object.
func ExampleClient_apply() {
// Using a typed object.
configMap := corev1ac.ConfigMap("name", "namespace").WithData(map[string]string{"key": "value"})
Expand Down