Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Gonzalez <[email protected]>
  • Loading branch information
sergio-chainguard committed Mar 26, 2024
1 parent 716d9ab commit f08aabd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 192 deletions.
12 changes: 11 additions & 1 deletion images/multus-cni/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
name: multus-cni

image: cgr.dev/chainguard/multus-cni

logo: https://storage.googleapis.com/chainguard-academy/logos/multus-cni.svg

endoflife: ""

console_summary: ""

short_description: A CNI meta-plugin for multi-homed pods in Kubernetes

compatibility_notes: ""

readme_file: README.md
upstream_url:

upstream_url:

keywords: []

116 changes: 83 additions & 33 deletions images/multus-cni/tests/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,98 @@
#!/usr/bin/env bash
#!/bin/bash

set -o errexit -o nounset -o errtrace -o pipefail -x
# Function to check pod status
check_pod_status() {
local pod_name="$1"
local namespace="$2"
kubectl get pod "$pod_name" -n "$namespace" &>/dev/null
}

# Replace "multus-cni" with the name of your Helm release
HELM_RELEASE="multus-cni"
# Function to delete the pod if it exists
delete_pod_if_exists() {
local pod_name="$1"
local namespace="$2"
if check_pod_status "$pod_name" "$namespace"; then
kubectl delete pod "$pod_name" -n "$namespace" &>/dev/null
fi
}

POD_SPEC='apiVersion: v1
# Apply the Multus CNI DaemonSet manifest
kubectl apply -f https://raw.githubusercontent.com/k8snetworkplumbingwg/multus-cni/master/deployments/multus-daemonset-thick.yml &>/dev/null

NAMESPACE="multus"
kubectl create namespace "$NAMESPACE" &>/dev/null
kubectl create serviceaccount multus -n "$NAMESPACE" &>/dev/null

cat <<EOF > multus-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pod
name: multus-test-pod
namespace: $NAMESPACE
annotations:
k8s.v1.cni.cncf.io/networks: '[{"name":"flannel-network"},{"name":"default-network"}]'
spec:
serviceAccountName: multus
containers:
- name: test-container
- name: multus-test-container
image: busybox
command:
- /bin/sh
- -c
- "trap : TERM INT; (while true; do sleep 1; done) & wait"
- sleep
- "3600"
stdin: true
tty: true
'

# Create the test pod
kubectl apply -f - <<< "$POD_SPEC"

# Wait until the pod is in "Running" state
echo "Waiting for pod to be in 'Running' state..."
while true; do
POD_STATUS=$(kubectl get pod test-pod -o jsonpath='{.status.phase}')
if [ "$POD_STATUS" = "Running" ]; then
break
fi
sleep 1
done
echo "Pod is now in 'Running' state."
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
EOF

kubectl apply -f multus-pod.yaml &>/dev/null

# Total time to wait in seconds (3 minutes)
TOTAL_WAIT_TIME=$((3 * 60))

# Waiting time between checks in seconds (5 seconds)
WAIT_INTERVAL=5

# Get the IP address of the pod
POD_IP=$(kubectl get pod test-pod -o jsonpath='{.status.podIPs[0].ip}')
# Total expected time
TOTAL_WAITED=0

# Test network connectivity
echo "Testing connectivity to Google..."
ping -c 3 google.com
# pod name
POD_NAME="multus-test-pod"

# Delete the test pod
kubectl delete pod test-pod
while [ $TOTAL_WAITED -lt $TOTAL_WAIT_TIME ]; do
# Get pod status
POD_STATUS=$(kubectl get pod "$POD_NAME" -n "$NAMESPACE" -o jsonpath='{.status.phase}')

# Check if the status is 'Running'
if [ "$POD_STATUS" == "Running" ]; then
echo "The pod is in Running state."

# Test connectivity from the pod to another resource within the cluster
kubectl exec "$POD_NAME" -n "$NAMESPACE" -- sh -c "ping -c 3 google.com"

# Check if the pod still exists before trying to delete it
if check_pod_status "$POD_NAME" "$NAMESPACE"; then
delete_pod_if_exists "$POD_NAME" "$NAMESPACE"
fi

# Delete the Multus CNI DaemonSet
kubectl delete -f https://raw.githubusercontent.com/k8snetworkplumbingwg/multus-cni/master/deployments/multus-daemonset-thick.yml &>/dev/null

exit 0
fi

# Increase total expected time
TOTAL_WAITED=$((TOTAL_WAITED + WAIT_INTERVAL))

