@@ -31,10 +31,12 @@ var (
3131
3232type stateFileIpsFunc func ([]byte ) (map [string ]string , error )
3333
34- type LinuxClient struct {}
35-
3634type LinuxValidator struct {
37- Validator
35+ clientset * kubernetes.Clientset
36+ config * rest.Config
37+ namespace string
38+ cni string
39+ restartCase bool
3840}
3941
4042type CnsState struct {
@@ -62,35 +64,31 @@ type Address struct {
6264 Addr string `json:"ipv4"`
6365}
6466
65- func ( l * LinuxClient ) CreateClient ( ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) IValidator {
67+ func CreateLinuxValidator ( ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) ( * LinuxValidator , error ) {
6668 // deploy privileged pod
6769 privilegedDaemonSet , err := k8sutils .MustParseDaemonSet (privilegedDaemonSetPath )
6870 if err != nil {
69- panic (err )
71+ return nil , errors . Wrap (err , "unable to parse daemonset" )
7072 }
7173 daemonsetClient := clienset .AppsV1 ().DaemonSets (privilegedNamespace )
72- err = k8sutils .MustCreateDaemonset (ctx , daemonsetClient , privilegedDaemonSet )
73- if err != nil {
74- panic (err )
74+ if err := k8sutils .MustCreateDaemonset (ctx , daemonsetClient , privilegedDaemonSet ); err != nil {
75+ return nil , errors .Wrap (err , "unable to create daemonset" )
7576 }
76- err = k8sutils .WaitForPodsRunning (ctx , clienset , privilegedNamespace , privilegedLabelSelector )
77- if err != nil {
78- panic (err )
77+ if err := k8sutils .WaitForPodsRunning (ctx , clienset , privilegedNamespace , privilegedLabelSelector ); err != nil {
78+ return nil , errors .Wrap (err , "error while waiting for pods to be running" )
7979 }
80+
8081 return & LinuxValidator {
81- Validator : Validator {
82- ctx : ctx ,
83- clientset : clienset ,
84- config : config ,
85- namespace : namespace ,
86- cni : cni ,
87- restartCase : restartCase ,
88- },
89- }
82+ clientset : clienset ,
83+ config : config ,
84+ namespace : namespace ,
85+ cni : cni ,
86+ restartCase : restartCase ,
87+ }, nil
9088}
9189
9290// Todo: Based on cni version validate different state files
93- func (v * LinuxValidator ) ValidateStateFile () error {
91+ func (v * LinuxValidator ) ValidateStateFile (ctx context. Context ) error {
9492 checkSet := make (map [string ][]check ) // key is cni type, value is a list of check
9593 // TODO: add cniv1 when adding Linux related test cases
9694 checkSet ["cilium" ] = []check {
@@ -104,34 +102,34 @@ func (v *LinuxValidator) ValidateStateFile() error {
104102 }
105103
106104 for _ , check := range checkSet [v .cni ] {
107- err := v .validateIPs (check .stateFileIps , check .cmd , check .name , check .podNamespace , check .podLabelSelector )
105+ err := v .validateIPs (ctx , check .stateFileIps , check .cmd , check .name , check .podNamespace , check .podLabelSelector )
108106 if err != nil {
109107 return err
110108 }
111109 }
112110 return nil
113111}
114112
115- func (v * LinuxValidator ) ValidateRestartNetwork () error {
116- nodes , err := k8sutils .GetNodeList (v . ctx , v .clientset )
113+ func (v * LinuxValidator ) ValidateRestartNetwork (ctx context. Context ) error {
114+ nodes , err := k8sutils .GetNodeList (ctx , v .clientset )
117115 if err != nil {
118116 return errors .Wrapf (err , "failed to get node list" )
119117 }
120118
121119 for index := range nodes .Items {
122120 // get the privileged pod
123- pod , err := k8sutils .GetPodsByNode (v . ctx , v .clientset , privilegedNamespace , privilegedLabelSelector , nodes .Items [index ].Name )
121+ pod , err := k8sutils .GetPodsByNode (ctx , v .clientset , privilegedNamespace , privilegedLabelSelector , nodes .Items [index ].Name )
124122 if err != nil {
125123 return errors .Wrapf (err , "failed to get privileged pod" )
126124 }
127125
128126 privelegedPod := pod .Items [0 ]
129127 // exec into the pod to get the state file
130- _ , err = k8sutils .ExecCmdOnPod (v . ctx , v .clientset , privilegedNamespace , privelegedPod .Name , restartNetworkCmd , v .config )
128+ _ , err = k8sutils .ExecCmdOnPod (ctx , v .clientset , privilegedNamespace , privelegedPod .Name , restartNetworkCmd , v .config )
131129 if err != nil {
132130 return errors .Wrapf (err , "failed to exec into privileged pod" )
133131 }
134- err = k8sutils .WaitForPodsRunning (v . ctx , v .clientset , "" , "" )
132+ err = k8sutils .WaitForPodsRunning (ctx , v .clientset , "" , "" )
135133 if err != nil {
136134 return errors .Wrapf (err , "failed to wait for pods running" )
137135 }
@@ -191,22 +189,22 @@ func cnsCacheStateFileIps(result []byte) (map[string]string, error) {
191189 return cnsPodIps , nil
192190}
193191
194- func (v * LinuxValidator ) validateIPs (stateFileIps stateFileIpsFunc , cmd []string , checkType , namespace , labelSelector string ) error {
192+ func (v * LinuxValidator ) validateIPs (ctx context. Context , stateFileIps stateFileIpsFunc , cmd []string , checkType , namespace , labelSelector string ) error {
195193 log .Printf ("Validating %s state file" , checkType )
196- nodes , err := k8sutils .GetNodeList (v . ctx , v .clientset )
194+ nodes , err := k8sutils .GetNodeList (ctx , v .clientset )
197195 if err != nil {
198196 return errors .Wrapf (err , "failed to get node list" )
199197 }
200198
201199 for index := range nodes .Items {
202200 // get the privileged pod
203- pod , err := k8sutils .GetPodsByNode (v . ctx , v .clientset , namespace , labelSelector , nodes .Items [index ].Name )
201+ pod , err := k8sutils .GetPodsByNode (ctx , v .clientset , namespace , labelSelector , nodes .Items [index ].Name )
204202 if err != nil {
205203 return errors .Wrapf (err , "failed to get privileged pod" )
206204 }
207205 podName := pod .Items [0 ].Name
208206 // exec into the pod to get the state file
209- result , err := k8sutils .ExecCmdOnPod (v . ctx , v .clientset , namespace , podName , cmd , v .config )
207+ result , err := k8sutils .ExecCmdOnPod (ctx , v .clientset , namespace , podName , cmd , v .config )
210208 if err != nil {
211209 return errors .Wrapf (err , "failed to exec into privileged pod" )
212210 }
@@ -219,7 +217,7 @@ func (v *LinuxValidator) validateIPs(stateFileIps stateFileIpsFunc, cmd []string
219217 continue
220218 }
221219 // get the pod ips
222- podIps := getPodIPsWithoutNodeIP (v . ctx , v .clientset , nodes .Items [index ])
220+ podIps := getPodIPsWithoutNodeIP (ctx , v .clientset , nodes .Items [index ])
223221
224222 check := compareIPs (filePodIps , podIps )
225223
0 commit comments