gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/gvisor_k8s_tool/cluster/cluster.go (about) 1 // Copyright 2023 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package cluster provides functions for dealing with Kubernetes clusters. 16 package cluster 17 18 import ( 19 "context" 20 "fmt" 21 22 appsv1 "k8s.io/api/apps/v1" 23 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/fields" 25 "k8s.io/client-go/kubernetes" 26 "k8s.io/client-go/rest" 27 ) 28 29 const ( 30 // NamespaceDefault is the name of the default Kubernetes namespace. 31 NamespaceDefault = "default" 32 ) 33 34 // Cluster presents Kubernetes API method over a Kubernetes cluster. 35 type Cluster struct { 36 client kubernetes.Interface 37 } 38 39 // New initializes a new Cluster from the given client REST config. 40 func New(config *rest.Config) (*Cluster, error) { 41 clientSet, err := kubernetes.NewForConfig(config) 42 if err != nil { 43 return nil, fmt.Errorf("kubernetes.NewForConfig: %w", err) 44 } 45 return &Cluster{clientSet}, nil 46 } 47 48 // CreateDaemonset creates a daemonset with default options. 49 func (c *Cluster) CreateDaemonset(ctx context.Context, ds *appsv1.DaemonSet) (*appsv1.DaemonSet, error) { 50 if ds.GetObjectMeta().GetNamespace() == "" { 51 ds.SetNamespace(NamespaceDefault) 52 } 53 return c.client.AppsV1().DaemonSets(ds.GetNamespace()).Create(ctx, ds, v1.CreateOptions{}) 54 } 55 56 // DeleteDaemonset deletes a daemonset from this cluster. 57 func (c *Cluster) DeleteDaemonset(ctx context.Context, ds *appsv1.DaemonSet) error { 58 return c.client.AppsV1().DaemonSets(ds.GetNamespace()).Delete(ctx, ds.GetName(), v1.DeleteOptions{}) 59 } 60 61 // WaitForDaemonset waits until a daemonset has propagated containers across the affected nodes. 62 func (c *Cluster) WaitForDaemonset(ctx context.Context, ds *appsv1.DaemonSet) error { 63 w, err := c.client.AppsV1().DaemonSets(ds.GetNamespace()).Watch(ctx, v1.ListOptions{ 64 FieldSelector: fields.SelectorFromSet(fields.Set{v1.ObjectNameField: ds.ObjectMeta.Name}).String(), 65 }) 66 67 if err != nil { 68 return fmt.Errorf("failed to watch DaemonSet: %w", err) 69 } 70 defer w.Stop() 71 for { 72 select { 73 case <-ctx.Done(): 74 return fmt.Errorf("context canceled before DaemonSet was healthy") 75 case e, ok := <-w.ResultChan(): 76 d, ok := e.Object.(*appsv1.DaemonSet) 77 if !ok { 78 return fmt.Errorf("invalid object type: %T", d) 79 } 80 if d.Status.NumberReady == d.Status.DesiredNumberScheduled && d.Status.NumberUnavailable == 0 { 81 return nil 82 } 83 } 84 } 85 }