Skip to content

Commit 82f2f75

Browse files
authored
Merge pull request #22 from calebdoxsey/token
add support for cloudflare tokens
2 parents 4186d80 + 8e23597 commit 82f2f75

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Kubernetes Cloudflare Sync
22

3-
[![Docker Repository on Quay](https://quay.io/repository/calebdoxsey/kubernetes-cloudflare-sync/status "Docker Repository on Quay")](https://quay.io/repository/calebdoxsey/kubernetes-cloudflare-sync)
43

5-
This App is intended to run in your Kubernetes Cluster on GKE and sync DNS records on Cloudflare with your nodes IPs.
4+
This App is intended to run in your Kubernetes Cluster and sync DNS records on Cloudflare with your nodes' IPs.
65

76
## Example Usage
87
You can read this article to get an idea on why you would want to use it: http://www.doxsey.net/blog/kubernetes--the-surprisingly-affordable-platform-for-personal-projects
@@ -103,6 +102,7 @@ Applying all configs by running:
103102
#### ENV
104103
* ```CF_API_EMAIL``` The email address to use for cloudflare
105104
* ```CF_API_KEY``` The key to use for cloudflare
105+
* ```CF_API_TOKEN``` The token to use for cloudflare (in lieu of email and key)
106106
* ```CF_PROXY``` Enable cloudflare proxy on dns (default false)
107107
* ```CF_TTL``` TTL for dns (default 120)
108108
* ```DNS_NAME``` The dns name for the nodes, comma-separated for multiple (same root)

main.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
var options = struct {
2222
CloudflareAPIEmail string
2323
CloudflareAPIKey string
24+
CloudflareAPIToken string
2425
CloudflareProxy string
2526
CloudflareTTL string
2627
DNSName string
@@ -30,6 +31,7 @@ var options = struct {
3031
}{
3132
CloudflareAPIEmail: os.Getenv("CF_API_EMAIL"),
3233
CloudflareAPIKey: os.Getenv("CF_API_KEY"),
34+
CloudflareAPIToken: os.Getenv("CF_API_TOKEN"),
3335
CloudflareProxy: os.Getenv("CF_PROXY"),
3436
CloudflareTTL: os.Getenv("CF_TTL"),
3537
DNSName: os.Getenv("DNS_NAME"),
@@ -42,20 +44,18 @@ func main() {
4244
flag.StringVar(&options.DNSName, "dns-name", options.DNSName, "the dns name for the nodes, comma-separated for multiple (same root)")
4345
flag.StringVar(&options.CloudflareAPIEmail, "cloudflare-api-email", options.CloudflareAPIEmail, "the email address to use for cloudflare")
4446
flag.StringVar(&options.CloudflareAPIKey, "cloudflare-api-key", options.CloudflareAPIKey, "the key to use for cloudflare")
47+
flag.StringVar(&options.CloudflareAPIToken, "cloudflare-api-token", options.CloudflareAPIToken, "the token to use for cloudflare")
4548
flag.StringVar(&options.CloudflareProxy, "cloudflare-proxy", options.CloudflareProxy, "enable cloudflare proxy on dns (default false)")
4649
flag.StringVar(&options.CloudflareTTL, "cloudflare-ttl", options.CloudflareTTL, "ttl for dns (default 120)")
4750
flag.BoolVar(&options.UseInternalIP, "use-internal-ip", options.UseInternalIP, "use internal ips too if external ip's are not available")
4851
flag.BoolVar(&options.SkipExternalIP, "skip-external-ip", options.SkipExternalIP, "don't sync external IPs (use in conjunction with --use-internal-ip)")
4952
flag.StringVar(&options.NodeSelector, "node-selector", options.NodeSelector, "node selector query")
5053
flag.Parse()
5154

52-
if options.CloudflareAPIEmail == "" {
55+
if options.CloudflareAPIToken == "" &&
56+
(options.CloudflareAPIEmail == "" || options.CloudflareAPIKey == "") {
5357
flag.Usage()
54-
log.Fatalln("cloudflare api email is required")
55-
}
56-
if options.CloudflareAPIKey == "" {
57-
flag.Usage()
58-
log.Fatalln("cloudflare api key is required")
58+
log.Fatalln("cloudflare api token or email+key is required")
5959
}
6060

6161
dnsNames := strings.Split(options.DNSName, ",")

sync.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func sync(ctx context.Context, ips []string, dnsNames []string, cloudflareTTL int, cloudflareProxy bool) error {
13-
api, err := cloudflare.New(options.CloudflareAPIKey, options.CloudflareAPIEmail)
13+
api, err := newCloudflareClient(options.CloudflareAPIToken, options.CloudflareAPIEmail, options.CloudflareAPIKey)
1414
if err != nil {
1515
return errors.Wrap(err, "failed to access cloudflare api")
1616
}
@@ -113,3 +113,12 @@ func findZoneID(ctx context.Context, api interface {
113113

114114
return "", errors.New("zone id not found")
115115
}
116+
117+
func newCloudflareClient(token, email, key string) (api *cloudflare.API, err error) {
118+
if token != "" {
119+
api, err = cloudflare.NewWithAPIToken(token)
120+
} else {
121+
api, err = cloudflare.New(key, email)
122+
}
123+
return api, err
124+
}

sync_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,21 @@ func TestFindZoneID(t *testing.T) {
6464
assert.Equal(t, "1", zoneID)
6565
})
6666
}
67+
68+
func TestNewCloudflareClient(t *testing.T) {
69+
t.Run("token", func(t *testing.T) {
70+
api, err := newCloudflareClient("TEST", "", "")
71+
assert.NoError(t, err)
72+
assert.Equal(t, "TEST", api.APIToken)
73+
})
74+
t.Run("email", func(t *testing.T) {
75+
api, err := newCloudflareClient("", "EMAIL", "KEY")
76+
assert.NoError(t, err)
77+
assert.Equal(t, "EMAIL", api.APIEmail)
78+
assert.Equal(t, "KEY", api.APIKey)
79+
})
80+
t.Run("missing", func(t *testing.T) {
81+
_, err := newCloudflareClient("", "", "")
82+
assert.Error(t, err)
83+
})
84+
}

0 commit comments

Comments
 (0)