Skip to content

Commit f4df511

Browse files
author
Shawn Hurley
committed
Adding documentation for using unstructured for client and controller
1 parent 161208a commit f4df511

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

pkg/client/example_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package client_test
18+
19+
import (
20+
"context"
21+
22+
corev1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
var (
30+
c client.Client
31+
)
32+
33+
// This example shows how to use the client with typed and unstrucurted objects to retrieve a objects.
34+
func ExampleClient_get() {
35+
// Using a typed object.
36+
p := &corev1.Pod{}
37+
// c is a created client.
38+
_ = c.Get(context.Background(), client.ObjectKey{
39+
Namespace: "namespace",
40+
Name: "name",
41+
}, p)
42+
43+
// Using a unstructured object.
44+
u := &unstructured.Unstructured{}
45+
u.SetGroupVersionKind(schema.GroupVersionKind{
46+
Group: "apps",
47+
Kind: "Deployment",
48+
Version: "v1",
49+
})
50+
_ = c.Get(context.Background(), client.ObjectKey{
51+
Namespace: "namespace",
52+
Name: "name",
53+
}, u)
54+
}
55+
56+
// This example shows how to use the client with typed and unstrucurted objects to create objects.
57+
func ExampleClient_create() {
58+
// Using a typed object.
59+
p := &corev1.Pod{
60+
ObjectMeta: v1.ObjectMeta{
61+
Namespace: "namespace",
62+
Name: "name",
63+
},
64+
Spec: corev1.PodSpec{
65+
Containers: []corev1.Container{
66+
corev1.Container{
67+
Image: "nginx",
68+
Name: "nginx",
69+
},
70+
},
71+
},
72+
}
73+
// c is a created client.
74+
_ = c.Create(context.Background(), p)
75+
76+
// Using a unstructured object.
77+
u := &unstructured.Unstructured{}
78+
u.Object = map[string]interface{}{
79+
"name": "name",
80+
"namespace": "namespace",
81+
"spec": map[string]interface{}{
82+
"replicas": 2,
83+
"selector": map[string]interface{}{
84+
"matchLabels": map[string]interface{}{
85+
"foo": "bar",
86+
},
87+
},
88+
"template": map[string]interface{}{
89+
"labels": map[string]interface{}{
90+
"foo": "bar",
91+
},
92+
"spec": map[string]interface{}{
93+
"containers": []map[string]interface{}{
94+
{
95+
"name": "nginx",
96+
"image": "nginx",
97+
},
98+
},
99+
},
100+
},
101+
},
102+
}
103+
u.SetGroupVersionKind(schema.GroupVersionKind{
104+
Group: "apps",
105+
Kind: "Deployment",
106+
Version: "v1",
107+
})
108+
_ = c.Create(context.Background(), u)
109+
}
110+
111+
// This example shows how to use the client with typed and unstrucurted objects to list objects.
112+
func ExampleClient_list() {
113+
// Using a typed object.
114+
p := &corev1.PodList{}
115+
// c is a created client.
116+
_ = c.List(context.Background(), nil, p)
117+
118+
// Using a unstructured object.
119+
u := &unstructured.UnstructuredList{}
120+
u.SetGroupVersionKind(schema.GroupVersionKind{
121+
Group: "apps",
122+
Kind: "DeploymentList",
123+
Version: "v1",
124+
})
125+
_ = c.List(context.Background(), nil, u)
126+
}
127+
128+
// This example shows how to use the client with typed and unstrucurted objects to update objects.
129+
func ExampleClient_update() {
130+
// Using a typed object.
131+
p := &corev1.Pod{}
132+
// c is a created client.
133+
_ = c.Get(context.Background(), client.ObjectKey{
134+
Namespace: "namespace",
135+
Name: "name",
136+
}, p)
137+
p.SetFinalizers(append(p.GetFinalizers(), "new-finalizer"))
138+
_ = c.Update(context.Background(), p)
139+
140+
// Using a unstructured object.
141+
u := &unstructured.Unstructured{}
142+
u.SetGroupVersionKind(schema.GroupVersionKind{
143+
Group: "apps",
144+
Kind: "Deployment",
145+
Version: "v1",
146+
})
147+
_ = c.Get(context.Background(), client.ObjectKey{
148+
Namespace: "namespace",
149+
Name: "name",
150+
}, u)
151+
u.SetFinalizers(append(u.GetFinalizers(), "new-finalizer"))
152+
_ = c.Update(context.Background(), u)
153+
}
154+
155+
// This example shows how to use the client with typed and unstrucurted objects to delete objects.
156+
func ExampleClient_delete() {
157+
// Using a typed object.
158+
p := &corev1.Pod{
159+
ObjectMeta: v1.ObjectMeta{
160+
Namespace: "namespace",
161+
Name: "name",
162+
},
163+
}
164+
// c is a created client.
165+
_ = c.Delete(context.Background(), p)
166+
167+
// Using a unstructured object.
168+
u := &unstructured.Unstructured{}
169+
u.Object = map[string]interface{}{
170+
"name": "name",
171+
"namespace": "namespace",
172+
}
173+
u.SetGroupVersionKind(schema.GroupVersionKind{
174+
Group: "apps",
175+
Kind: "Deployment",
176+
Version: "v1",
177+
})
178+
_ = c.Delete(context.Background(), u)
179+
}

pkg/controller/example_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"os"
2121

2222
"k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
2325
"sigs.k8s.io/controller-runtime/pkg/controller"
2426
"sigs.k8s.io/controller-runtime/pkg/handler"
2527
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -77,3 +79,37 @@ func ExampleController() {
7779
// Start the Controller through the manager.
7880
mgr.Start(signals.SetupSignalHandler())
7981
}
82+
83+
// This example starts a new Controller named "pod-controller" to Watch Pods with the unstructured object and call a no-op Reconciler.
84+
func ExampleController_unstructured() {
85+
// mgr is a manager.Manager
86+
87+
// Create a new Controller that will call the provided Reconciler function in response
88+
// to events.
89+
c, err := controller.New("pod-controller", mgr, controller.Options{
90+
Reconciler: reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
91+
// Your business logic to implement the API by creating, updating, deleting objects goes here.
92+
return reconcile.Result{}, nil
93+
}),
94+
})
95+
if err != nil {
96+
log.Error(err, "unable to create pod-controller")
97+
os.Exit(1)
98+
}
99+
100+
u := &unstructured.Unstructured{}
101+
u.SetGroupVersionKind(schema.GroupVersionKind{
102+
Kind: "Pod",
103+
Group: "",
104+
Version: "v1",
105+
})
106+
// Watch for Pod create / update / delete events and call Reconcile
107+
err = c.Watch(&source.Kind{Type: u}, &handler.EnqueueRequestForObject{})
108+
if err != nil {
109+
log.Error(err, "unable to watch pods")
110+
os.Exit(1)
111+
}
112+
113+
// Start the Controller through the manager.
114+
mgr.Start(signals.SetupSignalHandler())
115+
}

0 commit comments

Comments
 (0)