Skip to content

Commit c386fb0

Browse files
committed
Add example of using resource builder to load a manifest file
1 parent 8d74486 commit c386fb0

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

staging/src/k8s.io/cli-runtime/pkg/resource/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ go_library(
5656
go_test(
5757
name = "go_default_test",
5858
srcs = [
59+
"builder_example_test.go",
5960
"builder_test.go",
6061
"crd_finder_test.go",
6162
"dry_run_verifier_test.go",

staging/src/k8s.io/cli-runtime/pkg/resource/builder.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
"k8s.io/apimachinery/pkg/runtime/serializer"
3636
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3737
"k8s.io/apimachinery/pkg/util/sets"
38+
"k8s.io/client-go/discovery"
39+
"k8s.io/client-go/rest"
3840
"k8s.io/client-go/restmapper"
3941
)
4042

@@ -176,6 +178,25 @@ func newBuilder(clientConfigFn ClientConfigFunc, restMapper RESTMapperFunc, cate
176178
}
177179
}
178180

181+
// noopClientGetter implements RESTClientGetter returning only errors.
182+
// used as a dummy getter in a local-only builder.
183+
type noopClientGetter struct{}
184+
185+
func (noopClientGetter) ToRESTConfig() (*rest.Config, error) {
186+
return nil, fmt.Errorf("local operation only")
187+
}
188+
func (noopClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
189+
return nil, fmt.Errorf("local operation only")
190+
}
191+
func (noopClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
192+
return nil, fmt.Errorf("local operation only")
193+
}
194+
195+
// NewLocalBuilder returns a builder that is configured not to create REST clients and avoids asking the server for results.
196+
func NewLocalBuilder() *Builder {
197+
return NewBuilder(noopClientGetter{}).Local()
198+
}
199+
179200
func NewBuilder(restClientGetter RESTClientGetter) *Builder {
180201
categoryExpanderFn := func() (restmapper.CategoryExpander, error) {
181202
discoveryClient, err := restClientGetter.ToDiscoveryClient()
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Copyright 2020 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 resource_test
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
23+
"k8s.io/cli-runtime/pkg/resource"
24+
"k8s.io/client-go/kubernetes/scheme"
25+
)
26+
27+
var exampleManifest = `
28+
apiVersion: admissionregistration.k8s.io/v1
29+
kind: MutatingWebhookConfiguration
30+
metadata:
31+
name: mutating1
32+
---
33+
apiVersion: admissionregistration.k8s.io/v1
34+
kind: MutatingWebhookConfigurationList
35+
items:
36+
- apiVersion: admissionregistration.k8s.io/v1
37+
kind: MutatingWebhookConfiguration
38+
metadata:
39+
name: mutating2
40+
- apiVersion: admissionregistration.k8s.io/v1
41+
kind: MutatingWebhookConfiguration
42+
metadata:
43+
name: mutating3
44+
---
45+
apiVersion: admissionregistration.k8s.io/v1
46+
kind: ValidatingWebhookConfiguration
47+
metadata:
48+
name: validating1
49+
---
50+
apiVersion: admissionregistration.k8s.io/v1
51+
kind: ValidatingWebhookConfigurationList
52+
items:
53+
- apiVersion: admissionregistration.k8s.io/v1
54+
kind: ValidatingWebhookConfiguration
55+
metadata:
56+
name: validating2
57+
- apiVersion: admissionregistration.k8s.io/v1
58+
kind: ValidatingWebhookConfiguration
59+
metadata:
60+
name: validating3
61+
---
62+
apiVersion: v1
63+
kind: List
64+
items:
65+
- apiVersion: admissionregistration.k8s.io/v1
66+
kind: MutatingWebhookConfiguration
67+
metadata:
68+
name: mutating4
69+
- apiVersion: admissionregistration.k8s.io/v1
70+
kind: ValidatingWebhookConfiguration
71+
metadata:
72+
name: validating4
73+
---
74+
`
75+
76+
// ExampleLocalBuilderLoad demonstrates using a local resource builder to read typed resources from a manifest
77+
func ExampleLocalBuilder() {
78+
// Create a local builder...
79+
builder := resource.NewLocalBuilder().
80+
// Configure with a scheme to get typed objects in the versions registered with the scheme.
81+
// As an alternative, could call Unstructured() to get unstructured objects.
82+
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
83+
// Provide input via a Reader.
84+
// As an alternative, could call Path(false, "/path/to/file") to read from a file.
85+
Stream(bytes.NewBufferString(exampleManifest), "input").
86+
// Flatten items contained in List objects
87+
Flatten().
88+
// Accumulate as many items as possible
89+
ContinueOnError()
90+
91+
// Run the builder
92+
result := builder.Do()
93+
94+
if err := result.Err(); err != nil {
95+
fmt.Println("builder error:", err)
96+
return
97+
}
98+
99+
items, err := result.Infos()
100+
if err != nil {
101+
fmt.Println("infos error:", err)
102+
return
103+
}
104+
105+
for _, item := range items {
106+
fmt.Printf("%s (%T)\n", item.String(), item.Object)
107+
}
108+
109+
// Output:
110+
// Name: "mutating1", Namespace: "" (*v1.MutatingWebhookConfiguration)
111+
// Name: "mutating2", Namespace: "" (*v1.MutatingWebhookConfiguration)
112+
// Name: "mutating3", Namespace: "" (*v1.MutatingWebhookConfiguration)
113+
// Name: "validating1", Namespace: "" (*v1.ValidatingWebhookConfiguration)
114+
// Name: "validating2", Namespace: "" (*v1.ValidatingWebhookConfiguration)
115+
// Name: "validating3", Namespace: "" (*v1.ValidatingWebhookConfiguration)
116+
// Name: "mutating4", Namespace: "" (*v1.MutatingWebhookConfiguration)
117+
// Name: "validating4", Namespace: "" (*v1.ValidatingWebhookConfiguration)
118+
}

0 commit comments

Comments
 (0)