Skip to content

Commit

Permalink
copy secret in each namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Nov 2, 2023
1 parent 3f928ca commit 863717a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
29 changes: 28 additions & 1 deletion internal/controllers/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *Reconciler) setupTLS(ctx context.Context, cr *model.CryostatInstance) (
}

// Create CA certificate for Cryostat using the self-signed issuer
caCert := resources.NewCryostatCACert(cr)
caCert := resources.NewCryostatCACert(cr, cr.InstallNamespace)
err = r.createOrUpdateCertificate(ctx, caCert, cr.Object)
if err != nil {
return nil, err
Expand Down Expand Up @@ -122,6 +122,33 @@ func (r *Reconciler) setupTLS(ctx context.Context, cr *model.CryostatInstance) (
return nil, err
}

// Copy Cryostat CA secret in each target namespace
for _, ns := range cr.TargetNamespaces {
secret, err := r.GetCertificateSecret(ctx, caCert)
if err != nil {
return nil, err
}
namespaceSecret := secret.DeepCopy()
namespaceSecret.Namespace = ns
}
// Delete any Cryostat CA secrets in target namespaces that are no longer requested
for _, ns := range toDelete(cr) {
secret, err := r.GetCertificateSecret(ctx, caCert)
if err != nil {
return nil, err
}
namespaceSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secret.Name,
Namespace: ns,
},
}
err = r.deleteSecret(ctx, namespaceSecret)
if err != nil {
return nil, err
}
}

// Get the Cryostat CA certificate bytes from certificate secret
caBytes, err := r.getCertficateBytes(ctx, caCert)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions internal/controllers/clustercryostat_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ var _ = Describe("ClusterCryostatController", func() {
t.expectRBAC()
})

It("should create Cryostat CA Certificate in each namespace", func() {
t.expectCertificates()
})

It("should update the target namespaces in Status", func() {
t.expectTargetNamespaces()
})
Expand Down Expand Up @@ -94,6 +98,15 @@ var _ = Describe("ClusterCryostatController", func() {
Expect(err).ToNot(BeNil())
Expect(errors.IsNotFound(err)).To(BeTrue())
})
It("leave Cryostat CA Certificate for the first namespace", func() {
t.expectCertificates()
})
It("should remove Cryostat CA Certificate from the second namespace", func() {
certificate := t.NewCACert(targetNamespaces[1])
err := t.Client.Get(context.Background(), types.NamespacedName{Name: certificate.Name, Namespace: certificate.Namespace}, certificate)
Expect(err).ToNot(BeNil())
Expect(errors.IsNotFound(err)).To(BeTrue())
})
It("should update the target namespaces in Status", func() {
t.expectTargetNamespaces()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func NewCryostatCAIssuer(cr *model.CryostatInstance) *certv1.Issuer {
}
}

func NewCryostatCACert(cr *model.CryostatInstance) *certv1.Certificate {
func NewCryostatCACert(cr *model.CryostatInstance, namespace string) *certv1.Certificate {
return &certv1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name + "-ca",
Namespace: cr.InstallNamespace,
Namespace: namespace,
},
Spec: certv1.CertificateSpec{
CommonName: fmt.Sprintf("ca.%s.cert-manager", cr.Name),
Expand Down
14 changes: 12 additions & 2 deletions internal/controllers/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,7 +1838,7 @@ func (c *controllerTest) commonTests() {
})

It("should fail to reconcile", func() {
t.expectAlreadyOwnedError(reconcileErr, "RoleBinding", t.NewRoleBinding(nsInput.Namespace), otherInput)
t.expectAlreadyOwnedError(reconcileErr, "Certificate", t.NewCACert(nsInput.Namespace), otherInput)
})

It("should emit a CryostatNameConflict event", func() {
Expand Down Expand Up @@ -2267,7 +2267,7 @@ func (t *cryostatTestInput) expectWaitingForCertificate() {

func (t *cryostatTestInput) expectCertificates() {
// Check certificates
certs := []*certv1.Certificate{t.NewCryostatCert(), t.NewCACert(), t.NewReportsCert()}
certs := []*certv1.Certificate{t.NewCryostatCert(), t.NewCACert(t.Namespace), t.NewReportsCert()}
if !t.Minimal {
certs = append(certs, t.NewGrafanaCert())
} else {
Expand All @@ -2284,6 +2284,16 @@ func (t *cryostatTestInput) expectCertificates() {
t.checkMetadata(actual, expected)
Expect(actual.Spec).To(Equal(expected.Spec))
}
// Check for Cryostat CA Certificate in each target namespace
Expect(t.TargetNamespaces).ToNot(BeEmpty()) // Sanity check for tests
for _, ns := range t.TargetNamespaces {
actual := &certv1.Certificate{}
expected := t.NewCACert(ns)
err := t.Client.Get(context.Background(), types.NamespacedName{Name: expected.Name, Namespace: expected.Namespace}, actual)
Expect(err).ToNot(HaveOccurred())
t.checkMetadata(actual, expected)
Expect(actual.Spec).To(Equal(expected.Spec))
}
// Check issuers as well
issuers := []*certv1.Issuer{t.NewSelfSignedIssuer(), t.NewCryostatCAIssuer()}
for _, expected := range issuers {
Expand Down
2 changes: 1 addition & 1 deletion internal/test/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *testClient) makeCertificatesReady(ctx context.Context, obj runtime.Obje
// If this object is one of the operator-managed certificates, mock the behaviour
// of cert-manager processing those certificates
cert, ok := obj.(*certv1.Certificate)
if ok && c.matchesName(cert, c.NewCryostatCert(), c.NewCACert(), c.NewGrafanaCert(), c.NewReportsCert()) &&
if ok && c.matchesName(cert, c.NewCryostatCert(), c.NewCACert(c.Namespace), c.NewGrafanaCert(), c.NewReportsCert()) &&
len(cert.Status.Conditions) == 0 {
// Create certificate secret
c.createCertSecret(ctx, cert)
Expand Down
4 changes: 2 additions & 2 deletions internal/test/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,11 +1063,11 @@ func (r *TestResources) NewReportsCert() *certv1.Certificate {
}
}

func (r *TestResources) NewCACert() *certv1.Certificate {
func (r *TestResources) NewCACert(ns string) *certv1.Certificate {
return &certv1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: r.Name + "-ca",
Namespace: r.Namespace,
Namespace: ns,
},
Spec: certv1.CertificateSpec{
CommonName: fmt.Sprintf("ca.%s.cert-manager", r.Name),
Expand Down

0 comments on commit 863717a

Please sign in to comment.