Skip to content

Commit

Permalink
Merge pull request #12 from buroa/main
Browse files Browse the repository at this point in the history
fix: everything
  • Loading branch information
kashalls committed May 24, 2024
2 parents 2d47d02 + a802689 commit e223079
Show file tree
Hide file tree
Showing 17 changed files with 429 additions and 342 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
FROM golang:1.22-alpine as builder
ARG PKG=github.com/kashalls/external-dns-unifi-webhook
ARG VERSION=dev
ARG REVISION=dev
WORKDIR /build
COPY go.mod go.sum /build/
RUN go mod download
COPY . .
RUN go build -o /external-dns-unifi-webhook
RUN go build -ldflags "-s -w -X main.Version=${VERSION} -X main.Gitsha=${REVISION}" ./cmd/webhook

FROM gcr.io/distroless/static-debian12:nonroot
USER 8675:8675
COPY --from=builder --chmod=555 /external-dns-unifi-webhook /usr/local/bin/external-dns-unifi-webhook
COPY --from=builder --chmod=555 /build/webhook /usr/local/bin/external-dns-unifi-webhook
EXPOSE 8888/tcp
ENTRYPOINT ["/usr/local/bin/external-dns-unifi-webhook"]
29 changes: 29 additions & 0 deletions cmd/webhook/init/configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package configuration

import (
"time"

"github.com/caarlos0/env/v11"
log "github.com/sirupsen/logrus"
)

// Config struct for configuration environmental variables
type Config struct {
ServerHost string `env:"SERVER_HOST" envDefault:"localhost"`
ServerPort int `env:"SERVER_PORT" envDefault:"8888"`
ServerReadTimeout time.Duration `env:"SERVER_READ_TIMEOUT"`
ServerWriteTimeout time.Duration `env:"SERVER_WRITE_TIMEOUT"`
DomainFilter []string `env:"DOMAIN_FILTER" envDefault:""`
ExcludeDomains []string `env:"EXCLUDE_DOMAIN_FILTER" envDefault:""`
RegexDomainFilter string `env:"REGEXP_DOMAIN_FILTER" envDefault:""`
RegexDomainExclusion string `env:"REGEXP_DOMAIN_FILTER_EXCLUSION" envDefault:""`
}

// Init sets up configuration by reading set environmental variables
func Init() Config {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
log.Fatalf("error reading configuration from environment: %v", err)
}
return cfg
}
54 changes: 54 additions & 0 deletions cmd/webhook/init/dnsprovider/dnsprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dnsprovider

import (
"fmt"
"regexp"
"strings"

"github.com/caarlos0/env/v11"
"github.com/kashalls/external-dns-provider-unifi/cmd/webhook/init/configuration"
"github.com/kashalls/external-dns-provider-unifi/internal/unifi"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/provider"

log "github.com/sirupsen/logrus"
)

type UnifiProviderFactory func(baseProvider *provider.BaseProvider, unifiConfig *unifi.Configuration) provider.Provider

func Init(config configuration.Config) (provider.Provider, error) {
var domainFilter endpoint.DomainFilter
createMsg := "creating unifi provider with "

if config.RegexDomainFilter != "" {
createMsg += fmt.Sprintf("regexp domain filter: '%s', ", config.RegexDomainFilter)
if config.RegexDomainExclusion != "" {
createMsg += fmt.Sprintf("with exclusion: '%s', ", config.RegexDomainExclusion)
}
domainFilter = endpoint.NewRegexDomainFilter(
regexp.MustCompile(config.RegexDomainFilter),
regexp.MustCompile(config.RegexDomainExclusion),
)
} else {
if config.DomainFilter != nil && len(config.DomainFilter) > 0 {
createMsg += fmt.Sprintf("domain filter: '%s', ", strings.Join(config.DomainFilter, ","))
}
if config.ExcludeDomains != nil && len(config.ExcludeDomains) > 0 {
createMsg += fmt.Sprintf("exclude domain filter: '%s', ", strings.Join(config.ExcludeDomains, ","))
}
domainFilter = endpoint.NewDomainFilterWithExclusions(config.DomainFilter, config.ExcludeDomains)
}

createMsg = strings.TrimSuffix(createMsg, ", ")
if strings.HasSuffix(createMsg, "with ") {
createMsg += "no kind of domain filters"
}
log.Info(createMsg)

unifiConfig := unifi.Configuration{}
if err := env.Parse(&unifiConfig); err != nil {
return nil, fmt.Errorf("reading adguard configuration failed: %v", err)
}

return unifi.NewUnifiProvider(domainFilter, &unifiConfig)
}
37 changes: 37 additions & 0 deletions cmd/webhook/init/logging/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package logging

import (
"os"

log "github.com/sirupsen/logrus"
)

func Init() {
setLogLevel()
setLogFormat()
}

func setLogFormat() {
format := os.Getenv("LOG_FORMAT")
if format == "test" {
log.SetFormatter(&log.TextFormatter{})
} else {
log.SetFormatter(&log.JSONFormatter{})
}
}

func setLogLevel() {
level := os.Getenv("LOG_LEVEL")
switch level {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/kashalls/external-dns-unifi-webhook/webhook"
"github.com/kashalls/external-dns-unifi-webhook/webhook/configuration"
"github.com/kashalls/external-dns-provider-unifi/cmd/webhook/init/configuration"
"github.com/kashalls/external-dns-provider-unifi/pkg/webhook"

log "github.com/sirupsen/logrus"
)
Expand All @@ -26,8 +25,8 @@ import (
// - /adjustendpoints (POST): executes the AdjustEndpoints method
func Init(config configuration.Config, p *webhook.Webhook) *http.Server {
r := chi.NewRouter()

r.Use(webhook.Health)
r.Use(middleware.Logger)
r.Get("/", p.Negotiate)
r.Get("/records", p.Records)
r.Post("/records", p.ApplyChanges)
Expand Down Expand Up @@ -57,6 +56,7 @@ func ShutdownGracefully(srv *http.Server) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-sigCh

log.Infof("shutting down server due to received signal: %v", sig)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
if err := srv.Shutdown(ctx); err != nil {
Expand Down
38 changes: 38 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"

"github.com/kashalls/external-dns-provider-unifi/cmd/webhook/init/configuration"
"github.com/kashalls/external-dns-provider-unifi/cmd/webhook/init/dnsprovider"
"github.com/kashalls/external-dns-provider-unifi/cmd/webhook/init/logging"
"github.com/kashalls/external-dns-provider-unifi/cmd/webhook/init/server"
"github.com/kashalls/external-dns-provider-unifi/pkg/webhook"
log "github.com/sirupsen/logrus"
)

const banner = `
external-dns-provider-unifi
version: %s (%s)
`

var (
Version = "local"
Gitsha = "?"
)

func main() {
fmt.Printf(banner, Version, Gitsha)

logging.Init()

config := configuration.Init()
provider, err := dnsprovider.Init(config)
if err != nil {
log.Fatalf("failed to initialize provider: %v", err)
}

srv := server.Init(config, webhook.New(provider))
server.ShutdownGracefully(srv)
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kashalls/external-dns-unifi-webhook
module github.com/kashalls/external-dns-provider-unifi

go 1.22.2

Expand All @@ -9,7 +9,6 @@ require (
github.com/go-chi/chi/v5 v5.0.12
github.com/sirupsen/logrus v1.9.3
sigs.k8s.io/external-dns v0.14.2

)

require (
Expand All @@ -20,11 +19,13 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.30.1 // indirect
Expand Down
Loading

0 comments on commit e223079

Please sign in to comment.