Skip to content

Commit

Permalink
more debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKoss committed Oct 17, 2023
1 parent 3785e02 commit 9c23f99
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 39 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
[![GitHub stars](https://img.shields.io/github/stars/stackitcloud/external-dns-stackit-webhook.svg?style=social&label=Star&maxAge=2592000)](https://github.com/stackitcloud/external-dns-stackit-webhook/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/stackitcloud/external-dns-stackit-webhook.svg?style=social&label=Fork&maxAge=2592000)](https://github.com/stackitcloud/external-dns-stackit-webhook/network)

⚠️ CAUTION: This Webhook is designed on an unreleased edition of
[ExternalDNS](https://github.com/kubernetes-sigs/external-dns), specifically focusing on the novel integration
method via webhooks, as deliberated and constructed
in [PR-3063](https://github.com/kubernetes-sigs/external-dns/pull/3063).

ExternalDNS serves as an add-on for Kubernetes designed to automate the management of Domain Name System (DNS)
records for Kubernetes services by utilizing various DNS providers. While Kubernetes traditionally manages DNS
records internally, ExternalDNS augments this functionality by transferring the responsibility of DNS records
Expand Down Expand Up @@ -225,6 +220,7 @@ setting this number excessively high to prevent receiving 429 rate limiting from
- `--base-url`/`BASE_URL` (optional): Identifies the Base URL for utilizing the API (default "https://dns.api.stackit.cloud").
- `--api-port`/`API_PORT` (optional): Specifies the port to listen on (default 8888).
- `--domain-filter`/`DOMAIN_FILER` (optional): Establishes a filter for DNS zone names (default []).
- `--dry-run`/`DRY_RUN` (optional): Specifies whether to perform a dry run (default false).

## Development
Run the app:
Expand Down
44 changes: 39 additions & 5 deletions cmd/webhook/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"go.uber.org/zap/zapcore"
"log"
"net/http"
"strings"
Expand All @@ -24,6 +25,8 @@ var (
projectID string
worker int
domainFilter []string
dryRun bool
logLevel string
)

var rootCmd = &cobra.Command{
Expand All @@ -35,10 +38,7 @@ var rootCmd = &cobra.Command{
panic("auth-token is required")
}

logger, errLogger := zap.NewProduction()
if errLogger != nil {
panic(errLogger)
}
logger := getLogger()
defer func(logger *zap.Logger) {
err := logger.Sync()
if err != nil {
Expand All @@ -53,7 +53,7 @@ var rootCmd = &cobra.Command{
Token: authBearerToken,
ProjectId: projectID,
DomainFilter: endpointDomainFilter,
DryRun: false,
DryRun: dryRun,
Workers: worker,
}, logger.With(zap.String("component", "stackitprovider")), &http.Client{
Timeout: 10 * time.Second,
Expand All @@ -70,6 +70,38 @@ var rootCmd = &cobra.Command{
},
}

func getLogger() *zap.Logger {
cfg := zap.Config{
Level: zap.NewAtomicLevelAt(getZapLogLevel()),
Encoding: "json", // or "console"
// ... other zap configuration as needed
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}

logger, errLogger := cfg.Build()
if errLogger != nil {
panic(errLogger)
}

return logger
}

func getZapLogLevel() zapcore.Level {
switch logLevel {
case "DEBUG":
return zapcore.DebugLevel
case "INFO":
return zapcore.InfoLevel
case "WARN":
return zapcore.WarnLevel
case "ERROR":
return zapcore.ErrorLevel
default:
return zapcore.InfoLevel
}
}

func Execute() error {
return rootCmd.Execute()
}
Expand All @@ -86,6 +118,8 @@ func init() {
"records, it can be parallelized. However, it is important to avoid setting this number "+
"excessively high to prevent receiving 429 rate limiting from the API.")
rootCmd.PersistentFlags().StringArrayVar(&domainFilter, "domain-filter", []string{}, "Establishes a filter for DNS zone names")
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Specifies whether to perform a dry run.")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "INFO", "Specifies the log level. Possible values are: DEBUG, INFO, WARN, ERROR")
}

func initConfig() {
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/adjust_endpoints.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"fmt"

"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"sigs.k8s.io/external-dns/endpoint"
Expand All @@ -16,12 +18,11 @@ func (w webhook) AdjustEndpoints(ctx *fiber.Ctx) error {
return ctx.Status(fiber.StatusBadRequest).SendString(err.Error())
}

w.logger.Debug("requesting adjust endpoints count", zap.Int("count", len(pve)))

pve = w.provider.AdjustEndpoints(pve)

ctx.Response().Header.Set(varyHeader, contentTypeHeader)
ctx.Response().Header.Set(contentTypeHeader, string(mediaTypeVersion1))
w.logger.Debug("adjusted endpoints", zap.String("endpoints", fmt.Sprintf("%v", pve)))

ctx.Set(varyHeader, contentTypeHeader)

return ctx.JSON(pve)
}
15 changes: 13 additions & 2 deletions pkg/api/domain_filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package api

import "github.com/gofiber/fiber/v2"
import (
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
)

func (w webhook) GetDomainFilter(ctx *fiber.Ctx) error {
return ctx.JSON(w.provider.GetDomainFilter())
data, err := json.Marshal(w.provider.GetDomainFilter())
if err != nil {
return err
}

ctx.Set(varyHeader, contentTypeHeader)
ctx.Set(contentTypeHeader, mediaTypeFormat)

return ctx.Send(data)
}
27 changes: 5 additions & 22 deletions pkg/api/models.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
package api

const (
mediaTypeFormat = "application/external.dns.webhook+json;version=1"
contentTypeHeader = "Content-Type"
contentTypePlaintext = "text/plain"
acceptHeader = "Accept"
varyHeader = "Vary"
supportedMediaVersions = "1"
healthPath = "/health"
logFieldRequestPath = "requestPath"
logFieldRequestMethod = "requestMethod"
logFieldError = "err"
mediaTypeFormat = "application/external.dns.webhook+json;version=1"
contentTypeHeader = "Content-Type"
contentTypePlaintext = "text/plain"
varyHeader = "Vary"
logFieldError = "err"
)

var mediaTypeVersion1 = mediaTypeVersion("1")

type mediaType string

func mediaTypeVersion(v string) mediaType {
return mediaType(mediaTypeFormat + "version=" + v)
}

func (m mediaType) Is(headerValue string) bool {
return string(m) == headerValue
}

type Message struct {
Message string `json:"message"`
}
1 change: 0 additions & 1 deletion pkg/api/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func (w webhook) Records(ctx *fiber.Ctx) error {
w.logger.Debug("returning records", zap.String("records", fmt.Sprintf("%v", records)))

ctx.Response().Header.Set(varyHeader, contentTypeHeader)
ctx.Response().Header.Set(contentTypeHeader, string(mediaTypeVersion1))

return ctx.JSON(records)
}

0 comments on commit 9c23f99

Please sign in to comment.