This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
looper.go
214 lines (191 loc) · 5.56 KB
/
looper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package controllers
import (
"context"
"fmt"
cfv1 "github.com/arikkfir/cloudflare-operator/api/v1"
"github.com/cloudflare/cloudflare-go"
"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"
)
const SyncInterval = "1m"
type looper struct {
log logr.Logger
dnsRecord *cfv1.DNSRecord
client client.Client
stopChannel chan struct{}
}
func (l *looper) getAPIToken(ctx context.Context) (*string, error) {
var secretNamespace, secretName string
secretTokens := strings.SplitN(l.dnsRecord.Spec.APIToken.Secret, "/", 2)
if len(secretTokens) == 2 {
secretNamespace = secretTokens[0]
secretName = secretTokens[1]
} else {
secretNamespace = l.dnsRecord.Namespace
secretName = secretTokens[0]
}
secret := &v1.Secret{}
err := l.client.Get(ctx, client.ObjectKey{Namespace: secretNamespace, Name: secretName}, secret)
if err != nil {
return nil, fmt.Errorf("failed to find secret '%s': %w", l.dnsRecord.Spec.APIToken.Secret, err)
}
token, ok := secret.Data[l.dnsRecord.Spec.APIToken.Key]
if !ok {
return nil, fmt.Errorf("failed to find key '%s' in secret '%s': %w", l.dnsRecord.Spec.APIToken.Key, l.dnsRecord.Spec.APIToken.Secret, err)
}
tokenString := string(token)
return &tokenString, nil
}
func (l *looper) sync(ctx context.Context) {
apiToken, err := l.getAPIToken(ctx)
if err != nil {
l.log.V(1).Error(err, "Failed finding APIKey")
return
}
dnsrec := l.dnsRecord
// Construct a new API object
api, err := cloudflare.NewWithAPIToken(*apiToken)
if err != nil {
l.log.V(1).Error(err, "Failed creating Cloudflare client")
return
}
zoneID, err := api.ZoneIDByName(dnsrec.Spec.Zone)
if err != nil {
l.log.V(1).Error(err, "Failed looking up Cloudflare zone", "zone", dnsrec.Spec.Zone)
return
}
records, err := api.DNSRecords(ctx, zoneID, cloudflare.DNSRecord{
Type: dnsrec.Spec.Type,
Name: dnsrec.Spec.Name,
})
if err != nil {
l.log.V(1).Error(err, "Failed listing DNS records")
return
}
if len(records) == 0 {
l.log.V(1).Info("DNS record not found, creating")
_, err := api.CreateDNSRecord(ctx, zoneID, cloudflare.DNSRecord{
Type: dnsrec.Spec.Type,
Name: dnsrec.Spec.Name,
Content: dnsrec.Spec.Content,
Proxied: dnsrec.Spec.Proxied,
TTL: dnsrec.Spec.TTL,
Priority: dnsrec.Spec.Priority,
})
if err != nil {
l.log.V(1).Error(err, "Failed creating DNS record")
return
}
l.log.V(3).Info("Created record")
} else if len(records) == 1 {
cfRec := records[0]
diff := false
if cfRec.Content != dnsrec.Spec.Content {
diff = true
}
if cfRec.Proxied == nil && dnsrec.Spec.Proxied != nil || cfRec.Proxied != nil && dnsrec.Spec.Proxied == nil {
diff = true
}
if cfRec.Proxied != nil && dnsrec.Spec.Proxied != nil && *cfRec.Proxied != *dnsrec.Spec.Proxied {
diff = true
}
if cfRec.Proxied != nil && dnsrec.Spec.Proxied != nil && !*cfRec.Proxied && cfRec.TTL != dnsrec.Spec.TTL {
diff = true
}
if cfRec.Priority == nil && dnsrec.Spec.Priority != nil || cfRec.Priority != nil && dnsrec.Spec.Priority == nil {
diff = true
}
if cfRec.Priority != nil && dnsrec.Spec.Priority != nil && *cfRec.Priority != *dnsrec.Spec.Priority {
diff = true
}
if diff {
l.log.V(1).Info("DNS record not up to date, syncing")
err := api.UpdateDNSRecord(ctx, zoneID, records[0].ID, cloudflare.DNSRecord{
Type: dnsrec.Spec.Type,
Name: dnsrec.Spec.Name,
Content: dnsrec.Spec.Content,
Proxied: dnsrec.Spec.Proxied,
TTL: dnsrec.Spec.TTL,
Priority: dnsrec.Spec.Priority,
})
if err != nil {
l.log.V(1).Error(err, "Failed updating DNS record")
return
}
l.log.V(3).Info("Updated record")
}
} else {
l.log.Error(fmt.Errorf("too many records found"), "Too many records", "type", dnsrec.Spec.Type, "name", dnsrec.Spec.Name, "records", records)
}
}
func (l *looper) start() error {
if l.stopChannel != nil {
return nil
}
interval, err := time.ParseDuration(SyncInterval)
if err != nil {
return fmt.Errorf("invalid duration '%s': %w", SyncInterval, err)
}
l.stopChannel = make(chan struct{})
ticker := time.NewTicker(interval)
go func(done chan struct{}, ticket *time.Ticker) {
l.log.V(1).Info("Starting sync loop")
defer ticker.Stop()
for {
select {
case <-done:
return
case _ = <-ticker.C:
ctx := context.TODO()
l.sync(ctx)
}
}
}(l.stopChannel, ticker)
return nil
}
func (l *looper) stop() error {
if l.stopChannel != nil {
l.stopChannel <- struct{}{}
}
return nil
}
func (l *looper) delete(ctx context.Context) {
apiToken, err := l.getAPIToken(ctx)
if err != nil {
l.log.V(1).Error(err, "Failed finding APIKey")
return
}
dnsrec := l.dnsRecord
// Construct a new API object
api, err := cloudflare.NewWithAPIToken(*apiToken)
if err != nil {
l.log.V(1).Error(err, "Failed creating Cloudflare client")
return
}
zoneID, err := api.ZoneIDByName(dnsrec.Spec.Zone)
if err != nil {
l.log.V(1).Error(err, "Failed looking up Cloudflare zone", "zone", dnsrec.Spec.Zone)
return
}
records, err := api.DNSRecords(ctx, zoneID, cloudflare.DNSRecord{
Type: dnsrec.Spec.Type,
Name: dnsrec.Spec.Name,
})
if err != nil {
l.log.V(1).Error(err, "Failed listing DNS records")
return
}
if len(records) == 1 {
l.log.V(1).Info("Deleting DNS record")
err := api.DeleteDNSRecord(ctx, zoneID, records[0].ID)
if err != nil {
l.log.V(1).Error(err, "Failed deleting DNS record")
return
}
} else {
l.log.Error(fmt.Errorf("too many records found"), "Too many records", "type", dnsrec.Spec.Type, "name", dnsrec.Spec.Name, "records", records)
}
}