@@ -31,10 +31,20 @@ 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
+ },
45
+ "cniv2" : {
46
+ {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
47
+ },
38
48
}
39
49
40
50
type CnsState struct {
@@ -62,7 +72,11 @@ type Address struct {
62
72
Addr string `json:"ipv4"`
63
73
}
64
74
65
- func (l * LinuxClient ) CreateClient (ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) IValidator {
75
+ type LinuxClient struct {
76
+ validator linuxValidator
77
+ }
78
+
79
+ func (l * LinuxClient ) CreateClient (ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) * LinuxClient {
66
80
// deploy privileged pod
67
81
privilegedDaemonSet , err := k8sutils .MustParseDaemonSet (privilegedDaemonSetPath )
68
82
if err != nil {
@@ -77,64 +91,31 @@ func (l *LinuxClient) CreateClient(ctx context.Context, clienset *kubernetes.Cli
77
91
if err != nil {
78
92
panic (err )
79
93
}
80
- return & LinuxValidator {
81
- Validator : Validator {
94
+
95
+ return & LinuxClient {
96
+ validator : & validator {
82
97
ctx : ctx ,
83
98
clientset : clienset ,
84
99
config : config ,
85
100
namespace : namespace ,
86
101
cni : cni ,
87
102
restartCase : restartCase ,
103
+ checks : linuxChecksMap [cni ],
88
104
},
89
105
}
90
106
}
91
107
92
- // Todo: Based on cni version validate different state files
93
- func (v * LinuxValidator ) ValidateStateFile () error {
94
- checkSet := make (map [string ][]check ) // key is cni type, value is a list of check
95
- // TODO: add cniv1 when adding Linux related test cases
96
- checkSet ["cilium" ] = []check {
97
- {"cns" , cnsStateFileIps , cnsLabelSelector , privilegedNamespace , cnsStateFileCmd },
98
- {"cilium" , ciliumStateFileIps , ciliumLabelSelector , privilegedNamespace , ciliumStateFileCmd },
99
- {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
100
- }
101
-
102
- checkSet ["cniv2" ] = []check {
103
- {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
104
- }
105
-
106
- for _ , check := range checkSet [v .cni ] {
107
- err := v .validateIPs (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 )
108
+ func (l * LinuxClient ) Validate () error {
109
+ log .Printf ("Validating State File" )
110
+ err := l .validator .ValidateStateFile ()
117
111
if err != nil {
118
- return errors .Wrapf (err , "failed to get node list " )
112
+ return errors .Wrapf (err , "failed to validate state file " )
119
113
}
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
- }
114
+ // 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
115
+ log .Printf ("Validating the restart network scenario" )
116
+ err = l .validator .ValidateRestartNetwork ()
117
+ if err != nil {
118
+ return errors .Wrapf (err , "failed to validate restart network scenario" )
138
119
}
139
120
return nil
140
121
}
@@ -190,43 +171,3 @@ func cnsCacheStateFileIps(result []byte) (map[string]string, error) {
190
171
}
191
172
return cnsPodIps , nil
192
173
}
193
-
194
- func (v * LinuxValidator ) validateIPs (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