|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package core_plugins |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/manifest" |
| 21 | + k8sUtils "github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/utils" |
| 22 | + "github.com/aws/amazon-vpc-cni-k8s/test/framework/utils" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + awsNodeLabelKey = "k8s-app" |
| 30 | + cniBinPath = "/host/opt/cni/bin" |
| 31 | +) |
| 32 | + |
| 33 | +// expectedCorePlugins lists all containernetworking/plugins binaries shipped |
| 34 | +// in the amazon-k8s-cni-init image via core-plugins/binaries/. |
| 35 | +var expectedCorePlugins = []string{ |
| 36 | + "bandwidth", |
| 37 | + "bridge", |
| 38 | + "dhcp", |
| 39 | + "dummy", |
| 40 | + "firewall", |
| 41 | + "host-device", |
| 42 | + "host-local", |
| 43 | + "ipvlan", |
| 44 | + "loopback", |
| 45 | + "macvlan", |
| 46 | + "portmap", |
| 47 | + "ptp", |
| 48 | + "sbr", |
| 49 | + "static", |
| 50 | + "tap", |
| 51 | + "tuning", |
| 52 | + "vlan", |
| 53 | + "vrf", |
| 54 | +} |
| 55 | + |
| 56 | +var _ = Describe("CNI init container core plugins", func() { |
| 57 | + It("all core plugins should be present with executable permissions", func() { |
| 58 | + By("getting an aws-node pod") |
| 59 | + podList, err := f.K8sResourceManagers.PodManager(). |
| 60 | + GetPodsWithLabelSelector(awsNodeLabelKey, utils.AwsNodeName) |
| 61 | + Expect(err).ToNot(HaveOccurred()) |
| 62 | + Expect(podList.Items).ToNot(BeEmpty(), "expected at least one aws-node pod") |
| 63 | + |
| 64 | + pod := podList.Items[0] |
| 65 | + By(fmt.Sprintf("checking core plugin executability on node %s via pod %s", pod.Spec.NodeName, pod.Name)) |
| 66 | + |
| 67 | + var failures []string |
| 68 | + for _, plugin := range expectedCorePlugins { |
| 69 | + p := fmt.Sprintf("%s/%s", cniBinPath, plugin) |
| 70 | + // Invoke the binary directly; CNI plugins print version info on |
| 71 | + // stdin-EOF and exit 0 or 1, but the exec itself will fail with a |
| 72 | + // clear error if the file is missing or not executable. |
| 73 | + cmd := []string{p} |
| 74 | + _, _, err := f.K8sResourceManagers.PodManager(). |
| 75 | + PodExecWithContainer(pod.Namespace, pod.Name, "aws-node", cmd) |
| 76 | + if err != nil { |
| 77 | + errMsg := err.Error() |
| 78 | + if strings.Contains(errMsg, "not found") || strings.Contains(errMsg, "no such file") { |
| 79 | + failures = append(failures, fmt.Sprintf("MISSING: %s", plugin)) |
| 80 | + } else if strings.Contains(errMsg, "permission denied") { |
| 81 | + failures = append(failures, fmt.Sprintf("NOT_EXEC: %s", plugin)) |
| 82 | + } |
| 83 | + // Other errors (e.g. non-zero exit) are expected — the binary |
| 84 | + // exists and is executable but needs CNI_COMMAND env, so ignore. |
| 85 | + } |
| 86 | + } |
| 87 | + Expect(failures).To(BeEmpty(), |
| 88 | + fmt.Sprintf("core plugin issues found:\n%s", strings.Join(failures, "\n"))) |
| 89 | + }) |
| 90 | + |
| 91 | + Context("when ENABLE_BANDWIDTH_PLUGIN is enabled", func() { |
| 92 | + AfterEach(func() { |
| 93 | + By("disabling ENABLE_BANDWIDTH_PLUGIN") |
| 94 | + k8sUtils.RemoveVarFromDaemonSetAndWaitTillUpdated(f, |
| 95 | + utils.AwsNodeName, utils.AwsNodeNamespace, utils.AwsNodeName, |
| 96 | + map[string]struct{}{ |
| 97 | + "ENABLE_BANDWIDTH_PLUGIN": {}, |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + It("pods should reach Running state", func() { |
| 102 | + By("enabling ENABLE_BANDWIDTH_PLUGIN on aws-node daemonset") |
| 103 | + k8sUtils.AddEnvVarToDaemonSetAndWaitTillUpdated(f, |
| 104 | + utils.AwsNodeName, utils.AwsNodeNamespace, utils.AwsNodeName, |
| 105 | + map[string]string{ |
| 106 | + "ENABLE_BANDWIDTH_PLUGIN": "true", |
| 107 | + }) |
| 108 | + |
| 109 | + By("creating a deployment to verify pods can start") |
| 110 | + deployment := manifest.NewBusyBoxDeploymentBuilder(f.Options.TestImageRegistry). |
| 111 | + Replicas(3). |
| 112 | + PodLabel("app", "bandwidth-test"). |
| 113 | + PodAnnotation("kubernetes.io/ingress-bandwidth", "10M"). |
| 114 | + PodAnnotation("kubernetes.io/egress-bandwidth", "10M"). |
| 115 | + Build() |
| 116 | + |
| 117 | + deployment, err := f.K8sResourceManagers.DeploymentManager(). |
| 118 | + CreateAndWaitTillDeploymentIsReady(deployment, utils.DefaultDeploymentReadyTimeout) |
| 119 | + Expect(err).ToNot(HaveOccurred(), |
| 120 | + "pods failed to reach Running state with ENABLE_BANDWIDTH_PLUGIN=true") |
| 121 | + |
| 122 | + By("deleting the test deployment") |
| 123 | + err = f.K8sResourceManagers.DeploymentManager(). |
| 124 | + DeleteAndWaitTillDeploymentIsDeleted(deployment) |
| 125 | + Expect(err).ToNot(HaveOccurred()) |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments