Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions jenkins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Jenkins pipeline to run performance test

## Prerequisites

1. Install a jenkins server
1. Configure the following environment variables in jenkins global configuration
- CNCF_EKS_ACCESS_KEY with the username(access key) and password (secret key), the user must have permission to create/delete eks clusters
1. Add a jenkins node to the jenkins server, add label `aws-ec2` to the node.
1. Create the following three jenkins jobs
- create-cluster -- create/Jenkinsfile
- run-performance-test -- test/Jenkinsfile
- delete-cluster -- delete/Jenkinsfile
233 changes: 233 additions & 0 deletions jenkins/create/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
pipeline {

agent { node { label 'aws-ec2' } }

parameters {
string(name: 'AWS_REGION', defaultValue: 'us-east-1', description: 'AWS Region')
string(name: 'CLUSTER_NAME', defaultValue: 'demo-eks', description: 'EKS Cluster Name')
string(name: 'NODE_GROUP_NAME', defaultValue: 'demo-nodes', description: 'EKS Node Group Name')
string(name: 'NODE_TYPE', defaultValue: 't3.medium', description: 'EC2 Worker Node Type (Example: t3.medium)')
string(name: 'NODE_COUNT', defaultValue: '2', description: 'Number of Worker Nodes')
string(name: 'HARBOR_HELM_VERSION', defaultValue: '1.18.0', description: 'The Harbor Helm Chart version to install')
booleanParam(name: 'SKIP_CREATE_CLUSTER', defaultValue: false, description: 'Whether skip to create the EKS cluster')
}

stages {
stage('Install unzip') {
steps {
sh '''
if ! command -v unzip &> /dev/null; then
echo "Installing unzip..."
sudo apt-get update -y
sudo apt-get install -y unzip
else
echo "unzip already installed"
fi
'''
}
}
stage('Install AWS CLI') {
steps {
sh """
if [ ! -x "/usr/local/bin/aws" ]; then
echo "Installing AWS CLI..."
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -o awscliv2.zip
sudo ./aws/install --update
aws --version
else
echo "AWS CLI already installed."
aws --version
fi
"""
}
}

stage('Setup AWS CLI Credentials') {
steps {
withCredentials([
usernamePassword(credentialsId: 'CNCF_EKS_ACCESS_KEY', usernameVariable: 's3_access_key', passwordVariable: 's3_secret_key'),
]){
sh """
mkdir -p ~/.aws
cat > ~/.aws/credentials <<EOF
[default]
aws_access_key_id = ${s3_access_key}
aws_secret_access_key = ${s3_secret_key}
EOF

cat > ~/.aws/config <<EOF
[default]
region = ${params.AWS_REGION}
output = json
EOF
"""
}
}
}


stage('Install kubectl') {
steps {
sh '''
if ! command -v kubectl &>/dev/null; then
echo "Installing kubectl..."
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
else
echo "kubectl already installed"
fi
kubectl version --client
'''
}
}

stage('Install Helm CLI') {
steps {
sh '''
if ! command -v helm &>/dev/null; then
echo "Installing Helm..."
curl -L https://get.helm.sh/helm-v3.14.3-linux-amd64.tar.gz -o helm.tar.gz
tar -xzf helm.tar.gz
sudo mv linux-amd64/helm /usr/local/bin/
else
echo "Helm already installed"
fi
helm version
'''
}
}

stage('Download eksctl') {
steps {
sh """
if ! command -v eksctl &> /dev/null; then
echo "Downloading eksctl..."
curl -L https://github.com/eksctl-io/eksctl/releases/download/v0.215.0/eksctl_Linux_amd64.tar.gz -o eksctl.tar.gz
tar -xzf eksctl.tar.gz
sudo mv eksctl /usr/local/bin/
fi
eksctl version
"""
}
}

stage('Create EKS Cluster') {
when {
expression { return params.SKIP_CREATE_CLUSTER == false }
}
steps {
sh """
eksctl create cluster \\
--name ${params.CLUSTER_NAME} \\
--region ${params.AWS_REGION} \\
--nodegroup-name ${params.NODE_GROUP_NAME} \\
--node-type ${params.NODE_TYPE} \\
--nodes ${params.NODE_COUNT} \\
--managed
"""
}
}

stage('Add CSI Policy to Node Role') {
when {
expression { return params.SKIP_CREATE_CLUSTER == false }
}
steps {
sh '''
echo "Attaching AmazonEBSCSIDriverPolicy to Node Role"
cat > set_csi_policy.sh <<EOF
#!/bin/bash
set -e
NODE_ROLE_NAME=\\$(aws eks describe-nodegroup --cluster-name \\$1 --nodegroup-name \\$2 --query "nodegroup.nodeRole" --output text | awk -F/ '{print \\$2}')
aws iam attach-role-policy --role-name \\$NODE_ROLE_NAME --policy-arn arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy
EOF
chmod +x set_csi_policy.sh
'''
sh """
./set_csi_policy.sh ${params.CLUSTER_NAME} ${params.NODE_GROUP_NAME}
"""
}
}


stage('Install Ingress') {
when {
expression { return params.SKIP_CREATE_CLUSTER == false }
}
steps {
sh """
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install ingress-nginx ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace
kubectl wait --namespace ingress-nginx \\
--for=condition=ready pod \\
--selector=app.kubernetes.io/component=controller \\
--timeout=180s
"""
}
}

stage('Install CSI Driver') {
when {
expression { return params.SKIP_CREATE_CLUSTER == false }
}
steps {
sh """
helm repo add aws-ebs-csi-driver https://kubernetes-sigs.github.io/aws-ebs-csi-driver
helm repo update
helm install aws-ebs-csi-driver aws-ebs-csi-driver/aws-ebs-csi-driver --namespace kube-system
"""
}
}

stage('Set default storage class'){
when {
expression { return params.SKIP_CREATE_CLUSTER == false }
}
steps {
sh """
kubectl patch storageclass gp2 -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
"""
}
}

stage('Install Harbor'){
steps {
sh """
helm repo add harbor https://helm.goharbor.io
helm repo update
kubectl create namespace harbor || true
helm install myrelease harbor/harbor --version ${params.HARBOR_HELM_VERSION} --set expose.type=ingress,expose.ingress.hosts.core=core.harbor.domain,persistence.persistentVolumeClaim.registry.size=300Gi,persistence.persistentVolumeClaim.database.size=5Gi,persistence.persistentVolumeClaim.redis.size=5Gi,externalURL=https://core.harbor.domain,expose.ingress.className=nginx
kubectl wait \\
--for=condition=ready pod \\
--selector=app.kubernetes.io/component=core \\
--timeout=180s
"""
}
}

stage('Export kubeconfig') {
steps {
sh """
mkdir -p ~/.kube
aws eks update-kubeconfig --name ${params.CLUSTER_NAME} --region ${params.AWS_REGION}
cp ~/.kube/config kubeconfig-${params.CLUSTER_NAME}
"""
}
}

stage('Publish kubeconfig') {
steps {
archiveArtifacts artifacts: "kubeconfig-${params.CLUSTER_NAME}", fingerprint: true
}
}
}

post {
always {
echo "Pipeline complete. kubeconfig exported and archived."
}
}
}
97 changes: 97 additions & 0 deletions jenkins/delete/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
pipeline {
agent { node { label 'aws-ec2' } }

parameters {
string(name: 'REGION', defaultValue: 'us-east-1', description: 'AWS Region')
string(name: 'CLUSTER_NAME', defaultValue: 'demo-eks', description: 'EKS Cluster Name to Delete')
}


stages {

stage('Install unzip (if missing)') {
steps {
sh '''
if ! command -v unzip &> /dev/null; then
echo "Installing unzip..."
sudo apt-get update -y
sudo apt-get install -y unzip
else
echo "unzip already installed"
fi
'''
}
}

stage('Install AWS CLI (if missing)') {
steps {
sh '''
if ! command -v aws &> /dev/null; then
echo "Installing AWS CLI..."
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -o awscliv2.zip
sudo ./aws/install --update
else
echo "AWS CLI already installed"
fi
aws --version
'''
}
}

stage('Setup AWS CLI Credentials') {
steps {
withCredentials([
usernamePassword(credentialsId: 'CNCF_EKS_ACCESS_KEY', usernameVariable: 's3_access_key', passwordVariable: 's3_secret_key'),
]){
sh """
mkdir -p ~/.aws
cat > ~/.aws/credentials <<EOF
[default]
aws_access_key_id = ${s3_access_key}
aws_secret_access_key = ${s3_secret_key}
EOF

cat > ~/.aws/config <<EOF
[default]
region = ${params.REGION}
output = json
EOF
"""
}
}
}

stage('Install eksctl (if missing)') {
steps {
sh '''
if ! command -v eksctl &> /dev/null; then
echo "Installing eksctl..."
curl -L "https://github.com/eksctl-io/eksctl/releases/download/v0.215.0/eksctl_Linux_amd64.tar.gz" -o eksctl.tar.gz
tar -xzf eksctl.tar.gz
sudo mv eksctl /usr/local/bin/
else
echo "eksctl already installed"
fi
eksctl version
'''
}
}

stage('Delete EKS Cluster') {
steps {
echo "Preparing to delete EKS cluster: ${params.CLUSTER_NAME} in region: ${params.REGION}"
sh '''
echo "Deleting EKS cluster ${CLUSTER_NAME}..."
eksctl delete cluster --name ${CLUSTER_NAME} --region ${REGION} --wait
'''
}
}
}

post {
always {
echo "Cluster deletion process completed."
}
}
}
Loading
Loading