diff --git a/.gitea/workflows/main.yaml b/.gitea/workflows/main.yaml new file mode 100644 index 0000000..899078d --- /dev/null +++ b/.gitea/workflows/main.yaml @@ -0,0 +1,49 @@ +name: Gitea Docker Build Demo +run-name: Reference Branch Build 🚀 +on: + push: + branches: + - main + +jobs: + Test: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.24.x' + - name: Test with the Go CLI + run: go test ./... + Build_Image: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Set up Cosign + uses: sigstore/cosign-installer@v3.10.0 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver: remote + endpoint: tcp://buildkit-service.buildkit-service.svc:1234 + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_PASSWORD }} + - name: Build and Push the Docker Image + uses: docker/build-push-action@v6 + id: build-and-push + with: + context: . + file: ./Dockerfile + push: false + tags: | + lerentis/canada-kaktus:{{ github.sha }} + - name: Sign the published Docker image + env: + COSIGN_EXPERIMENTAL: "true" + run: cosign sign lerentis/canada-kaktus:${{ github.sha }}@${{ steps.build-and-push.outputs.digest }} \ No newline at end of file diff --git a/.gitea/workflows/pr.yaml b/.gitea/workflows/pr.yaml new file mode 100644 index 0000000..33c0273 --- /dev/null +++ b/.gitea/workflows/pr.yaml @@ -0,0 +1,34 @@ +name: PR Build +run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 +on: [pull_request] + +jobs: + Test: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.24.x' + - name: Test with the Go CLI + run: go test ./... + Build_Image: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver: remote + endpoint: tcp://buildkit-service.buildkit-service.svc:1234 + - name: Build and Push the Docker Image + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + push: false + tags: | + lerentis/canada-kaktus:latest diff --git a/.gitea/workflows/release.yaml b/.gitea/workflows/release.yaml new file mode 100644 index 0000000..df96bd9 --- /dev/null +++ b/.gitea/workflows/release.yaml @@ -0,0 +1,46 @@ +name: Release +run-name: ${{ gitea.actor }} is Releasing a new version 🚀 +on: [release] + +jobs: + Test: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.24.x' + - name: Test with the Go CLI + run: go test ./... + Build_Image: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: Set up Cosign + uses: sigstore/cosign-installer@v3.10.0 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver: remote + endpoint: tcp://buildkit-service.buildkit-service.svc:1234 + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_PASSWORD }} + - name: Build and Push the Docker Image + uses: docker/build-push-action@v6 + id: build-and-push + with: + context: . + file: ./Dockerfile + push: true + tags: | + lerentis/canada-kaktus:{{ github.event.release.tag_name }} + - name: Sign the published Docker image + env: + COSIGN_EXPERIMENTAL: "true" + run: cosign sign lerentis/canada-kaktus:${{ github.event.release.tag_name }}@${{ steps.build-and-push.outputs.digest }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4482373 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM golang:1.24 as build + +WORKDIR /app + +COPY . . + +RUN go mod tidy && CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o canada-kaktus ./cmd/ + +FROM scratch + +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=build /etc/passwd /etc/passwd +COPY --from=build /etc/group /etc/group +COPY --from=build --chown=65534:65534 /app/canada-kaktus /usr/local/bin/canada-kaktus + +USER nobody + +ENTRYPOINT ["/usr/local/bin/canada-kaktus"] diff --git a/README.md b/README.md index 7479000..dbcc1c7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# mail-dns-service +# canada-kaktus diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..fbf63e0 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "time" + + "git.uploadfilter24.eu/covidnetes/canada-kaktus/internal" + "git.uploadfilter24.eu/covidnetes/canada-kaktus/internal/utils" + log "github.com/sirupsen/logrus" +) + +func main() { + + cfg, err := internal.GenConfig() + utils.ConfigureLogger(cfg) + + if err != nil { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Fatal(fmt.Sprintf("Error generating Config: %s", err.Error())) + } + + go func() { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Info("Starting Health Endpoint") + internal.StartHealthEndpoint() + }() + + log.WithFields(log.Fields{ + "Caller": "Main", + }).Info("Entering main event loop") + + for { + servers, err := internal.GetAllNodes(cfg) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Error(fmt.Sprintf("Error getting all Nodes: %s", err.Error())) + } + ips, err := internal.GetAllIps(servers) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Error(fmt.Sprintf("Error getting all IPs: %s", err.Error())) + } + err = internal.RecreateIPPoolCrd(cfg, "covidnetes-pool", ips) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Error(fmt.Sprintf("Error recreating IP Pool CRD: %s", err.Error())) + } else { + log.WithFields(log.Fields{ + "Caller": "Main", + }).Info("Successfully recreated IP Pool CRD") + } + time.Sleep(15 * time.Minute) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3a861e5 --- /dev/null +++ b/go.mod @@ -0,0 +1,48 @@ +module git.uploadfilter24.eu/covidnetes/canada-kaktus + +go 1.24.0 + +require ( + github.com/gorilla/mux v1.8.1 + github.com/hetznercloud/hcloud-go v1.59.2 + github.com/jinzhu/configor v1.2.2 + github.com/sirupsen/logrus v1.9.3 + k8s.io/client-go v0.34.1 +) + +require ( + github.com/BurntSushi/toml v1.5.0 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fxamacker/cbor/v2 v2.9.0 // indirect + github.com/go-logr/logr v1.4.3 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/prometheus/client_golang v1.23.2 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.17.0 // indirect + github.com/x448/float16 v0.8.4 // indirect + go.yaml.in/yaml/v2 v2.4.3 // indirect + golang.org/x/net v0.44.0 // indirect + golang.org/x/oauth2 v0.31.0 // indirect + golang.org/x/sys v0.36.0 // indirect + golang.org/x/term v0.35.0 // indirect + golang.org/x/text v0.29.0 // indirect + golang.org/x/time v0.13.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + k8s.io/api v0.34.1 // indirect + k8s.io/apimachinery v0.34.1 // indirect + k8s.io/klog/v2 v2.130.1 // indirect + k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 // indirect + sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect + sigs.k8s.io/randfill v1.0.0 // indirect + sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect + sigs.k8s.io/yaml v1.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..211b446 --- /dev/null +++ b/go.sum @@ -0,0 +1,166 @@ +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= +github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emicklei/go-restful/v3 v3.12.2 h1:DhwDP0vY3k8ZzE0RunuJy8GhNpPL6zqLkDf9B/a0/xU= +github.com/emicklei/go-restful/v3 v3.12.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM= +github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= +github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= +github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= +github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= +github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= +github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/gnostic-models v0.7.0 h1:qwTtogB15McXDaNqTZdzPJRHvaVJlAl+HVQnLmJEJxo= +github.com/google/gnostic-models v0.7.0/go.mod h1:whL5G0m6dmc5cPxKc5bdKdEN3UjI7OUGxBlw57miDrQ= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/hetznercloud/hcloud-go v1.59.2 h1:NkCPwYiPv85FnOV3IW9/gxfW61TPIUSwyPHRSLwCkHA= +github.com/hetznercloud/hcloud-go v1.59.2/go.mod h1:oTebZCjd+osj75jlI76Z+zjN1sTxmMiQ1MWoO8aRl1c= +github.com/jinzhu/configor v1.2.2 h1:sLgh6KMzpCmaQB4e+9Fu/29VErtBUqsS2t8C9BNIVsA= +github.com/jinzhu/configor v1.2.2/go.mod h1:iFFSfOBKP3kC2Dku0ZGB3t3aulfQgTGJknodhFavsU8= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFdJifH4BDsTlE89Zl93FEloxaWZfGcifgq8= +github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0= +github.com/prometheus/procfs v0.17.0/go.mod h1:oPQLaDAMRbA+u8H5Pbfq+dl3VDAvHxMUOVhe0wYB2zw= +github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= +github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0= +go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I= +golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= +golang.org/x/oauth2 v0.31.0 h1:8Fq0yVZLh4j4YA47vHKFTa9Ew5XIrCP8LC6UeNZnLxo= +golang.org/x/oauth2 v0.31.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= +golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ= +golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk= +golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4= +golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI= +golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/evanphx/json-patch.v4 v4.12.0 h1:n6jtcsulIzXPJaxegRbvFNNrZDjbij7ny3gmSPG+6V4= +gopkg.in/evanphx/json-patch.v4 v4.12.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +k8s.io/api v0.34.1 h1:jC+153630BMdlFukegoEL8E/yT7aLyQkIVuwhmwDgJM= +k8s.io/api v0.34.1/go.mod h1:SB80FxFtXn5/gwzCoN6QCtPD7Vbu5w2n1S0J5gFfTYk= +k8s.io/apimachinery v0.34.1 h1:dTlxFls/eikpJxmAC7MVE8oOeP1zryV7iRyIjB0gky4= +k8s.io/apimachinery v0.34.1/go.mod h1:/GwIlEcWuTX9zKIg2mbw0LRFIsXwrfoVxn+ef0X13lw= +k8s.io/client-go v0.34.1 h1:ZUPJKgXsnKwVwmKKdPfw4tB58+7/Ik3CrjOEhsiZ7mY= +k8s.io/client-go v0.34.1/go.mod h1:kA8v0FP+tk6sZA0yKLRG67LWjqufAoSHA2xVGKw9Of8= +k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= +k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= +k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b h1:MloQ9/bdJyIu9lb1PzujOPolHyvO06MXG5TUIj2mNAA= +k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b/go.mod h1:UZ2yyWbFTpuhSbFhv24aGNOdoRdJZgsIObGBUaYVsts= +k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 h1:SjGebBtkBqHFOli+05xYbK8YF1Dzkbzn+gDM4X9T4Ck= +k8s.io/utils v0.0.0-20251002143259-bc988d571ff4/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 h1:IpInykpT6ceI+QxKBbEflcR5EXP7sU1kvOlxwZh5txg= +sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg= +sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU= +sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= +sigs.k8s.io/structured-merge-diff/v6 v6.3.0 h1:jTijUJbW353oVOd9oTlifJqOGEkUw2jB/fXCbTiQEco= +sigs.k8s.io/structured-merge-diff/v6 v6.3.0/go.mod h1:M3W8sfWvn2HhQDIbGWj3S099YozAsymCo/wrT5ohRUE= +sigs.k8s.io/yaml v1.6.0 h1:G8fkbMSAFqgEFgh4b1wmtzDnioxFCUgTZhlbj5P9QYs= +sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4= diff --git a/internal/config.go b/internal/config.go new file mode 100644 index 0000000..e1163a2 --- /dev/null +++ b/internal/config.go @@ -0,0 +1,30 @@ +package internal + +import ( + "fmt" + "time" + + "github.com/jinzhu/configor" +) + +type Config = struct { + LogLevel string `default:"Info" env:"CANADA_KAKTUS_LOGLEVEL"` + LabelSelector string `default:"kops.k8s.io/instance-role=Node" env:"CANADA_KAKTUS_LABELSELECTOR"` + HcloudToken string `default:"" env:"CANADA_KAKTUS_HCLOUD_TOKEN"` +} + +func GenConfig() (cfg *Config, err error) { + + cfg = &Config{} + + err = configor.New(&configor.Config{ + ENVPrefix: "CANADA_KAKTUS", + AutoReload: true, + Silent: true, + AutoReloadInterval: time.Minute}).Load(cfg, "config.json") + if err != nil { + return nil, fmt.Errorf("error generating Config: %s", err.Error()) + } + return cfg, nil + +} diff --git a/internal/config_test.go b/internal/config_test.go new file mode 100644 index 0000000..b7edd7f --- /dev/null +++ b/internal/config_test.go @@ -0,0 +1,22 @@ +package internal + +import ( + "reflect" + "testing" +) + +var defaultConfig = Config{ + LogLevel: "Info", + LabelSelector: "kops.k8s.io/instance-role=Node", + HcloudToken: "", +} + +func TestConfigDefaults(t *testing.T) { + cfg, err := GenConfig() + if err != nil { + t.Errorf("%s", err.Error()) + } + if !reflect.DeepEqual(&defaultConfig, cfg) { + t.Errorf("got %+v, want %+v", cfg, defaultConfig) + } +} diff --git a/internal/health.go b/internal/health.go new file mode 100644 index 0000000..bbe2592 --- /dev/null +++ b/internal/health.go @@ -0,0 +1,31 @@ +package internal + +import ( + "fmt" + "net/http" + + "github.com/gorilla/mux" + log "github.com/sirupsen/logrus" +) + +func StartHealthEndpoint() { + r := mux.NewRouter() + r.Use(mux.CORSMethodMiddleware(r)) + r.HandleFunc("/health", send200).Methods(http.MethodGet) + err := http.ListenAndServe("0.0.0.0:8080", r) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "StartHealthEndpoint", + }).Error(fmt.Sprintf("Error creating health endpoint: %s", err.Error())) + } +} + +func send200(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte{}) + if err != nil { + log.WithFields(log.Fields{ + "Caller": "send200", + }).Error(fmt.Sprintf("Error answering health endpoint: %s", err.Error())) + } +} diff --git a/internal/health_test.go b/internal/health_test.go new file mode 100644 index 0000000..e7e8ed7 --- /dev/null +++ b/internal/health_test.go @@ -0,0 +1,23 @@ +package internal + +import ( + "net/http" + "strings" + "testing" +) + +func TestHealth(t *testing.T) { + go func() { + StartHealthEndpoint() + }() + request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/health", strings.NewReader("")) + resp, err := http.DefaultClient.Do(request) + + if err != nil { + t.Errorf("Health endpoint did not start: %v", err) + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("Bad response from health endpoint. Want: %d, got %d", http.StatusOK, resp.StatusCode) + } +} diff --git a/internal/hetzner.go b/internal/hetzner.go new file mode 100644 index 0000000..a61adc9 --- /dev/null +++ b/internal/hetzner.go @@ -0,0 +1,40 @@ +package internal + +import ( + "context" + "fmt" + + "github.com/hetznercloud/hcloud-go/hcloud" + log "github.com/sirupsen/logrus" +) + +func GetAllNodes(cfg *Config) ([]*hcloud.Server, error) { + client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken)) + servers, _, err := client.Server.List(context.TODO(), hcloud.ServerListOpts{ + ListOpts: hcloud.ListOpts{ + LabelSelector: cfg.LabelSelector, + }}) + if err != nil { + return nil, fmt.Errorf("error listing Hetzner Nodes: %s", err.Error()) + } + + if servers == nil { + return nil, fmt.Errorf("no Nodes found with label selector: %s", cfg.LabelSelector) + } + return servers, nil + +} + +func GetAllIps(servers []*hcloud.Server) ([]string, error) { + ips := make([]string, len(servers)) + for i, instance := range servers { + if instance.PublicNet.IPv4.IP == nil { + return []string{""}, fmt.Errorf("instance %s has no public Addresses", instance.Name) + } + log.WithFields(log.Fields{ + "Caller": "GetAllIps", + }).Info(fmt.Sprintf("Found IP: %s", instance.PrivateNet[0].IP.String())) + ips[i] = instance.PublicNet.IPv4.IP.String() + } + return ips, nil +} diff --git a/internal/hetzner_test.go b/internal/hetzner_test.go new file mode 100644 index 0000000..175ded9 --- /dev/null +++ b/internal/hetzner_test.go @@ -0,0 +1,30 @@ +package internal + +import ( + "testing" + + "github.com/hetznercloud/hcloud-go/hcloud" +) + +func TestGetAllIps(t *testing.T) { + + servers := []*hcloud.Server{{ + Status: hcloud.ServerStatusRunning, + Name: "Test", + }} + + servers = append(servers, &hcloud.Server{ + Status: hcloud.ServerStatusRunning, + Name: "Test2", + }) + + expectedError := "instance Test has no public Addresses" + _, err := GetAllIps(servers) + if err == nil { + t.Error("GetAllIps did not error with missing data") + } + if err.Error() != expectedError { + t.Errorf("Wrong error message. want %s, got %s", expectedError, err.Error()) + } + +} diff --git a/internal/k8s.go b/internal/k8s.go new file mode 100644 index 0000000..7dd19a2 --- /dev/null +++ b/internal/k8s.go @@ -0,0 +1,111 @@ +package internal + +import ( + "bytes" + "context" + "fmt" + "html/template" + + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" +) + +var IP_POOL_TEMPLATE = ` +{ + "apiVersion": "cilium.io/v2alpha1", + "kind": "CiliumLoadBalancerIPPool", + "metadata": { + "name": "{{ .Name }}", + "annotations": { + "argocd.argoproj.io/tracking-id": "cilium-lb:cilium.io/CiliumLoadBalancerIPPool:kube-system/covidnetes-pool" + } + }, + "spec": { + "blocks": [ + {{- range $i, $ip := .IPs }} + {{- if $i}},{{ end }} + { + "cidr": "{{ $ip }}" + } + {{- end }} + ], + "disabled": false + } +} +` + +type CrdConfig struct { + Name string + IPs []string +} + +func RecreateIPPoolCrd(cfg *Config, name string, ips []string) error { + + routeclient, err := createRestClient() + + if err != nil { + return fmt.Errorf("error creating REST Client: %v", err.Error()) + } + + body, err := generateIpPool(name, ips) + + if err != nil { + return fmt.Errorf("error generating CRD: %v", err.Error()) + } + + decode := scheme.Codecs.UniversalDeserializer().Decode + + obj, _, err := decode([]byte(body), nil, nil) + if err != nil { + return fmt.Errorf("could not deserialize CRD: %v", err.Error()) + } + + res := routeclient.Post(). + Resource("routes"). + Body(&obj). + Do(context.TODO()) + + var status int + res.StatusCode(&status) + + if status >= 200 && status <= 400 { + return fmt.Errorf("failed to post CRD to kube api: %v", res.Error().Error()) + } + + return nil +} + +func createRestClient() (*rest.RESTClient, error) { + k8s_config, err := rest.InClusterConfig() + if err != nil { + return nil, fmt.Errorf("could not create in cluster k8s config: %v", err) + } + + k8s_config.APIPath = "/apis" + k8s_config.NegotiatedSerializer = scheme.Codecs.WithoutConversion() + + routeclient, err := rest.RESTClientFor(k8s_config) + + if err != nil { + return nil, fmt.Errorf("could not create k8s client: %v", err) + } + return routeclient, nil + +} + +func generateIpPool(name string, ips []string) (string, error) { + config := CrdConfig{ + Name: name, + IPs: ips, + } + tmpl, err := template.New("ippool").Parse(IP_POOL_TEMPLATE) + if err != nil { + return "", fmt.Errorf("errors in ippool template: %s", err.Error()) + } + var buf bytes.Buffer + err = tmpl.Execute(&buf, &config) + if err != nil { + return "", fmt.Errorf("could not render ippool template: %s", err.Error()) + } + return buf.String(), nil +} diff --git a/internal/k8s_test.go b/internal/k8s_test.go new file mode 100644 index 0000000..76fc439 --- /dev/null +++ b/internal/k8s_test.go @@ -0,0 +1,38 @@ +package internal + +import ( + "testing" +) + +func TestGenerateIpPoolCRD(t *testing.T) { + expected_ip_pool := ` +{ + "apiVersion": "cilium.io/v2alpha1", + "kind": "CiliumLoadBalancerIPPool", + "metadata": { + "name": "covidnetes-pool", + "annotations": { + "argocd.argoproj.io/tracking-id": "cilium-lb:cilium.io/CiliumLoadBalancerIPPool:kube-system/covidnetes-pool" + } + }, + "spec": { + "blocks": [ + { + "cidr": "49.13.48.9/32" + }, + { + "cidr": "91.107.211.117/32" + } + ], + "disabled": false + } +} +` + got, err := generateIpPool("covidnetes-pool", []string{"49.13.48.9/32", "91.107.211.117/32"}) + if err != nil { + t.Errorf("%s", err.Error()) + } + if expected_ip_pool != got { + t.Errorf("got %+v, want %+v", got, expected_ip_pool) + } +} diff --git a/internal/utils/logging.go b/internal/utils/logging.go new file mode 100644 index 0000000..4cebd0e --- /dev/null +++ b/internal/utils/logging.go @@ -0,0 +1,28 @@ +package utils + +import ( + "os" + + "git.uploadfilter24.eu/covidnetes/canada-kaktus/internal" + log "github.com/sirupsen/logrus" +) + +func ConfigureLogger(cfg *internal.Config) { + + switch cfg.LogLevel { + case "Debug": + log.SetLevel(log.DebugLevel) + case "Info": + log.SetLevel(log.InfoLevel) + case "Warn": + log.SetLevel(log.WarnLevel) + case "Error": + log.SetLevel(log.ErrorLevel) + default: + log.SetLevel(log.InfoLevel) + log.Warnf("Home: invalid log level supplied: '%s'", cfg.LogLevel) + } + + log.SetFormatter(&log.JSONFormatter{}) + log.SetOutput(os.Stdout) +}