Skip to content

Commit

Permalink
fix(api/types): generate DNS name default before creating Certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Oct 22, 2021
1 parent 9bc8dd6 commit eff76bc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
35 changes: 26 additions & 9 deletions api/v1alpha1/rpaasinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package v1alpha1

import "sort"
import (
"fmt"
"sort"
)

const (
teamOwnerLabel = "rpaas.extensions.tsuru.io/team-owner"
Expand All @@ -16,32 +19,46 @@ func (i *RpaasInstance) CertManagerRequests() (reqs []CertManager) {
return
}

uniqueCerts := make(map[string]CertManager)
uniqueCerts := make(map[string]*CertManager)
if req := i.Spec.DynamicCertificates.CertManager; req != nil {
uniqueCerts[req.Issuer] = *req
r := req.DeepCopy()
r.DNSNames = r.dnsNames(i)
uniqueCerts[r.Issuer] = r
}

for _, req := range i.Spec.DynamicCertificates.CertManagerRequests {
r, found := uniqueCerts[req.Issuer]
if found {
r.DNSNames = append(r.DNSNames, req.DNSNames...)
r.IPAddresses = append(r.IPAddresses, req.IPAddresses...)
uniqueCerts[req.Issuer] = r
if !found {
uniqueCerts[req.Issuer] = req.DeepCopy()
continue
}

uniqueCerts[req.Issuer] = req
r.DNSNames = append(r.DNSNames, req.dnsNames(i)...)
r.IPAddresses = append(r.IPAddresses, req.IPAddresses...)
}

for _, v := range uniqueCerts {
reqs = append(reqs, v)
reqs = append(reqs, *v)
}

sort.Slice(reqs, func(i, j int) bool { return reqs[i].Issuer < reqs[j].Issuer })

return
}

func (c *CertManager) dnsNames(i *RpaasInstance) (names []string) {
if c == nil {
return
}

names = append(names, c.DNSNames...)
if c.DNSNamesDefault && i.Spec.DNS != nil && i.Spec.DNS.Zone != "" {
names = append(names, fmt.Sprintf("%s.%s", i.Name, i.Spec.DNS.Zone))
}

return
}

func (i *RpaasInstance) SetTeamOwner(team string) {
newLabels := map[string]string{teamOwnerLabel: team}
i.appendNewLabels(newLabels)
Expand Down
28 changes: 20 additions & 8 deletions api/v1alpha1/rpaasinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,40 @@ func TestCertManagerRequests(t *testing.T) {
IPAddresses: []string{
"10.1.1.1",
},
DNSNamesDefault: true,
},
CertManagerRequests: []CertManager{
{
Issuer: "my-issuer",
DNSNames: []string{
"custom-domain.my-company.io",
},
IPAddresses: []string{
"10.1.1.2",
},
Issuer: "my-issuer",
DNSNames: []string{"custom-domain.my-company.io"},
IPAddresses: []string{"10.1.1.2"},
},
{
Issuer: "another-issuer",
DNSNames: []string{"www.example.com"},
IPAddresses: []string{"169.254.254.101"},
},
{
Issuer: "another-issuer",
DNSNames: []string{"web.example.com"},
IPAddresses: []string{"169.254.254.102"},
},
},
},
},
}

assert.Equal(t, []CertManager{
{
Issuer: "another-issuer",
DNSNames: []string{"www.example.com", "web.example.com"},
IPAddresses: []string{"169.254.254.101", "169.254.254.102"},
},
{
Issuer: "my-issuer",
DNSNames: []string{"default-domain.my-company.io", "custom-domain.my-company.io"},
IPAddresses: []string{"10.1.1.1", "10.1.1.2"},
DNSNamesDefault: false,
DNSNamesDefault: true,
},
}, instance.CertManagerRequests())

Expand Down
14 changes: 1 addition & 13 deletions internal/controllers/certificates/cert_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package certificates

import (
"context"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -145,17 +144,6 @@ func getCertificates(ctx context.Context, c client.Client, i *v1alpha1.RpaasInst
}

func newCertificate(instance *v1alpha1.RpaasInstance, issuer *cmmeta.ObjectReference, req v1alpha1.CertManager) (*cmv1.Certificate, error) {
dnsNames := req.DNSNames
if len(dnsNames) == 0 && req.DNSNamesDefault {
if instance.Spec.DNS == nil || instance.Spec.DNS.Zone == "" {
return nil, errors.New("DNS zone not provided")
}

dnsNames = []string{
fmt.Sprintf("%s.%s", instance.Name, instance.Spec.DNS.Zone),
}
}

return &cmv1.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", instance.Name, cmCertificateName(req)),
Expand All @@ -174,7 +162,7 @@ func newCertificate(instance *v1alpha1.RpaasInstance, issuer *cmmeta.ObjectRefer
},
Spec: cmv1.CertificateSpec{
IssuerRef: *issuer,
DNSNames: dnsNames,
DNSNames: req.DNSNames,
IPAddresses: req.IPAddresses,
SecretName: fmt.Sprintf("%s-%s", instance.Name, cmCertificateName(req)),
},
Expand Down

0 comments on commit eff76bc

Please sign in to comment.