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: {}