From 4ef588f66b9bd370de0ad87c45bc206dc4a5a652 Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 25 Nov 2021 19:42:54 +0100 Subject: [PATCH] feat: initial commit --- .github/workflows/docker.yaml | 39 +++++++++ .gitignore | 2 + Dockerfile | 13 +++ app/main.py | 41 ++++++++++ helm/kubernetes-csr-exporter/.helmignore | 23 ++++++ helm/kubernetes-csr-exporter/Chart.yaml | 24 ++++++ .../templates/NOTES.txt | 22 ++++++ .../templates/_helpers.tpl | 62 +++++++++++++++ .../templates/deployment.yaml | 61 ++++++++++++++ .../templates/service.yaml | 15 ++++ .../templates/serviceaccount.yaml | 12 +++ helm/kubernetes-csr-exporter/values.yaml | 79 +++++++++++++++++++ requirements.txt | 17 ++++ 13 files changed, 410 insertions(+) create mode 100644 .github/workflows/docker.yaml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app/main.py create mode 100644 helm/kubernetes-csr-exporter/.helmignore create mode 100644 helm/kubernetes-csr-exporter/Chart.yaml create mode 100644 helm/kubernetes-csr-exporter/templates/NOTES.txt create mode 100644 helm/kubernetes-csr-exporter/templates/_helpers.tpl create mode 100644 helm/kubernetes-csr-exporter/templates/deployment.yaml create mode 100644 helm/kubernetes-csr-exporter/templates/service.yaml create mode 100644 helm/kubernetes-csr-exporter/templates/serviceaccount.yaml create mode 100644 helm/kubernetes-csr-exporter/values.yaml create mode 100644 requirements.txt diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..6ec5ec5 --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,39 @@ +name: Docker +on: + push: + tags: + - v*.*.* +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c3b4f44 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/* +venv/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4585fa5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3-alpine + +RUN mkdir -p /usr/src/app + +WORKDIR /usr/src/app +COPY requirements.txt /usr/src/app/ + +RUN pip3 install --no-cache-dir -r requirements.txt +COPY . /usr/src/app + +EXPOSE 5000 +USER 1001 +CMD [ "python", "./main.py" ] \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..048236a --- /dev/null +++ b/app/main.py @@ -0,0 +1,41 @@ +from __future__ import print_function +import time +import kubernetes.client +from kubernetes import config +from kubernetes.client.rest import ApiException +from pprint import pprint +import json +from flask import Flask,render_template,Response +from prometheus_client import Gauge,generate_latest + +CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8') + +c = Gauge('kube_csr_pending', 'List of unapproved CSR', ['name']) +config.load_kube_config() + +app = Flask(__name__) + +@app.route("/metrics") +def metrics(): + try: + configuration = kubernetes.client.Configuration().get_default_copy() + + with kubernetes.client.ApiClient(configuration) as api_client: + api_instance = kubernetes.client.CertificatesV1beta1Api(api_client) + + certificates = api_instance.list_certificate_signing_request() + for certificate in certificates.items: + if certificate.status.certificate is None: + c.labels(name=certificate.metadata.name).set(1) + else: + c.labels(name=certificate.metadata.name).set(0) + except ApiException as e: + print("Exception when calling CertificatesV1beta1Api->list_certificate_signing_request: %s\n" % e) + except AttributeError: + configuration = Configuration() + configuration.assert_hostname = False + + return Response(generate_latest(), mimetype=CONTENT_TYPE_LATEST) + +if __name__ == "__main__": + app.run(host='0.0.0.0',port="8080") \ No newline at end of file diff --git a/helm/kubernetes-csr-exporter/.helmignore b/helm/kubernetes-csr-exporter/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/helm/kubernetes-csr-exporter/.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/kubernetes-csr-exporter/Chart.yaml b/helm/kubernetes-csr-exporter/Chart.yaml new file mode 100644 index 0000000..2474b3f --- /dev/null +++ b/helm/kubernetes-csr-exporter/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: kubernetes-csr-exporter +description: A Helm chart for Kubernetes + +# 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: 0.1.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: "1.16.0" diff --git a/helm/kubernetes-csr-exporter/templates/NOTES.txt b/helm/kubernetes-csr-exporter/templates/NOTES.txt new file mode 100644 index 0000000..41b0b2e --- /dev/null +++ b/helm/kubernetes-csr-exporter/templates/NOTES.txt @@ -0,0 +1,22 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "kubernetes-csr-exporter.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "kubernetes-csr-exporter.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "kubernetes-csr-exporter.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "kubernetes-csr-exporter.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }} diff --git a/helm/kubernetes-csr-exporter/templates/_helpers.tpl b/helm/kubernetes-csr-exporter/templates/_helpers.tpl new file mode 100644 index 0000000..063c05e --- /dev/null +++ b/helm/kubernetes-csr-exporter/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "kubernetes-csr-exporter.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 "kubernetes-csr-exporter.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 "kubernetes-csr-exporter.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "kubernetes-csr-exporter.labels" -}} +helm.sh/chart: {{ include "kubernetes-csr-exporter.chart" . }} +{{ include "kubernetes-csr-exporter.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "kubernetes-csr-exporter.selectorLabels" -}} +app.kubernetes.io/name: {{ include "kubernetes-csr-exporter.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "kubernetes-csr-exporter.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "kubernetes-csr-exporter.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/helm/kubernetes-csr-exporter/templates/deployment.yaml b/helm/kubernetes-csr-exporter/templates/deployment.yaml new file mode 100644 index 0000000..241dbca --- /dev/null +++ b/helm/kubernetes-csr-exporter/templates/deployment.yaml @@ -0,0 +1,61 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "kubernetes-csr-exporter.fullname" . }} + labels: + {{- include "kubernetes-csr-exporter.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "kubernetes-csr-exporter.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "kubernetes-csr-exporter.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "kubernetes-csr-exporter.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/helm/kubernetes-csr-exporter/templates/service.yaml b/helm/kubernetes-csr-exporter/templates/service.yaml new file mode 100644 index 0000000..d7f0f84 --- /dev/null +++ b/helm/kubernetes-csr-exporter/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "kubernetes-csr-exporter.fullname" . }} + labels: + {{- include "kubernetes-csr-exporter.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "kubernetes-csr-exporter.selectorLabels" . | nindent 4 }} diff --git a/helm/kubernetes-csr-exporter/templates/serviceaccount.yaml b/helm/kubernetes-csr-exporter/templates/serviceaccount.yaml new file mode 100644 index 0000000..5aeb1a6 --- /dev/null +++ b/helm/kubernetes-csr-exporter/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "kubernetes-csr-exporter.serviceAccountName" . }} + labels: + {{- include "kubernetes-csr-exporter.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/helm/kubernetes-csr-exporter/values.yaml b/helm/kubernetes-csr-exporter/values.yaml new file mode 100644 index 0000000..c0dff4d --- /dev/null +++ b/helm/kubernetes-csr-exporter/values.yaml @@ -0,0 +1,79 @@ + +replicaCount: 1 + +image: + repository: ghcr.io/camptocamp/kubernetes-csr-exporter + 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: "kubernetes-csr-exporter" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 5000 + +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 + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4368d2c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +cachetools==4.2.4 +certifi==2021.10.8 +charset-normalizer==2.0.8 +google-auth==2.3.3 +idna==3.3 +kubernetes==19.15.0 +oauthlib==3.1.1 +pyasn1==0.4.8 +pyasn1-modules==0.2.8 +python-dateutil==2.8.2 +PyYAML==6.0 +requests==2.26.0 +requests-oauthlib==1.3.0 +rsa==4.8 +six==1.16.0 +urllib3==1.26.7 +websocket-client==1.2.1