@@ -2,6 +2,7 @@ package kubernetes
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "strings"
78 "time"
@@ -14,14 +15,19 @@ import (
1415 "k8s.io/client-go/tools/clientcmd"
1516)
1617
18+ var (
19+ ErrNoWindowsPodFound = errors .New ("no Windows Pod found in label" )
20+ ErrNoCommandOutput = errors .New ("no output from command" )
21+ ErrLoadPinBPFFailed = errors .New ("error in loading and pinning BPF maps and program" )
22+ )
23+
1724type LoadAndPinWinBPF struct {
1825 KubeConfigFilePath string
1926 LoadAndPinWinBPFDeamonSetNamespace string
2027 LoadAndPinWinBPFDeamonSetName string
2128}
2229
2330func WaitForPodReadyWithTimeOut (ctx context.Context , kubeConfigFilePath , namespace , labelSelector string , timeout time.Duration ) error {
24-
2531 config , _ := clientcmd .BuildConfigFromFlags ("" , kubeConfigFilePath )
2632 clientset , _ := kubernetes .NewForConfig (config )
2733
@@ -31,12 +37,12 @@ func WaitForPodReadyWithTimeOut(ctx context.Context, kubeConfigFilePath, namespa
3137 return WaitForPodReady (timeoutCtx , clientset , namespace , labelSelector )
3238}
3339
34- func ExecCommandInWinPod (KubeConfigFilePath string , cmd string , Namespace string , LabelSelector string , expecNonEmptyOutput bool ) (string , error ) {
40+ func ExecCommandInWinPod (kubeConfigFilePath , cmd , namespace , labelSelector string , expecNonEmptyOutput bool ) (string , error ) {
3541 defaultRetrier = retry.Retrier {Attempts : 15 , Delay : 5 * time .Second }
3642 // Create a context with a timeout (e.g., 120 seconds)
3743 ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
3844 defer cancel ()
39- config , err := clientcmd .BuildConfigFromFlags ("" , KubeConfigFilePath )
45+ config , err := clientcmd .BuildConfigFromFlags ("" , kubeConfigFilePath )
4046 if err != nil {
4147 return "" , fmt .Errorf ("error building kubeconfig: %w" , err )
4248 }
@@ -46,8 +52,8 @@ func ExecCommandInWinPod(KubeConfigFilePath string, cmd string, Namespace string
4652 return "" , fmt .Errorf ("error creating Kubernetes client: %w" , err )
4753 }
4854
49- pods , err := clientset .CoreV1 ().Pods (Namespace ).List (ctx , metav1.ListOptions {
50- LabelSelector : LabelSelector ,
55+ pods , err := clientset .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {
56+ LabelSelector : labelSelector ,
5157 })
5258 if err != nil {
5359 return "" , fmt .Errorf ("error listing pods: %w" , err )
@@ -65,7 +71,7 @@ func ExecCommandInWinPod(KubeConfigFilePath string, cmd string, Namespace string
6571 }
6672
6773 if windowsPod == nil {
68- return "" , fmt .Errorf ("no Windows Pod found in label %s" , LabelSelector )
74+ return "" , fmt .Errorf ("%w: %s" , ErrNoWindowsPodFound , labelSelector )
6975 }
7076
7177 var outputBytes []byte
@@ -77,12 +83,11 @@ func ExecCommandInWinPod(KubeConfigFilePath string, cmd string, Namespace string
7783 }
7884
7985 if len (outputBytes ) == 0 && expecNonEmptyOutput {
80- return fmt . Errorf ( "no output from command" )
86+ return ErrNoCommandOutput
8187 }
8288
8389 return nil
8490 })
85-
8691 if err != nil {
8792 return "" , err
8893 }
@@ -92,7 +97,7 @@ func ExecCommandInWinPod(KubeConfigFilePath string, cmd string, Namespace string
9297
9398func (a * LoadAndPinWinBPF ) Run () error {
9499 // Copy Event Writer into Node
95- LoadAndPinWinBPFDLabelSelector := fmt . Sprintf ( "name=%s" , a .LoadAndPinWinBPFDeamonSetName )
100+ LoadAndPinWinBPFDLabelSelector := "name=" + a .LoadAndPinWinBPFDeamonSetName
96101 _ , err := ExecCommandInWinPod (a .KubeConfigFilePath , "copy /Y .\\ event-writer-helper.bat C:\\ event-writer-helper.bat" , a .LoadAndPinWinBPFDeamonSetNamespace , LoadAndPinWinBPFDLabelSelector , true )
97102 if err != nil {
98103 return err
@@ -111,7 +116,7 @@ func (a *LoadAndPinWinBPF) Run() error {
111116
112117 fmt .Println (output )
113118 if strings .Contains (output , "error" ) || strings .Contains (output , "failed" ) || strings .Contains (output , "existing" ) {
114- return fmt .Errorf ("error in loading and pinning BPF maps and program : %s" , output )
119+ return fmt .Errorf ("%w : %s" , ErrLoadPinBPFFailed , output )
115120 }
116121 return nil
117122}
0 commit comments