Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cronJob): allow override timeZone #94

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/dailyclean-docker-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up JDK 20
- name: Set up JDK 21
uses: actions/setup-java@v2
with:
java-version: '20'
java-version: '21'
distribution: 'adopt'
cache: maven
- name: Build with Maven
Expand Down
57 changes: 27 additions & 30 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

FROM node:18-buster AS web

FROM registry.access.redhat.com/ubi9/nodejs-22 AS web
ENV \
APP_ROOT=/app_root \
USER=patrick \
HOME=/home

RUN groupadd -r usersdocker && useradd -r -g usersdocker patrick
APP_ROOT=/opt/app-root \
HOME=/opt/app-root \
PATH=/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
USER=1001

WORKDIR ${APP_ROOT}
COPY --chown=${USER} ./web .
Expand All @@ -15,30 +13,29 @@ RUN npm ci
RUN npm test -- --run
RUN npm run build

FROM registry.access.redhat.com/ubi9/ubi:9.1.0-1782 AS build

FROM registry.access.redhat.com/ubi9/ubi:latest AS build
ENV \
APP_ROOT=/app_root \
USER=1004 \
HOME=/home
APP_ROOT=/opt/app-root \
HOME=/opt/app-root \
PATH=/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
USER=1001

WORKDIR ${APP_ROOT}

USER root

ENV MAVEN_VERSION 3.9.5
ENV MAVEN_DOWNLOAD_SUM 4810523ba025104106567d8a15a8aa19db35068c8c8be19e30b219a1d7e83bcab96124bf86dc424b1cd3c5edba25d69ec0b31751c136f88975d15406cab3842b
ENV MAVEN_VERSION 3.9.6
ENV MAVEN_DOWNLOAD_SUM 706f01b20dec0305a822ab614d51f32b07ee11d0218175e55450242e49d2156386483b506b3a4e8a03ac8611bae96395fd5eec15f50d3013d5deed6d1ee18224
ENV MAVEN_DIR=/opt/maven

RUN INSTALL_PKGS="java-17-openjdk java-17-openjdk-devel" && \
RUN INSTALL_PKGS="java-21-openjdk java-21-openjdk-devel" && \
dnf -y --setopt=tsflags=nodocs install $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
dnf -y clean all --enablerepo='*'

WORKDIR ${HOME}

RUN curl -fsSL https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz -O && \
#echo "sha512sum apache-maven-${MAVEN_VERSION}-bin.tar.gz" && \
echo "$MAVEN_DOWNLOAD_SUM apache-maven-${MAVEN_VERSION}-bin.tar.gz" | sha512sum -c - && \
tar xvfz apache-maven-${MAVEN_VERSION}-bin.tar.gz && \
mkdir -p ${MAVEN_DIR} && \
Expand All @@ -59,35 +56,36 @@ RUN rm -r /var/cache/dnf && \
rpm -V libstdc++-static && \
dnf -y clean all --enablerepo='*'

ENV JAVA_VERSION java20
ENV MANDREL_VERSION 23.0.0.0-Final
ENV MANDREL_DOWNLOAD_SUM 4b120a3272b4e637c3832b6245e100105ff07f46ad5b304f4844de5fad6b7fb7
ENV JAVA_VERSION java21
ENV MANDREL_VERSION 23.1.0.0-Final
ENV MANDREL_DOWNLOAD_SUM 49b5a7672c542a5636a92abc2bae53ab024207261f45766774b77ee44e40ba82
ENV MANDREL_DIR /opt/mandrel

WORKDIR ${APP_ROOT}

RUN curl -fsSL https://github.com/graalvm/mandrel/releases/download/mandrel-${MANDREL_VERSION}/mandrel-${JAVA_VERSION}-linux-amd64-${MANDREL_VERSION}.tar.gz -O && \
echo "$MANDREL_DOWNLOAD_SUM mandrel-${JAVA_VERSION}-linux-amd64-${MANDREL_VERSION}.tar.gz" | sha256sum -c - && \
tar xvfz mandrel-${JAVA_VERSION}-linux-amd64-${MANDREL_VERSION}.tar.gz && \
rm mandrel-${JAVA_VERSION}-linux-amd64-${MANDREL_VERSION}.tar.gz && \
mv mandrel-${JAVA_VERSION}-${MANDREL_VERSION} mandrel && \
rm -Rf mandrel/demo mandrel/man
mv mandrel-${JAVA_VERSION}-${MANDREL_VERSION} ${MANDREL_DIR} && \
rm -Rf ${MANDREL_DIR}/demo ${MANDREL_DIR}/man

ENV JAVA_HOME=${APP_ROOT}/mandrel
ENV GRAALVM_HOME=${APP_ROOT}/mandrel
ENV PATH=${JAVA_HOME}/bin:${PATH}
ENV JAVA_HOME=${MANDREL_DIR}
ENV GRAALVM_HOME=${MANDREL_DIR}
ENV PATH=${JAVA_HOME}/bin:${MAVEN_DIR}/bin:${PATH}

COPY --chown=${USER} ./api .
RUN rm -Rf ./src/main/resources/META-INF/resources
COPY --chown=${USER} --from=web ${APP_ROOT}/dist ./src/main/resources/META-INF/resources

RUN mvn package -Pnative -B

FROM registry.access.redhat.com/ubi9/ubi-minimal:9.1.0-1793 AS runtime

