-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
429 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.