From 7fe154e1822fd6449c7dc896c0d9904f61adbc86 Mon Sep 17 00:00:00 2001 From: Daniel Drack Date: Thu, 5 May 2022 15:36:42 +0200 Subject: [PATCH 1/3] added Dockerfile --- container/.gitignore | 1 + container/Dockerfile | 19 +++++++++++++++++++ container/customrun.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 container/.gitignore create mode 100644 container/Dockerfile create mode 100644 container/customrun.sh diff --git a/container/.gitignore b/container/.gitignore new file mode 100644 index 0000000..b708448 --- /dev/null +++ b/container/.gitignore @@ -0,0 +1 @@ +certificates/**/* \ No newline at end of file diff --git a/container/Dockerfile b/container/Dockerfile new file mode 100644 index 0000000..5ef27b1 --- /dev/null +++ b/container/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.9.12-slim-buster +ARG VERSION + +WORKDIR /app + +COPY customrun.sh . + +COPY certificates/*.crt /usr/local/share/ca-certificates/ +RUN update-ca-certificates + +# set CERT paths +# - Requests Library +ENV REQUESTS_CA_BUNDLE /etc/ssl/certs/ca-certificates.crt +# - openssl +ENV SSL_CERT_FILE /etc/ssl/certs/ca-certificates.crt + +RUN pip install artifactory-cleanup==${VERSION} + +CMD ["bash", "customrun.sh"] \ No newline at end of file diff --git a/container/customrun.sh b/container/customrun.sh new file mode 100644 index 0000000..c7ad4ba --- /dev/null +++ b/container/customrun.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +if [[ -z "$ARTI_USER" ]];then + echo "mandatory ARTI_USER environment variable not set!" + exit 3 +fi +if [[ -z "$ARTI_URL" ]];then + echo "mandatory ARTI_URL environment variable not set!" + exit 3 +fi +if [[ -z "$ARTI_PW" ]];then + echo "mandatory ARTI_PW environment variable not set!" + exit 3 +fi +if [[ -z "$RULES_CONFIG" ]];then + echo "mandatory RULES_CONFIG environment variable not set!" + exit 3 +fi + +# check if /tmp/rules.py exists +[ ! -f "$RULES_CONFIG" ] && echo "$RULES_CONFIG not found" && exit 3 + +# move to rules config parent directory +cd $( dirname $RULES_CONFIG) + +DRY_RUN="" +if [[ -v DISABLE_DRY_RUN ]]; then + DRY_RUN="--destroy" +fi + +# execute artifactory cleanup +echo "artifactory-cleanup $DRY_RUN --user $ARTI_USER --password $ARTI_PW --artifactory-server $ARTI_URL --config $( basename $RULES_CONFIG)" +artifactory-cleanup $DRY_RUN --user $ARTI_USER --password $ARTI_PW --artifactory-server $ARTI_URL --config $( basename $RULES_CONFIG) \ No newline at end of file From 208cfbf519be422cad5ff4d23b451ff15d400229 Mon Sep 17 00:00:00 2001 From: Daniel Drack Date: Tue, 10 May 2022 08:30:00 +0200 Subject: [PATCH 2/3] added helm chart --- README.md | 23 ++++++- helm/artifactory-cleanup/.helmignore | 23 +++++++ helm/artifactory-cleanup/Chart.yaml | 26 ++++++++ helm/artifactory-cleanup/templates/NOTES.txt | 5 ++ .../templates/_helpers.tpl | 62 +++++++++++++++++ .../templates/configMap-env.yaml | 8 +++ .../templates/configMap-rules.yaml | 7 ++ .../templates/cronJob.yaml | 50 ++++++++++++++ .../templates/serviceaccount.yaml | 12 ++++ helm/artifactory-cleanup/values.yaml | 66 +++++++++++++++++++ 10 files changed, 279 insertions(+), 3 deletions(-) create mode 100644 helm/artifactory-cleanup/.helmignore create mode 100644 helm/artifactory-cleanup/Chart.yaml create mode 100644 helm/artifactory-cleanup/templates/NOTES.txt create mode 100644 helm/artifactory-cleanup/templates/_helpers.tpl create mode 100644 helm/artifactory-cleanup/templates/configMap-env.yaml create mode 100644 helm/artifactory-cleanup/templates/configMap-rules.yaml create mode 100644 helm/artifactory-cleanup/templates/cronJob.yaml create mode 100644 helm/artifactory-cleanup/templates/serviceaccount.yaml create mode 100644 helm/artifactory-cleanup/values.yaml diff --git a/README.md b/README.md index 19b85e8..ba26f40 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,14 @@ +- [Artifactory cleanup](#artifactory-cleanup) +- [Tables of Contents](#tables-of-contents) - [Installation](#installation) - [Usage](#usage) - * [Commands](#commands) - * [Available Rules](#available-rules) - * [Artifact cleanup policies](#artifactory-cleanup-policies) + - [Commands](#commands) + - [Available Rules](#available-rules) + - [Artifact cleanup policies](#artifact-cleanup-policies) + - [Helm Chart Usage](#helm-chart-usage) @@ -110,3 +113,17 @@ RULES = [ ), ] ``` + +## Helm Chart Usage ## + +Make sure you have (helm installed)[https://helm.sh/docs/intro/install/]! +Put your rules.py, containing your [cleanup rules](#available-rules), into the helm chart folder (besides `Chart.yaml`). +Create a copy of `values.yaml` called `answer.yaml` in the same folder. +In the `answer.yaml` remove everything but the values you want to overwrite. +There you should also provide your credentials and the Artifactory URL in the `artifactoryCleanupConfig` section. +On your terminal, navigate to the helm chart folder and install the chart using the following command: + +```bash + helm upgrade -i . -n -f answer.yaml + # e.g.: helm upgrade -i artifactory-cleanup . -n artifactory-cleanup -f answer.yaml +``` diff --git a/helm/artifactory-cleanup/.helmignore b/helm/artifactory-cleanup/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/helm/artifactory-cleanup/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/helm/artifactory-cleanup/Chart.yaml b/helm/artifactory-cleanup/Chart.yaml new file mode 100644 index 0000000..ba5e3dc --- /dev/null +++ b/helm/artifactory-cleanup/Chart.yaml @@ -0,0 +1,26 @@ +apiVersion: v2 +name: artifactory-cleanup +description: | + artifactory-cleanup helm chart deploys a workload of type "CronJob" to periodically run "artifactory-cleanup". + Configuration if done in values.yaml. + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 1.0.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "0.3" diff --git a/helm/artifactory-cleanup/templates/NOTES.txt b/helm/artifactory-cleanup/templates/NOTES.txt new file mode 100644 index 0000000..f69d493 --- /dev/null +++ b/helm/artifactory-cleanup/templates/NOTES.txt @@ -0,0 +1,5 @@ +The cronJob is deployed at namespace: {{ .Release.Namespace }}. +The workload name is: {{ include "artifactory-cleanup.fullname" . }}. +To view the logs run 'kubectl -n {{ .Release.Namespace }} logs '. +Use the of the job you want to see the logs from. +To get a list of all jobs of the parenting cronJob run: 'kubectl -n {{ .Release.Namespace }} describe cronjob {{ include "artifactory-cleanup.fullname" . }}'. diff --git a/helm/artifactory-cleanup/templates/_helpers.tpl b/helm/artifactory-cleanup/templates/_helpers.tpl new file mode 100644 index 0000000..4f3071e --- /dev/null +++ b/helm/artifactory-cleanup/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "artifactory-cleanup.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "artifactory-cleanup.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "artifactory-cleanup.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "artifactory-cleanup.labels" -}} +helm.sh/chart: {{ include "artifactory-cleanup.chart" . }} +{{ include "artifactory-cleanup.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "artifactory-cleanup.selectorLabels" -}} +app.kubernetes.io/name: {{ include "artifactory-cleanup.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "artifactory-cleanup.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "artifactory-cleanup.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/helm/artifactory-cleanup/templates/configMap-env.yaml b/helm/artifactory-cleanup/templates/configMap-env.yaml new file mode 100644 index 0000000..a4abf11 --- /dev/null +++ b/helm/artifactory-cleanup/templates/configMap-env.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: artifactory-cleanup-env +data: + {{- range $key, $val := .Values.artifactoryCleanupConfig }} + {{ $key }}: {{ $val | quote }} + {{- end }} \ No newline at end of file diff --git a/helm/artifactory-cleanup/templates/configMap-rules.yaml b/helm/artifactory-cleanup/templates/configMap-rules.yaml new file mode 100644 index 0000000..36a305d --- /dev/null +++ b/helm/artifactory-cleanup/templates/configMap-rules.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: artifactory-cleanup-config-rules +data: + rules.py: | +{{ .Files.Get "rules.py" | indent 4 }} \ No newline at end of file diff --git a/helm/artifactory-cleanup/templates/cronJob.yaml b/helm/artifactory-cleanup/templates/cronJob.yaml new file mode 100644 index 0000000..3b04c31 --- /dev/null +++ b/helm/artifactory-cleanup/templates/cronJob.yaml @@ -0,0 +1,50 @@ +apiVersion: batch/v1 +kind: CronJob +metadata: + name: {{ include "artifactory-cleanup.fullname" . }} + labels: + {{- include "artifactory-cleanup.labels" . | nindent 4 }} +spec: + schedule: {{ .Values.cronJob.schedule }} + jobTemplate: + spec: + template: + spec: + restartPolicy: {{ .Values.cronJob.restartPolicy }} + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 12 }} + {{- end }} + serviceAccountName: {{ include "artifactory-cleanup.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 12 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 16 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + envFrom: + - configMapRef: + name: artifactory-cleanup-env + volumeMounts: + - name: config-rules-volume + mountPath: /tmp/ + resources: + {{- toYaml .Values.resources | nindent 16 }} + volumes: + - name: config-rules-volume + configMap: + name: artifactory-cleanup-config-rules + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 12 }} + {{- end }} diff --git a/helm/artifactory-cleanup/templates/serviceaccount.yaml b/helm/artifactory-cleanup/templates/serviceaccount.yaml new file mode 100644 index 0000000..023a61d --- /dev/null +++ b/helm/artifactory-cleanup/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "artifactory-cleanup.serviceAccountName" . }} + labels: + {{- include "artifactory-cleanup.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/helm/artifactory-cleanup/values.yaml b/helm/artifactory-cleanup/values.yaml new file mode 100644 index 0000000..5aca3f6 --- /dev/null +++ b/helm/artifactory-cleanup/values.yaml @@ -0,0 +1,66 @@ +# Default values for artifactory-cleanup. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +cronJob: + restartPolicy: Never + schedule: "0 * * * *" + +artifactoryCleanupConfig: + ARTIFACTORY_RULES_CONFIG: "/tmp/rules.py" + ARTIFACTORY_URL: https://myarti.com/artifactory + ARTIFACTORY_USER: dummy + ARTIFACTORY_PASSWORD: xxx + ARTIFACTORY_DESTROY_ARTEFACTS: "" + +image: + repository: my-repo.docer.com/artifactory-cleanup + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "latest" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: 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: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +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 + +# recommendation: +# kubernetes.io/os: linux +nodeSelector: {} + +tolerations: [] + +affinity: {} From ef758a7037e87917bc1389c7b41ffca94dd43bb2 Mon Sep 17 00:00:00 2001 From: Daniel Drack Date: Tue, 10 May 2022 08:37:52 +0200 Subject: [PATCH 3/3] removed container folder --- container/.gitignore | 1 - container/Dockerfile | 19 ------------------- container/customrun.sh | 33 --------------------------------- 3 files changed, 53 deletions(-) delete mode 100644 container/.gitignore delete mode 100644 container/Dockerfile delete mode 100644 container/customrun.sh diff --git a/container/.gitignore b/container/.gitignore deleted file mode 100644 index b708448..0000000 --- a/container/.gitignore +++ /dev/null @@ -1 +0,0 @@ -certificates/**/* \ No newline at end of file diff --git a/container/Dockerfile b/container/Dockerfile deleted file mode 100644 index 5ef27b1..0000000 --- a/container/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM python:3.9.12-slim-buster -ARG VERSION - -WORKDIR /app - -COPY customrun.sh . - -COPY certificates/*.crt /usr/local/share/ca-certificates/ -RUN update-ca-certificates - -# set CERT paths -# - Requests Library -ENV REQUESTS_CA_BUNDLE /etc/ssl/certs/ca-certificates.crt -# - openssl -ENV SSL_CERT_FILE /etc/ssl/certs/ca-certificates.crt - -RUN pip install artifactory-cleanup==${VERSION} - -CMD ["bash", "customrun.sh"] \ No newline at end of file diff --git a/container/customrun.sh b/container/customrun.sh deleted file mode 100644 index c7ad4ba..0000000 --- a/container/customrun.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -if [[ -z "$ARTI_USER" ]];then - echo "mandatory ARTI_USER environment variable not set!" - exit 3 -fi -if [[ -z "$ARTI_URL" ]];then - echo "mandatory ARTI_URL environment variable not set!" - exit 3 -fi -if [[ -z "$ARTI_PW" ]];then - echo "mandatory ARTI_PW environment variable not set!" - exit 3 -fi -if [[ -z "$RULES_CONFIG" ]];then - echo "mandatory RULES_CONFIG environment variable not set!" - exit 3 -fi - -# check if /tmp/rules.py exists -[ ! -f "$RULES_CONFIG" ] && echo "$RULES_CONFIG not found" && exit 3 - -# move to rules config parent directory -cd $( dirname $RULES_CONFIG) - -DRY_RUN="" -if [[ -v DISABLE_DRY_RUN ]]; then - DRY_RUN="--destroy" -fi - -# execute artifactory cleanup -echo "artifactory-cleanup $DRY_RUN --user $ARTI_USER --password $ARTI_PW --artifactory-server $ARTI_URL --config $( basename $RULES_CONFIG)" -artifactory-cleanup $DRY_RUN --user $ARTI_USER --password $ARTI_PW --artifactory-server $ARTI_URL --config $( basename $RULES_CONFIG) \ No newline at end of file