Skip to content

Commit

Permalink
tests: add test to ensure CNI is updated on cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
rquitales committed Mar 26, 2024
1 parent 3faa42d commit fd2c7e7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/ensure-cni-upgrade/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: ensure-cni-upgrade
description: EKS cluster example to ensure CNI is upgraded when the base manifest changes
runtime: nodejs
18 changes: 18 additions & 0 deletions examples/ensure-cni-upgrade/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as pulumi from "@pulumi/pulumi";
import * as awsx from "@pulumi/awsx";
import * as eks from "@pulumi/eks";

const projectName = pulumi.getProject();

// Create an EKS cluster with the default configuration.
const cluster = new eks.Cluster(`${projectName}-1`);

// Create a VPC CNI resource for the cluster.
// We have to use a strigified version of the kubeconfig because earlier versions
// of the EKS provider did not parse the kubeconfig correctly.
// https://github.com/pulumi/pulumi-eks/issues/1092
const vpcCni = new eks.VpcCni(`${projectName}-1-cni`, cluster.kubeconfigJson);


// Export the clusters' kubeconfig.
export const kubeconfig = cluster.kubeconfigJson;
12 changes: 12 additions & 0 deletions examples/ensure-cni-upgrade/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "ensure-cni-upgrade",
"devDependencies": {
"typescript": "^4.0.0",
"@types/node": "latest"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/awsx": "^2.0.2",
"@pulumi/eks": "latest"
}
}
24 changes: 24 additions & 0 deletions examples/ensure-cni-upgrade/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"lib": [
"es6"
],
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"stripInternal": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}
56 changes: 56 additions & 0 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/opttest"
"github.com/pulumi/pulumi-eks/examples/utils"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -621,3 +624,56 @@ func getJSBaseOptions(t *testing.T) integration.ProgramTestOptions {

return baseJS
}

// TestCNIAcrossUpdates tests that the CNI manifest is reapplied when the EKS provider changes its base manifest.
func TestCNIAcrossUpdates(t *testing.T) {
t.Log("Running `pulumi up` with v2.1.0 of the EKS provider")
pt := pulumitest.NewPulumiTest(t, "ensure-cni-upgrade", opttest.AttachDownloadedPlugin("eks", "2.1.0"))
result := pt.Up()

t.Log("Ensuring a kubeconfig output is present")
kcfg, ok := result.Outputs["kubeconfig"]
require.True(t, ok)

t.Log("Validating that the v1.11.0 CNI manifest is applied correctly")
var awsNodeContainerFound bool
assert.NoError(t, utils.ValidateDaemonSet(t, kcfg.Value, "kube-system", "aws-node", func(ds *appsv1.DaemonSet) {
for _, c := range ds.Spec.Template.Spec.Containers {
switch c.Name {
case "aws-node":
awsNodeContainerFound = true
assert.Equal(t, "602401143452.dkr.ecr.us-west-2.amazonaws.com/amazon-k8s-cni:v1.11.0", c.Image)
}
}
}))
assert.True(t, awsNodeContainerFound)

t.Log("Running `pulumi up` with the latest version of the EKS provider")
pt = pt.CopyToTempDir(opttest.Defaults())

prevResult := pt.Preview()
assert.Equal(t, 1, len(prevResult.ChangeSummary))

result = pt.Up()
kcfg, ok = result.Outputs["kubeconfig"]
require.True(t, ok)

t.Log("Validating that the CNI manifests has been updated to the latest v1.16.0 version")
awsNodeContainerFound = false
assert.NoError(t, utils.ValidateDaemonSet(t, kcfg.Value, "kube-system", "aws-node", func(ds *appsv1.DaemonSet) {
// The exact image names/versions are obtained from the manifest in `nodejs/eks/cni/aws-k8s-cni.yaml`.
for _, c := range ds.Spec.Template.Spec.Containers {
switch c.Name {
case "aws-node":
awsNodeContainerFound = true
assert.Equal(t, "602401143452.dkr.ecr.us-west-2.amazonaws.com/amazon-k8s-cni:v1.16.0", c.Image)
case "aws-eks-nodeagent":
assert.Equal(t, "602401143452.dkr.ecr.us-west-2.amazonaws.com/amazon/aws-network-policy-agent:v1.0.7", c.Image)
}
}
}))
assert.True(t, awsNodeContainerFound)

t.Log("Ensuring that re-running `pulumi up` results in no changes and no spurious diffs")
pt.Up(optup.ExpectNoChanges())
}

0 comments on commit fd2c7e7

Please sign in to comment.