From cd89b1c580fc96f10f7a43374b1480fbdad0e6d3 Mon Sep 17 00:00:00 2001 From: InputObject2 <30133702+InputObject2@users.noreply.github.com> Date: Fri, 24 May 2024 12:09:30 -0400 Subject: [PATCH] Added chart, auto-build and updated dockerfile --- .github/workflows/build-and-push.yml | 61 ++++++++++++++++++++++++++ Dockerfile | 64 +++++++++++++++------------- helm/snmpd/Chart.yaml | 6 +++ helm/snmpd/templates/configmap.yaml | 7 +++ helm/snmpd/templates/daemonset.yaml | 38 +++++++++++++++++ helm/snmpd/templates/service.yaml | 25 +++++++++++ helm/snmpd/values.yaml | 36 ++++++++++++++++ hooks/build | 5 --- 8 files changed, 208 insertions(+), 34 deletions(-) create mode 100644 .github/workflows/build-and-push.yml create mode 100644 helm/snmpd/Chart.yaml create mode 100644 helm/snmpd/templates/configmap.yaml create mode 100644 helm/snmpd/templates/daemonset.yaml create mode 100644 helm/snmpd/templates/service.yaml create mode 100644 helm/snmpd/values.yaml delete mode 100755 hooks/build diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml new file mode 100644 index 0000000..512fb20 --- /dev/null +++ b/.github/workflows/build-and-push.yml @@ -0,0 +1,61 @@ +name: Build and Push Docker Image + +on: + schedule: + - cron: '0 0 * * 0' # Runs every Sunday at midnight + workflow_dispatch: # Allows manual trigger + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Get latest snmpd version + id: get-version + run: | + latest_version=$(curl -s "https://sourceforge.net/projects/net-snmp/files/net-snmp/" | grep -Po 'href="\/projects\/net-snmp\/files\/net-snmp\/\K[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -1) + echo "latest_version=${latest_version}" >> $GITHUB_ENV + major_version=$(echo $latest_version | cut -d'.' -f1) + minor_version=$(echo $latest_version | cut -d'.' -f1-2) + echo "major_version=${major_version}" >> $GITHUB_ENV + echo "minor_version=${minor_version}" >> $GITHUB_ENV + + - name: Build Docker image + run: | + docker build --build-arg SNMPD_VERSION=${{ env.latest_version }} -t ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.latest_version }} . + + - name: Tag Docker image + run: | + docker tag ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.latest_version }} ghcr.io/${{ github.repository_owner }}/snmpd:latest + docker tag ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.latest_version }} ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.major_version }} + docker tag ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.latest_version }} ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.minor_version }} + + - name: Push Docker image + run: | + docker push ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.latest_version }} + docker push ghcr.io/${{ github.repository_owner }}/snmpd:latest + docker push ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.major_version }} + docker push ghcr.io/${{ github.repository_owner }}/snmpd:${{ env.minor_version }} + + - name: Tag the repository with the latest version + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git tag ${latest_version} + git push origin ${latest_version} diff --git a/Dockerfile b/Dockerfile index de2c4cb..59de2d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,35 +1,41 @@ -FROM alpine:latest - -MAINTAINER Troy Kelly - -# Build-time metadata as defined at http://label-schema.org -ARG BUILD_DATE -ARG VCS_REF -ARG VERSION -LABEL org.label-schema.build-date=$BUILD_DATE \ - org.label-schema.name="Docker image to provide the net-snmp daemon" \ - org.label-schema.description="Provides snmpd for CoreOS and other small footprint environments without package managers" \ - org.label-schema.url="https://really.ai/about/opensource" \ - org.label-schema.vcs-ref=$VCS_REF \ - org.label-schema.vcs-url="https://github.com/reallyreally/docker-snmpd" \ - org.label-schema.vendor="Really Really, Inc." \ - org.label-schema.version=$VERSION \ - org.label-schema.schema-version="1.0" +# Use a specific version of Debian to ensure compatibility +FROM debian:buster-slim + +ARG SNMPD_VERSION=5.9.4 EXPOSE 161 161/udp -RUN apk add --update --no-cache linux-headers alpine-sdk curl findutils sed && \ - mkdir -p /etc/snmp && \ - curl -L "https://sourceforge.net/projects/net-snmp/files/5.4.5-pre-releases/net-snmp-5.4.5.rc1.tar.gz/download" -o net-snmp.tgz && \ - tar zxvf net-snmp.tgz && \ - cd net-snmp-* && \ - find . -type f -print0 | xargs -0 sed -i 's/\"\/proc/\"\/host_proc/g' && \ - ./configure --prefix=/usr/local --disable-ipv6 --disable-snmpv1 --with-defaults && \ - make && \ - make install && \ - cd .. && \ - rm -Rf ./net-snmp* && \ - apk del linux-headers alpine-sdk curl findutils sed +# Install build dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + curl \ + ca-certificates \ + libssl-dev \ + libperl-dev \ + libwrap0-dev \ + libreadline-dev \ + libsnmp-dev \ + file + +# Download and extract net-snmp +RUN mkdir -p /etc/snmp \ + && curl -L "https://sourceforge.net/projects/net-snmp/files/net-snmp/$SNMPD_VERSION/net-snmp-$SNMPD_VERSION.tar.gz/download" -o net-snmp.tgz \ + && tar zxvf net-snmp.tgz \ + && rm net-snmp.tgz + +# Build and install net-snmp +RUN cd net-snmp-$SNMPD_VERSION \ + && find . -type f -print0 | xargs -0 sed -i 's/\"\/proc/\"\/host_proc/g' \ + && ./configure --prefix=/usr/local --disable-ipv6 --disable-snmpv1 --with-defaults \ + && make \ + && make install \ + && cd .. \ + && rm -rf net-snmp-$SNMPD_VERSION + +# Remove build dependencies to keep the image size small +RUN apt-get purge -y --auto-remove \ + build-essential \ + curl COPY snmpd.conf /etc/snmp diff --git a/helm/snmpd/Chart.yaml b/helm/snmpd/Chart.yaml new file mode 100644 index 0000000..f94da11 --- /dev/null +++ b/helm/snmpd/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: snmpd +appVersion: "5.9.4" +version: "1.0.0" +type: application +description: A Helm chart for deploying snmpd as a DaemonSet diff --git a/helm/snmpd/templates/configmap.yaml b/helm/snmpd/templates/configmap.yaml new file mode 100644 index 0000000..7dcbdb0 --- /dev/null +++ b/helm/snmpd/templates/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: snmpd-config +data: + snmpd.conf: |- +{{ .Values.config.snmpdConfig | indent 4 }} diff --git a/helm/snmpd/templates/daemonset.yaml b/helm/snmpd/templates/daemonset.yaml new file mode 100644 index 0000000..8bd5c63 --- /dev/null +++ b/helm/snmpd/templates/daemonset.yaml @@ -0,0 +1,38 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: snmpd-daemonset +spec: + selector: + matchLabels: + app: snmpd + template: + metadata: + labels: + app: snmpd + spec: + nodeSelector: + {{- toYaml .Values.nodeSelector | nindent 8 }} + containers: + - name: snmpd + image: {{ .Values.image.repository }}:{{ .Values.image.tag }} + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + {{- range .Values.ports }} + - name: {{ .name }} + containerPort: {{ .containerPort }} + hostPort: {{ .hostPort }} + protocol: {{ .protocol }} + {{- end }} + volumeMounts: + - name: snmpd-config + mountPath: /etc/snmp/snmpd.conf + subPath: snmpd.conf + securityContext: + runAsUser: 0 + runAsGroup: 0 + allowPrivilegeEscalation: true + volumes: + - name: snmpd-config + configMap: + name: snmpd-config diff --git a/helm/snmpd/templates/service.yaml b/helm/snmpd/templates/service.yaml new file mode 100644 index 0000000..45f2461 --- /dev/null +++ b/helm/snmpd/templates/service.yaml @@ -0,0 +1,25 @@ +apiVersion: v1 +kind: Service +metadata: + name: snmpd-service +spec: + selector: + app: snmpd + ports: + - name: snmp-tcp + port: 161 + protocol: TCP + nodePort: 3161 + - name: snmp-trap-tcp + port: 162 + protocol: TCP + nodePort: 3162 + - name: snmp-udp + port: 161 + protocol: UDP + nodePort: 3161 + - name: snmp-trap-udp + port: 162 + protocol: UDP + nodePort: 3162 + type: NodePort diff --git a/helm/snmpd/values.yaml b/helm/snmpd/values.yaml new file mode 100644 index 0000000..26639da --- /dev/null +++ b/helm/snmpd/values.yaml @@ -0,0 +1,36 @@ +image: + repository: ghcr.io/inputobject2/snmpd:latest + tag: latest + pullPolicy: IfNotPresent + +config: + snmpdConfig: | + # Change "public" on the line below to your preferred SNMP community string + com2sec readonly default public + + group MyROGroup v2c readonly + view all included .1 80 + access MyROGroup "" any noauth exact all none none + + syslocation Rack, Room, Building, City, Country [GPSX,Y] + syscontact Your Name + +ports: + - name: snmp-tcp + containerPort: 161 + hostPort: 161 + protocol: TCP + - name: snmp-trap-tcp + containerPort: 162 + hostPort: 162 + protocol: TCP + - name: snmp-udp + containerPort: 161 + hostPort: 161 + protocol: UDP + - name: snmp-trap-udp + containerPort: 162 + hostPort: 162 + protocol: UDP + +nodeSelector: {} diff --git a/hooks/build b/hooks/build deleted file mode 100755 index 99a7ef4..0000000 --- a/hooks/build +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -# $IMAGE_NAME var is injected into the build so the tag is correct. -docker build --build-arg VCS_REF=`git rev-parse — short HEAD` \ - --build-arg BUILD_DATE=`date -u +”%Y-%m-%dT%H:%M:%SZ”` \ - -t $IMAGE_NAME .