Skip to content

Commit d1570e5

Browse files
142: create clients layer (#162)
* create clients layer * remove stutter from naming * refactor cloudflare_configuration.go * goimports * appease the linter * appease the linter
1 parent 0485b77 commit d1570e5

File tree

10 files changed

+81
-59
lines changed

10 files changed

+81
-59
lines changed
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package controller
1+
package cf
22

33
import (
44
"context"
@@ -15,8 +15,8 @@ import (
1515
// TXT_PREFIX is the prefix added to TXT records for whom the corresponding DNS records are managed by the operator.
1616
const TXT_PREFIX = "_managed."
1717

18-
// CloudflareAPI config object holding all relevant fields to use the API
19-
type CloudflareAPI struct {
18+
// API config object holding all relevant fields to use the API
19+
type API struct {
2020
Log logr.Logger
2121
TunnelName string
2222
TunnelId string
@@ -30,8 +30,8 @@ type CloudflareAPI struct {
3030
CloudflareClient *cloudflare.API
3131
}
3232

33-
// CloudflareTunnelCredentialsFile object containing the fields that make up a Cloudflare Tunnel's credentials
34-
type CloudflareTunnelCredentialsFile struct {
33+
// TunnelCredentialsFile object containing the fields that make up a Cloudflare Tunnel's credentials
34+
type TunnelCredentialsFile struct {
3535
AccountTag string `json:"AccountTag"`
3636
TunnelID string `json:"TunnelID"`
3737
TunnelName string `json:"TunnelName"`
@@ -45,8 +45,8 @@ type DnsManagedRecordTxt struct {
4545
TunnelId string // TunnelId of the managed record
4646
}
4747

48-
// CreateCloudflareTunnel creates a Cloudflare Tunnel and returns the tunnel Id and credentials file
49-
func (c *CloudflareAPI) CreateCloudflareTunnel() (string, string, error) {
48+
// CreateTunnel creates a Cloudflare Tunnel and returns the tunnel Id and credentials file
49+
func (c *API) CreateTunnel() (string, string, error) {
5050
if _, err := c.GetAccountId(); err != nil {
5151
c.Log.Error(err, "error code in getting account ID")
5252
return "", "", err
@@ -78,7 +78,7 @@ func (c *CloudflareAPI) CreateCloudflareTunnel() (string, string, error) {
7878
c.ValidTunnelId = tunnel.ID
7979
c.ValidTunnelName = tunnel.Name
8080

81-
credentialsFile := CloudflareTunnelCredentialsFile{
81+
credentialsFile := TunnelCredentialsFile{
8282
AccountTag: c.ValidAccountId,
8383
TunnelID: tunnel.ID,
8484
TunnelName: tunnel.Name,
@@ -90,8 +90,8 @@ func (c *CloudflareAPI) CreateCloudflareTunnel() (string, string, error) {
9090
return tunnel.ID, string(creds), err
9191
}
9292

93-
// DeleteCloudflareTunnel deletes a Cloudflare Tunnel
94-
func (c *CloudflareAPI) DeleteCloudflareTunnel() error {
93+
// DeleteTunnel deletes a Cloudflare Tunnel
94+
func (c *API) DeleteTunnel() error {
9595
if err := c.ValidateAll(); err != nil {
9696
c.Log.Error(err, "Error in validation")
9797
return err
@@ -117,8 +117,8 @@ func (c *CloudflareAPI) DeleteCloudflareTunnel() error {
117117
return nil
118118
}
119119

120-
// ValidateAll validates the contents of the CloudflareAPI struct
121-
func (c *CloudflareAPI) ValidateAll() error {
120+
// ValidateAll validates the contents of the API struct
121+
func (c *API) ValidateAll() error {
122122
c.Log.Info("In validation")
123123
if _, err := c.GetAccountId(); err != nil {
124124
return err
@@ -137,7 +137,7 @@ func (c *CloudflareAPI) ValidateAll() error {
137137
}
138138

139139
// GetAccountId gets AccountId from Account Name
140-
func (c *CloudflareAPI) GetAccountId() (string, error) {
140+
func (c *API) GetAccountId() (string, error) {
141141
if c.ValidAccountId != "" {
142142
return c.ValidAccountId, nil
143143
}
@@ -161,7 +161,7 @@ func (c *CloudflareAPI) GetAccountId() (string, error) {
161161
return c.ValidAccountId, nil
162162
}
163163

164-
func (c CloudflareAPI) validateAccountId() bool {
164+
func (c *API) validateAccountId() bool {
165165
if c.AccountId == "" {
166166
c.Log.Info("Account ID not provided")
167167
return false
@@ -178,7 +178,7 @@ func (c CloudflareAPI) validateAccountId() bool {
178178
return account.ID == c.AccountId
179179
}
180180

181-
func (c *CloudflareAPI) getAccountIdByName() (string, error) {
181+
func (c *API) getAccountIdByName() (string, error) {
182182
ctx := context.Background()
183183
params := cloudflare.AccountsListParams{
184184
Name: c.AccountName,
@@ -204,7 +204,7 @@ func (c *CloudflareAPI) getAccountIdByName() (string, error) {
204204
}
205205

206206
// GetTunnelId gets Tunnel Id from available information
207-
func (c *CloudflareAPI) GetTunnelId() (string, error) {
207+
func (c *API) GetTunnelId() (string, error) {
208208
if c.ValidTunnelId != "" {
209209
return c.ValidTunnelId, nil
210210
}
@@ -231,7 +231,7 @@ func (c *CloudflareAPI) GetTunnelId() (string, error) {
231231
return c.ValidTunnelId, nil
232232
}
233233

234-
func (c *CloudflareAPI) validateTunnelId() bool {
234+
func (c *API) validateTunnelId() bool {
235235
if c.TunnelId == "" {
236236
c.Log.Info("Tunnel ID not provided")
237237
return false
@@ -254,7 +254,7 @@ func (c *CloudflareAPI) validateTunnelId() bool {
254254
return tunnel.ID == c.TunnelId
255255
}
256256

257-
func (c *CloudflareAPI) getTunnelIdByName() (string, error) {
257+
func (c *API) getTunnelIdByName() (string, error) {
258258
if _, err := c.GetAccountId(); err != nil {
259259
c.Log.Error(err, "error in getting account ID")
260260
return "", err
@@ -288,7 +288,7 @@ func (c *CloudflareAPI) getTunnelIdByName() (string, error) {
288288
}
289289

290290
// GetTunnelCreds gets Tunnel Credentials from Tunnel secret
291-
func (c *CloudflareAPI) GetTunnelCreds(tunnelSecret string) (string, error) {
291+
func (c *API) GetTunnelCreds(tunnelSecret string) (string, error) {
292292
if _, err := c.GetAccountId(); err != nil {
293293
c.Log.Error(err, "error in getting account ID")
294294
return "", err
@@ -310,7 +310,7 @@ func (c *CloudflareAPI) GetTunnelCreds(tunnelSecret string) (string, error) {
310310
}
311311

312312
// GetZoneId gets Zone Id from DNS domain
313-
func (c *CloudflareAPI) GetZoneId() (string, error) {
313+
func (c *API) GetZoneId() (string, error) {
314314
if c.ValidZoneId != "" {
315315
return c.ValidZoneId, nil
316316
}
@@ -329,7 +329,7 @@ func (c *CloudflareAPI) GetZoneId() (string, error) {
329329
return c.ValidZoneId, nil
330330
}
331331

332-
func (c *CloudflareAPI) getZoneIdByName() (string, error) {
332+
func (c *API) getZoneIdByName() (string, error) {
333333
ctx := context.Background()
334334
zones, err := c.CloudflareClient.ListZones(ctx, c.Domain)
335335

@@ -353,7 +353,7 @@ func (c *CloudflareAPI) getZoneIdByName() (string, error) {
353353
}
354354

355355
// InsertOrUpdateCName upsert DNS CNAME record for the given FQDN to point to the tunnel
356-
func (c *CloudflareAPI) InsertOrUpdateCName(fqdn, dnsId string) (string, error) {
356+
func (c *API) InsertOrUpdateCName(fqdn, dnsId string) (string, error) {
357357
ctx := context.Background()
358358
rc := cloudflare.ZoneIdentifier(c.ValidZoneId)
359359
if dnsId != "" {
@@ -395,7 +395,7 @@ func (c *CloudflareAPI) InsertOrUpdateCName(fqdn, dnsId string) (string, error)
395395
}
396396

397397
// DeleteDNSId deletes DNS entry for the given dnsId
398-
func (c *CloudflareAPI) DeleteDNSId(fqdn, dnsId string, created bool) error {
398+
func (c *API) DeleteDNSId(fqdn, dnsId string, created bool) error {
399399
// Do not delete if we did not create the DNS in this cycle
400400
if !created {
401401
return nil
@@ -414,7 +414,7 @@ func (c *CloudflareAPI) DeleteDNSId(fqdn, dnsId string, created bool) error {
414414
}
415415

416416
// GetDNSCNameId returns the ID of the CNAME record requested
417-
func (c *CloudflareAPI) GetDNSCNameId(fqdn string) (string, error) {
417+
func (c *API) GetDNSCNameId(fqdn string) (string, error) {
418418
if _, err := c.GetZoneId(); err != nil {
419419
c.Log.Error(err, "error in getting Zone ID")
420420
return "", err
@@ -447,7 +447,7 @@ func (c *CloudflareAPI) GetDNSCNameId(fqdn string) (string, error) {
447447
}
448448

449449
// GetManagedDnsTxt gets the TXT record corresponding to the fqdn
450-
func (c *CloudflareAPI) GetManagedDnsTxt(fqdn string) (string, DnsManagedRecordTxt, bool, error) {
450+
func (c *API) GetManagedDnsTxt(fqdn string) (string, DnsManagedRecordTxt, bool, error) {
451451
if _, err := c.GetZoneId(); err != nil {
452452
c.Log.Error(err, "error in getting Zone ID")
453453
return "", DnsManagedRecordTxt{}, false, err
@@ -488,7 +488,7 @@ func (c *CloudflareAPI) GetManagedDnsTxt(fqdn string) (string, DnsManagedRecordT
488488
}
489489

490490
// InsertOrUpdateTXT upsert DNS TXT record for the given FQDN to point to the tunnel
491-
func (c *CloudflareAPI) InsertOrUpdateTXT(fqdn, txtId, dnsId string) error {
491+
func (c *API) InsertOrUpdateTXT(fqdn, txtId, dnsId string) error {
492492
content, err := json.Marshal(DnsManagedRecordTxt{
493493
DnsId: dnsId,
494494
TunnelId: c.ValidTunnelId,

internal/controller/cloudflare_configuration.go renamed to internal/clients/cf/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package controller
1+
package cf
22

33
import (
44
"time"

internal/k8s/kubectl_apply_test.go renamed to internal/clients/k8s/kubectl_apply_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"slices"
66
"testing"
77

8-
"github.com/adyanth/cloudflare-operator/internal/k8s"
8+
"github.com/adyanth/cloudflare-operator/internal/clients/k8s"
9+
910
appsv1 "k8s.io/api/apps/v1"
1011
corev1 "k8s.io/api/core/v1"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

internal/controller/accesstunnel/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"context"
2222
"fmt"
2323

24+
"github.com/adyanth/cloudflare-operator/internal/clients/k8s"
25+
2426
appsv1 "k8s.io/api/apps/v1"
2527
corev1 "k8s.io/api/core/v1"
2628
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -36,7 +38,6 @@ import (
3638
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
3739

3840
networkingv1alpha1 "github.com/adyanth/cloudflare-operator/api/v1alpha1"
39-
"github.com/adyanth/cloudflare-operator/internal/k8s"
4041
"github.com/go-logr/logr"
4142
)
4243

internal/controller/clustertunnel_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package controller
1919
import (
2020
"context"
2121

22+
"github.com/adyanth/cloudflare-operator/internal/clients/cf"
23+
2224
appsv1 "k8s.io/api/apps/v1"
2325
corev1 "k8s.io/api/core/v1"
2426
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -44,7 +46,7 @@ type ClusterTunnelReconciler struct {
4446
ctx context.Context
4547
log logr.Logger
4648
tunnel Tunnel
47-
cfAPI *CloudflareAPI
49+
cfAPI *cf.API
4850
cfSecret *corev1.Secret
4951
tunnelCreds string
5052
}
@@ -73,11 +75,11 @@ func (r *ClusterTunnelReconciler) GetTunnel() Tunnel {
7375
return r.tunnel
7476
}
7577

76-
func (r *ClusterTunnelReconciler) GetCfAPI() *CloudflareAPI {
78+
func (r *ClusterTunnelReconciler) GetCfAPI() *cf.API {
7779
return r.cfAPI
7880
}
7981

80-
func (r *ClusterTunnelReconciler) SetCfAPI(in *CloudflareAPI) {
82+
func (r *ClusterTunnelReconciler) SetCfAPI(in *cf.API) {
8183
r.cfAPI = in
8284
}
8385

internal/controller/generic_tunnel_reconciler.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/adyanth/cloudflare-operator/internal/k8s"
8+
"github.com/adyanth/cloudflare-operator/internal/clients/cf"
9+
"github.com/adyanth/cloudflare-operator/internal/clients/k8s"
10+
911
"gopkg.in/yaml.v3"
1012
appsv1 "k8s.io/api/apps/v1"
1113
corev1 "k8s.io/api/core/v1"
@@ -28,8 +30,8 @@ type GenericTunnelReconciler interface {
2830

2931
GetScheme() *runtime.Scheme
3032
GetTunnel() Tunnel
31-
GetCfAPI() *CloudflareAPI
32-
SetCfAPI(*CloudflareAPI)
33+
GetCfAPI() *cf.API
34+
SetCfAPI(*cf.API)
3335
GetCfSecret() *corev1.Secret
3436
GetTunnelCreds() string
3537
SetTunnelCreds(string)
@@ -122,7 +124,7 @@ func setupNewTunnel(r GenericTunnelReconciler) error {
122124
if r.GetTunnel().GetStatus().TunnelId == "" {
123125
r.GetRecorder().Event(r.GetTunnel().GetObject(), corev1.EventTypeNormal, "Creating", "Tunnel is being created")
124126
r.GetCfAPI().TunnelName = r.GetTunnel().GetSpec().NewTunnel.Name
125-
_, creds, err := r.GetCfAPI().CreateCloudflareTunnel()
127+
_, creds, err := r.GetCfAPI().CreateTunnel()
126128
if err != nil {
127129
r.GetLog().Error(err, "unable to create Tunnel")
128130
r.GetRecorder().Event(r.GetTunnel().GetObject(), corev1.EventTypeWarning, "FailedCreate", "Unable to create Tunnel on Cloudflare")
@@ -180,7 +182,7 @@ func cleanupTunnel(r GenericTunnelReconciler) (ctrl.Result, bool, error) {
180182
return ctrl.Result{RequeueAfter: 5 * time.Second}, false, nil
181183
}
182184
if bypass || *cfDeployment.Spec.Replicas == 0 {
183-
if err := r.GetCfAPI().DeleteCloudflareTunnel(); err != nil {
185+
if err := r.GetCfAPI().DeleteTunnel(); err != nil {
184186
r.GetRecorder().Event(r.GetTunnel().GetObject(), corev1.EventTypeWarning, "FailedDeleting", "Tunnel deletion failed")
185187
return ctrl.Result{}, false, err
186188
}
@@ -271,20 +273,20 @@ func createManagedResources(r GenericTunnelReconciler) (ctrl.Result, error) {
271273
func configMapForTunnel(r GenericTunnelReconciler) *corev1.ConfigMap {
272274
ls := labelsForTunnel(r.GetTunnel())
273275
noTlsVerify := r.GetTunnel().GetSpec().NoTlsVerify
274-
originRequest := OriginRequestConfig{
276+
originRequest := cf.OriginRequestConfig{
275277
NoTLSVerify: &noTlsVerify,
276278
}
277279
if r.GetTunnel().GetSpec().OriginCaPool != "" {
278280
defaultCaPool := "/etc/cloudflared/certs/tls.crt"
279281
originRequest.CAPool = &defaultCaPool
280282
}
281-
initialConfigBytes, _ := yaml.Marshal(Configuration{
283+
initialConfigBytes, _ := yaml.Marshal(cf.Configuration{
282284
TunnelId: r.GetTunnel().GetStatus().TunnelId,
283285
SourceFile: "/etc/cloudflared/creds/credentials.json",
284286
Metrics: "0.0.0.0:2000",
285287
NoAutoUpdate: true,
286288
OriginRequest: originRequest,
287-
Ingress: []UnvalidatedIngressRule{{
289+
Ingress: []cf.UnvalidatedIngressRule{{
288290
Service: r.GetTunnel().GetSpec().FallbackTarget,
289291
}},
290292
})

internal/controller/tunnel_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package controller
1919
import (
2020
"context"
2121

22+
"github.com/adyanth/cloudflare-operator/internal/clients/cf"
23+
2224
corev1 "k8s.io/api/core/v1"
2325
apierrors "k8s.io/apimachinery/pkg/api/errors"
2426
"k8s.io/apimachinery/pkg/runtime"
@@ -43,7 +45,7 @@ type TunnelReconciler struct {
4345
ctx context.Context
4446
log logr.Logger
4547
tunnel Tunnel
46-
cfAPI *CloudflareAPI
48+
cfAPI *cf.API
4749
cfSecret *corev1.Secret
4850
tunnelCreds string
4951
}
@@ -72,11 +74,11 @@ func (r *TunnelReconciler) GetTunnel() Tunnel {
7274
return r.tunnel
7375
}
7476

75-
func (r *TunnelReconciler) GetCfAPI() *CloudflareAPI {
77+
func (r *TunnelReconciler) GetCfAPI() *cf.API {
7678
return r.cfAPI
7779
}
7880

79-
func (r *TunnelReconciler) SetCfAPI(in *CloudflareAPI) {
81+
func (r *TunnelReconciler) SetCfAPI(in *cf.API) {
8082
r.cfAPI = in
8183
}
8284

0 commit comments

Comments
 (0)