|
| 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