Skip to content

Commit

Permalink
exoscale: replace endpoint config with environment
Browse files Browse the repository at this point in the history
  • Loading branch information
kobajagi committed Sep 16, 2022
1 parent ef71d09 commit b9a903e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
2 changes: 0 additions & 2 deletions docs/tutorials/exoscale.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Additionally you will have to provide the Exoscale...:

* API Key
* API Secret
* API Endpoint
* Elastic IP address, to access the workers

## Deployment
Expand Down Expand Up @@ -48,7 +47,6 @@ spec:
- --domain-filter={{ my-domain }}
- --policy=sync # if you want DNS entries to get deleted as well
- --txt-owner-id={{ owner-id-for-this-external-dns }}
- --exoscale-endpoint={{ endpoint }} # usually https://api.exoscale.com/v2
- --exoscale-apikey={{ api-key}}
- --exoscale-apisecret={{ api-secret }}
```
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ func main() {
)
case "exoscale":
p, err = exoscale.NewExoscaleProvider(
cfg.ExoscaleEndpoint,
cfg.ExoscaleAPIEnvironment,
cfg.ExoscaleAPIZone,
cfg.ExoscaleAPIKey,
cfg.ExoscaleAPISecret,
cfg.ExoscaleAPIZone,
cfg.DryRun,
exoscale.ExoscaleWithDomain(domainFilter),
exoscale.ExoscaleWithLogging(),
Expand Down
13 changes: 7 additions & 6 deletions pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ type Config struct {
LogLevel string
TXTCacheInterval time.Duration
TXTWildcardReplacement string
ExoscaleEndpoint string
ExoscaleAPIEnvironment string
ExoscaleAPIZone string
ExoscaleAPIKey string `secure:"yes"`
ExoscaleAPISecret string `secure:"yes"`
ExoscaleAPIZone string
CRDSourceAPIVersion string
CRDSourceKind string
ServiceTypeFilter []string
Expand Down Expand Up @@ -297,10 +297,10 @@ var defaultConfig = &Config{
LogFormat: "text",
MetricsAddress: ":7979",
LogLevel: logrus.InfoLevel.String(),
ExoscaleEndpoint: "https://api.exoscale.com/v2",
ExoscaleAPIEnvironment: "api",
ExoscaleAPIZone: "ch-gva-2",
ExoscaleAPIKey: "",
ExoscaleAPISecret: "",
ExoscaleAPIZone: "ch-gva-2",
CRDSourceAPIVersion: "externaldns.k8s.io/v1alpha1",
CRDSourceKind: "DNSEndpoint",
ServiceTypeFilter: []string{},
Expand Down Expand Up @@ -516,10 +516,11 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("tls-client-cert", "When using TLS communication, the path to the certificate to present as a client (not required for TLS)").Default(defaultConfig.TLSClientCert).StringVar(&cfg.TLSClientCert)
app.Flag("tls-client-cert-key", "When using TLS communication, the path to the certificate key to use with the client certificate (not required for TLS)").Default(defaultConfig.TLSClientCertKey).StringVar(&cfg.TLSClientCertKey)

app.Flag("exoscale-endpoint", "Provide the endpoint for the Exoscale provider").Default(defaultConfig.ExoscaleEndpoint).StringVar(&cfg.ExoscaleEndpoint)
// Flags related to Exoscale provider
app.Flag("exoscale-apienv", "When using Exoscale prvider, specify the API environment (optional)").Default(defaultConfig.ExoscaleAPIEnvironment).StringVar(&cfg.ExoscaleAPIEnvironment)
app.Flag("exoscale-apizone", "When using Exoscale provider, specify the API Zone (optional)").Default(defaultConfig.ExoscaleAPIZone).StringVar(&cfg.ExoscaleAPIZone)
app.Flag("exoscale-apikey", "Provide your API Key for the Exoscale provider").Default(defaultConfig.ExoscaleAPIKey).StringVar(&cfg.ExoscaleAPIKey)
app.Flag("exoscale-apisecret", "Provide your API Secret for the Exoscale provider").Default(defaultConfig.ExoscaleAPISecret).StringVar(&cfg.ExoscaleAPISecret)
app.Flag("exoscale-apizone", "Provide API Zone for Exoscale provider").Default(defaultConfig.ExoscaleAPIZone).StringVar(&cfg.ExoscaleAPIZone)

// Flags related to RFC2136 provider
app.Flag("rfc2136-host", "When using the RFC2136 provider, specify the host of the DNS server").Default(defaultConfig.RFC2136Host).StringVar(&cfg.RFC2136Host)
Expand Down
16 changes: 8 additions & 8 deletions pkg/apis/externaldns/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ var (
MetricsAddress: ":7979",
LogLevel: logrus.InfoLevel.String(),
ConnectorSourceServer: "localhost:8080",
ExoscaleEndpoint: "https://api.exoscale.com/v2",
ExoscaleAPIEnvironment: "api",
ExoscaleAPIZone: "ch-gva-2",
ExoscaleAPIKey: "",
ExoscaleAPISecret: "",
ExoscaleAPIZone: "ch-gva-2",
CRDSourceAPIVersion: "externaldns.k8s.io/v1alpha1",
CRDSourceKind: "DNSEndpoint",
RcodezeroTXTEncrypt: false,
Expand Down Expand Up @@ -223,10 +223,10 @@ var (
MetricsAddress: "127.0.0.1:9099",
LogLevel: logrus.DebugLevel.String(),
ConnectorSourceServer: "localhost:8081",
ExoscaleEndpoint: "https://api.foo.ch/v2",
ExoscaleAPIEnvironment: "api1",
ExoscaleAPIZone: "zone1",
ExoscaleAPIKey: "1",
ExoscaleAPISecret: "2",
ExoscaleAPIZone: "zone1",
CRDSourceAPIVersion: "test.k8s.io/v1alpha1",
CRDSourceKind: "Endpoint",
RcodezeroTXTEncrypt: true,
Expand Down Expand Up @@ -362,10 +362,10 @@ func TestParseFlags(t *testing.T) {
"--metrics-address=127.0.0.1:9099",
"--log-level=debug",
"--connector-source-server=localhost:8081",
"--exoscale-endpoint=https://api.foo.ch/v2",
"--exoscale-apienv=api1",
"--exoscale-apizone=zone1",
"--exoscale-apikey=1",
"--exoscale-apisecret=2",
"--exoscale-apizone=zone1",
"--crd-source-apiversion=test.k8s.io/v1alpha1",
"--crd-source-kind=Endpoint",
"--rcodezero-txt-encrypt",
Expand Down Expand Up @@ -480,10 +480,10 @@ func TestParseFlags(t *testing.T) {
"EXTERNAL_DNS_METRICS_ADDRESS": "127.0.0.1:9099",
"EXTERNAL_DNS_LOG_LEVEL": "debug",
"EXTERNAL_DNS_CONNECTOR_SOURCE_SERVER": "localhost:8081",
"EXTERNAL_DNS_EXOSCALE_ENDPOINT": "https://api.foo.ch/v2",
"EXTERNAL_DNS_EXOSCALE_APIENV": "api1",
"EXTERNAL_DNS_EXOSCALE_APIZONE": "zone1",
"EXTERNAL_DNS_EXOSCALE_APIKEY": "1",
"EXTERNAL_DNS_EXOSCALE_APISECRET": "2",
"EXTERNAL_DNS_EXOSCALE_APIZONE": "zone1",
"EXTERNAL_DNS_CRD_SOURCE_APIVERSION": "test.k8s.io/v1alpha1",
"EXTERNAL_DNS_CRD_SOURCE_KIND": "Endpoint",
"EXTERNAL_DNS_RCODEZERO_TXT_ENCRYPT": "1",
Expand Down
20 changes: 13 additions & 7 deletions provider/exoscale/exoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
log "github.com/sirupsen/logrus"

"sigs.k8s.io/external-dns/endpoint"
Expand All @@ -43,6 +44,7 @@ type ExoscaleProvider struct {
provider.BaseProvider
domain endpoint.DomainFilter
client EgoscaleClientI
apiEnv string
apiZone string
filter *zoneFilter
OnApplyChanges func(changes *plan.Changes)
Expand All @@ -53,27 +55,27 @@ type ExoscaleProvider struct {
type ExoscaleOption func(*ExoscaleProvider)

// NewExoscaleProvider returns ExoscaleProvider DNS provider interface implementation
func NewExoscaleProvider(endpoint, apiKey, apiSecret, apiZone string, dryRun bool, opts ...ExoscaleOption) (*ExoscaleProvider, error) {
func NewExoscaleProvider(env, zone, key, secret string, dryRun bool, opts ...ExoscaleOption) (*ExoscaleProvider, error) {
client, err := egoscale.NewClient(
apiKey,
apiSecret,
egoscale.ClientOptWithAPIEndpoint(endpoint),
key,
secret,
)
if err != nil {
return nil, err
}

return NewExoscaleProviderWithClient(client, apiZone, dryRun, opts...), nil
return NewExoscaleProviderWithClient(client, env, zone, dryRun, opts...), nil
}

// NewExoscaleProviderWithClient returns ExoscaleProvider DNS provider interface implementation (Client provided)
func NewExoscaleProviderWithClient(client EgoscaleClientI, apiZone string, dryRun bool, opts ...ExoscaleOption) *ExoscaleProvider {
func NewExoscaleProviderWithClient(client EgoscaleClientI, env, zone string, dryRun bool, opts ...ExoscaleOption) *ExoscaleProvider {
ep := &ExoscaleProvider{
filter: &zoneFilter{},
OnApplyChanges: func(changes *plan.Changes) {},
domain: endpoint.NewDomainFilter([]string{""}),
client: client,
apiZone: apiZone,
apiEnv: env,
apiZone: zone,
dryRun: dryRun,
}
for _, opt := range opts {
Expand All @@ -83,6 +85,7 @@ func NewExoscaleProviderWithClient(client EgoscaleClientI, apiZone string, dryRu
}

func (ep *ExoscaleProvider) getZones(ctx context.Context) (map[string]string, error) {
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(ep.apiEnv, ep.apiZone))
domains, err := ep.client.ListDNSDomains(ctx, ep.apiZone)
if err != nil {
return nil, err
Expand All @@ -107,6 +110,8 @@ func (ep *ExoscaleProvider) ApplyChanges(ctx context.Context, changes *plan.Chan
return nil
}

ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(ep.apiEnv, ep.apiZone))

zones, err := ep.getZones(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -221,6 +226,7 @@ func (ep *ExoscaleProvider) ApplyChanges(ctx context.Context, changes *plan.Chan

// Records returns the list of endpoints
func (ep *ExoscaleProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(ep.apiEnv, ep.apiZone))
endpoints := make([]*endpoint.Endpoint, 0)

domains, err := ep.client.ListDNSDomains(ctx, ep.apiZone)
Expand Down
4 changes: 2 additions & 2 deletions provider/exoscale/exoscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func contains(arr []*endpoint.Endpoint, name string) bool {
}

func TestExoscaleGetRecords(t *testing.T) {
provider := NewExoscaleProviderWithClient(NewExoscaleClientStub(), "", false)
provider := NewExoscaleProviderWithClient(NewExoscaleClientStub(), "", "", false)

recs, err := provider.Records(context.Background())
if err == nil {
Expand All @@ -143,7 +143,7 @@ func TestExoscaleGetRecords(t *testing.T) {
}

func TestExoscaleApplyChanges(t *testing.T) {
provider := NewExoscaleProviderWithClient(NewExoscaleClientStub(), "", false)
provider := NewExoscaleProviderWithClient(NewExoscaleClientStub(), "", "", false)

plan := &plan.Changes{
Create: []*endpoint.Endpoint{
Expand Down

0 comments on commit b9a903e

Please sign in to comment.