Skip to content

Commit 4c8c19d

Browse files
authored
Merge pull request #56 from stonezdj/25dec08_add_jenkins_file
Add jenkinsfile for performance test
2 parents 89e8f44 + 73d91b5 commit 4c8c19d

File tree

4 files changed

+450
-0
lines changed

4 files changed

+450
-0
lines changed

jenkins/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Jenkins pipeline to run performance test
2+
3+
## Prerequisites
4+
5+
1. Install a jenkins server
6+
1. Configure the following environment variables in jenkins global configuration
7+
- CNCF_EKS_ACCESS_KEY with the username(access key) and password (secret key), the user must have permission to create/delete eks clusters
8+
1. Add a jenkins node to the jenkins server, add label `aws-ec2` to the node.
9+
1. Create the following three jenkins jobs
10+
- create-cluster -- create/Jenkinsfile
11+
- run-performance-test -- test/Jenkinsfile
12+
- delete-cluster -- delete/Jenkinsfile

jenkins/create/Jenkinsfile

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
pipeline {
2+
3+
agent { node { label 'aws-ec2' } }
4+
5+
parameters {
6+
string(name: 'AWS_REGION', defaultValue: 'us-east-1', description: 'AWS Region')
7+
string(name: 'CLUSTER_NAME', defaultValue: 'demo-eks', description: 'EKS Cluster Name')
8+
string(name: 'NODE_GROUP_NAME', defaultValue: 'demo-nodes', description: 'EKS Node Group Name')
9+
string(name: 'NODE_TYPE', defaultValue: 't3.medium', description: 'EC2 Worker Node Type (Example: t3.medium)')
10+
string(name: 'NODE_COUNT', defaultValue: '2', description: 'Number of Worker Nodes')
11+
string(name: 'HARBOR_HELM_VERSION', defaultValue: '1.18.0', description: 'The Harbor Helm Chart version to install')
12+
booleanParam(name: 'SKIP_CREATE_CLUSTER', defaultValue: false, description: 'Whether skip to create the EKS cluster')
13+
}
14+
15+
stages {
16+
stage('Install unzip') {
17+
steps {
18+
sh '''
19+
if ! command -v unzip &> /dev/null; then
20+
echo "Installing unzip..."
21+
sudo apt-get update -y
22+
sudo apt-get install -y unzip
23+
else
24+
echo "unzip already installed"
25+
fi
26+
'''
27+
}
28+
}
29+
stage('Install AWS CLI') {
30+
steps {
31+
sh """
32+
if [ ! -x "/usr/local/bin/aws" ]; then
33+
echo "Installing AWS CLI..."
34+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
35+
unzip -o awscliv2.zip
36+
sudo ./aws/install --update
37+
aws --version
38+
else
39+
echo "AWS CLI already installed."
40+
aws --version
41+
fi
42+
"""
43+
}
44+
}
45+
46+
stage('Setup AWS CLI Credentials') {
47+
steps {
48+
withCredentials([
49+
usernamePassword(credentialsId: 'CNCF_EKS_ACCESS_KEY', usernameVariable: 's3_access_key', passwordVariable: 's3_secret_key'),
50+
]){
51+
sh """
52+
mkdir -p ~/.aws
53+
cat > ~/.aws/credentials <<EOF
54+
[default]
55+
aws_access_key_id = ${s3_access_key}
56+
aws_secret_access_key = ${s3_secret_key}
57+
EOF
58+
59+
cat > ~/.aws/config <<EOF
60+
[default]
61+
region = ${params.AWS_REGION}
62+
output = json
63+
EOF
64+
"""
65+
}
66+
}
67+
}
68+
69+
70+
stage('Install kubectl') {
71+
steps {
72+
sh '''
73+
if ! command -v kubectl &>/dev/null; then
74+
echo "Installing kubectl..."
75+
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
76+
chmod +x kubectl
77+
sudo mv kubectl /usr/local/bin/
78+
else
79+
echo "kubectl already installed"
80+
fi
81+
kubectl version --client
82+
'''
83+
}
84+
}
85+
86+
stage('Install Helm CLI') {
87+
steps {
88+
sh '''
89+
if ! command -v helm &>/dev/null; then
90+
echo "Installing Helm..."
91+
curl -L https://get.helm.sh/helm-v3.14.3-linux-amd64.tar.gz -o helm.tar.gz
92+
tar -xzf helm.tar.gz
93+
sudo mv linux-amd64/helm /usr/local/bin/
94+
else
95+
echo "Helm already installed"
96+
fi
97+
helm version
98+
'''
99+
}
100+
}
101+
102+
stage('Download eksctl') {
103+
steps {
104+
sh """
105+
if ! command -v eksctl &> /dev/null; then
106+
echo "Downloading eksctl..."
107+
curl -L https://github.com/eksctl-io/eksctl/releases/download/v0.215.0/eksctl_Linux_amd64.tar.gz -o eksctl.tar.gz
108+
tar -xzf eksctl.tar.gz
109+
sudo mv eksctl /usr/local/bin/
110+
fi
111+
eksctl version
112+
"""
113+
}
114+
}
115+
116+
stage('Create EKS Cluster') {
117+
when {
118+
expression { return params.SKIP_CREATE_CLUSTER == false }
119+
}
120+
steps {
121+
sh """
122+
eksctl create cluster \\
123+
--name ${params.CLUSTER_NAME} \\
124+
--region ${params.AWS_REGION} \\
125+
--nodegroup-name ${params.NODE_GROUP_NAME} \\
126+
--node-type ${params.NODE_TYPE} \\
127+
--nodes ${params.NODE_COUNT} \\
128+
--managed
129+
"""
130+
}
131+
}
132+
133+
stage('Add CSI Policy to Node Role') {
134+
when {
135+
expression { return params.SKIP_CREATE_CLUSTER == false }
136+
}
137+
steps {
138+
sh '''
139+
echo "Attaching AmazonEBSCSIDriverPolicy to Node Role"
140+
cat > set_csi_policy.sh <<EOF
141+
#!/bin/bash
142+
set -e
143+
NODE_ROLE_NAME=\\$(aws eks describe-nodegroup --cluster-name \\$1 --nodegroup-name \\$2 --query "nodegroup.nodeRole" --output text | awk -F/ '{print \\$2}')
144+
aws iam attach-role-policy --role-name \\$NODE_ROLE_NAME --policy-arn arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy
145+
EOF
146+
chmod +x set_csi_policy.sh
147+
'''
148+
sh """
149+
./set_csi_policy.sh ${params.CLUSTER_NAME} ${params.NODE_GROUP_NAME}
150+
"""
151+
}
152+
}
153+
154+
155+
stage('Install Ingress') {
156+
when {
157+
expression { return params.SKIP_CREATE_CLUSTER == false }
158+
}
159+
steps {
160+
sh """
161+
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
162+
helm repo update
163+
helm install ingress-nginx ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace
164+
kubectl wait --namespace ingress-nginx \\
165+
--for=condition=ready pod \\
166+
--selector=app.kubernetes.io/component=controller \\
167+
--timeout=180s
168+
"""
169+
}
170+
}
171+
172+
stage('Install CSI Driver') {
173+
when {
174+
expression { return params.SKIP_CREATE_CLUSTER == false }
175+
}
176+
steps {
177+
sh """
178+
helm repo add aws-ebs-csi-driver https://kubernetes-sigs.github.io/aws-ebs-csi-driver
179+
helm repo update
180+
helm install aws-ebs-csi-driver aws-ebs-csi-driver/aws-ebs-csi-driver --namespace kube-system
181+
"""
182+
}
183+
}
184+
185+
stage('Set default storage class'){
186+
when {
187+
expression { return params.SKIP_CREATE_CLUSTER == false }
188+
}
189+
steps {
190+
sh """
191+
kubectl patch storageclass gp2 -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
192+
"""
193+
}
194+
}
195+
196+
stage('Install Harbor'){
197+
steps {
198+
sh """
199+
helm repo add harbor https://helm.goharbor.io
200+
helm repo update
201+
kubectl create namespace harbor || true
202+
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
203+
kubectl wait \\
204+
--for=condition=ready pod \\
205+
--selector=app.kubernetes.io/component=core \\
206+
--timeout=180s
207+
"""
208+
}
209+
}
210+
211+
stage('Export kubeconfig') {
212+
steps {
213+
sh """
214+
mkdir -p ~/.kube
215+
aws eks update-kubeconfig --name ${params.CLUSTER_NAME} --region ${params.AWS_REGION}
216+
cp ~/.kube/config kubeconfig-${params.CLUSTER_NAME}
217+
"""
218+
}
219+
}
220+
221+
stage('Publish kubeconfig') {
222+
steps {
223+
archiveArtifacts artifacts: "kubeconfig-${params.CLUSTER_NAME}", fingerprint: true
224+
}
225+
}
226+
}
227+
228+
post {
229+
always {
230+
echo "Pipeline complete. kubeconfig exported and archived."
231+
}
232+
}
233+
}

