Skip to content

Commit 9e24ce7

Browse files
committed
refactor: [CI-CNI] Removing the predefined interfaces from validate package
1 parent 986a1f0 commit 9e24ce7

File tree

5 files changed

+201
-284
lines changed

5 files changed

+201
-284
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ test-integration: ## run all integration tests.
741741
go test -mod=readonly -buildvcs=false -timeout 1h -coverpkg=./... -race -covermode atomic -coverprofile=coverage.out -tags=integration ./test/integration...
742742

743743
test-validate-state:
744-
cd test/integration/load && go test -count 1 -timeout 30m -tags load -run ^TestValidateState -tags=load -restart-case=$(RESTART_CASE) -os=$(OS)
744+
cd test/integration/load && go test -mod=readonly -count=1 -timeout 30m -tags load -run ^TestValidateState -tags=load -restart-case=$(RESTART_CASE) -os=$(OS)
745745
cd ../../..
746746

747747
test-cyclonus: ## run the cyclonus test for npm.

test/integration/load/load_test.go

+8-42
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package load
55
import (
66
"context"
77
"flag"
8-
"fmt"
98
"testing"
109
"time"
1110

@@ -36,13 +35,6 @@ var noopDeploymentMap = map[string]string{
3635
"linux": manifestDir + "/noop-deployment-linux.yaml",
3736
}
3837

39-
// Todo: Add the validation for the data path function for the linux/windows client.
40-
type stateValidator interface {
41-
ValidateStateFile(context.Context) error
42-
ValidateRestartNetwork(context.Context) error
43-
// ValidateDataPath() error
44-
}
45-
4638
/*
4739
In order to run the scale tests, you need a k8s cluster and its kubeconfig.
4840
If no kubeconfig is passed, the test will attempt to find one in the default location for kubectl config.
@@ -60,10 +52,10 @@ todo: consider adding the following scenarios
6052
- [x] Test the CNS Local cache.
6153
- [x] Test the Cilium state file.
6254
- [x] Test the Node restart.
63-
- [ ] Test based on operating system.
64-
- [ ] Test the HNS state file.
65-
- [ ] Parameterize the os, cni and number of iterations.
66-
- [ ] Add deployment yaml for windows.
55+
- [x] Test based on operating system.
56+
- [x] Test the HNS state file.
57+
- [x] Parameterize the os, cni and number of iterations.
58+
- [x] Add deployment yaml for windows.
6759
*/
6860
func TestLoad(t *testing.T) {
6961
clientset, err := k8sutils.MustGetClientset()
@@ -139,39 +131,13 @@ func TestValidateState(t *testing.T) {
139131
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
140132
defer cancel()
141133

142-
var validator stateValidator
143-
144-
t.Log("Validating the state file")
145-
switch *osType {
146-
case "linux":
147-
validator, err = validate.CreateLinuxValidator(ctx, clientset, config, namespace, *cniType, *restartCase)
148-
if err != nil {
149-
t.Fatal(err)
150-
}
151-
case "windows":
152-
validator, err = validate.CreateWindowsValidator(ctx, clientset, config, namespace, *cniType, *restartCase)
153-
if err != nil {
154-
t.Fatal(err)
155-
}
156-
default:
157-
t.Fatalf("unknown os type %s", *osType)
158-
}
159-
160-
err = validator.ValidateStateFile(ctx)
134+
validator, err := validate.CreateValidator(ctx, clientset, config, namespace, *cniType, *restartCase, *osType)
161135
if err != nil {
162136
t.Fatal(err)
163137
}
164-
165-
// 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
166-
t.Log("Validating the restart network scenario")
167-
t.Run(fmt.Sprintf("validate network restart - %s", *osType), func(t *testing.T) {
168-
if *osType == "windows" {
169-
t.Skip("validate network restart not implemented on Windows")
170-
}
171-
if err := validator.ValidateRestartNetwork(ctx); err != nil {
172-
t.Fatal(err)
173-
}
174-
})
138+
if err := validator.Validate(ctx); err != nil {
139+
t.Fatal(err)
140+
}
175141
}
176142

177143
// TestScaleDeployment scales the deployment up/down based on the replicas passed.

test/validate/linux_validate.go

+9-128
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
package validate
22

33
import (
4-
"context"
54
"encoding/json"
6-
"log"
75

86
"github.com/Azure/azure-container-networking/cns"
97
restserver "github.com/Azure/azure-container-networking/cns/restserver"
10-
k8sutils "github.com/Azure/azure-container-networking/test/internal/k8sutils"
118
"github.com/pkg/errors"
12-
"k8s.io/client-go/kubernetes"
13-
"k8s.io/client-go/rest"
149
)
1510

1611
const (
17-
privilegedDaemonSetPath = "../manifests/load/privileged-daemonset.yaml"
18-
privilegedLabelSelector = "app=privileged-daemonset"
19-
privilegedNamespace = "kube-system"
20-
2112
cnsLabelSelector = "k8s-app=azure-cns"
2213
ciliumLabelSelector = "k8s-app=cilium"
2314
)
@@ -31,12 +22,15 @@ var (
3122

3223
type stateFileIpsFunc func([]byte) (map[string]string, error)
3324

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+
},
4034
}
4135

4236
type CnsState struct {
@@ -64,79 +58,6 @@ type Address struct {
6458
Addr string `json:"ipv4"`
6559
}
6660

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-
14061
func cnsStateFileIps(result []byte) (map[string]string, error) {
14162
var cnsResult CnsState
14263
err := json.Unmarshal(result, &cnsResult)
@@ -188,43 +109,3 @@ func cnsCacheStateFileIps(result []byte) (map[string]string, error) {
188109
}
189110
return cnsPodIps, nil
190111
}
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

Comments
 (0)