1
1
package validate
2
2
3
3
import (
4
- "context"
5
4
"encoding/json"
6
- "log"
7
5
8
6
"github.com/Azure/azure-container-networking/cns"
9
7
restserver "github.com/Azure/azure-container-networking/cns/restserver"
10
- k8sutils "github.com/Azure/azure-container-networking/test/internal/k8sutils"
11
8
"github.com/pkg/errors"
12
- "k8s.io/client-go/kubernetes"
13
- "k8s.io/client-go/rest"
14
9
)
15
10
16
11
const (
17
- privilegedDaemonSetPath = "../manifests/load/privileged-daemonset.yaml"
18
- privilegedLabelSelector = "app=privileged-daemonset"
19
- privilegedNamespace = "kube-system"
20
-
21
12
cnsLabelSelector = "k8s-app=azure-cns"
22
13
ciliumLabelSelector = "k8s-app=cilium"
23
14
)
@@ -31,12 +22,15 @@ var (
31
22
32
23
type stateFileIpsFunc func ([]byte ) (map [string ]string , error )
33
24
34
- type LinuxValidator struct {
35
- clientset * kubernetes.Clientset
36
- config * rest.Config
37
- namespace string
38
- cni string
39
- restartCase bool
25
+ var linuxChecksMap = map [string ][]check {
26
+ "cilium" : {
27
+ {"cns" , cnsStateFileIps , cnsLabelSelector , privilegedNamespace , cnsStateFileCmd },
28
+ {"cilium" , ciliumStateFileIps , ciliumLabelSelector , privilegedNamespace , ciliumStateFileCmd },
29
+ {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
30
+ },
31
+ "cniv2" : {
32
+ {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
33
+ },
40
34
}
41
35
42
36
type CnsState struct {
@@ -64,79 +58,6 @@ type Address struct {
64
58
Addr string `json:"ipv4"`
65
59
}
66
60
67
- func CreateLinuxValidator (ctx context.Context , clienset * kubernetes.Clientset , config * rest.Config , namespace , cni string , restartCase bool ) (* LinuxValidator , error ) {
68
- // deploy privileged pod
69
- privilegedDaemonSet , err := k8sutils .MustParseDaemonSet (privilegedDaemonSetPath )
70
- if err != nil {
71
- return nil , errors .Wrap (err , "unable to parse daemonset" )
72
- }
73
- daemonsetClient := clienset .AppsV1 ().DaemonSets (privilegedNamespace )
74
- if err := k8sutils .MustCreateDaemonset (ctx , daemonsetClient , privilegedDaemonSet ); err != nil {
75
- return nil , errors .Wrap (err , "unable to create daemonset" )
76
- }
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" )
79
- }
80
-
81
- return & LinuxValidator {
82
- clientset : clienset ,
83
- config : config ,
84
- namespace : namespace ,
85
- cni : cni ,
86
- restartCase : restartCase ,
87
- }, nil
88
- }
89
-
90
- // Todo: Based on cni version validate different state files
91
- func (v * LinuxValidator ) ValidateStateFile (ctx context.Context ) error {
92
- checkSet := make (map [string ][]check ) // key is cni type, value is a list of check
93
- // TODO: add cniv1 when adding Linux related test cases
94
- checkSet ["cilium" ] = []check {
95
- {"cns" , cnsStateFileIps , cnsLabelSelector , privilegedNamespace , cnsStateFileCmd },
96
- {"cilium" , ciliumStateFileIps , ciliumLabelSelector , privilegedNamespace , ciliumStateFileCmd },
97
- {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
98
- }
99
-
100
- checkSet ["cniv2" ] = []check {
101
- {"cns cache" , cnsCacheStateFileIps , cnsLabelSelector , privilegedNamespace , cnsLocalCacheCmd },
102
- }
103
-
104
- for _ , check := range checkSet [v .cni ] {
105
- err := v .validateIPs (ctx , check .stateFileIps , check .cmd , check .name , check .podNamespace , check .podLabelSelector )
106
- if err != nil {
107
- return err
108
- }
109
- }
110
- return nil
111
- }
112
-
113
- func (v * LinuxValidator ) ValidateRestartNetwork (ctx context.Context ) error {
114
- nodes , err := k8sutils .GetNodeList (ctx , v .clientset )
115
- if err != nil {
116
- return errors .Wrapf (err , "failed to get node list" )
117
- }
118
-
119
- for index := range nodes .Items {
120
- // get the privileged pod
121
- pod , err := k8sutils .GetPodsByNode (ctx , v .clientset , privilegedNamespace , privilegedLabelSelector , nodes .Items [index ].Name )
122
- if err != nil {
123
- return errors .Wrapf (err , "failed to get privileged pod" )
124
- }
125
-
126
- privelegedPod := pod .Items [0 ]
127
- // exec into the pod to get the state file
128
- _ , err = k8sutils .ExecCmdOnPod (ctx , v .clientset , privilegedNamespace , privelegedPod .Name , restartNetworkCmd , v .config )
129
- if err != nil {
130
- return errors .Wrapf (err , "failed to exec into privileged pod" )
131
- }
132
- err = k8sutils .WaitForPodsRunning (ctx , v .clientset , "" , "" )
133
- if err != nil {
134
- return errors .Wrapf (err , "failed to wait for pods running" )
135
- }
136
- }
137
- return nil
138
- }
139
-
140
61
func cnsStateFileIps (result []byte ) (map [string ]string , error ) {
141
62
var cnsResult CnsState
142
63
err := json .Unmarshal (result , & cnsResult )
@@ -188,43 +109,3 @@ func cnsCacheStateFileIps(result []byte) (map[string]string, error) {
188
109
}
189
110
return cnsPodIps , nil
190
111
}
191
-
192
- func (v * LinuxValidator ) validateIPs (ctx context.Context , stateFileIps stateFileIpsFunc , cmd []string , checkType , namespace , labelSelector string ) error {
193
- log .Printf ("Validating %s state file" , checkType )
194
- nodes , err := k8sutils .GetNodeList (ctx , v .clientset )
195
- if err != nil {
196
- return errors .Wrapf (err , "failed to get node list" )
197
- }
198
-
199
- for index := range nodes .Items {
200
- // get the privileged pod
201
- pod , err := k8sutils .GetPodsByNode (ctx , v .clientset , namespace , labelSelector , nodes .Items [index ].Name )
202
- if err != nil {
203
- return errors .Wrapf (err , "failed to get privileged pod" )
204
- }
205
- podName := pod .Items [0 ].Name
206
- // exec into the pod to get the state file
207
- result , err := k8sutils .ExecCmdOnPod (ctx , v .clientset , namespace , podName , cmd , v .config )
208
- if err != nil {
209
- return errors .Wrapf (err , "failed to exec into privileged pod" )
210
- }
211
- filePodIps , err := stateFileIps (result )
212
- if err != nil {
213
- return errors .Wrapf (err , "failed to get pod ips from state file" )
214
- }
215
- if len (filePodIps ) == 0 && v .restartCase {
216
- log .Printf ("No pods found on node %s" , nodes .Items [index ].Name )
217
- continue
218
- }
219
- // get the pod ips
220
- podIps := getPodIPsWithoutNodeIP (ctx , v .clientset , nodes .Items [index ])
221
-
222
- check := compareIPs (filePodIps , podIps )
223
-
224
- if ! check {
225
- return errors .Wrapf (errors .New ("State file validation failed" ), "for %s on node %s" , checkType , nodes .Items [index ].Name )
226
- }
227
- }
228
- log .Printf ("State file validation for %s passed" , checkType )
229
- return nil
230
- }
0 commit comments