Skip to content

Commit a8ea205

Browse files
authored
Merge pull request kubernetes-sigs#184 from shawn-hurley/doc/add-unstructured-docs
Adding documentation for using unstructured for client and controller
2 parents 5fd1e9e + d31a1c1 commit a8ea205

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

pkg/client/example_test.go

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

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)