-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (106 loc) · 3.23 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"golang.org/x/exp/slices"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
networkingv1 "k8s.io/api/networking/v1"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
var (
DUCKDNS_TOKEN = os.Getenv("DUCKDNS_TOKEN")
DOMAINS = strings.Split(os.Getenv("DOMAINS"), ",")
)
func main() {
logf.SetLogger(zap.New())
var log = logf.Log.WithName("builder-examples")
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
if err != nil {
log.Error(err, "could not create manager")
os.Exit(1)
}
err = builder.
ControllerManagedBy(mgr).
For(&networkingv1.Ingress{}).
Complete(&IngressDnsController{})
if err != nil {
log.Error(err, "could not create controller")
os.Exit(1)
}
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Error(err, "could not start manager")
os.Exit(1)
}
}
// IngressDnsController is a simple ControllerManagedBy example implementation.
type IngressDnsController struct {
client.Client
}
func (a *IngressDnsController) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
ing := &networkingv1.Ingress{}
err := a.Get(ctx, req.NamespacedName, ing)
if err != nil {
return reconcile.Result{}, err
}
for _, rule := range ing.Spec.Rules {
host := rule.Host
if !strings.HasSuffix(host, "duckdns.org") {
continue
}
logf.Log.Info("got duckdns domain", "host", host)
prefix := strings.TrimSuffix(host, ".duckdns.org")
if !slices.Contains(DOMAINS, prefix) {
logf.Log.Info("skipping reonciliation for domain not listed in the filter")
continue
}
ip := getIp(ing)
url := fmt.Sprintf("https://www.duckdns.org/update?domains=%s&token=%s&ip=%s&verbose=true", prefix, DUCKDNS_TOKEN, ip)
r, err := http.Get(url)
if err != nil {
logf.Log.Error(err, "unable to update domain", "host", host)
return reconcile.Result{}, err
}
if r.StatusCode != 200 {
logf.Log.Info("unable to update domain", "host", host, "statusCode", r.StatusCode)
return reconcile.Result{}, errors.New("unable to update domain")
}
res, err := ioutil.ReadAll(r.Body)
if err != nil {
logf.Log.Error(err, "unable to parse response", "host", host)
return reconcile.Result{}, err
}
logf.Log.Info("got response", "r", string(res))
if !strings.HasPrefix(string(res), "OK") {
logf.Log.Info("unable to update domain", "host", host, "response", string(res))
return reconcile.Result{}, errors.New("unable to update domain")
}
logf.Log.Info("updated IP for the domain", "domain", host, "ip", ip)
}
return reconcile.Result{RequeueAfter: 60 * time.Second}, nil
}
func (a *IngressDnsController) InjectClient(c client.Client) error {
a.Client = c
return nil
}
func getIp(ing *networkingv1.Ingress) string {
ingStatus := ing.Status.LoadBalancer.Ingress
if len(ingStatus) == 0 {
return ""
}
if ingStatus[0].IP == "" {
return ingStatus[0].Hostname
}
return ingStatus[0].IP
}