From 608ce2947be78d9cd5a7abb899d1bc9d7507e925 Mon Sep 17 00:00:00 2001 From: Eduardo Oliveira de Carvalho Date: Tue, 13 Apr 2021 11:44:25 +0200 Subject: [PATCH 1/2] Extract code from external-dns and apply LICENSE header. --- designate.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 91 +---------------------------------------- 2 files changed, 112 insertions(+), 90 deletions(-) create mode 100644 designate.go diff --git a/designate.go b/designate.go new file mode 100644 index 00000000..e5026a77 --- /dev/null +++ b/designate.go @@ -0,0 +1,111 @@ +/* +Copyright 2017 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main +import ( + "net" + "net/http" + "os" + "strings" + "time" + + "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack" + + log "github.com/sirupsen/logrus" + "github.com/kubernetes-incubator/external-dns/pkg/tlsutils" + +) + +// copies environment variables to new names without overwriting existing values +func remapEnv(mapping map[string]string) { + for k, v := range mapping { + currentVal := os.Getenv(k) + newVal := os.Getenv(v) + if currentVal == "" && newVal != "" { + os.Setenv(k, newVal) + } + } +} + +// returns OpenStack Keystone authentication settings by obtaining values from standard environment variables. +// also fixes incompatibilities between gophercloud implementation and *-stackrc files that can be downloaded +// from OpenStack dashboard in latest versions +func getAuthSettings() (gophercloud.AuthOptions, error) { + remapEnv(map[string]string{ + "OS_TENANT_NAME": "OS_PROJECT_NAME", + "OS_TENANT_ID": "OS_PROJECT_ID", + "OS_DOMAIN_NAME": "OS_USER_DOMAIN_NAME", + "OS_DOMAIN_ID": "OS_USER_DOMAIN_ID", + }) + + opts, err := openstack.AuthOptionsFromEnv() + if err != nil { + return gophercloud.AuthOptions{}, err + } + opts.AllowReauth = true + if !strings.HasSuffix(opts.IdentityEndpoint, "/") { + opts.IdentityEndpoint += "/" + } + if !strings.HasSuffix(opts.IdentityEndpoint, "/v2.0/") && !strings.HasSuffix(opts.IdentityEndpoint, "/v3/") { + opts.IdentityEndpoint += "v2.0/" + } + return opts, nil +} + +// authenticate in OpenStack and obtain Designate service endpoint +func createDesignateServiceClient() (*gophercloud.ServiceClient, error) { + opts, err := getAuthSettings() + if err != nil { + return nil, err + } + log.Infof("Using OpenStack Keystone at %s", opts.IdentityEndpoint) + authProvider, err := openstack.NewClient(opts.IdentityEndpoint) + if err != nil { + return nil, err + } + + tlsConfig, err := tlsutils.CreateTLSConfig("OPENSTACK") + if err != nil { + return nil, err + } + + transport := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + TLSClientConfig: tlsConfig, + } + authProvider.HTTPClient.Transport = transport + + if err = openstack.Authenticate(authProvider, opts); err != nil { + return nil, err + } + + eo := gophercloud.EndpointOpts{ + Region: os.Getenv("OS_REGION_NAME"), + } + + client, err := openstack.NewDNSV2(authProvider, eo) + if err != nil { + return nil, err + } + log.Infof("Found OpenStack Designate service at %s", client.Endpoint) + return client, nil +} diff --git a/main.go b/main.go index 61928088..120e2ade 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,10 @@ package main import ( "fmt" - "net" - "net/http" - "os" "strconv" "strings" - "time" "github.com/gophercloud/gophercloud" - "github.com/gophercloud/gophercloud/openstack" "github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets" "github.com/gophercloud/gophercloud/openstack/dns/v2/zones" @@ -19,11 +14,11 @@ import ( "github.com/jetstack/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1" "github.com/jetstack/cert-manager/pkg/acme/webhook/cmd" - "github.com/kubernetes-incubator/external-dns/pkg/tlsutils" ) const GroupName = "acme.syseleven.de" + func main() { cmd.RunWebhookServer(GroupName, &designateDNSProviderSolver{}, @@ -35,7 +30,6 @@ type designateDNSProviderSolver struct { } func (c *designateDNSProviderSolver) Name() string { - log.Debugf("Name() called") return "designateDNS" } @@ -135,89 +129,6 @@ func (c *designateDNSProviderSolver) Initialize(kubeClientConfig *rest.Config, s return nil } -// copies environment variables to new names without overwriting existing values -func remapEnv(mapping map[string]string) { - for k, v := range mapping { - currentVal := os.Getenv(k) - newVal := os.Getenv(v) - if currentVal == "" && newVal != "" { - os.Setenv(k, newVal) - } - } -} - -// returns OpenStack Keystone authentication settings by obtaining values from standard environment variables. -// also fixes incompatibilities between gophercloud implementation and *-stackrc files that can be downloaded -// from OpenStack dashboard in latest versions -func getAuthSettings() (gophercloud.AuthOptions, error) { - remapEnv(map[string]string{ - "OS_TENANT_NAME": "OS_PROJECT_NAME", - "OS_TENANT_ID": "OS_PROJECT_ID", - "OS_DOMAIN_NAME": "OS_USER_DOMAIN_NAME", - "OS_DOMAIN_ID": "OS_USER_DOMAIN_ID", - }) - - opts, err := openstack.AuthOptionsFromEnv() - if err != nil { - return gophercloud.AuthOptions{}, err - } - opts.AllowReauth = true - if !strings.HasSuffix(opts.IdentityEndpoint, "/") { - opts.IdentityEndpoint += "/" - } - if !strings.HasSuffix(opts.IdentityEndpoint, "/v2.0/") && !strings.HasSuffix(opts.IdentityEndpoint, "/v3/") { - opts.IdentityEndpoint += "v2.0/" - } - return opts, nil -} - -// authenticate in OpenStack and obtain Designate service endpoint -func createDesignateServiceClient() (*gophercloud.ServiceClient, error) { - opts, err := getAuthSettings() - if err != nil { - return nil, err - } - log.Infof("Using OpenStack Keystone at %s", opts.IdentityEndpoint) - authProvider, err := openstack.NewClient(opts.IdentityEndpoint) - if err != nil { - return nil, err - } - - tlsConfig, err := tlsutils.CreateTLSConfig("OPENSTACK") - if err != nil { - return nil, err - } - - transport := &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - }).DialContext, - MaxIdleConns: 100, - IdleConnTimeout: 90 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - TLSClientConfig: tlsConfig, - } - authProvider.HTTPClient.Transport = transport - - if err = openstack.Authenticate(authProvider, opts); err != nil { - return nil, err - } - - eo := gophercloud.EndpointOpts{ - Region: os.Getenv("OS_REGION_NAME"), - } - - client, err := openstack.NewDNSV2(authProvider, eo) - if err != nil { - return nil, err - } - log.Infof("Found OpenStack Designate service at %s", client.Endpoint) - return client, nil -} - func quoteRecord(r string) string { if strings.HasPrefix(r, "\"") && strings.HasSuffix(r, "\"") { return r From 2f8267ea83211c49501b91075f6e0cd9666890f4 Mon Sep 17 00:00:00 2001 From: Eduardo Oliveira de Carvalho Date: Tue, 13 Apr 2021 15:51:32 +0200 Subject: [PATCH 2/2] Apply lint. --- designate.go | 4 ++-- main.go | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/designate.go b/designate.go index e5026a77..99301cdb 100644 --- a/designate.go +++ b/designate.go @@ -12,6 +12,7 @@ limitations under the License. */ package main + import ( "net" "net/http" @@ -22,9 +23,8 @@ import ( "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack" - log "github.com/sirupsen/logrus" "github.com/kubernetes-incubator/external-dns/pkg/tlsutils" - + log "github.com/sirupsen/logrus" ) // copies environment variables to new names without overwriting existing values diff --git a/main.go b/main.go index 120e2ade..3731ecd4 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ import ( const GroupName = "acme.syseleven.de" - func main() { cmd.RunWebhookServer(GroupName, &designateDNSProviderSolver{},