diff --git a/Makefile b/Makefile index e9ddb03..30b68be 100644 --- a/Makefile +++ b/Makefile @@ -26,12 +26,9 @@ helm-docs: $(LOCALBIN)/helm-docs --template-files "${GITROOT}/chart/README.md.gotmpl" cat "${GITROOT}/Header.md" "${GITROOT}/chart/README.md" > "${GITROOT}/README.md" -create_kind: +new_kind: ./scripts/create_kind.sh -delete_kind: - kind delete clusters "${DEPLOYMENT_NAME}-cluster" - init: fmt vet deploy_debug: init @@ -43,6 +40,18 @@ deploy_e2e_debug: init deploy_local: init ./scripts/deploy.sh -deploy_e2e: init ginkgo delete_kind create_kind +deploy_e2e: init ginkgo new_kind ENV='e2e' ./scripts/deploy.sh +release-docker: + GITHUB_TOKEN="${GITHUB_TOKEN}" VERSION="${VERSION}" ./scripts/release-docker.sh + +release: release-docker helm-docs + # ex. make GITHUB_TOKEN=asdfasdf VERSION=1.0.0 release + helm package chart --destination chart + helm repo index --merge index.yaml chart/. + +release-github: + # ex. make VERSION=1.0.0 release-github + gh auth login --web + gh release create ${VERSION} --generate-notes diff --git a/README.md b/README.md index 48b210c..2854f31 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,11 @@ Currently, this image is not being hosted and so you have to build it yourself a ## Helm Install +```bash +helm repo add k8s-ephemeral-storage-metrics https://jmcgrath207.github.io/k8s-ephemeral-storage-metrics/chart +helm install my-deployment k8s-ephemeral-storage-metrics/k8s-ephemeral-storage-metrics +``` + ## Values | Key | Type | Default | Description | @@ -22,14 +27,19 @@ Currently, this image is not being hosted and so you have to build it yourself a | deploy_type | string | `"DaemonSet"` | | | dev.enabled | bool | `false` | | | image.imagePullPolicy | string | `"ifNotPresent"` | | -| image.repository | string | `"registry.lab.com/k8s-ephemeral-storage-metrics"` | | -| image.tag | string | `"asdf"` | | +| image.repository | string | `"ghcr.io/jmcgrath207/k8s-ephermeral-storage-metrics"` | | +| image.tag | string | `"1.0.0"` | | | interval | int | `15` | | | log_level | string | `"info"` | | -| prometheus.release | string | `"kube-prometheus"` | | +| prometheus.release | string | `"kube-prometheus-stack"` | | ## Contribute +### Start Kind +```bash +make new_kind +``` + ### Run locally ```bash make deploy_local diff --git a/chart/.helmignore b/chart/.helmignore index c86040e..89cb3a1 100644 --- a/chart/.helmignore +++ b/chart/.helmignore @@ -20,4 +20,5 @@ .idea/ *.tmproj .vscode/ -README.md.gotmpl \ No newline at end of file +README.md.gotmpl +*.tgz \ No newline at end of file diff --git a/chart/Chart.yaml b/chart/Chart.yaml index 6a240df..a384e2a 100644 --- a/chart/Chart.yaml +++ b/chart/Chart.yaml @@ -1,7 +1,7 @@ apiVersion: v2 name: k8s-ephemeral-storage-metrics version: 1.0.0 -appVersion: "1.0.0" +appVersion: 1.0.0 kubeVersion: ">=1.21.0-0" description: Ephemeral storage metrics for prometheus operator. home: https://github.com/jmcgrath207/k8s-ephemeral-storage-metrics diff --git a/chart/README.md b/chart/README.md index e37688d..c7a5bf6 100644 --- a/chart/README.md +++ b/chart/README.md @@ -1,5 +1,10 @@ ## Helm Install +```bash +helm repo add k8s-ephemeral-storage-metrics https://jmcgrath207.github.io/k8s-ephemeral-storage-metrics/chart +helm install my-deployment k8s-ephemeral-storage-metrics/k8s-ephemeral-storage-metrics +``` + ## Values | Key | Type | Default | Description | @@ -7,14 +12,19 @@ | deploy_type | string | `"DaemonSet"` | | | dev.enabled | bool | `false` | | | image.imagePullPolicy | string | `"ifNotPresent"` | | -| image.repository | string | `"registry.lab.com/k8s-ephemeral-storage-metrics"` | | -| image.tag | string | `"asdf"` | | +| image.repository | string | `"ghcr.io/jmcgrath207/k8s-ephermeral-storage-metrics"` | | +| image.tag | string | `"1.0.0"` | | | interval | int | `15` | | | log_level | string | `"info"` | | -| prometheus.release | string | `"kube-prometheus"` | | +| prometheus.release | string | `"kube-prometheus-stack"` | | ## Contribute +### Start Kind +```bash +make new_kind +``` + ### Run locally ```bash make deploy_local diff --git a/chart/README.md.gotmpl b/chart/README.md.gotmpl index 6464dff..e7ef3aa 100644 --- a/chart/README.md.gotmpl +++ b/chart/README.md.gotmpl @@ -1,9 +1,19 @@ ## Helm Install +```bash +helm repo add k8s-ephemeral-storage-metrics https://jmcgrath207.github.io/k8s-ephemeral-storage-metrics/chart +helm install my-deployment k8s-ephemeral-storage-metrics/k8s-ephemeral-storage-metrics +``` + {{ template "chart.valuesSection" . }} ## Contribute +### Start Kind +```bash +make new_kind +``` + ### Run locally ```bash make deploy_local diff --git a/chart/index.yaml b/chart/index.yaml new file mode 100644 index 0000000..235fd4c --- /dev/null +++ b/chart/index.yaml @@ -0,0 +1,26 @@ +apiVersion: v1 +entries: + k8s-ephemeral-storage-metrics: + - annotations: + artifacthub.io/license: MIT + artifacthub.io/links: | + - name: Documentation + url: https://github.com/jmcgrath207/k8s-ephemeral-storage-metrics + artifacthub.io/prerelease: "false" + apiVersion: v2 + appVersion: 1.0.0 + created: "2023-10-15T15:00:31.42811114-05:00" + description: Ephemeral storage metrics for prometheus operator. + digest: 1429b287b53e9bffe50db3cd3a9cd7e7d0ab8e645faa709aaa598959080e8d94 + home: https://github.com/jmcgrath207/k8s-ephemeral-storage-metrics + keywords: + - kubernetes + - metrics + kubeVersion: '>=1.21.0-0' + name: k8s-ephemeral-storage-metrics + sources: + - https://github.com/jmcgrath207/k8s-ephemeral-storage-metrics + urls: + - k8s-ephemeral-storage-metrics-1.0.0.tgz + version: 1.0.0 +generated: "2023-10-15T15:00:31.427665652-05:00" diff --git a/chart/k8s-ephemeral-storage-metrics-1.0.0.tgz b/chart/k8s-ephemeral-storage-metrics-1.0.0.tgz new file mode 100644 index 0000000..c748c03 Binary files /dev/null and b/chart/k8s-ephemeral-storage-metrics-1.0.0.tgz differ diff --git a/chart/templates/localDebugService.yaml b/chart/templates/localDebugService.yaml index b8f1558..710ccd3 100644 --- a/chart/templates/localDebugService.yaml +++ b/chart/templates/localDebugService.yaml @@ -1,14 +1,16 @@ -{{ if .Values.dev }} +{{ if .Values.dev.enabled }} apiVersion: v1 kind: Service metadata: name: debug labels: - {{- include "chart.labels" . | nindent 4 }} + {{- include "chart.labels" . | nindent 4 }} + app.kubernetes.io/name: k8s-ephemeral-storage-metrics-debug spec: type: NodePort selector: - {{- include "chart.selectorLabels" . | nindent 6 }} + {{- include "chart.selectorLabels" . | nindent 4 }} + app.kubernetes.io/name: k8s-ephemeral-storage-metrics-debug ports: - protocol: TCP port: 9999 diff --git a/chart/templates/metrics.yaml b/chart/templates/metrics.yaml index 400cabe..5dce8e6 100644 --- a/chart/templates/metrics.yaml +++ b/chart/templates/metrics.yaml @@ -3,10 +3,6 @@ kind: ServiceMonitor metadata: name: k8s-ephemeral-storage-metrics labels: - app.kubernetes.io/component: metrics - app.kubernetes.io/created-by: par - app.kubernetes.io/part-of: par - control-plane: controller-manager {{- include "chart.labels" . | nindent 4 }} spec: endpoints: @@ -19,7 +15,7 @@ spec: - {{ .Release.Namespace }} selector: matchLabels: - control-plane: controller-manager + {{- include "chart.selectorLabels" . | nindent 6 }} --- @@ -28,7 +24,7 @@ kind: Service metadata: name: k8s-ephemeral-storage-metrics labels: - {{- include "chart.labels" . | nindent 4 }} + {{- include "chart.labels" . | nindent 6 }} spec: type: ClusterIP selector: diff --git a/chart/templates/test_deployments.yaml b/chart/templates/test_deployments.yaml index b11d2ba..e0a70f8 100644 --- a/chart/templates/test_deployments.yaml +++ b/chart/templates/test_deployments.yaml @@ -1,4 +1,4 @@ -{{ if .Values.dev }} +{{ if .Values.dev.enabled }} apiVersion: apps/v1 kind: Deployment metadata: diff --git a/chart/values.yaml b/chart/values.yaml index 0e22ca6..7726031 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -1,13 +1,13 @@ image: - repository: registry.lab.com/k8s-ephemeral-storage-metrics - tag: "asdf" + repository: ghcr.io/jmcgrath207/k8s-ephermeral-storage-metrics + tag: 1.0.0 imagePullPolicy: ifNotPresent log_level: info deploy_type: DaemonSet # Note in testing, Kube API does not refresh faster than 10 seconds interval: 15 # Seconds prometheus: - release: kube-prometheus + release: kube-prometheus-stack # For local development and testing dev: diff --git a/charts/k8s-ephemeral-storage-metrics/templates/PodMonitor.yaml b/charts/k8s-ephemeral-storage-metrics/templates/PodMonitor.yaml deleted file mode 100644 index 62e5c3f..0000000 --- a/charts/k8s-ephemeral-storage-metrics/templates/PodMonitor.yaml +++ /dev/null @@ -1,29 +0,0 @@ -apiVersion: monitoring.coreos.com/v1 -kind: PodMonitor -# https://doc.crds.dev/github.com/prometheus-operator/kube-prometheus/monitoring.coreos.com/PodMonitor/v1@v0.7.0 -metadata: - name: {{ .Release.Name }} - # Change this to the namespace the Prometheus instance is running in - namespace: {{ .Release.Namespace }} - labels: - release: {{ .Values.prometheus.release }} - k8s-app: {{ .Release.Name }} -spec: - namespaceSelector: - matchNames: - - {{ .Release.Namespace }} - selector: - matchLabels: - k8s-app: {{ .Release.Name }} - podMetricsEndpoints: - - port: http - interval: {{ .Values.interval }} - relabelings: - - action: replace - sourceLabels: - - __meta_kubernetes_pod_node_name - targetLabel: node_name - - action: replace - sourceLabels: - - __meta_kubernetes_pod_host_ip - targetLabel: instance \ No newline at end of file diff --git a/charts/k8s-ephemeral-storage-metrics/values.yaml b/charts/k8s-ephemeral-storage-metrics/values.yaml deleted file mode 100644 index bbc1ba5..0000000 --- a/charts/k8s-ephemeral-storage-metrics/values.yaml +++ /dev/null @@ -1,12 +0,0 @@ -image: registry.lab.com/k8s-ephemeral-storage-metrics -log_level: info -deploy_type: DaemonSet -interval: 15s -prometheus: - release: kube-prometheus -resources: - limits: - memory: 200Mi - requests: - cpu: 100m - memory: 200Mi diff --git a/img/screenshot.png b/img/screenshot.png index e33f353..bb233ac 100644 Binary files a/img/screenshot.png and b/img/screenshot.png differ diff --git a/index.yaml b/index.yaml new file mode 100644 index 0000000..0dd2924 --- /dev/null +++ b/index.yaml @@ -0,0 +1,3 @@ +apiVersion: v1 +entries: {} +generated: "2023-10-15T12:07:02.648748624-05:00" diff --git a/main.go b/main.go index a3c0847..10dad77 100644 --- a/main.go +++ b/main.go @@ -144,6 +144,7 @@ func getMetrics() { log.Debug().Msg(fmt.Sprintf("pod %s/%s on %s with usedBytes: %f", podNamespace, podName, nodeName, usedBytes)) } + // TODO: Fix Sleep time for polling. elapsedTime := time.Now().Sub(start).Milliseconds() / 1000 adjustTime := sampleInterval - elapsedTime log.Debug().Msgf("Adjusted Poll time: %d seconds", adjustTime) diff --git a/scripts/create_kind.sh b/scripts/create_kind.sh index 663dcc4..c862c0c 100755 --- a/scripts/create_kind.sh +++ b/scripts/create_kind.sh @@ -8,6 +8,7 @@ function trap_func_kind() { trap 'trap_func_kind' ERR +kind delete clusters "${DEPLOYMENT_NAME}-cluster" kind create cluster \ --verbosity=6 \ @@ -22,3 +23,21 @@ kubectl get nodes -o wide # Deploy Service Monitor CRD kubectl apply --server-side -f https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/v0.65.1/example/prometheus-operator-crd/monitoring.coreos.com_servicemonitors.yaml + +if [[ ! $ENV =~ "e2e" ]]; then + # Deploy Prometheus + helm repo add prometheus-community https://prometheus-community.github.io/helm-charts + helm repo update + helm upgrade --install kube-prometheus-stack prometheus-community/kube-prometheus-stack --version 46.8.0 -n "${DEPLOYMENT_NAME}" --create-namespace \ + --set grafana.enabled=false \ + --set kubeApiServer.enabled=false \ + --set kubernetesServiceMonitors.enabled=false \ + --set kubelet.enabled=false \ + --set kubeControllerManager.enabled=false \ + --set kube-state-metrics.enabled=false \ + --set prometheus-node-exporter.enabled=false \ + --set alertmanager.enabled=false\ + --set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false \ + --set prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues=false \ + --set prometheus.prometheusSpec.probeSelectorNilUsesHelmValues=false +fi diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 9643e27..8f4ed6e 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -28,6 +28,7 @@ function main() { "image.imagePullPolicy=Never", "deploy_type=Deployment", "log_level=debug" + "dev.enabled=true" ) if [[ $ENV =~ "e2e" ]]; then @@ -37,7 +38,7 @@ function main() { docker build --build-arg TARGETOS=linux --build-arg TARGETARCH=amd64 -f DockerfileTestShrink -t local.io/local/shrink-test:latest . kind load docker-image -v 9 --name "${DEPLOYMENT_NAME}-cluster" --nodes "${DEPLOYMENT_NAME}-cluster-worker" "local.io/local/shrink-test:latest" - e2e_values_arr=("test.enabled=true", "interval=1") + e2e_values_arr=("interval=5") common_set_values_arr+=("${e2e_values_arr[@]}") fi @@ -46,6 +47,8 @@ function main() { docker build --build-arg TARGETOS=linux --build-arg TARGETARCH=amd64 -f ${dockerfile} -t local.io/local/$DEPLOYMENT_NAME:$image_tag . kind load docker-image -v 9 --name "${DEPLOYMENT_NAME}-cluster" --nodes "${DEPLOYMENT_NAME}-cluster-worker" "local.io/local/${DEPLOYMENT_NAME}:${image_tag}" + + # Install Par Chart helm upgrade --install $DEPLOYMENT_NAME ./chart \ --set "${common_set_values}" \ @@ -59,15 +62,25 @@ function main() { '{ "spec": {"template": { "spec":{"securityContext": null, "containers":[{"name":"metrics", "livenessProbe": null, "readinessProbe": null, "securityContext": null, "command": null, "args": null }]}}}}' fi + # kill dangling port forwards if found. + # Exporter Porter sudo ss -aK '( dport = :9100 or sport = :9100 )' | true + # Prometheus Port + sudo ss -aK '( dport = :9090 or sport = :9090 )' | true - # Start Prometheus Port Forward + # Start Exporter Port Forward ( sleep 10 printf "\n\n" && while :; do kubectl port-forward -n $DEPLOYMENT_NAME service/k8s-ephemeral-storage-metrics 9100:9100 || sleep 5; done ) & + # Start Prometheus Port Forward + ( + sleep 10 + printf "\n\n" && while :; do kubectl port-forward -n $DEPLOYMENT_NAME service/prometheus-operated 9090:9090 || sleep 5; done + ) & + if [[ $ENV == "debug" ]]; then # Background log following for manager ( diff --git a/scripts/release-docker.sh b/scripts/release-docker.sh new file mode 100755 index 0000000..83cb467 --- /dev/null +++ b/scripts/release-docker.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# Brief description of your script +# Copyright 2023 john + +set -e + + +package=k8s-ephermeral-storage-metrics + +if [ -z "$VERSION" ]; then + echo "The VERSION env is not set." + exit 1 +fi + +if [ -z "$GITHUB_TOKEN" ]; then + echo "The GITHUB_TOKEN env is not set." + exit 1 +fi + + +sed -i "s/tag.*/tag: ${VERSION}/g" chart/values.yaml +sed -i "s/version.*/version: ${VERSION}/g" chart/Chart.yaml +sed -i "s/appVersion.*/appVersion: ${VERSION}/g" chart/Chart.yaml + +docker login ghcr.io --username jmcgrath207 -p "${GITHUB_TOKEN}" +docker build -f Dockerfile -t ghcr.io/jmcgrath207/$package:$VERSION . +docker build -f Dockerfile -t ghcr.io/jmcgrath207/$package:latest . +docker push ghcr.io/jmcgrath207/$package:$VERSION +docker push ghcr.io/jmcgrath207/$package:latest \ No newline at end of file