FROM registry.access.redhat.com/ubi9/ubi-minimal:latest AS runtime
ENV \
APP_ROOT=/app_root \
USER=1004 \
HOME=/home
APP_ROOT=/opt/app-root \
HOME=/opt/app-root \
PATH=/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
USER=1001

WORKDIR ${APP_ROOT}
COPY --chown=${USER} --from=build ${APP_ROOT}/target/*-runner ./application
Expand All @@ -96,4 +94,3 @@ EXPOSE 8080
USER ${USER}

CMD ["./application", "-Dquarkus.http.host=0.0.0.0", "-Xms40m", "-Xmx60m", "-Xmn20m"]

25 changes: 13 additions & 12 deletions api/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
<artifactId>dailyclean-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>3.1.2.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>3.1.2.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
<package>fr.axa.openpaas.dailyclean</package>
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>3.17.3</quarkus-plugin.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>3.17.3</quarkus.platform.version>
<surefire-plugin.version>3.5.2</surefire-plugin.version>
<package>fr.axa.openpaas.dailyclean</package>
</properties>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package fr.axa.openpaas.dailyclean.resource;


import fr.axa.openpaas.dailyclean.model.StartStopResponse;
import fr.axa.openpaas.dailyclean.service.KubernetesService;

import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.event.Observes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Path("pods")
public class PodsResource {

private static final Logger LOGGER = LoggerFactory.getLogger(PodsResource.class);

public final static String STOP_MESSAGE = "Service is stoping ...";
public final static String START_MESSAGE = "Service is starting ...";

Expand All @@ -36,4 +41,9 @@ public StartStopResponse start() {
response.setMessage(START_MESSAGE);
return response;
}

public void onStart(@Observes StartupEvent ev) {
LOGGER.info("The application is starting, default CronjobStop will be create if not exist...");
kubernetesService.createDefaultStopCronJobIfNotExist();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class KubernetesService {
@ConfigProperty(name = "service.job.serviceAccountName")
String serviceAccountName;

@ConfigProperty(name = "service.job.timeZone")
String timeZone;

@ConfigProperty(name = "service.job.defaultCronStop")
String defaultCronStop;

@ConfigProperty(name = "service.deployment.label.dailyclean")
String dailycleanLabelName;

Expand Down Expand Up @@ -146,6 +152,13 @@ public void updatingCronJobIfNeeded() {
}
}

public void createDefaultStopCronJobIfNotExist() {
CronJob stop = getCronJob(STOP);
if(stop == null) {
createCronJob(defaultCronStop, STOP);
}
}

private boolean isUpdatingCronJobNeeded(CronJob cronJob) {
boolean res = false;
if(cronJob != null) {
Expand Down Expand Up @@ -193,7 +206,7 @@ private void createCronJob(String cron, KubernetesArgument argument) {

logger.info("Creating cron job from object");
kubernetesClient.batch().v1().cronjobs().inNamespace(namespace)
.load(KubernetesUtils.createCronJobAsInputStream(argument, cron, imgName,serviceAccountName))
.load(KubernetesUtils.createCronJobAsInputStream(argument, cron, imgName, serviceAccountName, timeZone))
.createOrReplace();
logger.info("Successfully created cronjob with name {}", KubernetesUtils.getCronName(argument));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ private KubernetesUtils() {}
public static InputStream createCronJobAsInputStream(KubernetesArgument argument,
String cron,
String imgName,
String serviceAccountName) {
String serviceAccountName,
String timeZone) {
String text = getFileAsString("scripts/cronjob.yml");
String cronJobAsdString = text.replace(NAME.getPlaceholder(), getCronName(argument))
.replace(ARGUMENT.getPlaceholder(), argument.getValue())
.replace(SCHEDULE.getPlaceholder(), cron)
.replace(IMG_NAME.getPlaceholder(), imgName)
.replace(SERVICE_ACCOUNT_NAME.getPlaceholder(), serviceAccountName);
.replace(SERVICE_ACCOUNT_NAME.getPlaceholder(), serviceAccountName)
.replace(TIME_ZONE.getPlaceholder(), timeZone);

return new ByteArrayInputStream(cronJobAsdString.getBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public enum ScriptPlaceholder {
ARGUMENT("{{argument}}"),
SCHEDULE("{{schedule}}"),
IMG_NAME("{{imgName}}"),
SERVICE_ACCOUNT_NAME("{{serviceAccountName}}"),;
SERVICE_ACCOUNT_NAME("{{serviceAccountName}}"),
TIME_ZONE("{{timeZone}}"),;

private String placeholder;
ScriptPlaceholder(String placeholder) {
Expand Down
2 changes: 2 additions & 0 deletions api/api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ service.name=Dailyclean
service.version=1.0.0
service.job.imageName=axaguildev/dailyclean-job:latest
service.job.serviceAccountName=default
service.job.timeZone=UTC
service.job.defaultCronStop=0 18 * * *
service.deployment.label.dailyclean=axa.com/dailyclean

quarkus.scheduler.cron-type=unix
Expand Down
1 change: 1 addition & 0 deletions api/api/src/main/resources/scripts/cronjob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
spec:
concurrencyPolicy: Forbid
schedule: '{{schedule}}'
timeZone: '{{timeZone}}'
jobTemplate:
spec:
template:
Expand Down
Loading
Loading