Skip to content

Commit

Permalink
feat: Add support for setting a default comment to all records that d…
Browse files Browse the repository at this point in the history
…o not have one set (#180)

* add comment to MikrotikDefaults

* enforce default comment

* update test cases for endpoint comparison

* fix typo

* add more test cases

* add empty-value check

* major refactor on tests

* update test

* go mod tidy

* update example

* rename symbols

* update example

* update readme

* update readme
  • Loading branch information
mircea-pavel-anton authored Jan 21, 2025
1 parent 7586aa2 commit 55a7ce0
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 516 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,28 @@ See mirceanton/external-dns-provider-mikrotik#140 and kubernetes-sigs/external-d

## ⚙️ Configuration Options

### MikroTik Configuration
### MikroTik Connection Configuration

| Environment Variable | Description | Default Value |
|-----------------------------|------------------------------------------------------------------------------------|---------------|
| `MIKROTIK_BASEURL` | URL at which the RouterOS API is available. (ex. `https://192.168.88.1:443`) | N/A |
| `MIKROTIK_USERNAME` | Username for the RouterOS API authentication. | N/A |
| `MIKROTIK_PASSWORD` | Password for the RouterOS API authentication. | N/A |
| `MIKROTIK_SKIP_TLS_VERIFY` | Whether to skip TLS verification (true or false). | `false` |
| `LOG_FORMAT` | The format in which logs will be printed. (`text` or `json`) | `json` |
| `LOG_LEVEL` | The verbosity at which logs are printed logs. (`debug`, `info`, `warn` or `error`) | `info` |
| `MIKROTIK_SKIP_TLS_VERIFY` | Whether to skip TLS verification (`true` or `false`). | `false` |

### Logging Configuration

| Environment Variable | Description | Default Value |
|-----------------------|------------------------------------------------------------------------------------|---------------|
| `LOG_FORMAT` | The format in which logs will be printed. (`text` or `json`) | `text` |
| `LOG_LEVEL` | The verbosity at which logs are printed logs. (`debug`, `info`, `warn` or `error`) | `info` |

### Default Values Configuration

| Environment Variable | Description | Default Value |
|-----------------------------|------------------------------------------------------------------------------------|---------------|
| `MIKROTIK_DEFAULT_TTL` | Default TTL value to be set for DNS records with no specified TTL. | `3600` |
| `MIKROTIK_DEFAULT_Comment` | Default Comment value to be set for DNS records with no specified Comment. | N/A |

### Webhook Server Configuration

Expand Down
9 changes: 7 additions & 2 deletions example/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ fullnameOverride: external-dns-mikrotik

logLevel: debug
logFormat: text
interval: 1s

interval: 5s
sources: ["ingress", "service", "crd"]
registry: txt
txtOwnerId: default
Expand All @@ -17,15 +18,19 @@ provider:
webhook:
image:
repository: ghcr.io/mirceanton/external-dns-provider-mikrotik
tag: v1.2.7@sha256:c0605f76bd88bab38c8f68e33aa917e882b4793e51d92cd8bf7879c84eda1680
tag: v1.3.0-SNAPSHOT-39afb81-amd64
pullPolicy: IfNotPresent
env:
- name: LOG_FORMAT
value: json
- name: LOG_LEVEL
value: debug

- name: MIKROTIK_DEFAULT_TTL
value: "1800"
- name: MIKROTIK_DEFAULT_COMMENT
value: Managed by ExternalDNS

- name: MIKROTIK_BASEURL
valueFrom:
secretKeyRef:
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -94,8 +92,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
3 changes: 2 additions & 1 deletion internal/mikrotik/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

type MikrotikDefaults struct {
TTL int64 `env:"MIKROTIK_DEFAULT_TTL" envDefault:"3600"`
DefaultTTL int64 `env:"MIKROTIK_DEFAULT_TTL" envDefault:"3600"`
DefaultComment string `env:"MIKROTIK_DEFAULT_COMMENT" envDefault:""`
}

// MikrotikConnectionConfig holds the connection details for the API client
Expand Down
9 changes: 8 additions & 1 deletion internal/mikrotik/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func TestNewMikrotikClient(t *testing.T) {
SkipTLSVerify: true,
}

defaults := &MikrotikDefaults{}
defaults := &MikrotikDefaults{
DefaultTTL: 1900,
DefaultComment: "test",
}

client, err := NewMikrotikClient(config, defaults)
if err != nil {
Expand All @@ -35,6 +38,10 @@ func TestNewMikrotikClient(t *testing.T) {
t.Errorf("Expected config to be %v, got %v", config, client.MikrotikConnectionConfig)
}

if client.MikrotikDefaults != defaults {
t.Errorf("Expected defaults to be %v, got %v", defaults, client.MikrotikDefaults)
}

if client.Client == nil {
t.Errorf("Expected HTTP client to be initialized")
}
Expand Down
37 changes: 30 additions & 7 deletions internal/mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ func (p *MikrotikProvider) GetDomainFilter() endpoint.DomainFilterInterface {
// If the property is not found, it returns the specified default value.
func (p *MikrotikProvider) getProviderSpecificOrDefault(ep *endpoint.Endpoint, ps string, defaultValue string) string {
value, valueExists := ep.GetProviderSpecificProperty(ps)
if valueExists {
if valueExists && value != "" {
log.Debugf("Found provider-specific property '%s' with value: %s", ps, value)
return value
}

log.Debugf("Provider-specific property '%s' not found, checking for webhook/%s", ps, ps)
value, valueExists = ep.GetProviderSpecificProperty(fmt.Sprintf("webhook/%s", ps))
if valueExists {
if valueExists && value != "" {
log.Debugf("Found provider-specific property 'webhook/%s' with value: %s", ps, value)
return value
}
Expand All @@ -131,16 +131,18 @@ func (p *MikrotikProvider) compareEndpoints(a *endpoint.Endpoint, b *endpoint.En
return false
}

aRelevantTTL := a.RecordTTL != 0 && a.RecordTTL != endpoint.TTL(p.client.TTL)
bRelevantTTL := b.RecordTTL != 0 && b.RecordTTL != endpoint.TTL(p.client.TTL)
aRelevantTTL := a.RecordTTL != 0 && a.RecordTTL != endpoint.TTL(p.client.DefaultTTL)
bRelevantTTL := b.RecordTTL != 0 && b.RecordTTL != endpoint.TTL(p.client.DefaultTTL)
if a.RecordTTL != b.RecordTTL && (aRelevantTTL || bRelevantTTL) {
log.Debugf("RecordTTL mismatch: %v != %v", a.RecordTTL, b.RecordTTL)
return false
}

aComment := p.getProviderSpecificOrDefault(a, "comment", "")
bComment := p.getProviderSpecificOrDefault(b, "comment", "")
if aComment != bComment {
aRelevantComment := aComment != "" && aComment != p.client.DefaultComment
bRelevantComment := bComment != "" && bComment != p.client.DefaultComment
if aComment != bComment && (aRelevantComment || bRelevantComment) {
log.Debugf("Comment mismatch: %v != %v", aComment, bComment)
return false
}
Expand Down Expand Up @@ -204,10 +206,20 @@ func (p *MikrotikProvider) changes(changes *plan.Changes) *plan.Changes {

// Process Create changes
for _, create := range changes.Create {
// Enforce Default TTL
if !create.RecordTTL.IsConfigured() {
log.Debugf("Setting default TTL for created endpoint: %v", create)
create.RecordTTL = endpoint.TTL(p.client.TTL)
create.RecordTTL = endpoint.TTL(p.client.DefaultTTL)
}

// Enforce Default Comment
if p.client.DefaultComment != "" {
if p.getProviderSpecificOrDefault(create, "comment", "") == "" {
log.Debugf("Setting default comment for created endpoint: %v", create)
create.SetProviderSpecificProperty("comment", p.client.DefaultComment)
}
}

newChanges.Create = append(newChanges.Create, create)
}

Expand All @@ -234,10 +246,21 @@ func (p *MikrotikProvider) changes(changes *plan.Changes) *plan.Changes {
for _, new := range changes.UpdateNew {
if !p.listContains(duplicates, new) {
log.Debugf("Adding non-duplicate UpdateNew endpoint: %v", new)

// Enforce Default TTL
if !new.RecordTTL.IsConfigured() {
log.Debugf("Setting default TTL for UpdateNew endpoint: %v", new)
new.RecordTTL = endpoint.TTL(p.client.TTL)
new.RecordTTL = endpoint.TTL(p.client.DefaultTTL)
}

// Enforce Default Comment
if p.client.DefaultComment != "" {
if p.getProviderSpecificOrDefault(new, "comment", "") == "" {
log.Debugf("Setting default comment for UpdateNew endpoint: %v", new)
new.SetProviderSpecificProperty("comment", p.client.DefaultComment)
}
}

newChanges.UpdateNew = append(newChanges.UpdateNew, new)
}
}
Expand Down
Loading

0 comments on commit 55a7ce0

Please sign in to comment.