jenkins/delete/Jenkinsfile

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
pipeline {
2+
agent { node { label 'aws-ec2' } }
3+
4+
parameters {
5+
string(name: 'REGION', defaultValue: 'us-east-1', description: 'AWS Region')
6+
string(name: 'CLUSTER_NAME', defaultValue: 'demo-eks', description: 'EKS Cluster Name to Delete')
7+
}
8+
9+
10+
stages {
11+
12+
stage('Install unzip (if missing)') {
13+
steps {
14+
sh '''
15+
if ! command -v unzip &> /dev/null; then
16+
echo "Installing unzip..."
17+
sudo apt-get update -y
18+
sudo apt-get install -y unzip
19+
else
20+
echo "unzip already installed"
21+
fi
22+
'''
23+
}
24+
}
25+
26+
stage('Install AWS CLI (if missing)') {
27+
steps {
28+
sh '''
29+
if ! command -v aws &> /dev/null; then
30+
echo "Installing AWS CLI..."
31+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
32+
unzip -o awscliv2.zip
33+
sudo ./aws/install --update
34+
else
35+
echo "AWS CLI already installed"
36+
fi
37+
aws --version
38+
'''
39+
}
40+
}
41+
42+
stage('Setup AWS CLI Credentials') {
43+
steps {
44+
withCredentials([
45+
usernamePassword(credentialsId: 'CNCF_EKS_ACCESS_KEY', usernameVariable: 's3_access_key', passwordVariable: 's3_secret_key'),
46+
]){
47+
sh """
48+
mkdir -p ~/.aws
49+
cat > ~/.aws/credentials <<EOF
50+
[default]
51+
aws_access_key_id = ${s3_access_key}
52+
aws_secret_access_key = ${s3_secret_key}
53+
EOF
54+
55+
cat > ~/.aws/config <<EOF
56+
[default]
57+
region = ${params.REGION}
58+
output = json
59+
EOF
60+
"""
61+
}
62+
}
63+
}
64+
65+
stage('Install eksctl (if missing)') {
66+
steps {
67+
sh '''
68+
if ! command -v eksctl &> /dev/null; then
69+
echo "Installing eksctl..."
70+
curl -L "https://github.com/eksctl-io/eksctl/releases/download/v0.215.0/eksctl_Linux_amd64.tar.gz" -o eksctl.tar.gz
71+
tar -xzf eksctl.tar.gz
72+
sudo mv eksctl /usr/local/bin/
73+
else
74+
echo "eksctl already installed"
75+
fi
76+
eksctl version
77+
'''
78+
}
79+
}
80+
81+
stage('Delete EKS Cluster') {
82+
steps {
83+
echo "Preparing to delete EKS cluster: ${params.CLUSTER_NAME} in region: ${params.REGION}"
84+
sh '''
85+
echo "Deleting EKS cluster ${CLUSTER_NAME}..."
86+
eksctl delete cluster --name ${CLUSTER_NAME} --region ${REGION} --wait
87+
'''
88+
}
89+
}
90+
}
91+
92+
post {
93+
always {
94+
echo "Cluster deletion process completed."
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)