-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adding documentation for using unstructured for client and controller #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:master
from
shawn-hurley:doc/add-unstructured-docs
Oct 29, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package client_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
) | ||
|
||
var ( | ||
c client.Client | ||
) | ||
|
||
func ExampleNew() { | ||
cl, err := client.New(config.GetConfigOrDie(), client.Options{}) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
os.Exit(1) | ||
} | ||
|
||
podList := &corev1.PodList{} | ||
|
||
err = cl.List(context.Background(), client.InNamespace("default"), podList) | ||
if err != nil { | ||
fmt.Printf("failed to list pods in namespace default: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// This example shows how to use the client with typed and unstructured objects to retrieve a objects. | ||
func ExampleClient_get() { | ||
// Using a typed object. | ||
pod := &corev1.Pod{} | ||
// c is a created client. | ||
_ = c.Get(context.Background(), client.ObjectKey{ | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, pod) | ||
|
||
// Using a unstructured object. | ||
u := &unstructured.Unstructured{} | ||
u.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "apps", | ||
Kind: "Deployment", | ||
Version: "v1", | ||
}) | ||
_ = c.Get(context.Background(), client.ObjectKey{ | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, u) | ||
} | ||
|
||
// This example shows how to use the client with typed and unstrucurted objects to create objects. | ||
func ExampleClient_create() { | ||
// Using a typed object. | ||
pod := &corev1.Pod{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Image: "nginx", | ||
Name: "nginx", | ||
}, | ||
}, | ||
}, | ||
} | ||
// c is a created client. | ||
_ = c.Create(context.Background(), pod) | ||
|
||
// Using a unstructured object. | ||
u := &unstructured.Unstructured{} | ||
u.Object = map[string]interface{}{ | ||
"name": "name", | ||
"namespace": "namespace", | ||
"spec": map[string]interface{}{ | ||
"replicas": 2, | ||
"selector": map[string]interface{}{ | ||
"matchLabels": map[string]interface{}{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
"template": map[string]interface{}{ | ||
"labels": map[string]interface{}{ | ||
"foo": "bar", | ||
}, | ||
"spec": map[string]interface{}{ | ||
"containers": []map[string]interface{}{ | ||
{ | ||
"name": "nginx", | ||
"image": "nginx", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
u.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "apps", | ||
Kind: "Deployment", | ||
Version: "v1", | ||
}) | ||
_ = c.Create(context.Background(), u) | ||
} | ||
|
||
// This example shows how to use the client with typed and unstrucurted objects to list objects. | ||
func ExampleClient_list() { | ||
// Using a typed object. | ||
pod := &corev1.PodList{} | ||
// c is a created client. | ||
_ = c.List(context.Background(), nil, pod) | ||
|
||
// Using a unstructured object. | ||
u := &unstructured.UnstructuredList{} | ||
u.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "apps", | ||
Kind: "DeploymentList", | ||
Version: "v1", | ||
}) | ||
_ = c.List(context.Background(), nil, u) | ||
} | ||
|
||
// This example shows how to use the client with typed and unstrucurted objects to update objects. | ||
func ExampleClient_update() { | ||
// Using a typed object. | ||
pod := &corev1.Pod{} | ||
// c is a created client. | ||
_ = c.Get(context.Background(), client.ObjectKey{ | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, pod) | ||
pod.SetFinalizers(append(pod.GetFinalizers(), "new-finalizer")) | ||
_ = c.Update(context.Background(), pod) | ||
|
||
// Using a unstructured object. | ||
u := &unstructured.Unstructured{} | ||
u.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "apps", | ||
Kind: "Deployment", | ||
Version: "v1", | ||
}) | ||
_ = c.Get(context.Background(), client.ObjectKey{ | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, u) | ||
shawn-hurley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
u.SetFinalizers(append(u.GetFinalizers(), "new-finalizer")) | ||
_ = c.Update(context.Background(), u) | ||
} | ||
|
||
// This example shows how to use the client with typed and unstrucurted objects to delete objects. | ||
func ExampleClient_delete() { | ||
// Using a typed object. | ||
pod := &corev1.Pod{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Namespace: "namespace", | ||
Name: "name", | ||
}, | ||
} | ||
// c is a created client. | ||
_ = c.Delete(context.Background(), pod) | ||
|
||
// Using a unstructured object. | ||
u := &unstructured.Unstructured{} | ||
u.Object = map[string]interface{}{ | ||
"name": "name", | ||
"namespace": "namespace", | ||
} | ||
u.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "apps", | ||
Kind: "Deployment", | ||
Version: "v1", | ||
}) | ||
_ = c.Delete(context.Background(), u) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.