/
combined_controller.go
180 lines (144 loc) · 6.09 KB
/
combined_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 2023 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/controller"
v1alpha1 "github.com/cert-manager/issuer-lib/api/v1alpha1"
"github.com/cert-manager/issuer-lib/controllers/signer"
"github.com/cert-manager/issuer-lib/internal/kubeutil"
)
type CombinedController struct {
IssuerTypes []v1alpha1.Issuer
ClusterIssuerTypes []v1alpha1.Issuer
FieldOwner string
MaxRetryDuration time.Duration
// Check connects to a CA and checks if it is available
signer.Check
// Sign connects to a CA and returns a signed certificate for the supplied CertificateRequest.
signer.Sign
// IgnoreCertificateRequest is an optional function that can prevent the CertificateRequest
// and Kubernetes CSR controllers from reconciling a CertificateRequest resource.
signer.IgnoreCertificateRequest
// IgnoreIssuer is an optional function that can prevent the issuer controllers from
// reconciling an issuer resource.
signer.IgnoreIssuer
// EventRecorder is used for creating Kubernetes events on resources.
EventRecorder record.EventRecorder
// Clock is used to mock condition transition times in tests.
Clock clock.PassiveClock
// SetCAOnCertificateRequest is used to enable setting the CA status field on
// the CertificateRequest resource. This is disabled by default.
// Deprecated: this option is for backwards compatibility only. The use of
// ca.crt is discouraged. Instead, the CA certificate should be provided
// separately using a tool such as trust-manager.
SetCAOnCertificateRequest bool
// DisableCertificateRequestController is used to disable the CertificateRequest
// controller. This controller is enabled by default.
// You should only disable this controller if you eg. don't want to rely on the cert-manager
// CRDs to be installed.
// Note: in the future, we might remove this option and always enable the CertificateRequest
// controller.
DisableCertificateRequestController bool
// DisableKubernetesCSRController is used to disable the Kubernetes CSR controller.
// This controller is enabled by default.
// You should only disable this controller if you really don't want to support signing
// Kubernetes CSRs.
// Note: in the future, we might remove this option and always enable the Kubernetes CSR
// controller.
DisableKubernetesCSRController bool
// PreSetupWithManager is an optional function that can be used to perform
// additional setup before the controller is built and registered with the
// manager.
PreSetupWithManager func(context.Context, schema.GroupVersionKind, ctrl.Manager, *builder.Builder) error
// PostSetupWithManager is an optional function that can be used to perform
// additional setup after the controller is built and registered with the
// manager.
PostSetupWithManager func(context.Context, schema.GroupVersionKind, ctrl.Manager, controller.Controller) error
}
func (r *CombinedController) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
var err error
cl := mgr.GetClient()
eventSource := kubeutil.NewEventStore()
if r.Clock == nil {
r.Clock = clock.RealClock{}
}
for _, issuerType := range append(r.IssuerTypes, r.ClusterIssuerTypes...) {
if err = (&IssuerReconciler{
ForObject: issuerType,
FieldOwner: r.FieldOwner,
EventSource: eventSource,
Client: cl,
Check: r.Check,
IgnoreIssuer: r.IgnoreIssuer,
EventRecorder: r.EventRecorder,
Clock: r.Clock,
PreSetupWithManager: r.PreSetupWithManager,
PostSetupWithManager: r.PostSetupWithManager,
}).SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("%T: %w", issuerType, err)
}
}
if r.DisableCertificateRequestController && r.DisableKubernetesCSRController {
return fmt.Errorf("both CertificateRequest and Kubernetes CSR controllers are disabled, must enable at least one")
}
if !r.DisableCertificateRequestController {
if err = (&CertificateRequestReconciler{
RequestController: RequestController{
IssuerTypes: r.IssuerTypes,
ClusterIssuerTypes: r.ClusterIssuerTypes,
FieldOwner: r.FieldOwner,
MaxRetryDuration: r.MaxRetryDuration,
EventSource: eventSource,
Client: cl,
Sign: r.Sign,
IgnoreCertificateRequest: r.IgnoreCertificateRequest,
EventRecorder: r.EventRecorder,
Clock: r.Clock,
PreSetupWithManager: r.PreSetupWithManager,
PostSetupWithManager: r.PostSetupWithManager,
},
SetCAOnCertificateRequest: r.SetCAOnCertificateRequest,
}).SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("CertificateRequestReconciler: %w", err)
}
}
if !r.DisableKubernetesCSRController {
if err = (&CertificateSigningRequestReconciler{
RequestController: RequestController{
IssuerTypes: r.IssuerTypes,
ClusterIssuerTypes: r.ClusterIssuerTypes,
FieldOwner: r.FieldOwner,
MaxRetryDuration: r.MaxRetryDuration,
EventSource: eventSource,
Client: cl,
Sign: r.Sign,
IgnoreCertificateRequest: r.IgnoreCertificateRequest,
EventRecorder: r.EventRecorder,
Clock: r.Clock,
PreSetupWithManager: r.PreSetupWithManager,
PostSetupWithManager: r.PostSetupWithManager,
},
}).SetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("CertificateRequestReconciler: %w", err)
}
}
return nil
}