|
| 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 kubeobjects |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 21 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// DaemonSet is a wrapper around the apps/v1 DaemonSet type. |
| 27 | +type DaemonSet struct { |
| 28 | + u *unstructured.Unstructured |
| 29 | +} |
| 30 | + |
| 31 | +// Name returns the name of the daemonset. |
| 32 | +func (d *DaemonSet) Name() string { |
| 33 | + return d.u.GetName() |
| 34 | +} |
| 35 | + |
| 36 | +// Namespace returns the namespace of the daemonset. |
| 37 | +func (d *DaemonSet) Namespace() string { |
| 38 | + return d.u.GetNamespace() |
| 39 | +} |
| 40 | + |
| 41 | +// NumberReady returns the number of ready pods in the daemonset. |
| 42 | +func (d *DaemonSet) NumberReady() int64 { |
| 43 | + val, _, _ := unstructured.NestedInt64(d.u.Object, "status", "numberReady") |
| 44 | + return val |
| 45 | +} |
| 46 | + |
| 47 | +// DesiredNumberScheduled returns the desired number of pods in the daemonset. |
| 48 | +func (d *DaemonSet) DesiredNumberScheduled() int64 { |
| 49 | + val, _, _ := unstructured.NestedInt64(d.u.Object, "status", "desiredNumberScheduled") |
| 50 | + return val |
| 51 | +} |
| 52 | + |
| 53 | +var daemonSetGVR = schema.GroupVersionResource{ |
| 54 | + Group: "apps", |
| 55 | + Version: "v1", |
| 56 | + Resource: "daemonsets", |
| 57 | +} |
| 58 | + |
| 59 | +// ListDaemonSets lists all daemonsets in the given namespace. |
| 60 | +func (c *Client) ListDaemonSets(namespace string) []*DaemonSet { |
| 61 | + objectList, err := c.dynamicClient.Resource(daemonSetGVR).Namespace(namespace).List(c.ctx, metav1.ListOptions{}) |
| 62 | + if err != nil { |
| 63 | + c.t.Fatalf("failed to list daemonsets: %v", err) |
| 64 | + } |
| 65 | + var out []*DaemonSet |
| 66 | + for i := range objectList.Items { |
| 67 | + out = append(out, &DaemonSet{u: &objectList.Items[i]}) |
| 68 | + } |
| 69 | + return out |
| 70 | +} |
0 commit comments