@@ -31,10 +31,17 @@ var (
31
31
32
32
type stateFileIpsFunc func ([]byte ) (map [string ]string , error )
33
33
34
- type LinuxClient struct {}
34
+ type linuxValidator interface {
35
+ ValidateStateFile () error
36
+ ValidateRestartNetwork () error
37
+ }
35
38
36
- type LinuxValidator struct {
37
- Validator
39
+ var linuxChecksMap = map [string ][]check {
40
+ "cilium" : {
41
+ {"cns" , cnsStateFileIps , cnsLabelSelector , privilegedNamespace , cnsStateFileCmd },
42
+ {"cilium" , ciliumStateFileIps , ciliumLabelSelector , privilegedNamespace , ciliumStateFileCmd },
43
+ {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
44
+ },
38
45
}
39
46
40
47
type CnsState struct {
@@ -62,7 +69,11 @@ type Address struct {
62
69
Addr string `json:"ipv4"`
63
70
}
64
71
65
- func (l * LinuxClient ) CreateClient (ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) IValidator {
72
+ type LinuxClient struct {
73
+ validator linuxValidator
74
+ }
75
+
76
+ func (l * LinuxClient ) CreateClient (ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) * LinuxClient {
66
77
// deploy privileged pod
67
78
privilegedDaemonSet , err := k8sutils .MustParseDaemonSet (privilegedDaemonSetPath )
68
79
if err != nil {
@@ -77,64 +88,31 @@ func (l *LinuxClient) CreateClient(ctx context.Context, clienset *kubernetes.Cli
77
88
if err != nil {
78
89
panic (err )
79
90
}
80
- return & LinuxValidator {
81
- Validator : Validator {
91
+
92
+ return & LinuxClient {
93
+ validator : & validator {
82
94
ctx : ctx ,
83
95
clientset : clienset ,
84
96
config : config ,
85
97
namespace : namespace ,
86
98
cni : cni ,
87
99
restartCase : restartCase ,
100
+ checks : linuxChecksMap [cni ],
88
101
},
89
102
}
90
103
}
91
104
92
- // Todo: Based on cni version validate different state files
93
- func (v * LinuxValidator ) ValidateStateFile () error {
94
- checks := []struct {
95
- name string
96
- stateFileIps func ([]byte ) (map [string ]string , error )
97
- podLabelSelector string
98
- podNamespace string
99
- cmd []string
100
- }{
101
- {"cns" , cnsStateFileIps , cnsLabelSelector , privilegedNamespace , cnsStateFileCmd },
102
- {"cilium" , ciliumStateFileIps , ciliumLabelSelector , privilegedNamespace , ciliumStateFileCmd },
103
- {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
104
- }
105
-
106
- for _ , check := range checks {
107
- err := v .validate (check .stateFileIps , check .cmd , check .name , check .podNamespace , check .podLabelSelector )
108
- if err != nil {
109
- return err
110
- }
111
- }
112
- return nil
113
- }
114
-
115
- func (v * LinuxValidator ) ValidateRestartNetwork () error {
116
- nodes , err := k8sutils .GetNodeList (v .ctx , v .clientset )
105
+ func (l * LinuxClient ) Validate () error {
106
+ log .Printf ("Validating State File" )
107
+ err := l .validator .ValidateStateFile ()
117
108
if err != nil {
118
- return errors . Wrapf ( err , "failed to get node list" )
109
+ return err
119
110
}
120
-
121
- for index := range nodes .Items {
122
- // get the privileged pod
123
- pod , err := k8sutils .GetPodsByNode (v .ctx , v .clientset , privilegedNamespace , privilegedLabelSelector , nodes .Items [index ].Name )
124
- if err != nil {
125
- return errors .Wrapf (err , "failed to get privileged pod" )
126
- }
127
-
128
- privelegedPod := pod .Items [0 ]
129
- // exec into the pod to get the state file
130
- _ , err = k8sutils .ExecCmdOnPod (v .ctx , v .clientset , privilegedNamespace , privelegedPod .Name , restartNetworkCmd , v .config )
131
- if err != nil {
132
- return errors .Wrapf (err , "failed to exec into privileged pod" )
133
- }
134
- err = k8sutils .WaitForPodsRunning (v .ctx , v .clientset , "" , "" )
135
- if err != nil {
136
- return errors .Wrapf (err , "failed to wait for pods running" )
137
- }
111
+ //We are restarting the systmemd network and checking that the connectivity works after the restart. For more details: https://github.com/cilium/cilium/issues/18706
112
+ log .Printf ("Validating the restart network scenario" )
113
+ err = l .validator .ValidateRestartNetwork ()
114
+ if err != nil {
115
+ return err
138
116
}
139
117
return nil
140
118
}
@@ -190,43 +168,3 @@ func cnsCacheStateFileIps(result []byte) (map[string]string, error) {
190
168
}
191
169
return cnsPodIps , nil
192
170
}
193
-
194
- func (v * LinuxValidator ) validate (stateFileIps stateFileIpsFunc , cmd []string , checkType , namespace , labelSelector string ) error {
195
- log .Printf ("Validating %s state file" , checkType )
196
- nodes , err := k8sutils .GetNodeList (v .ctx , v .clientset )
197
- if err != nil {
198
- return errors .Wrapf (err , "failed to get node list" )
199
- }
200
-
201
- for index := range nodes .Items {
202
- // get the privileged pod
203
- pod , err := k8sutils .GetPodsByNode (v .ctx , v .clientset , namespace , labelSelector , nodes .Items [index ].Name )
204
- if err != nil {
205
- return errors .Wrapf (err , "failed to get privileged pod" )
206
- }
207
- podName := pod .Items [0 ].Name
208
- // exec into the pod to get the state file
209
- result , err := k8sutils .ExecCmdOnPod (v .ctx , v .clientset , namespace , podName , cmd , v .config )
210
- if err != nil {
211
- return errors .Wrapf (err , "failed to exec into privileged pod" )
212
- }
213
- filePodIps , err := stateFileIps (result )
214
- if err != nil {
215
- return errors .Wrapf (err , "failed to get pod ips from state file" )
216
- }
217
- if len (filePodIps ) == 0 && v .restartCase {
218
- log .Printf ("No pods found on node %s" , nodes .Items [index ].Name )
219
- continue
220
- }
221
- // get the pod ips
222
- podIps := getPodIPsWithoutNodeIP (v .ctx , v .clientset , nodes .Items [index ])
223
-
224
- check := compareIPs (filePodIps , podIps )
225
-
226
- if ! check {
227
- return errors .Wrapf (errors .New ("State file validation failed" ), "for %s on node %s" , checkType , nodes .Items [index ].Name )
228
- }
229
- }
230
- log .Printf ("State file validation for %s passed" , checkType )
231
- return nil
232
- }
0 commit comments