Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cfapi): replace Factory with Builder #141

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,14 @@ func main() {
os.Exit(1)
}

httpClient := &http.Client{
Timeout: 30 * time.Second,
}
f := cfapi.FactoryFunc(func(serviceKey []byte) (cfapi.Interface, error) {
return cfapi.New(serviceKey, cfapi.WithClient(httpClient)), nil
})

err = builder.
ControllerManagedBy(mgr).
For(&v1.OriginIssuer{}).
Complete(reconcile.AsReconciler(mgr.GetClient(), &controllers.OriginIssuerController{
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
Clock: clock.RealClock{},
Factory: f,
Log: log.WithName("controllers").WithName("OriginIssuer"),
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
Clock: clock.RealClock{},
Log: log.WithName("controllers").WithName("OriginIssuer"),
}))

if err != nil {
Expand All @@ -106,7 +98,6 @@ func main() {
Reader: mgr.GetAPIReader(),
ClusterResourceNamespace: o.ClusterResourceNamespace,
Clock: clock.RealClock{},
Factory: f,
Log: log.WithName("controllers").WithName("ClusterOriginIssuer"),
}))

Expand All @@ -122,8 +113,10 @@ func main() {
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
ClusterResourceNamespace: o.ClusterResourceNamespace,
Factory: f,
Log: log.WithName("controllers").WithName("CertificateRequest"),
Builder: cfapi.NewBuilder().WithClient(&http.Client{
Timeout: 30 * time.Second,
}),
Log: log.WithName("controllers").WithName("CertificateRequest"),

Clock: clock.RealClock{},
CheckApprovedCondition: !o.DisableApprovedCheck,
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/cloudflare/origin-ca-issuer

go 1.21
go 1.22

toolchain go1.21.5
toolchain go1.23.1

require (
github.com/cert-manager/cert-manager v1.9.2
Expand All @@ -11,6 +11,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/rs/zerolog v1.25.0
github.com/spf13/pflag v1.0.5
gopkg.in/dnaeon/go-vcr.v4 v4.0.1
gotest.tools/v3 v3.0.3
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,8 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/dnaeon/go-vcr.v4 v4.0.1 h1:dIFuOqqDZIJ9BTcK+DXmElzypQ6PV9fBQZSIwY+J1yM=
gopkg.in/dnaeon/go-vcr.v4 v4.0.1/go.mod h1:65yxh9goQVrudqofKtHA4JNFWd6XZRkWfKN4YpMx7KI=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
Expand Down
35 changes: 35 additions & 0 deletions internal/cfapi/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cfapi

import (
"net/http"
)

type Builder struct {
hc *http.Client
serviceKey []byte
}

func NewBuilder() *Builder {
return &Builder{}
}

func (b *Builder) WithServiceKey(key []byte) *Builder {
b.serviceKey = key
return b
}

func (b *Builder) WithClient(hc *http.Client) *Builder {
b.hc = hc
return b
}

func (b *Builder) Clone() *Builder {
return &Builder{
hc: b.hc,
serviceKey: b.serviceKey,
}
}

func (b *Builder) Build() *Client {
return New(WithServiceKey(b.serviceKey), WithClient(b.hc))
}
13 changes: 9 additions & 4 deletions internal/cfapi/cfapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ type Client struct {
endpoint string
}

func New(serviceKey []byte, options ...Options) *Client {
func New(options ...Options) *Client {
c := &Client{
serviceKey: serviceKey,
client: http.DefaultClient,
endpoint: "https://api.cloudflare.com/client/v4/certificates",
client: http.DefaultClient,
endpoint: "https://api.cloudflare.com/client/v4/certificates",
}

for _, opt := range options {
Expand All @@ -36,6 +35,12 @@ func New(serviceKey []byte, options ...Options) *Client {

type Options func(c *Client)

func WithServiceKey(key []byte) Options {
return func(c *Client) {
c.serviceKey = key
}
}

func WithClient(client *http.Client) Options {
return func(c *Client) {
c.client = client
Expand Down
3 changes: 2 additions & 1 deletion internal/cfapi/cfapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func TestSign(t *testing.T) {
ts := httptest.NewTLSServer(tt.handler)
defer ts.Close()

client := New([]byte("v1.0-FFFF-FFFF"),
client := New(
WithServiceKey([]byte("v1.0-FFFF-FFFF")),
WithClient(ts.Client()),
Must(WithEndpoint(ts.URL)),
)
Expand Down
11 changes: 0 additions & 11 deletions internal/cfapi/factory.go

This file was deleted.

10 changes: 2 additions & 8 deletions pkgs/controllers/certificaterequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type CertificateRequestController struct {
Reader client.Reader
ClusterResourceNamespace string
Log logr.Logger
Factory cfapi.Factory
Builder *cfapi.Builder

Clock clock.Clock
CheckApprovedCondition bool
Expand Down Expand Up @@ -199,13 +199,7 @@ func (r *CertificateRequestController) Reconcile(ctx context.Context, cr *certma
return reconcile.Result{}, err
}

c, err := r.Factory.APIWith(serviceKey)
if err != nil {
log.Error(err, "failed to create API client")

return reconcile.Result{}, err
}

c := r.Builder.Clone().WithServiceKey(serviceKey).Build()
p, err := provisioners.New(c, issuerspec.RequestType, log)
if err != nil {
log.Error(err, "failed to create provisioner")
Expand Down
Loading
Loading