From 1a909b871d145994c0ee3e80e67f3331dcc5b3d9 Mon Sep 17 00:00:00 2001 From: romeroalx Date: Tue, 17 Sep 2024 17:55:48 +0200 Subject: [PATCH 1/2] pdns provider: added optional argument to make server_id configurable --- docs/tutorials/pdns.md | 1 + main.go | 1 + pkg/apis/externaldns/types.go | 3 +++ pkg/apis/externaldns/types_test.go | 4 ++++ provider/pdns/pdns.go | 11 ++++++----- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/tutorials/pdns.md b/docs/tutorials/pdns.md index 365fffb80..7e2ad674a 100644 --- a/docs/tutorials/pdns.md +++ b/docs/tutorials/pdns.md @@ -47,6 +47,7 @@ spec: - --source=service # or ingress or both - --provider=pdns - --pdns-server={{ pdns-api-url }} + - --pdns-server-id={{ pdns-server-id }} - --pdns-api-key={{ pdns-http-api-key }} - --txt-owner-id={{ owner-id-for-this-external-dns }} - --domain-filter=external-dns-test.my-org.com # will make ExternalDNS see only the zones matching provided domain; omit to process all available zones in PowerDNS diff --git a/main.go b/main.go index 985124349..619d25fce 100644 --- a/main.go +++ b/main.go @@ -286,6 +286,7 @@ func main() { DomainFilter: domainFilter, DryRun: cfg.DryRun, Server: cfg.PDNSServer, + ServerID: cfg.PDNSServerID, APIKey: cfg.PDNSAPIKey, TLSConfig: pdns.TLSConfig{ SkipTLSVerify: cfg.PDNSSkipTLSVerify, diff --git a/pkg/apis/externaldns/types.go b/pkg/apis/externaldns/types.go index 02b59a422..9155bccd3 100644 --- a/pkg/apis/externaldns/types.go +++ b/pkg/apis/externaldns/types.go @@ -122,6 +122,7 @@ type Config struct { OVHEndpoint string OVHApiRateLimit int PDNSServer string + PDNSServerID string PDNSAPIKey string `secure:"yes"` PDNSSkipTLSVerify bool TLSCA string @@ -276,6 +277,7 @@ var defaultConfig = &Config{ OVHEndpoint: "ovh-eu", OVHApiRateLimit: 20, PDNSServer: "http://localhost:8081", + PDNSServerID: "localhost", PDNSAPIKey: "", PDNSSkipTLSVerify: false, TLSCA: "", @@ -498,6 +500,7 @@ func (cfg *Config) ParseFlags(args []string) error { app.Flag("ovh-endpoint", "When using the OVH provider, specify the endpoint (default: ovh-eu)").Default(defaultConfig.OVHEndpoint).StringVar(&cfg.OVHEndpoint) app.Flag("ovh-api-rate-limit", "When using the OVH provider, specify the API request rate limit, X operations by seconds (default: 20)").Default(strconv.Itoa(defaultConfig.OVHApiRateLimit)).IntVar(&cfg.OVHApiRateLimit) app.Flag("pdns-server", "When using the PowerDNS/PDNS provider, specify the URL to the pdns server (required when --provider=pdns)").Default(defaultConfig.PDNSServer).StringVar(&cfg.PDNSServer) + app.Flag("pdns-server-id", "When using the PowerDNS/PDNS provider, specify the id of the server to retrieve. Should be `localhost` except when the server is behind a proxy (optional when --provider=pdns) (default: localhost)").Default(defaultConfig.PDNSServerID).StringVar(&cfg.PDNSServerID) app.Flag("pdns-api-key", "When using the PowerDNS/PDNS provider, specify the API key to use to authorize requests (required when --provider=pdns)").Default(defaultConfig.PDNSAPIKey).StringVar(&cfg.PDNSAPIKey) app.Flag("pdns-skip-tls-verify", "When using the PowerDNS/PDNS provider, disable verification of any TLS certificates (optional when --provider=pdns) (default: false)").Default(strconv.FormatBool(defaultConfig.PDNSSkipTLSVerify)).BoolVar(&cfg.PDNSSkipTLSVerify) app.Flag("ns1-endpoint", "When using the NS1 provider, specify the URL of the API endpoint to target (default: https://api.nsone.net/v1/)").Default(defaultConfig.NS1Endpoint).StringVar(&cfg.NS1Endpoint) diff --git a/pkg/apis/externaldns/types_test.go b/pkg/apis/externaldns/types_test.go index e52f87f49..70b959b56 100644 --- a/pkg/apis/externaldns/types_test.go +++ b/pkg/apis/externaldns/types_test.go @@ -88,6 +88,7 @@ var ( OVHEndpoint: "ovh-eu", OVHApiRateLimit: 20, PDNSServer: "http://localhost:8081", + PDNSServerID: "localhost", PDNSAPIKey: "", Policy: "sync", Registry: "txt", @@ -186,6 +187,7 @@ var ( OVHEndpoint: "ovh-ca", OVHApiRateLimit: 42, PDNSServer: "http://ns.example.com:8081", + PDNSServerID: "localhost", PDNSAPIKey: "some-secret-key", PDNSSkipTLSVerify: true, TLSCA: "/path/to/ca.crt", @@ -285,6 +287,7 @@ func TestParseFlags(t *testing.T) { "--ovh-endpoint=ovh-ca", "--ovh-api-rate-limit=42", "--pdns-server=http://ns.example.com:8081", + "--pdns-server-id=localhost", "--pdns-api-key=some-secret-key", "--pdns-skip-tls-verify", "--oci-config-file=oci.yaml", @@ -409,6 +412,7 @@ func TestParseFlags(t *testing.T) { "EXTERNAL_DNS_TARGET_NET_FILTER": "10.0.0.0/9\n10.1.0.0/9", "EXTERNAL_DNS_EXCLUDE_TARGET_NET": "1.0.0.0/9\n1.1.0.0/9", "EXTERNAL_DNS_PDNS_SERVER": "http://ns.example.com:8081", + "EXTERNAL_DNS_PDNS_ID": "localhost", "EXTERNAL_DNS_PDNS_API_KEY": "some-secret-key", "EXTERNAL_DNS_PDNS_SKIP_TLS_VERIFY": "1", "EXTERNAL_DNS_RDNS_ROOT_DOMAIN": "lb.rancher.cloud", diff --git a/provider/pdns/pdns.go b/provider/pdns/pdns.go index fbc7cc22f..f031d09b0 100644 --- a/provider/pdns/pdns.go +++ b/provider/pdns/pdns.go @@ -43,8 +43,6 @@ type pdnsChangeType string const ( apiBase = "/api/v1" - // Unless we use something like pdnsproxy (discontinued upstream), this value will _always_ be localhost - defaultServerID = "localhost" defaultTTL = 300 // PdnsDelete and PdnsReplace are effectively an enum for "pgo.RrSet.changetype" @@ -66,6 +64,7 @@ type PDNSConfig struct { DomainFilter endpoint.DomainFilter DryRun bool Server string + ServerID string APIKey string TLSConfig TLSConfig } @@ -137,6 +136,7 @@ type PDNSAPIProvider interface { // PDNSAPIClient : Struct that encapsulates all the PowerDNS specific implementation details type PDNSAPIClient struct { dryRun bool + serverID string authCtx context.Context client *pgo.APIClient domainFilter endpoint.DomainFilter @@ -146,7 +146,7 @@ type PDNSAPIClient struct { // ref: https://doc.powerdns.com/authoritative/http-api/zone.html#get--servers-server_id-zones func (c *PDNSAPIClient) ListZones() (zones []pgo.Zone, resp *http.Response, err error) { for i := 0; i < retryLimit; i++ { - zones, resp, err = c.client.ZonesApi.ListZones(c.authCtx, defaultServerID) + zones, resp, err = c.client.ZonesApi.ListZones(c.authCtx, c.serverID) if err != nil { log.Debugf("Unable to fetch zones %v", err) log.Debugf("Retrying ListZones() ... %d", i) @@ -180,7 +180,7 @@ func (c *PDNSAPIClient) PartitionZones(zones []pgo.Zone) (filteredZones []pgo.Zo // ref: https://doc.powerdns.com/authoritative/http-api/zone.html#get--servers-server_id-zones-zone_id func (c *PDNSAPIClient) ListZone(zoneID string) (zone pgo.Zone, resp *http.Response, err error) { for i := 0; i < retryLimit; i++ { - zone, resp, err = c.client.ZonesApi.ListZone(c.authCtx, defaultServerID, zoneID) + zone, resp, err = c.client.ZonesApi.ListZone(c.authCtx, c.serverID, zoneID) if err != nil { log.Debugf("Unable to fetch zone %v", err) log.Debugf("Retrying ListZone() ... %d", i) @@ -198,7 +198,7 @@ func (c *PDNSAPIClient) ListZone(zoneID string) (zone pgo.Zone, resp *http.Respo // ref: https://doc.powerdns.com/authoritative/http-api/zone.html#patch--servers-server_id-zones-zone_id func (c *PDNSAPIClient) PatchZone(zoneID string, zoneStruct pgo.Zone) (resp *http.Response, err error) { for i := 0; i < retryLimit; i++ { - resp, err = c.client.ZonesApi.PatchZone(c.authCtx, defaultServerID, zoneID, zoneStruct) + resp, err = c.client.ZonesApi.PatchZone(c.authCtx, c.serverID, zoneID, zoneStruct) if err != nil { log.Debugf("Unable to patch zone %v", err) log.Debugf("Retrying PatchZone() ... %d", i) @@ -245,6 +245,7 @@ func NewPDNSProvider(ctx context.Context, config PDNSConfig) (*PDNSProvider, err provider := &PDNSProvider{ client: &PDNSAPIClient{ dryRun: config.DryRun, + serverID: config.ServerID, authCtx: context.WithValue(ctx, pgo.ContextAPIKey, pgo.APIKey{Key: config.APIKey}), client: pgo.NewAPIClient(pdnsClientConfig), domainFilter: config.DomainFilter, From dad4d13a2dc47381c19582df4e657195479431a3 Mon Sep 17 00:00:00 2001 From: romeroalx Date: Thu, 19 Sep 2024 11:31:25 +0200 Subject: [PATCH 2/2] fix for linting issue in provider/pdns/pdns.go --- provider/pdns/pdns.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/pdns/pdns.go b/provider/pdns/pdns.go index f031d09b0..5a924b58a 100644 --- a/provider/pdns/pdns.go +++ b/provider/pdns/pdns.go @@ -43,7 +43,7 @@ type pdnsChangeType string const ( apiBase = "/api/v1" - defaultTTL = 300 + defaultTTL = 300 // PdnsDelete and PdnsReplace are effectively an enum for "pgo.RrSet.changetype" // TODO: Can we somehow get this from the pgo swagger client library itself?