# Wait before next check
sleep "$WAIT_INTERVAL"
done

exit 0
# If the pod is not in 'Running' state after 3 minutes, exit with exit code 1
echo "The pod is not in Running state after 3 minutes."
exit 1
166 changes: 8 additions & 158 deletions images/multus-cni/tests/values.yaml
Original file line number Diff line number Diff line change
@@ -1,158 +1,8 @@
# Note:
# This deployment file is designed for 'quickstart' of multus, easy installation to test it,
# hence this deployment yaml does not care about following things intentionally.
# - various configuration options
# - minor deployment scenario
# - upgrade/update/uninstall scenario
# Multus team understand users deployment scenarios are diverse, hence we do not cover
# comprehensive deployment scenario. We expect that it is covered by each platform deployment.
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: network-attachment-definitions.k8s.cni.cncf.io
spec:
group: k8s.cni.cncf.io
scope: Namespaced
names:
plural: network-attachment-definitions
singular: network-attachment-definition
kind: NetworkAttachmentDefinition
shortNames:
- net-attach-def
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
description: 'NetworkAttachmentDefinition is a CRD schema specified by the Network Plumbing
Working Group to express the intent for attaching pods to one or more logical or physical
networks. More information available at: https://github.com/k8snetworkplumbingwg/multi-net-spec'
type: object
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this represen
tation of an object. Servers should convert recognized schemas to the
latest internal value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: 'NetworkAttachmentDefinition spec defines the desired state of a network attachment'
type: object
properties:
config:
description: 'NetworkAttachmentDefinition config is a JSON-formatted CNI configuration'
type: string
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: multus
rules:
- apiGroups: ["k8s.cni.cncf.io"]
resources:
- '*'
verbs:
- '*'
- apiGroups:
- ""
resources:
- pods
- pods/status
verbs:
- get
- update
- apiGroups:
- ""
- events.k8s.io
resources:
- events
verbs:
- create
- patch
- update
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: multus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: multus
subjects:
- kind: ServiceAccount
name: multus
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: multus
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: multus-cni-config
namespace: kube-system
labels:
tier: node
app: multus
data:
# NOTE: If you'd prefer to manually apply a configuration file, you may create one here.
# In the case you'd like to customize the Multus installation, you should change the arguments to the Multus pod
# change the "args" line below from
# - "--multus-conf-file=auto"
# to:
# "--multus-conf-file=/tmp/multus-conf/70-multus.conf"
# Additionally -- you should ensure that the name "70-multus.conf" is the alphabetically first name in the
# /etc/cni/net.d/ directory on each node, otherwise, it will not be used by the Kubelet.
cni-conf.json: |
{
"name": "multus-cni-network",
"type": "multus",
"capabilities": {
"portMappings": true
},
"delegates": [
{
"cniVersion": "0.3.1",
"name": "default-cni-network",
"plugins": [
{
"type": "flannel",
"name": "flannel.1",
"delegate": {
"isDefaultGateway": true,
"hairpinMode": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
],
"kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig"
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-multus-ds
namespace: kube-system
namespace: multus
labels:
tier: node
app: multus
Expand Down Expand Up @@ -180,7 +30,7 @@ spec:
containers:
- name: kube-multus
# crio support requires multus:latest for now. support 3.3 or later.
image: ghcr.io/k8snetworkplumbingwg/multus-cni:stable
image: ghcr.io/k8snetworkplumbingwg/multus-cni:latest
command: ["/entrypoint.sh"]
args:
- "--cni-version=0.3.1"
Expand All @@ -203,9 +53,9 @@ spec:
mountPropagation: HostToContainer
- name: cni
mountPath: /host/etc/cni/net.d
- name: cnibin
- name: cnibin # Define el volumen cnibin
mountPath: /host/usr/libexec/cni
- name: multus-cfg
- name: multus-cfg # Define el volumen multus-cfg
mountPath: /tmp/multus-conf
terminationGracePeriodSeconds: 10
volumes:
Expand All @@ -214,13 +64,13 @@ spec:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: cnibin
path: /etc/cni
- name: cnibin # Define el volumen cnibin
hostPath:
path: /usr/libexec/cni
- name: multus-cfg
- name: multus-cfg # Define el volumen multus-cfg
configMap:
name: multus-cni-config
items:
- key: cni-conf.json
path: 70-multus.conf
path: 70-multus.conf

0 comments on commit f08aabd

Please sign in to comment.