Skip to content

Commit 0d3a030

Browse files
rxbngi8lino
andauthored
Kubernetes Ingress integration (#95)
* Initial kubernetes ingress implementation * Set timeout for golangci-lint * Update README.md * Add rbac example * Fix rbac * Update README.md Co-authored-by: giotto <giottolino@gmail.com> * Fix in-cluster kubernetes client * Simplification * Fix license and readme * Improve log message * Simplify kube client creation * Increase retries for asset upload Co-authored-by: giotto <giottolino@gmail.com>
1 parent 8ecbb42 commit 0d3a030

21 files changed

+852
-72
lines changed

.github/workflows/golangci-lint.yml

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ jobs:
1717
- uses: actions/checkout@v2
1818
- name: golangci-lint
1919
uses: golangci/golangci-lint-action@v2
20+
with:
21+
args: --timeout=3m

.github/workflows/release.yml

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
goarch: amd64
1818
binary_name: syncflaer
1919
project_path: cmd/syncflaer
20+
retry: 5
2021
release-linux-arm:
2122
name: release linux/arm
2223
runs-on: ubuntu-latest
@@ -29,6 +30,7 @@ jobs:
2930
goarch: arm
3031
binary_name: syncflaer
3132
project_path: cmd/syncflaer
33+
retry: 5
3234
release-linux-arm64:
3335
name: release linux/arm64
3436
runs-on: ubuntu-latest
@@ -41,6 +43,7 @@ jobs:
4143
goarch: arm64
4244
binary_name: syncflaer
4345
project_path: cmd/syncflaer
46+
retry: 5
4447
release-darwin-amd64:
4548
name: release darwin/amd64
4649
runs-on: ubuntu-latest
@@ -53,6 +56,7 @@ jobs:
5356
goarch: amd64
5457
binary_name: syncflaer
5558
project_path: cmd/syncflaer
59+
retry: 5
5660
release-darwin-arm64:
5761
name: release darwin/arm64
5862
runs-on: ubuntu-latest
@@ -65,6 +69,7 @@ jobs:
6569
goarch: arm64
6670
binary_name: syncflaer
6771
project_path: cmd/syncflaer
72+
retry: 5
6873
release-windows-amd64:
6974
name: release windows/amd64
7075
runs-on: ubuntu-latest
@@ -77,3 +82,4 @@ jobs:
7782
goarch: amd64
7883
binary_name: syncflaer
7984
project_path: cmd/syncflaer
85+
retry: 5

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## [v5.3.0](https://github.com/containeroo/SyncFlaer/tree/v5.3.0) (2022-01-21)
4+
5+
[All Commits](https://github.com/containeroo/SyncFlaer/compare/v5.2.0...v5.3.0)
6+
7+
**New features:**
8+
9+
- Add support for Kubernetes ingress objects (#80)
10+
311
## [v5.2.0](https://github.com/containeroo/SyncFlaer/tree/v5.2.0) (2021-12-24)
412

513
[All Commits](https://github.com/containeroo/SyncFlaer/compare/v5.1.6...v5.2.0)

LICENSE

+3-3
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ to attach them to the start of each source file to most effectively
631631
state the exclusion of warranty; and each file should have at least
632632
the "copyright" line and a pointer to where the full notice is found.
633633

634-
SyncFlaer: Synchronize Traefik host rules with Cloudflare®.
635-
Copyright (C) 2021 Containeroo
634+
SyncFlaer: Synchronize Traefik host rules and/or Kubernetes Ingresses with Cloudflare®.
635+
Copyright (C) 2022 containeroo
636636

637637
This program is free software: you can redistribute it and/or modify
638638
it under the terms of the GNU General Public License as published by
@@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
652652
If the program does terminal interaction, make it output a short
653653
notice like this when it starts in an interactive mode:
654654

655-
SyncFlaer Copyright (C) 2021 Containeroo
655+
SyncFlaer Copyright (C) 2022 containeroo
656656
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657657
This is free software, and you are welcome to redistribute it
658658
under certain conditions; type `show c' for details.

README.md

+28-23
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# SyncFlaer
22

3-
Synchronize Traefik host rules with Cloudflare®.
3+
Synchronize Traefik host rules and/or Kubernetes Ingresses with Cloudflare®.
44

55
![Docker Image Version (latest semver)](https://img.shields.io/docker/v/containeroo/syncflaer?sort=semver)
66
![Docker Pulls](https://img.shields.io/docker/pulls/containeroo/syncflaer)
77
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/containeroo/syncflaer)
88

99
## Why?
1010

11-
- Dynamically create, update or delete Cloudflare® DNS records based on Traefik http rules
11+
- Dynamically create, update or delete Cloudflare® DNS records based on Traefik http rules and/or Kubernetes Ingresses (apiVersion: networking.k8s.io/v1)
1212
- Supports multiple Traefik instances
13+
- Supports Kubernetes Ingresses (apiVersion: networking.k8s.io/v1)
1314
- Supports multiple Cloudflare zones
1415
- Update DNS records when public IP changes
1516
- Supports configuring additional DNS records for services outside Traefik (i.e. vpn server)
@@ -21,9 +22,9 @@ Synchronize Traefik host rules with Cloudflare®.
2122
- [Kubernetes](#kubernetes)
2223
- [Configuration](#configuration)
2324
- [Overview](#overview)
24-
- [Minimal Config File](#minimal-config-file)
25-
- [Full Config File](#full-config-file)
25+
- [Config File](#config-file)
2626
- [Using Multiple Traefik Instances](#using-multiple-traefik-instances)
27+
- [Kubernetes Ingress Support](#kubernetes-ingress-support)
2728
- [Environment Variables](#environment-variables)
2829
- [Defaults](#defaults)
2930
- [Additional Records](#additional-records)
@@ -61,25 +62,9 @@ You can run SyncFlaer as a Kubernetes CronJob. For an example deployment, please
6162

6263
### Overview
6364

64-
SyncFlaer must be configured via a [YAML config file](#full-config-file). Some secrets can be configured using [environment variables](#environment-variables).
65+
SyncFlaer must be configured via a [YAML config file](#config-file). Some secrets can be configured using [environment variables](#environment-variables).
6566

66-
#### Minimal Config File
67-
68-
The following configuration is required:
69-
70-
```yaml
71-
---
72-
traefikInstances:
73-
- name: main
74-
url: https://traefik.example.com
75-
76-
cloudflare:
77-
apiToken: abc
78-
zoneNames:
79-
- example.com
80-
```
81-
82-
#### Full Config File
67+
#### Config File
8368

8469
The full configuration file can be found at `configs/config.yml`.
8570

@@ -110,6 +95,25 @@ traefikInstances:
11095
11196
Every instance can be configured to use different HTTP basic auth, custom request headers and ignored rules.
11297
98+
#### Kubernetes Ingress Support
99+
100+
SyncFlaer can be configured to support Kubernetes Ingresses. By default, SyncFlaer will sync all Ingresses.
101+
102+
If you run SyncFlaer in a Kubernetes cluster, please refer to the `deployments/kubernetes` folder for an example deployment.
103+
If you run SyncFlaer outside a Kubernetes cluster, you can use the `KUBECONFIG` environment variable to configure a specific kubeconfig file.
104+
If the `KUBECONFIG` environment variable is not set, SyncFlaer will use the default kubeconfig file located at `$HOME/.kube/config`.
105+
106+
If you want to ignore specific Ingresses, use the annotation `syncflaer.containeroo.ch/ignore=true`.
107+
108+
To overwrite the default configuration for DNS records, you can specify the following annotations for each Ingress:
109+
110+
| Annotation | Example |
111+
|------------------------------------|----------------|
112+
| `syncflaer.containeroo.ch/type` | `A` or `CNAME` |
113+
| `syncflaer.containeroo.ch/content` | `example.com` |
114+
| `syncflaer.containeroo.ch/proxied` | `true` |
115+
| `syncflaer.containeroo.ch/ttl` | `120` |
116+
113117
#### Environment Variables
114118

115119
Instead of putting secrets in the config file, SyncFlaer can grab secrets from environment variables.
@@ -130,6 +134,7 @@ If not specified, the following defaults apply:
130134
| Name | Default Value |
131135
|--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
132136
| `ipProviders` | `["https://ifconfig.me/ip", "https://ipecho.net/plain", "https://myip.is/ip", "https://checkip.amazonaws.com", "https://api.ipify.org"]` |
137+
| `kubernetes.enabled` | `false` |
133138
| `managedRootRecord` | `true` |
134139
| `cloudflare.deleteGrace` | `0` (delete records instantly) |
135140
| `cloudflare.defaults.type` | `CNAME` |
@@ -184,7 +189,7 @@ Everything after the `env:` part will be used as the name of the environment var
184189

185190
## Copyright
186191

187-
2021 Containeroo
192+
2022 containeroo
188193

189194
Cloudflare and the Cloudflare logo are registered trademarks owned by Cloudflare Inc.
190195
This project is not affiliated with Cloudflare®.

cmd/syncflaer/main.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/cloudflare/cloudflare-go"
6+
"github.com/containeroo/syncflaer/internal/kube"
67
"os"
78
"strconv"
89

@@ -11,7 +12,7 @@ import (
1112
log "github.com/sirupsen/logrus"
1213
)
1314

14-
const version string = "5.2.0"
15+
const version string = "5.3.0"
1516

1617
func main() {
1718
log.SetOutput(os.Stdout)
@@ -48,8 +49,16 @@ func main() {
4849
deleteGraceRecords := internal.GetDeleteGraceRecords(cf, zoneID)
4950

5051
var userRecords []cloudflare.DNSRecord
51-
userRecords = internal.GetTraefikRules(config, currentIP, zoneName, userRecords)
52-
userRecords = internal.GetAdditionalRecords(config, currentIP, zoneName, userRecords)
52+
if config.TraefikInstances != nil {
53+
userRecords = internal.GetTraefikRules(config, currentIP, zoneName, userRecords)
54+
}
55+
if config.AdditionalRecords != nil {
56+
userRecords = internal.GetAdditionalRecords(config, currentIP, zoneName, userRecords)
57+
}
58+
if *config.Kubernetes.Enabled {
59+
kubeClient := kube.SetupKubernetesClient()
60+
userRecords = kube.GetIngresses(kubeClient, config, currentIP, zoneName, userRecords)
61+
}
5362

5463
missingRecords := internal.GetMissingDNSRecords(cloudflareDNSRecords, userRecords)
5564
if missingRecords != nil {

configs/config.yml

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ traefikInstances:
4949
- example.example.com
5050
- internal.example.com
5151

52+
# enable kubernetes ingress integration
53+
kubernetes:
54+
enabled: true
55+
5256
# set whether you want to have the root record managed by SyncFlaer
5357
# if you don't need a root record, you can set this to false
5458
# it is also helpful to set this to false if your root record points to a different server

deployments/kubernetes/configmap.yaml

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ data:
99
notifications:
1010
slack:
1111
webhookURL: env:SLACK_WEBHOOK_URL
12-
traefikInstances:
13-
- name: main
14-
url: https://traefik.example.com
15-
ignoredRules:
16-
- local.example.com
17-
- dev.example.com
12+
# traefikInstances:
13+
# - name: main
14+
# url: https://traefik.example.com
15+
# ignoredRules:
16+
# - local.example.com
17+
# - dev.example.com
18+
19+
kubernetes:
20+
enabled: true
1821
1922
additionalRecords:
2023
- name: vpn.example.com

deployments/kubernetes/cronjob.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ spec:
2929
name: config
3030
subPath: config.yml
3131
restartPolicy: Never
32-
automountServiceAccountToken: false
32+
serviceAccountName: syncflaer
3333
volumes:
3434
- name: config
3535
configMap:

deployments/kubernetes/rbac.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: syncflaer
6+
namespace: syncflaer
7+
---
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
kind: ClusterRole
10+
metadata:
11+
name: syncflaer
12+
rules:
13+
- apiGroups:
14+
- networking.k8s.io
15+
resources:
16+
- ingresses
17+
verbs:
18+
- get
19+
- list
20+
---
21+
apiVersion: rbac.authorization.k8s.io/v1
22+
kind: syncflaer
23+
metadata:
24+
name: syncflaer
25+
roleRef:
26+
apiGroup: rbac.authorization.k8s.io
27+
kind: ClusterRole
28+
name: syncflaer
29+
subjects:
30+
- kind: ServiceAccount
31+
name: syncflaer
32+
namespace: syncflaer

go.mod

+30-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,39 @@ require (
88
github.com/slack-go/slack v0.10.1
99
github.com/spf13/pflag v1.0.5
1010
gopkg.in/yaml.v2 v2.4.0
11+
k8s.io/apimachinery v0.23.2
12+
k8s.io/client-go v0.23.2
1113
)
1214

1315
require (
16+
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/go-logr/logr v1.2.0 // indirect
18+
github.com/gogo/protobuf v1.3.2 // indirect
19+
github.com/golang/protobuf v1.5.2 // indirect
20+
github.com/google/go-cmp v0.5.5 // indirect
21+
github.com/google/gofuzz v1.1.0 // indirect
22+
github.com/googleapis/gnostic v0.5.5 // indirect
1423
github.com/gorilla/websocket v1.4.2 // indirect
15-
github.com/kr/text v0.2.0 // indirect
24+
github.com/imdario/mergo v0.3.5 // indirect
25+
github.com/json-iterator/go v1.1.12 // indirect
26+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
27+
github.com/modern-go/reflect2 v1.0.2 // indirect
1628
github.com/pkg/errors v0.9.1 // indirect
17-
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
18-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
19-
golang.org/x/text v0.3.6 // indirect
20-
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
29+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
30+
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
31+
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
32+
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
33+
golang.org/x/text v0.3.7 // indirect
34+
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
35+
google.golang.org/appengine v1.6.7 // indirect
36+
google.golang.org/protobuf v1.27.1 // indirect
37+
gopkg.in/inf.v0 v0.9.1 // indirect
38+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
39+
k8s.io/api v0.23.2 // indirect
40+
k8s.io/klog/v2 v2.30.0 // indirect
41+
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
42+
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
43+
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
44+
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
45+
sigs.k8s.io/yaml v1.2.0 // indirect
2146
)

0 commit comments

Comments
 (0)