Skip to content

Commit a44d577

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

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

pkg/client/example_test.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
)
27+
28+
var (
29+
client client.Client
30+
)
31+
32+
// This example shows how to use the client with typed and unstrucurted objects to retrieve a objects.
33+
func ExampleClient_get() {
34+
// Using a typed object.
35+
p := &corev1.Pod{}
36+
// client is a created client.
37+
err := client.Get(context.Background(), client.ObjectKey{
38+
Namespace: "namespace",
39+
Name: "name",
40+
}, p)
41+
42+
// Using a unstructured object.
43+
u := &unstructured.Unstructured{}
44+
u.SetGroupVersionKind(schema.GroupVersionKind{
45+
Group: "apps",
46+
Kind: "Deployment",
47+
Version: "v1",
48+
})
49+
err := client.Get(context.Background(), client.ObjectKey{
50+
Namespace: "namespace",
51+
Name: "name",
52+
}, u)
53+
}
54+
55+
// This example shows how to use the client with typed and unstrucurted objects to create objects.
56+
func ExampleClient_create() {
57+
// Using a typed object.
58+
p := &corev1.Pod{
59+
ObjectMeta: v1.ObjectMeta{
60+
Namespace: "namespace",
61+
Name: "name",
62+
},
63+
Spec: corev1.PodSpec{
64+
Container: []corev1.Container{
65+
corev1.Container{
66+
Image: "nginx",
67+
Name: "nginx",
68+
},
69+
},
70+
},
71+
}
72+
// client is a created client.
73+
err := client.Create(context.Background(), p)
74+
75+
// Using a unstructured object.
76+
u := &unstructured.Unstructured{}
77+
u.Object = map[string]interface{}{
78+
"name": "name",
79+
"namespace": "namespace",
80+
"spec": map[string]interface{}{
81+
"replicas": 2,
82+
"selector": map[string]interface{}{
83+
"matchLabels": map[string]interface{}{
84+
"foo": "bar",
85+
},
86+
},
87+
"template": map[string]interface{}{
88+
"labels": map[string]interface{}{
89+
"foo": "bar",
90+
},
91+
"spec": map[string]interface{}{
92+
"containers": []map[string]interface{}{
93+
{
94+
"name": "nginx",
95+
"image": "nginx",
96+
},
97+
},
98+
},
99+
},
100+
},
101+
}
102+
u.SetGroupVersionKind(schema.GroupVersionKind{
103+
Group: "apps",
104+
Kind: "Deployment",
105+
Version: "v1",
106+
})
107+
err := client.Create(context.Background(), u)
108+
}
109+
110+
// This example shows how to use the client with typed and unstrucurted objects to list objects.
111+
func ExampleClient_list() {
112+
// Using a typed object.
113+
p := &corev1.PodList{}
114+
// client is a created client.
115+
err := client.List(context.Background(), nil, p)
116+
117+
// Using a unstructured object.
118+
u := &unstructured.UnstructuredList{}
119+
u.SetGroupVersionKind(schema.GroupVersionKind{
120+
Group: "apps",
121+
Kind: "DeploymentList",
122+
Version: "v1",
123+
})
124+
err = client.List(context.Background(), nil, u)
125+
}
126+
127+
// This example shows how to use the client with typed and unstrucurted objects to update objects.
128+
func ExampleClient_update() {
129+
// Using a typed object.
130+
p := &corev1.Pod{}
131+
// client is a created client.
132+
err := client.Get(context.Background(), client.ObjectKey{
133+
Namespace: "namespace",
134+
Name: "name",
135+
}, p)
136+
p.SetFinalizers(append(p.GetFinalizers(), "new-finalizer"))
137+
err = client.Update(context.Background(), p)
138+
139+
// Using a unstructured object.
140+
u = &unstructured.Unstructured{}
141+
u.SetGroupVersionKind(schema.GroupVersionKind{
142+
Group: "apps",
143+
Kind: "Deployment",
144+
Version: "v1",
145+
})
146+
err := client.Get(context.Background(), client.ObjectKey{
147+
Namespace: "namespace",
148+
Name: "name",
149+
}, u)
150+
u.SetFinalizers(append(u.GetFinalizers(), "new-finalizer"))
151+
err = client.Update(context.Background(), u)
152+
}
153+
154+
// This example shows how to use the client with typed and unstrucurted objects to delete objects.
155+
func ExampleClient_delete() {
156+
// Using a typed object.
157+
p := &corev1.Pod{
158+
ObjectMeta: v1.ObjectMeta{
159+
Namespace: "namespace",
160+
Name: "name",
161+
},
162+
}
163+
// client is a created client.
164+
err := client.Delete(context.Background(), p)
165+
166+
// Using a unstructured object.
167+
u := &unstructured.Unstructured{}
168+
u.Object = map[string]interface{}{
169+
"name": "name",
170+
"namespace": "namespace",
171+
}
172+
u.SetGroupVersionKind(schema.GroupVersionKind{
173+
Group: "apps",
174+
Kind: "Deployment",
175+
Version: "v1",
176+
})
177+
err = client.Delete(context.Background(), u)
178+
}

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)