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

add support for specifying revisionHistoryLimit for the generated Certificate #104

Merged
merged 1 commit into from
Jun 17, 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
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
fs.String("service-name", "", "NamespacedName of the Contour LoadBalancer Service")
fs.String("default-issuer-name", "", "Issuer name used by default")
fs.String("default-issuer-kind", controllers.ClusterIssuerKind, "Issuer kind used by default")
fs.Uint("csr-revision-limit", 0, "Maximum number of CertificateRequest revisions to keep")
fs.String("ingress-class-name", "", "Ingress class name that watched by Contour Plus. If not specified, then all classes are watched")
fs.Bool("leader-election", true, "Enable/disable leader election")
if err := viper.BindPFlags(fs); err != nil {
Expand Down Expand Up @@ -65,6 +66,7 @@ In addition to flags, the following environment variables are read:
CP_SERVICE_NAME NamespacedName of the Contour LoadBalancer Service
CP_DEFAULT_ISSUER_NAME Issuer name used by default
CP_DEFAULT_ISSUER_KIND Issuer kind used by default
CP_CSR_REVISION_LIMIT Maximum number of CertificateRequest revisions to keep
CP_LEADER_ELECTION Disable leader election if set to "false"
CP_INGRESS_CLASS_NAME Ingress class name that watched by Contour Plus. If not specified, then all classes are watched`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func run() error {

opts.IngressClassName = viper.GetString("ingress-class-name")

opts.CSRRevisionLimit = viper.GetUint("csr-revision-limit")

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
Expand Down
17 changes: 12 additions & 5 deletions controllers/httpproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type HTTPProxyReconciler struct {
Prefix string
DefaultIssuerName string
DefaultIssuerKind string
CSRRevisionLimit uint
CreateDNSEndpoint bool
CreateCertificate bool
IngressClassName string
Expand Down Expand Up @@ -217,11 +218,7 @@ func (r *HTTPProxyReconciler) reconcileCertificate(ctx context.Context, hp *proj
return nil
}

obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(certManagerGroupVersion.WithKind(CertificateKind))
obj.SetName(r.Prefix + hp.Name)
obj.SetNamespace(hp.Namespace)
obj.UnstructuredContent()["spec"] = map[string]interface{}{
certificateSpec := map[string]interface{}{
"dnsNames": []string{vh.Fqdn},
"secretName": vh.TLS.SecretName,
"commonName": vh.Fqdn,
Expand All @@ -236,6 +233,16 @@ func (r *HTTPProxyReconciler) reconcileCertificate(ctx context.Context, hp *proj
usageClientAuth,
},
}

if r.CSRRevisionLimit > 0 {
certificateSpec["revisionHistoryLimit"] = r.CSRRevisionLimit
}

obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(certManagerGroupVersion.WithKind(CertificateKind))
obj.SetName(r.Prefix + hp.Name)
obj.SetNamespace(hp.Namespace)
obj.UnstructuredContent()["spec"] = certificateSpec
err := ctrl.SetControllerReference(hp, obj, r.Scheme)
if err != nil {
return err
Expand Down
47 changes: 47 additions & 0 deletions controllers/httpproxy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func testHTTPProxyReconcile() {
usageServerAuth,
usageClientAuth,
}))
Expect(crtSpec["revisionHistoryLimit"]).Should(BeNil())
})

It(`should not create DNSEndpoint and Certificate if "contour-plus.cybozu.com/exclude"" is "true"`, func() {
Expand Down Expand Up @@ -536,6 +537,52 @@ func testHTTPProxyReconcile() {
return k8sClient.Get(context.Background(), objKey, crt)
}, 5*time.Second).Should(Succeed())
})

It(`should create Certificate with revisionHistoryLimit set if specified`, func() {
ns := testNamespacePrefix + randomString(10)
Expect(k8sClient.Create(context.Background(), &corev1.Namespace{
ObjectMeta: ctrl.ObjectMeta{Name: ns},
})).ShouldNot(HaveOccurred())

scm, mgr := setupManager()

Expect(SetupReconciler(mgr, scm, ReconcilerOptions{
ServiceKey: testServiceKey,
DefaultIssuerName: "test-issuer",
DefaultIssuerKind: IssuerKind,
CreateCertificate: true,
CSRRevisionLimit: 1,
})).ShouldNot(HaveOccurred())

stopMgr := startTestManager(mgr)
defer stopMgr()

By("creating HTTPProxy")
hpKey := client.ObjectKey{Name: "foo", Namespace: ns}
Expect(k8sClient.Create(context.Background(), newDummyHTTPProxy(hpKey))).ShouldNot(HaveOccurred())

By("getting Certificate")
crt := certificate()
objKey := client.ObjectKey{
Name: hpKey.Name,
Namespace: hpKey.Namespace,
}
Eventually(func() error {
return k8sClient.Get(context.Background(), objKey, crt)
}).Should(Succeed())

crtSpec := crt.UnstructuredContent()["spec"].(map[string]interface{})
Expect(crtSpec["dnsNames"]).Should(Equal([]interface{}{dnsName}))
Expect(crtSpec["secretName"]).Should(Equal(testSecretName))
Expect(crtSpec["commonName"]).Should(Equal(dnsName))
Expect(crtSpec["usages"]).Should(Equal([]interface{}{
usageDigitalSignature,
usageKeyEncipherment,
usageServerAuth,
usageClientAuth,
}))
Expect(crtSpec["revisionHistoryLimit"]).Should(Equal(int64(1)))
})
}

func newDummyHTTPProxy(hpKey client.ObjectKey) *projectcontourv1.HTTPProxy {
Expand Down
2 changes: 2 additions & 0 deletions controllers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ReconcilerOptions struct {
Prefix string
DefaultIssuerName string
DefaultIssuerKind string
CSRRevisionLimit uint
CreateDNSEndpoint bool
CreateCertificate bool
IngressClassName string
Expand All @@ -40,6 +41,7 @@ func SetupReconciler(mgr manager.Manager, scheme *runtime.Scheme, opts Reconcile
Prefix: opts.Prefix,
DefaultIssuerName: opts.DefaultIssuerName,
DefaultIssuerKind: opts.DefaultIssuerKind,
CSRRevisionLimit: opts.CSRRevisionLimit,
CreateDNSEndpoint: opts.CreateDNSEndpoint,
CreateCertificate: opts.CreateCertificate,
IngressClassName: opts.IngressClassName,
Expand Down