Skip to content

Commit

Permalink
add ability to use multiple prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Dec 12, 2024
1 parent 1491454 commit 54c9702
Show file tree
Hide file tree
Showing 5 changed files with 1,189 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
tmp

node-dns-syncer
.env
20 changes: 0 additions & 20 deletions cloudbuild.yaml

This file was deleted.

76 changes: 40 additions & 36 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ var syncCmd = &cobra.Command{

for _, n := range viper.GetStringSlice("cf-zones") {
log.Infof("Processing zone %s", n)
id, err := api.ZoneIDByName(n)
zoneID, err := api.ZoneIDByName(n)
if err != nil {
log.Fatal(err)
}
recs, err := api.DNSRecords(ctx, id, cloudflare.DNSRecord{})
recs, _, err := api.ListDNSRecords(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{})
if err != nil {
log.Fatal(err)
}
sync(ctx, cl, api, n, id, recs, nodes)
sync(ctx, cl, api, n, zoneID, recs, nodes)
}
},
}
Expand Down Expand Up @@ -85,9 +85,9 @@ func makeSubdomainName(prefix string, ip string) string {

func sync(ctx context.Context, cl *kubernetes.Clientset, api *cloudflare.API, zoneName, zoneID string, recs []cloudflare.DNSRecord, nodes []Node) {
prefix := viper.GetString("domain-name-prefix")
suffix := viper.GetString("domain-name-suffix")
dryRun := viper.GetBool("dry-run")
deleted := []string{}
suffixes := strings.Split(viper.GetString("domain-name-suffix"), ",")
var deleted []string
for _, r := range recs {
if !strings.HasPrefix(r.Name, prefix) {
continue
Expand All @@ -102,49 +102,53 @@ func sync(ctx context.Context, cl *kubernetes.Clientset, api *cloudflare.API, zo
log.Infof("Remove record \"%s\"", r.Name)
deleted = append(deleted, r.Name)
if !dryRun {
err := api.DeleteDNSRecord(ctx, zoneID, r.ID)
err := api.DeleteDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), r.ID)
if err != nil {
log.Fatal(err)
}
}
}
}
for _, n := range nodes {

name := n.Subdomain + suffix + zoneName

found := false
for _, r := range recs {
del := false
for _, d := range deleted {
if d == r.Name {
del = true
for _, suffix := range suffixes {
suffix = strings.TrimSpace(suffix)

name := n.Subdomain + suffix + zoneName

found := false
for _, r := range recs {
del := false
for _, d := range deleted {
if d == r.Name {
del = true
}
}
if del {
continue
}
if r.Name == name {
found = true
log.Infof("Record \"%s\" with ip %s already exists", name, n.IP)
updateNodeLabel(ctx, cl, n)
}
}
if del {
continue
}
if r.Name == name {
found = true
log.Infof("Record \"%s\" with ip %s already exists", name, n.IP)
updateNodeLabel(ctx, cl, n)
}
}
if !found {
log.Infof("Add record \"%s\" with ip %s", name, n.IP)
if !dryRun {
_, err := api.CreateDNSRecord(ctx, zoneID, cloudflare.DNSRecord{
Type: "A",
Name: name,
Content: n.IP,
Proxied: cloudflare.BoolPtr(false),
})
if err != nil {
log.Fatal(err)
if !found {
log.Infof("Add record \"%s\" with ip %s", name, n.IP)
if !dryRun {
_, err := api.CreateDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), cloudflare.CreateDNSRecordParams{
Type: "A",
Name: name,
Content: n.IP,
Proxied: cloudflare.BoolPtr(false),
})
if err != nil {
log.Fatal(err)
}
updateNodeLabel(ctx, cl, n)
}
updateNodeLabel(ctx, cl, n)
}
}

}
}

Expand Down
97 changes: 55 additions & 42 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,75 +1,88 @@
module github.com/webtor-io/node-dns-syncer

go 1.18
go 1.23.0

require (
github.com/cloudflare/cloudflare-go v0.41.0
github.com/joonix/log v0.0.0-20200409080653-9c1d2ceb5f1d
github.com/cloudflare/cloudflare-go v0.111.0
github.com/joonix/log v0.0.0-20230221083239-7988383bab32
github.com/mitchellh/go-homedir v1.1.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
k8s.io/api v0.24.2
k8s.io/apimachinery v0.24.2
k8s.io/client-go v0.24.2
github.com/spf13/viper v1.19.0
k8s.io/api v0.32.0
k8s.io/apimachinery v0.32.0
k8s.io/client-go v0.32.0
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful v2.15.0+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/magiconair/properties v1.8.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/protobuf v1.28.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.30.0 // indirect
golang.org/x/exp v0.0.0-20241210194714-1829a127f884 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20241209162323-e6fa225c2576 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog v1.0.0 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220614142933-1062c7ade5f8 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20220525155127-227cbc7cc124 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20241212045625-5ad02ce6640f // indirect
k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 54c9702

Please sign in to comment.