Skip to content

Commit 2a7fde6

Browse files
authored
Merge pull request #37 from cert-manager/add_ignore_options
Add functions to ignore certain issuers and/or CertificateRequests, Kubernetes CSRs
2 parents 57e6d60 + c1a28da commit 2a7fde6

File tree

5 files changed

+86
-8
lines changed

5 files changed

+86
-8
lines changed

controllers/certificaterequest_controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ type CertificateRequestReconciler struct {
6262
client.Client
6363
// Sign connects to a CA and returns a signed certificate for the supplied CertificateRequest.
6464
signer.Sign
65+
// IgnoreCertificateRequest is an optional function that can prevent the CertificateRequest
66+
// and Kubernetes CSR controllers from reconciling a CertificateRequest resource.
67+
signer.IgnoreCertificateRequest
6568

6669
// EventRecorder is used for creating Kubernetes events on resources.
6770
EventRecorder record.EventRecorder
@@ -166,6 +169,17 @@ func (r *CertificateRequestReconciler) reconcileStatusPatch(
166169
return result, nil, nil // done
167170
}
168171

172+
if r.IgnoreCertificateRequest != nil {
173+
ignore, err := r.IgnoreCertificateRequest(ctx, signer.CertificateRequestObjectFromCertificateRequest(&cr), issuerGvk, issuerName)
174+
if err != nil {
175+
return result, nil, fmt.Errorf("failed to check if CertificateRequest should be ignored: %v", err) // retry
176+
}
177+
if ignore {
178+
logger.V(1).Info("Ignoring CertificateRequest")
179+
return result, nil, nil // done
180+
}
181+
}
182+
169183
// We now have a CertificateRequest that belongs to us so we are responsible
170184
// for updating its Status.
171185
crStatusPatch = &cmapi.CertificateRequestStatus{}

controllers/certificatesigningrequest_controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type CertificateSigningRequestReconciler struct {
6464
client.Client
6565
// Sign connects to a CA and returns a signed certificate for the supplied CertificateRequest.
6666
signer.Sign
67+
// IgnoreCertificateRequest is an optional function that can prevent the CertificateRequest
68+
// and Kubernetes CSR controllers from reconciling a CertificateRequest resource.
69+
signer.IgnoreCertificateRequest
6770

6871
// EventRecorder is used for creating Kubernetes events on resources.
6972
EventRecorder record.EventRecorder
@@ -150,6 +153,17 @@ func (r *CertificateSigningRequestReconciler) reconcileStatusPatch(
150153
return result, nil, nil // done
151154
}
152155

156+
if r.IgnoreCertificateRequest != nil {
157+
ignore, err := r.IgnoreCertificateRequest(ctx, signer.CertificateRequestObjectFromCertificateSigningRequest(&csr), issuerGvk, issuerName)
158+
if err != nil {
159+
return result, nil, fmt.Errorf("failed to check if CertificateSigningRequest should be ignored: %v", err) // retry
160+
}
161+
if ignore {
162+
logger.V(1).Info("Ignoring CertificateSigningRequest")
163+
return result, nil, nil // done
164+
}
165+
}
166+
153167
// We now have a CertificateSigningRequestStatus that belongs to us so we are responsible
154168
// for updating its Status.
155169
csrStatusPatch = &certificatesv1.CertificateSigningRequestStatus{}

controllers/combined_controller.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ type CombinedController struct {
4545
// Sign connects to a CA and returns a signed certificate for the supplied CertificateRequest.
4646
signer.Sign
4747

48+
// IgnoreCertificateRequest is an optional function that can prevent the CertificateRequest
49+
// and Kubernetes CSR controllers from reconciling a CertificateRequest resource.
50+
signer.IgnoreCertificateRequest
51+
// IgnoreIssuer is an optional function that can prevent the issuer controllers from
52+
// reconciling an issuer resource.
53+
signer.IgnoreIssuer
54+
4855
// EventRecorder is used for creating Kubernetes events on resources.
4956
EventRecorder record.EventRecorder
5057

@@ -79,6 +86,7 @@ func (r *CombinedController) SetupWithManager(ctx context.Context, mgr ctrl.Mana
7986

8087
Client: cl,
8188
Check: r.Check,
89+
IgnoreIssuer: r.IgnoreIssuer,
8290
EventRecorder: r.EventRecorder,
8391
Clock: r.Clock,
8492

@@ -96,10 +104,11 @@ func (r *CombinedController) SetupWithManager(ctx context.Context, mgr ctrl.Mana
96104
MaxRetryDuration: r.MaxRetryDuration,
97105
EventSource: eventSource,
98106

99-
Client: cl,
100-
Sign: r.Sign,
101-
EventRecorder: r.EventRecorder,
102-
Clock: r.Clock,
107+
Client: cl,
108+
Sign: r.Sign,
109+
IgnoreCertificateRequest: r.IgnoreCertificateRequest,
110+
EventRecorder: r.EventRecorder,
111+
Clock: r.Clock,
103112

104113
SetCAOnCertificateRequest: r.SetCAOnCertificateRequest,
105114

@@ -116,10 +125,11 @@ func (r *CombinedController) SetupWithManager(ctx context.Context, mgr ctrl.Mana
116125
MaxRetryDuration: r.MaxRetryDuration,
117126
EventSource: eventSource,
118127

119-
Client: cl,
120-
Sign: r.Sign,
121-
EventRecorder: r.EventRecorder,
122-
Clock: r.Clock,
128+
Client: cl,
129+
Sign: r.Sign,
130+
IgnoreCertificateRequest: r.IgnoreCertificateRequest,
131+
EventRecorder: r.EventRecorder,
132+
Clock: r.Clock,
123133

124134
PostSetupWithManager: r.PostSetupWithManager,
125135
}).SetupWithManager(ctx, mgr); err != nil {

controllers/issuer_controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type IssuerReconciler struct {
5656
client.Client
5757
// Check connects to a CA and checks if it is available
5858
signer.Check
59+
// IgnoreIssuer is an optional function that can prevent the issuer controllers from
60+
// reconciling an issuer resource.
61+
signer.IgnoreIssuer
5962

6063
// EventRecorder is used for creating Kubernetes events on resources.
6164
EventRecorder record.EventRecorder
@@ -125,6 +128,17 @@ func (r *IssuerReconciler) reconcileStatusPatch(
125128
return result, nil, nil // done
126129
}
127130

131+
if r.IgnoreIssuer != nil {
132+
ignore, err := r.IgnoreIssuer(ctx, issuer)
133+
if err != nil {
134+
return result, nil, fmt.Errorf("failed to check if issuer should be ignored: %v", err) // retry
135+
}
136+
if ignore {
137+
logger.V(1).Info("Ignoring issuer")
138+
return result, nil, nil // done
139+
}
140+
}
141+
128142
// We now have a Issuer that belongs to us so we are responsible
129143
// for updating its Status.
130144
issuerStatusPatch = &v1alpha1.IssuerStatus{}

controllers/signer/interface.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
2525
"github.com/cert-manager/cert-manager/pkg/util/pki"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/runtime/schema"
28+
"k8s.io/apimachinery/pkg/types"
2729

2830
"github.com/cert-manager/issuer-lib/api/v1alpha1"
2931
)
@@ -60,3 +62,27 @@ type CertificateRequestObject interface {
6062

6163
GetConditions() []cmapi.CertificateRequestCondition
6264
}
65+
66+
// IgnoreIssuer is an optional function that can prevent the issuer controllers from
67+
// reconciling an issuer resource. By default, the controllers will reconcile all
68+
// issuer resources that match the owned types.
69+
// This function will be called by the issuer reconcile loops for each type that matches
70+
// the owned types. If the function returns true, the controller will not reconcile the
71+
// issuer resource.
72+
type IgnoreIssuer func(
73+
ctx context.Context,
74+
issuerObject v1alpha1.Issuer,
75+
) (bool, error)
76+
77+
// IgnoreCertificateRequest is an optional function that can prevent the CertificateRequest
78+
// and Kubernetes CSR controllers from reconciling a CertificateRequest resource. By default,
79+
// the controllers will reconcile all CertificateRequest resources that match the issuerRef type.
80+
// This function will be called by the CertificateRequest reconcile loop and the Kubernetes CSR
81+
// reconcile loop for each type that matches the issuerRef type. If the function returns true,
82+
// the controller will not reconcile the CertificateRequest resource.
83+
type IgnoreCertificateRequest func(
84+
ctx context.Context,
85+
cr CertificateRequestObject,
86+
issuerGvk schema.GroupVersionKind,
87+
issuerName types.NamespacedName,
88+
) (bool, error)

0 commit comments

Comments
 (0)