|
| 1 | +/* |
| 2 | +Copyright 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 validators |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 21 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 22 | + "k8s.io/client-go/dynamic" |
| 23 | + |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | +) |
| 26 | + |
| 27 | +func (h *ValidatorHarness) DynamicClient() dynamic.Interface { |
| 28 | + if h.dynamicClient == nil { |
| 29 | + dynamicClient, err := dynamic.NewForConfig(h.restConfig) |
| 30 | + if err != nil { |
| 31 | + h.Fatalf("failed to create dynamic client: %v", err) |
| 32 | + } |
| 33 | + h.dynamicClient = dynamicClient |
| 34 | + } |
| 35 | + return h.dynamicClient |
| 36 | +} |
| 37 | + |
| 38 | +type DeviceClass struct { |
| 39 | + u *unstructured.Unstructured |
| 40 | +} |
| 41 | + |
| 42 | +func (d *DeviceClass) Name() string { |
| 43 | + return d.u.GetName() |
| 44 | +} |
| 45 | + |
| 46 | +var deviceClassGVR = schema.GroupVersionResource{ |
| 47 | + Group: "resource.k8s.io", |
| 48 | + Version: "v1", |
| 49 | + Resource: "deviceclasses", |
| 50 | +} |
| 51 | + |
| 52 | +func (h *ValidatorHarness) ListDeviceClasses() []*DeviceClass { |
| 53 | + objectList, err := h.DynamicClient().Resource(deviceClassGVR).List(h.Context(), metav1.ListOptions{}) |
| 54 | + if err != nil { |
| 55 | + h.Fatalf("failed to list device classes: %v", err) |
| 56 | + } |
| 57 | + var out []*DeviceClass |
| 58 | + for i := range objectList.Items { |
| 59 | + out = append(out, &DeviceClass{u: &objectList.Items[i]}) |
| 60 | + } |
| 61 | + return out |
| 62 | +} |
0 commit comments