Skip to content

Commit 9c23f99

Browse files
author
Patrick Koss
committed
more debug logging
1 parent 3785e02 commit 9c23f99

File tree

6 files changed

+63
-39
lines changed

6 files changed

+63
-39
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
[![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)
1212
[![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)
1313

14-
⚠️ CAUTION: This Webhook is designed on an unreleased edition of
15-
[ExternalDNS](https://github.com/kubernetes-sigs/external-dns), specifically focusing on the novel integration
16-
method via webhooks, as deliberated and constructed
17-
in [PR-3063](https://github.com/kubernetes-sigs/external-dns/pull/3063).
18-
1914
ExternalDNS serves as an add-on for Kubernetes designed to automate the management of Domain Name System (DNS)
2015
records for Kubernetes services by utilizing various DNS providers. While Kubernetes traditionally manages DNS
2116
records internally, ExternalDNS augments this functionality by transferring the responsibility of DNS records
@@ -225,6 +220,7 @@ setting this number excessively high to prevent receiving 429 rate limiting from
225220
- `--base-url`/`BASE_URL` (optional): Identifies the Base URL for utilizing the API (default "https://dns.api.stackit.cloud").
226221
- `--api-port`/`API_PORT` (optional): Specifies the port to listen on (default 8888).
227222
- `--domain-filter`/`DOMAIN_FILER` (optional): Establishes a filter for DNS zone names (default []).
223+
- `--dry-run`/`DRY_RUN` (optional): Specifies whether to perform a dry run (default false).
228224

229225
## Development
230226
Run the app:

cmd/webhook/cmd/root.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"go.uber.org/zap/zapcore"
56
"log"
67
"net/http"
78
"strings"
@@ -24,6 +25,8 @@ var (
2425
projectID string
2526
worker int
2627
domainFilter []string
28+
dryRun bool
29+
logLevel string
2730
)
2831

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

38-
logger, errLogger := zap.NewProduction()
39-
if errLogger != nil {
40-
panic(errLogger)
41-
}
41+
logger := getLogger()
4242
defer func(logger *zap.Logger) {
4343
err := logger.Sync()
4444
if err != nil {
@@ -53,7 +53,7 @@ var rootCmd = &cobra.Command{
5353
Token: authBearerToken,
5454
ProjectId: projectID,
5555
DomainFilter: endpointDomainFilter,
56-
DryRun: false,
56+
DryRun: dryRun,
5757
Workers: worker,
5858
}, logger.With(zap.String("component", "stackitprovider")), &http.Client{
5959
Timeout: 10 * time.Second,
@@ -70,6 +70,38 @@ var rootCmd = &cobra.Command{
7070
},
7171
}
7272

73+
func getLogger() *zap.Logger {
74+
cfg := zap.Config{
75+
Level: zap.NewAtomicLevelAt(getZapLogLevel()),
76+
Encoding: "json", // or "console"
77+
// ... other zap configuration as needed
78+
OutputPaths: []string{"stdout"},
79+
ErrorOutputPaths: []string{"stderr"},
80+
}
81+
82+
logger, errLogger := cfg.Build()
83+
if errLogger != nil {
84+
panic(errLogger)
85+
}
86+
87+
return logger
88+
}
89+
90+
func getZapLogLevel() zapcore.Level {
91+
switch logLevel {
92+
case "DEBUG":
93+
return zapcore.DebugLevel
94+
case "INFO":
95+
return zapcore.InfoLevel
96+
case "WARN":
97+
return zapcore.WarnLevel
98+
case "ERROR":
99+
return zapcore.ErrorLevel
100+
default:
101+
return zapcore.InfoLevel
102+
}
103+
}
104+
73105
func Execute() error {
74106
return rootCmd.Execute()
75107
}
@@ -86,6 +118,8 @@ func init() {
86118
"records, it can be parallelized. However, it is important to avoid setting this number "+
87119
"excessively high to prevent receiving 429 rate limiting from the API.")
88120
rootCmd.PersistentFlags().StringArrayVar(&domainFilter, "domain-filter", []string{}, "Establishes a filter for DNS zone names")
121+
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Specifies whether to perform a dry run.")
122+
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "INFO", "Specifies the log level. Possible values are: DEBUG, INFO, WARN, ERROR")
89123
}
90124

91125
func initConfig() {

pkg/api/adjust_endpoints.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package api
22

33
import (
4+
"fmt"
5+
46
"github.com/gofiber/fiber/v2"
57
"go.uber.org/zap"
68
"sigs.k8s.io/external-dns/endpoint"
@@ -16,12 +18,11 @@ func (w webhook) AdjustEndpoints(ctx *fiber.Ctx) error {
1618
return ctx.Status(fiber.StatusBadRequest).SendString(err.Error())
1719
}
1820

19-
w.logger.Debug("requesting adjust endpoints count", zap.Int("count", len(pve)))
20-
2121
pve = w.provider.AdjustEndpoints(pve)
2222

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

2627
return ctx.JSON(pve)
2728
}

pkg/api/domain_filter.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package api
22

3-
import "github.com/gofiber/fiber/v2"
3+
import (
4+
"github.com/goccy/go-json"
5+
"github.com/gofiber/fiber/v2"
6+
)
47

58
func (w webhook) GetDomainFilter(ctx *fiber.Ctx) error {
6-
return ctx.JSON(w.provider.GetDomainFilter())
9+
data, err := json.Marshal(w.provider.GetDomainFilter())
10+
if err != nil {
11+
return err
12+
}
13+
14+
ctx.Set(varyHeader, contentTypeHeader)
15+
ctx.Set(contentTypeHeader, mediaTypeFormat)
16+
17+
return ctx.Send(data)
718
}

pkg/api/models.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
package api
22

33
const (
4-
mediaTypeFormat = "application/external.dns.webhook+json;version=1"
5-
contentTypeHeader = "Content-Type"
6-
contentTypePlaintext = "text/plain"
7-
acceptHeader = "Accept"
8-
varyHeader = "Vary"
9-
supportedMediaVersions = "1"
10-
healthPath = "/health"
11-
logFieldRequestPath = "requestPath"
12-
logFieldRequestMethod = "requestMethod"
13-
logFieldError = "err"
4+
mediaTypeFormat = "application/external.dns.webhook+json;version=1"
5+
contentTypeHeader = "Content-Type"
6+
contentTypePlaintext = "text/plain"
7+
varyHeader = "Vary"
8+
logFieldError = "err"
149
)
1510

16-
var mediaTypeVersion1 = mediaTypeVersion("1")
17-
18-
type mediaType string
19-
20-
func mediaTypeVersion(v string) mediaType {
21-
return mediaType(mediaTypeFormat + "version=" + v)
22-
}
23-
24-
func (m mediaType) Is(headerValue string) bool {
25-
return string(m) == headerValue
26-
}
27-
2811
type Message struct {
2912
Message string `json:"message"`
3013
}

pkg/api/records.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func (w webhook) Records(ctx *fiber.Ctx) error {
1818
w.logger.Debug("returning records", zap.String("records", fmt.Sprintf("%v", records)))
1919

2020
ctx.Response().Header.Set(varyHeader, contentTypeHeader)
21-
ctx.Response().Header.Set(contentTypeHeader, string(mediaTypeVersion1))
2221

2322
return ctx.JSON(records)
2423
}

0 commit comments

Comments
 (0)