From 66eca4203e9c94bb4ae489555b5bc49557c60de4 Mon Sep 17 00:00:00 2001 From: Amit Kumar Nandi <11887616+aamitn@users.noreply.github.com> Date: Thu, 22 Feb 2024 21:39:09 +0530 Subject: [PATCH] github actions support --- k8s/data.yaml | 12 ++ k8s/deploy.yaml | 42 +++++++ k8s/docker-k8s/Dockerfile | 51 +++++++++ k8s/docker-k8s/docker-compose.yml | 26 +++++ k8s/docker-k8s/shortener.sh | 38 +++++++ k8s/service.yaml | 17 +++ k8s/shortener-chart-0.1.0.tgz | Bin 0 -> 3999 bytes k8s/shortener-chart/.helmignore | 23 ++++ k8s/shortener-chart/Chart.yaml | 24 ++++ k8s/shortener-chart/templates/NOTES.txt | 22 ++++ k8s/shortener-chart/templates/_helpers.tpl | 62 ++++++++++ k8s/shortener-chart/templates/deployment.yaml | 68 +++++++++++ k8s/shortener-chart/templates/hpa.yaml | 32 ++++++ k8s/shortener-chart/templates/ingress.yaml | 61 ++++++++++ k8s/shortener-chart/templates/service.yaml | 15 +++ .../templates/serviceaccount.yaml | 13 +++ .../templates/tests/test-connection.yaml | 15 +++ k8s/shortener-chart/values.yaml | 107 ++++++++++++++++++ 18 files changed, 628 insertions(+) create mode 100644 k8s/data.yaml create mode 100644 k8s/deploy.yaml create mode 100644 k8s/docker-k8s/Dockerfile create mode 100644 k8s/docker-k8s/docker-compose.yml create mode 100644 k8s/docker-k8s/shortener.sh create mode 100644 k8s/service.yaml create mode 100644 k8s/shortener-chart-0.1.0.tgz create mode 100644 k8s/shortener-chart/.helmignore create mode 100644 k8s/shortener-chart/Chart.yaml create mode 100644 k8s/shortener-chart/templates/NOTES.txt create mode 100644 k8s/shortener-chart/templates/_helpers.tpl create mode 100644 k8s/shortener-chart/templates/deployment.yaml create mode 100644 k8s/shortener-chart/templates/hpa.yaml create mode 100644 k8s/shortener-chart/templates/ingress.yaml create mode 100644 k8s/shortener-chart/templates/service.yaml create mode 100644 k8s/shortener-chart/templates/serviceaccount.yaml create mode 100644 k8s/shortener-chart/templates/tests/test-connection.yaml create mode 100644 k8s/shortener-chart/values.yaml diff --git a/k8s/data.yaml b/k8s/data.yaml new file mode 100644 index 0000000..791d12a --- /dev/null +++ b/k8s/data.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + labels: + io.kompose.service: data + name: data +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 100Mi diff --git a/k8s/deploy.yaml b/k8s/deploy.yaml new file mode 100644 index 0000000..0483365 --- /dev/null +++ b/k8s/deploy.yaml @@ -0,0 +1,42 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + name.full: "TUSC-The Url Shortener Company" + labels: + io.kompose.service: shortener-app + name: shortener-app +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: shortener-app + strategy: + type: Recreate + template: + metadata: + annotations: + name.full: "TUSC-The Url Shortener Company" + labels: + io.kompose.network/k8s-default: "true" + io.kompose.service: shortener-app + spec: + containers: + - image: nmpl/shortener:k8s + name: shortener-app + ports: + - containerPort: 8080 + hostPort: 8080 + protocol: TCP + - containerPort: 3306 + hostPort: 3306 + protocol: TCP + + volumeMounts: + - mountPath: /var/lib/mysql + name: data + restartPolicy: Always + volumes: + - name: data + persistentVolumeClaim: + claimName: data diff --git a/k8s/docker-k8s/Dockerfile b/k8s/docker-k8s/Dockerfile new file mode 100644 index 0000000..61a9632 --- /dev/null +++ b/k8s/docker-k8s/Dockerfile @@ -0,0 +1,51 @@ +# syntax=docker/dockerfile:1 + +# Add the following lines to tag the image (replace 'your_username' and 'shortener-app' with your Docker Hub username and repository name) +ARG VERSION=k8s +ARG IMAGE_NAME=bigwiz/shortener +ARG TAG=$VERSION + +# Stage 1: Build the application +FROM maven:3.9.6-eclipse-temurin-21 AS builder + +# Clone the repository +RUN git clone https://github.com/aamitn/URLShortener.git + +WORKDIR /URLShortener + +# Build the application +RUN mvn clean install + +# Stage 2: Create the final image +FROM tomcat:10-jdk21-openjdk-slim + +# Set environment variables +ENV CATALINA_BASE /usr/local/tomcat +ENV CATALINA_HOME /usr/local/tomcat +ENV PATH $CATALINA_HOME/bin:$PATH + + +# Copy the WAR file from the builder stage +COPY --from=builder /URLShortener/target/shortener.war $CATALINA_BASE/webapps/ + + +# Add configuration for document base path +COPY --from=builder /URLShortener/server.xml $CATALINA_BASE/conf/server.xml + + +# Expose ports +EXPOSE 8080 +EXPOSE 3306 + + +# Copy the startup script +COPY shortener.sh /usr/local/tomcat/shortener.sh + +# Copy the sql file +COPY --from=builder /URLShortener/create.sql /usr/local/tomcat/create.sql + +# Grant execute permissions to the startup.sh script +RUN chmod +x /usr/local/tomcat/shortener.sh + +# Start Tomcat and MariaDB using the startup script +CMD ["sh", "/usr/local/tomcat/shortener.sh"] diff --git a/k8s/docker-k8s/docker-compose.yml b/k8s/docker-k8s/docker-compose.yml new file mode 100644 index 0000000..5f6deb3 --- /dev/null +++ b/k8s/docker-k8s/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3.8' + +services: + shortener-app: + labels: + - "TUSC" + + #Build from docker hub image .Comment/Uncomment Below + image: nmpl/shortener:k8s + + #Build from local Dockerfile.Comment/Uncomment Below + #build: + #dockerfile: Dockerfile + + ports: + - "8080:8080" + - "3306:3306" + + volumes: + - data:/var/lib/mysql + + restart: unless-stopped + + +volumes: + data: diff --git a/k8s/docker-k8s/shortener.sh b/k8s/docker-k8s/shortener.sh new file mode 100644 index 0000000..660198d --- /dev/null +++ b/k8s/docker-k8s/shortener.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +echo "Installing MariaDB..." +# Install MariaDB +apt-get update +apt-get install -y mariadb-server + +echo "Starting MariaDB service..." +# Start MariaDB service +service mariadb start + +echo "Waiting for MariaDB to start (adjust sleep time as needed)..." + +# Wait for MariaDB to start +while ! mysqladmin ping -hlocalhost -uroot --silent; do + echo "MariaDB is not yet available. Waiting..." + sleep 5 +done + +echo "Running SQL script to initialize the database..." +# Access MySQL Command Line and run SQL script to initialize the database +mysql -u root -e "source /usr/local/tomcat/create.sql" + +echo "Displaying databases and tables..." +# Display the databases and tables +mysql -u root -e "SHOW ENGINE PERFORMANCE_SCHEMA STATUS;SHOW ENGINE INNODB STATUS;" +mysql -u root -e "SHOW DATABASES; USE shortener; SHOW TABLES; SHOW TABLE STATUS\G;" + +echo "Altering user and reloading privileges..." +# Run SQL commands to alter user and reload privileges +mysql -u root <giwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PH+zbKAI*`OII@OZ!!hjVQ@ivaHH(d7bP$*CdWA_T<&n)Km&0 zTN1_~zyUzn8pr$Fw*c@KWy!IhGyBH)VDkXF8x8bL>>up!?F{yw42FBVgM%k9xIYqBw@hgwpA7Dd zt3A2@Cyk`^GfG7T4`IIJku)uDhr#e57XNT~2WC_^40uv$O$83y}qekU% zl3)m}4dv-YiXo6RWi%oNh&L|~41!@W@V++({@SfA|8v4JRQIa@*2w?f{_vnJ|N94n zNBMt9iwDMk{t3gMSut)eTigy8$$Bx7vpp{G#JX@rMSWT$6|J54d7 z6N(BhW~gT1Lac% zhofUNHb$-~Vd}p;0AfZuhF8!y7usrPyKE$m%|FAS7I9{L)i;EzYdijIhF}-~V$wCr z0Lv5xw(s!-xX_?b1L1K#GrJx#!QWKECu3yp&fK12lu4==M}lj-)a4u|>IW&ZG=!bOU|?3SyIVw3GNz1bips4iKrBUC zT=(Gc{d;>Yk;ET4TYM5i{|{xT7D{7MxRgb88Fz>OGD>ZD***D@O1=cPFhsOabL0y{PY zKBfG!v!iCJM3KK?x@ctDl*JrPCo5I^gvmWbyIGj?rEm-)vCzcDo}S(=}A-~lMz;1{vQaL z%B=KBj6{+kkL||R0+gj`Q7W5qwB7Mq07EiI&_<^cqf;ZnRYXPR$ckpd2#?XkX3u8| zW#AMrVM+kyMrIT$Ln@Elf2oz(I3PG6pHnHg;gEt0s%OBc)-2y#1t~8OoLaW`A-1X3_2E-0T`=!|mHg~Y#Q%Dztw0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TnKrdJ2 z9r>C@QmC0G{^p$U-k(6Du9g41=I1VIV6FV`>^J3qXR!0?@%;ZFZKG1G_Q9*IDDv*N z)E75o$p0xPvDVz~@jzDoq(YM)XW|@rml`TbD8_w1FKe~@YgF3(?L~rfjO_Qd-=YTA z%YUo>YiGE>`}q4W57IitdL-fwS@t(K{=L?=@$X5dmqgSr%8+M5ockddyrtLBu7@c~ zJ1(ZE>m@=Vj5DgEn=HcdixZ&eA3ss`oeY&VyIl(!~ zhxG{B@*fz6(kT}b?+wQ~`QP8&U;6#0oyYv|gS0Ie5v@_WAO3L*4;M4!FwQ88DW5`0 zqH{7uRsIZ=%2MlXgPI{@FlA!wY9%S3z66PkXgWt5`>8!AJodJLAUVy{N@F76^R)8-`lr;Y>(}+J+_=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/k8s/shortener-chart/templates/service.yaml b/k8s/shortener-chart/templates/service.yaml new file mode 100644 index 0000000..157e0d6 --- /dev/null +++ b/k8s/shortener-chart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "shortener-chart.fullname" . }} + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "shortener-chart.selectorLabels" . | nindent 4 }} diff --git a/k8s/shortener-chart/templates/serviceaccount.yaml b/k8s/shortener-chart/templates/serviceaccount.yaml new file mode 100644 index 0000000..9c8188d --- /dev/null +++ b/k8s/shortener-chart/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "shortener-chart.serviceAccountName" . }} + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/k8s/shortener-chart/templates/tests/test-connection.yaml b/k8s/shortener-chart/templates/tests/test-connection.yaml new file mode 100644 index 0000000..c5e7377 --- /dev/null +++ b/k8s/shortener-chart/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "shortener-chart.fullname" . }}-test-connection" + labels: + {{- include "shortener-chart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "shortener-chart.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/k8s/shortener-chart/values.yaml b/k8s/shortener-chart/values.yaml new file mode 100644 index 0000000..64d7654 --- /dev/null +++ b/k8s/shortener-chart/values.yaml @@ -0,0 +1,107 @@ +# Default values for shortener-chart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nmpl/shortener + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "k8s" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Automatically mount a ServiceAccount's API credentials? + automount: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} +podLabels: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 8080 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +livenessProbe: + httpGet: + path: / + port: http +readinessProbe: + httpGet: + path: / + port: http + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +# Additional volumes on the output Deployment definition. +volumes: [] +# - name: foo +# secret: +# secretName: mysecret +# optional: false + +# Additional volumeMounts on the output Deployment definition. +volumeMounts: [] +# - name: foo +# mountPath: "/etc/foo" +# readOnly: true + +nodeSelector: {} + +tolerations: [] + +affinity: {}