Skip to content

Commit

Permalink
Merge pull request #6 from yashsharma127/PHEE-operator
Browse files Browse the repository at this point in the history
[1] - Enhancements: Add ConfigMap and Ingress Creation Methods; Include Script for Operator Setup on Mifos-Gazelle
  • Loading branch information
IOhacker authored Aug 21, 2024
2 parents 1b2c822 + a886fcc commit e561b08
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 30 deletions.
178 changes: 178 additions & 0 deletions PHEE-operator/deploy-operator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/bin/bash

# Commands that can be used for this script
# ./deploy-operator.sh -m deploy
# ./deploy-operator.sh -m cleanup

# Exit script on any error
set -e

# Text Formatting
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;94m'
BOLD='\033[1m'
NC='\033[0m' # No Color


# Common Environment Setup for both deploy and cleanup
export IMAGE_NAME="ph-ee-importer-rdbms-operator"
export IMAGE_TAG="latest"
export OPERATOR_NAMESPACE="default"
export HELM_CHART_PATH="../mifos-gazelle/src/mojafos/deployer/apps/ph_template/helm/gazelle"
export RELEASE_NAME="phee"
export VALUES_FILE="ys_values.yaml"
export HELM_NAMESPACE="paymenthub"

# Function to deploy the operator
deploy_operator() {

# Display script banner
echo -e "${BLUE}${BOLD}"
echo "===================================================="
echo " K8s Operator Deployment "
echo "===================================================="
echo -e "${NC}"

echo -e "${YELLOW}${BOLD}Starting Operator Deployment...${NC}"

# 1. Pre-requisites Check
echo -e "${BLUE}Checking pre-requisites...${NC}"
# Check if kubectl, docker, mvn, and helm are installed
if ! command -v kubectl &> /dev/null || ! command -v docker &> /dev/null || ! command -v helm &> /dev/null
then
echo -e "${RED}kubectl, docker, and helm are required but not installed. Exiting.${NC}"
exit 1
fi

# Check if Java is installed, if not, install it
if ! command -v java &> /dev/null
then
echo -e "${YELLOW}Java is not installed. Installing OpenJDK 21...${NC}"
sudo apt-get update
sudo apt-get install openjdk-21-jdk -y || { echo -e "${RED}Failed to install Java. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Java installed successfully.${NC}"
else
echo -e "${GREEN}Java is already installed.${NC}"
fi

# Check if Maven is installed, if not, install it
if ! command -v mvn &> /dev/null
then
echo -e "${YELLOW}Maven is not installed. Installing Maven...${NC}"
sudo apt-get install maven -y || { echo -e "${RED}Failed to install Maven. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Maven installed successfully.${NC}"
else
echo -e "${GREEN}Maven is already installed.${NC}"
fi

echo -e "${GREEN}Pre-requisites are met.${NC}"

# 2. Upgrading the Helm chart with ys_values.yaml
echo -e "${BLUE}Upgrading helm chart...${NC}"
helm upgrade $RELEASE_NAME $HELM_CHART_PATH -n $HELM_NAMESPACE -f $VALUES_FILE || { echo -e "${RED}Helm upgrade failed. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Helm chart upgrade successful.${NC}"

# 3. Build the Java Project
echo -e "${BLUE}Building the Java project...${NC}"
mvn clean install || { echo -e "${RED}Maven build failed. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Java project build successful.${NC}"

# 4. Build the Docker Image Locally
echo -e "${BLUE}Building the Docker image locally...${NC}"
mvn compile jib:dockerBuild -Dimage=$IMAGE_NAME:$IMAGE_TAG || { echo -e "${RED}Docker image build failed. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Docker image build successful.${NC}"

# 5. Save the Docker Image to a TAR File
echo -e "${BLUE}Saving the Docker image to a TAR file...${NC}"
sudo docker save $IMAGE_NAME:$IMAGE_TAG -o $IMAGE_NAME.tar || { echo -e "${RED}Docker save failed. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Docker image saved to TAR file.${NC}"

# 6. Import the Docker Image into k3s Cluster
echo -e "${BLUE}Importing the Docker image into the k3s cluster...${NC}"
sudo k3s ctr images import $IMAGE_NAME.tar || { echo -e "${RED}Image import to k3s failed. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Docker image successfully imported to k3s.${NC}"

# 7. Apply CRD, Operator, and CR
echo -e "${BLUE}Applying CRD...${NC}"
kubectl apply -f deploy/crds/ph-ee-importer-rdbms-crd.yaml
echo -e "${GREEN}CRD applied successfully.${NC}"

echo -e "${BLUE}Deploying the operator...${NC}"
kubectl apply -f deploy/operator/operator.yaml
echo -e "${GREEN}Operator deployed successfully.${NC}"

echo -e "${BLUE}Applying CR...${NC}"
kubectl apply -f deploy/cr/ph-ee-importer-rdbms-cr.yaml
echo -e "${GREEN}CR applied successfully.${NC}"

# 8. Post-Deployment Verification
echo -e "${BLUE}Verifying deployment...${NC}"
kubectl rollout status deployment/phee-importer-operator -n $OPERATOR_NAMESPACE || { echo -e "${RED}Deployment verification failed. Exiting.${NC}"; exit 1; }

echo -e "${GREEN}${BOLD}Operator deployment completed successfully!${NC}"
}

# Function to clean up the deployed resources
cleanup_operator() {

# Display script banner
echo -e "${BLUE}${BOLD}"
echo "===================================================="
echo " K8s Operator Cleaup "
echo "===================================================="
echo -e "${NC}"

echo -e "${YELLOW}${BOLD}Starting Cleanup...${NC}"

echo -e "${BLUE}Deleting CR...${NC}"
kubectl delete -f deploy/cr/ph-ee-importer-rdbms-cr.yaml || { echo -e "${RED}Failed to delete CR. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}CR deleted successfully.${NC}"

echo -e "${BLUE}Deleting operator...${NC}"
kubectl delete -f deploy/operator/operator.yaml || { echo -e "${RED}Failed to delete operator. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Operator deleted successfully.${NC}"

echo -e "${BLUE}Deleting CRD...${NC}"
kubectl delete -f deploy/crds/ph-ee-importer-rdbms-crd.yaml || { echo -e "${RED}Failed to delete CRD. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}CRD deleted successfully.${NC}"

if [[ -z "$IMAGE_NAME" || -z "$IMAGE_TAG" ]]; then
echo -e "${YELLOW}Image name or tag is not set correctly. Skipping Docker image removal.${NC}"
else
echo -e "${BLUE}Removing local Docker image...${NC}"
docker rmi $IMAGE_NAME:$IMAGE_TAG || { echo -e "${RED}Failed to remove Docker image. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}Docker image removed successfully.${NC}"
fi

echo -e "${BLUE}Removing the TAR file...${NC}"
rm $IMAGE_NAME.tar || { echo -e "${RED}Failed to remove TAR file. Exiting.${NC}"; exit 1; }
echo -e "${GREEN}TAR file removed successfully.${NC}"

echo -e "${GREEN}${BOLD}Cleanup completed successfully!${NC}"
}

# Main script logic to handle different modes
if [ "$1" == "-m" ]; then
case "$2" in
deploy)
deploy_operator
;;
cleanup)
cleanup_operator
;;
*)
echo -e "${RED}Invalid mode specified. Use '-m deploy' or '-m cleanup'.${NC}"
exit 1
;;
esac
else
echo -e "${YELLOW}Usage: ./deploy-operator.sh -m <mode>${NC}"
echo "Modes:"
echo " ${GREEN}deploy${NC} - Upgrade Helm chart, build, deploy, and verify the operator."
echo " ${GREEN}cleanup${NC} - Remove the operator and related resources."
exit 1
fi

# End of script
54 changes: 33 additions & 21 deletions PHEE-operator/deploy/cr/ph-ee-importer-rdbms-cr.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: my.custom.group/v1
kind: PhEeImporterRdbms
metadata:
name: importer-rdbms
name: ph-ee-importer-rdbms
namespace: paymenthub
spec:
enabled: true
replicas: 1
image: docker.io/openmf/ph-ee-importer-rdbms:v1.7.1
image: docker.io/openmf/ph-ee-importer-rdbms:v1.13.1 # Updated image version
springProfilesActive: local,tenantsConnection
datasource:
username: mifos
Expand All @@ -25,22 +25,34 @@ spec:
cpu: 100m
memory: 256Mi
javaToolOptions: "-Xmx256M"
bucketName: paymenthub-ee-dev
livenessProbe:
path: /actuator/health/liveness
port: 9191
initialDelaySeconds: 120
periodSeconds: 30
failureThreshold: 3
timeoutSeconds: 5
readinessProbe:
path: /actuator/health/readiness
port: 9191
initialDelaySeconds: 120
periodSeconds: 30
failureThreshold: 3
timeoutSeconds: 5
rbacEnabled: true # Toggle for RBACs
secretEnabled: true # Toggle for Secrets
configMapEnabled: false # Toggle for ConfigMaps
ingressEnabled: false # Toggle for Ingress
bucketName: paymenthub-ee # Corrected bucket name
# livenessProbe:
# path: /actuator/health/liveness
# port: 9191
# initialDelaySeconds: 120
# periodSeconds: 30
# failureThreshold: 3
# timeoutSeconds: 5
# readinessProbe:
# path: /actuator/health/readiness
# port: 9191
# initialDelaySeconds: 120
# periodSeconds: 30
# failureThreshold: 3
# timeoutSeconds: 5
ingress: # Ingress configuration
host: "ops.sandbox.fynarfin.io" # Host from the provided Ingress describe
path: "/" # Path as indicated in the Ingress describe
className: "nginx" # Ingress class as indicated in annotations
annotations: # Annotations from the provided Ingress describe
kubernetes.io/ingress.class: "nginx"
meta.helm.sh/release-name: "moja-ph"
meta.helm.sh/release-namespace: "paymenthub"
tls: # TLS configuration from the provided Ingress describe
- hosts:
- "ops.sandbox.fynarfin.io"
secretName: "sandbox-secret" # Secret name from the provided Ingress describe
rbacEnabled: true # Toggle for RBACs (optional, ensure it's necessary)
secretEnabled: true # Toggle for Secrets (relevant due to the usage of secrets in the deployment)
configMapEnabled: false # not working due to non dynamic naming,,, Enabled to match the usage of the ConfigMap `ph-ee-config`
ingressEnabled: true # Ingress not mentioned, keep as false unless required
26 changes: 26 additions & 0 deletions PHEE-operator/deploy/crds/ph-ee-importer-rdbms-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ spec:
type: integer
timeoutSeconds:
type: integer
# New Ingress Configuration Section
ingress:
type: object
properties:
host:
type: string
path:
type: string
default: "/"
className:
type: string
annotations:
type: object
additionalProperties:
type: string
tls:
type: array
items:
type: object
properties:
hosts:
type: array
items:
type: string
secretName:
type: string
rbacEnabled: # Toggle for RBACs
type: boolean
default: false
Expand Down
4 changes: 4 additions & 0 deletions PHEE-operator/deploy/operator/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ rules:
resources:
- ingresses
verbs:
- create
- get
- list
- watch
- update
- patch
- delete


---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
Loading

0 comments on commit e561b08

Please sign in to comment.