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: use issuer-lib #162

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
94 changes: 24 additions & 70 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"context"
"log/slog"
"net/http"
"os"
"time"
Expand All @@ -10,18 +12,14 @@ import (
"github.com/cloudflare/origin-ca-issuer/internal/cfapi"
v1 "github.com/cloudflare/origin-ca-issuer/pkgs/apis/v1"
"github.com/cloudflare/origin-ca-issuer/pkgs/controllers"
"github.com/go-logr/zerologr"
"github.com/rs/zerolog"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/clock"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
crlog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func main() {
Expand All @@ -31,36 +29,32 @@ func main() {

_ = fs.Parse(os.Args[1:])

zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
zerologr.NameFieldName = "logger"
zerologr.NameSeparator = "/"

zl := zerolog.New(os.Stderr).With().Caller().Timestamp().Logger()
logf.SetLogger(zerologr.New(&zl))
log := logf.Log.WithName("origin-issuer").V(8)
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
crlog.SetLogger(logr.FromSlogHandler(logger.Handler()))

if err := o.Validate(); err != nil {
log.Error(err, "error validating options")
logger.Error("error validating options", "error", err)
os.Exit(1)
}

scheme := runtime.NewScheme()
if err := clientgoscheme.AddToScheme(scheme); err != nil {
log.Error(err, "could not add to scheme")
logger.Error("could not add to scheme", "error", err)
os.Exit(1)
}
if err := certmanager.AddToScheme(scheme); err != nil {
log.Error(err, "could not add to scheme")
logger.Error("could not add to scheme", "error", err)
os.Exit(1)
}
if err := v1.AddToScheme(scheme); err != nil {
log.Error(err, "could not add to scheme")
logger.Error("could not add to scheme", "error", err)
os.Exit(1)
}

kubeCfg, err := config.GetConfig()
if err != nil {
log.Error(err, "could not load kubeconfig")
logger.Error("could not load kubeconfig", "error", err)
os.Exit(1)
}

Expand All @@ -71,64 +65,24 @@ func main() {
Scheme: scheme,
})
if err != nil {
log.Error(err, "could not create manager")
os.Exit(1)
}

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

if err != nil {
log.Error(err, "could not create origin issuer controller")
os.Exit(1)
}

err = builder.
ControllerManagedBy(mgr).
For(&v1.ClusterOriginIssuer{}).
Complete(reconcile.AsReconciler(mgr.GetClient(), &controllers.ClusterOriginIssuerController{
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
ClusterResourceNamespace: o.ClusterResourceNamespace,
Clock: clock.RealClock{},
Log: log.WithName("controllers").WithName("ClusterOriginIssuer"),
}))

if err != nil {
log.Error(err, "could not create cluster origin issuer controller")
logger.Error("could not create manager", "error", err)
os.Exit(1)
}

err = builder.
ControllerManagedBy(mgr).
For(&certmanager.CertificateRequest{}).
Complete(reconcile.AsReconciler(mgr.GetClient(), &controllers.CertificateRequestController{
Client: mgr.GetClient(),
Reader: mgr.GetAPIReader(),
ClusterResourceNamespace: o.ClusterResourceNamespace,
Builder: cfapi.NewBuilder().WithClient(&http.Client{
Timeout: 30 * time.Second,
}),
Log: log.WithName("controllers").WithName("CertificateRequest"),
ctx, cancel := context.WithCancel(signals.SetupSignalHandler())
defer cancel()

Clock: clock.RealClock{},
CheckApprovedCondition: !o.DisableApprovedCheck,
}))

if err != nil {
log.Error(err, "could not create certificaterequest controller")
os.Exit(1)
signer := &controllers.Signer{
Reader: mgr.GetAPIReader(),
ClusterResourceNamespace: o.ClusterResourceNamespace,
Builder: cfapi.NewBuilder().WithClient(&http.Client{
Timeout: 30 * time.Second,
}),
}
signer.SetupWithManager(ctx, mgr)

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Error(err, "could not start manager")
if err := mgr.Start(ctx); err != nil {
logger.Error("could not start manager", "error", err)
os.Exit(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ spec:
properties:
conditions:
description: |-
List of status conditions to indicate the status of an OriginIssuer
List of status conditions to indicate the status of an Issuer.
Known condition types are `Ready`.
items:
description: OriginIssuerCondition contains condition information
for the OriginIssuer.
description: IssuerCondition contains condition information for
an Issuer.
properties:
lastTransitionTime:
description: |-
Expand All @@ -114,31 +114,41 @@ spec:
message:
description: |-
Message is a human readable description of the details of the last
transition1, complementing reason.
transition, complementing reason.
type: string
observedGeneration:
description: |-
If set, this represents the .metadata.generation that the condition was
set based upon.
For instance, if .metadata.generation is currently 12, but the
.status.condition[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the Issuer.
format: int64
type: integer
reason:
description: |-
Reason is a brief machine readable explanation for the condition's last
transition.
type: string
status:
description: Status of the condition, one of ('True', 'False',
'Unknown')
description: Status of the condition, one of (`True`, `False`,
`Unknown`).
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: Type of the condition, known values are ('Ready')
enum:
- Ready
description: Type of the condition, known values are (`Ready`).
type: string
required:
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
type: object
type: object
served: true
Expand Down
28 changes: 19 additions & 9 deletions deploy/crds/cert-manager.k8s.cloudflare.com_originissuers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ spec:
properties:
conditions:
description: |-
List of status conditions to indicate the status of an OriginIssuer
List of status conditions to indicate the status of an Issuer.
Known condition types are `Ready`.
items:
description: OriginIssuerCondition contains condition information
for the OriginIssuer.
description: IssuerCondition contains condition information for
an Issuer.
properties:
lastTransitionTime:
description: |-
Expand All @@ -113,31 +113,41 @@ spec:
message:
description: |-
Message is a human readable description of the details of the last
transition1, complementing reason.
transition, complementing reason.
type: string
observedGeneration:
description: |-
If set, this represents the .metadata.generation that the condition was
set based upon.
For instance, if .metadata.generation is currently 12, but the
.status.condition[x].observedGeneration is 9, the condition is out of date
with respect to the current state of the Issuer.
format: int64
type: integer
reason:
description: |-
Reason is a brief machine readable explanation for the condition's last
transition.
type: string
status:
description: Status of the condition, one of ('True', 'False',
'Unknown')
description: Status of the condition, one of (`True`, `False`,
`Unknown`).
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: Type of the condition, known values are ('Ready')
enum:
- Ready
description: Type of the condition, known values are (`Ready`).
type: string
required:
- status
- type
type: object
type: array
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
type: object
type: object
served: true
Expand Down
27 changes: 22 additions & 5 deletions deploy/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,19 @@ rules:
verbs:
- get
- list
- update
- watch
- apiGroups:
- cert-manager.io
resources:
- certificaterequests/status
verbs:
- get
- patch
- update
- apiGroups:
- cert-manager.k8s.cloudflare.com
resources:
- clusteroriginissuers
- originissuers
verbs:
- create
- get
- list
- watch
Expand All @@ -52,6 +48,27 @@ rules:
- clusteroriginissuers/status
- originissuers/status
verbs:
- patch
- apiGroups:
- certificates.k8s.io
resources:
- certificatesigningrequests
verbs:
- get
- list
- watch
- apiGroups:
- certificates.k8s.io
resources:
- certificatesigningrequests/status
verbs:
- patch
- update
- apiGroups:
- certificates.k8s.io
resourceNames:
- clusteroriginissuers.cert-manager.k8s.cloudflare.com/*
- originissuers.cert-manager.k8s.cloudflare.com/*
resources:
- signers
verbs:
- sign
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module github.com/cloudflare/origin-ca-issuer
go 1.24.0

require (
github.com/cert-manager/cert-manager v1.17.0
github.com/cert-manager/cert-manager v1.17.1
github.com/cert-manager/issuer-lib v0.8.0
github.com/go-logr/logr v1.4.2
github.com/go-logr/zerologr v1.2.3
github.com/google/go-cmp v0.6.0
github.com/rs/zerolog v1.29.0
github.com/spf13/pflag v1.0.6
gopkg.in/dnaeon/go-vcr.v4 v4.0.1
gotest.tools/v3 v3.5.1
Expand Down Expand Up @@ -40,6 +38,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
Loading
Loading