chore: bump app version to v0.6.1 and enhance domain filtering logic with improved logging for better debugging
All checks were successful
Release / build-image (push) Successful in 38s
All checks were successful
Release / build-image (push) Successful in 38s
This commit is contained in:
parent
2279b8a879
commit
df84f6f7a1
3 changed files with 92 additions and 7 deletions
|
@ -1 +1 @@
|
|||
appVersion: v0.6.0
|
||||
appVersion: v0.6.1
|
|
@ -146,23 +146,88 @@ func (p *Provider) Records() []*endpoint.Endpoint {
|
|||
}
|
||||
|
||||
func getDomainZone(client *domeneshop.Client, DNSName string) (string, bool) {
|
||||
logLevel := strings.ToLower(os.Getenv("LOG_LEVEL"))
|
||||
|
||||
// First try using configured domain filters from environment
|
||||
domainFilters := os.Getenv("DOMAIN_FILTERS")
|
||||
if domainFilters != "" {
|
||||
filters := strings.Split(domainFilters, ",")
|
||||
|
||||
// Check if DNSName matches or is a subdomain of any filter
|
||||
for _, filter := range filters {
|
||||
filter = strings.TrimSpace(filter)
|
||||
|
||||
// Exact match
|
||||
if DNSName == filter {
|
||||
if logLevel == "debug" {
|
||||
log.Printf("DNSName '%s' exactly matches domain filter '%s'", DNSName, filter)
|
||||
}
|
||||
|
||||
// Verify domain exists in Domeneshop
|
||||
domain, err := client.GetDomainByName(filter)
|
||||
if err == nil && domain.Services.DNS {
|
||||
return domain.Name, true
|
||||
}
|
||||
}
|
||||
|
||||
// Subdomain match
|
||||
if strings.HasSuffix(DNSName, "."+filter) {
|
||||
if logLevel == "debug" {
|
||||
log.Printf("DNSName '%s' is a subdomain of domain filter '%s'", DNSName, filter)
|
||||
}
|
||||
|
||||
// Verify domain exists in Domeneshop
|
||||
domain, err := client.GetDomainByName(filter)
|
||||
if err == nil && domain.Services.DNS {
|
||||
return domain.Name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if logLevel == "debug" {
|
||||
log.Printf("DNSName '%s' does not match any domain filter: %s", DNSName, domainFilters)
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to original domain parsing logic if no filters matched
|
||||
zone := DNSName
|
||||
attempts := 0
|
||||
maxAttempts := 10 // Prevent infinite loops
|
||||
|
||||
// First try the exact domain
|
||||
domain, err := client.GetDomainByName(zone)
|
||||
if err == nil {
|
||||
return domain.Name, true
|
||||
}
|
||||
|
||||
// Then try parent domains
|
||||
for {
|
||||
attempts++
|
||||
if attempts > maxAttempts {
|
||||
break // Prevent infinite loops
|
||||
}
|
||||
|
||||
parts := strings.Split(zone, ".")
|
||||
if len(parts) < 2 {
|
||||
// Didn't find a valid domain before reaching top-level-domain
|
||||
break
|
||||
}
|
||||
|
||||
// Re-assemble the domain part
|
||||
domainPart := strings.Join(parts[1:], ".")
|
||||
domain, err := client.GetDomainByName(domainPart)
|
||||
if err != nil {
|
||||
continue
|
||||
if err == nil {
|
||||
return domain.Name, true
|
||||
}
|
||||
return domain.Name, true
|
||||
|
||||
// Try the next level up
|
||||
zone = domainPart
|
||||
}
|
||||
|
||||
// Failure
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Failed to find a matching domain for '%s'", DNSName)
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
|
|
|
@ -6,9 +6,12 @@ package webhook
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
domeneshopProvider "code.252.no/pub/external-dns-domeneshop-webhook/internal/provider"
|
||||
"sigs.k8s.io/external-dns/endpoint"
|
||||
|
@ -101,13 +104,31 @@ func (p *Webhook) Records(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// This handler returns DomainFilter and is used on the route for "/""
|
||||
// TODO: This should either accept a filter passed by environment variables or secrets or be generated by fetching all domains from Domeneshop API
|
||||
func (p *Webhook) DomainFilter(w http.ResponseWriter, r *http.Request) {
|
||||
// Log the request method and URL path
|
||||
fmt.Printf("Received %s request for %s\n", r.Method, r.URL.Path)
|
||||
// TODO: acceptHeaderCheck
|
||||
|
||||
b, err := DomainFilter{}.MarshalJSON()
|
||||
// Get domain filters from environment variable
|
||||
domainFilters := os.Getenv("DOMAIN_FILTERS")
|
||||
var df DomainFilter
|
||||
|
||||
if domainFilters != "" {
|
||||
// Split the comma-separated list
|
||||
filters := strings.Split(domainFilters, ",")
|
||||
// Trim whitespace
|
||||
for i, filter := range filters {
|
||||
filters[i] = strings.TrimSpace(filter)
|
||||
}
|
||||
df = DomainFilter{Filters: filters}
|
||||
|
||||
logLevel := strings.ToLower(os.Getenv("LOG_LEVEL"))
|
||||
if logLevel == "debug" {
|
||||
log.Printf("Using domain filters from environment: %v", filters)
|
||||
}
|
||||
}
|
||||
|
||||
b, err := df.MarshalJSON()
|
||||
if err != nil {
|
||||
fmt.Printf("failed to marshal domain filter, request method: %s, request path: %s", r.Method, r.URL.Path)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
@ -115,7 +136,6 @@ func (p *Webhook) DomainFilter(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
w.Header().Set(contentTypeHeader, string(mediaTypeFormat+"version="+"1"))
|
||||
if _, writeError := w.Write(b); writeError != nil {
|
||||
|
||||
fmt.Printf("Failure")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue