Skip to content

Commit

Permalink
cmd: add name field using cert-manager to allow multiples instances
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Aug 8, 2024
1 parent 69dbd2e commit 774cf34
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/rpaasinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ type DynamicCertificates struct {
}

type CertManager struct {
// Name is recently introduced to allow multiple certificates in the same instance.
// +optional
Name string `json:"name"`

// Issuer refers either to Issuer or ClusterIssuer resource.
//
// NOTE: when there's no Issuer on this name, it tries using ClusterIssuer instead.
Expand Down
9 changes: 7 additions & 2 deletions cmd/plugin/rpaasv2/cmd/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func NewCmdUpdateCertitifcate() *cli.Command {
&cli.StringFlag{
Name: "name",
Usage: "an identifier for the current certificate and key",
Value: "default",
},
&cli.PathFlag{
Name: "certificate",
Expand Down Expand Up @@ -104,9 +103,14 @@ func runUpdateCertificate(c *cli.Context) error {
return err
}

name := c.String("name")
if name == "" {
name = "default"
}

args := rpaasclient.UpdateCertificateArgs{
Instance: c.String("instance"),
Name: c.String("name"),
Name: name,
Certificate: string(certificate),
Key: string(key),
}
Expand All @@ -131,6 +135,7 @@ func updateCertManagerCertificate(c *cli.Context, client rpaasclient.Client) (bo
err := client.UpdateCertManager(c.Context, rpaasclient.UpdateCertManagerArgs{
Instance: c.String("instance"),
CertManager: clientTypes.CertManager{
Name: c.String("name"),
Issuer: c.String("issuer"),
DNSNames: c.StringSlice("dns"),
IPAddresses: c.StringSlice("ip"),
Expand Down
37 changes: 37 additions & 0 deletions cmd/plugin/rpaasv2/cmd/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
expected: "certificate \"my-instance.example.com\" updated in my-instance\n",
},

{
name: "when UpdateCertificate with default name",
args: []string{"./rpaasv2", "certificates", "update", "-i", "my-instance", "--cert", certFile.Name(), "--key", keyFile.Name()},
client: &fake.FakeClient{
FakeUpdateCertificate: func(args rpaasclient.UpdateCertificateArgs) error {
expected := rpaasclient.UpdateCertificateArgs{
Instance: "my-instance",
Name: "default",
Certificate: certPem,
Key: keyPem,
}
assert.Equal(t, expected, args)
return nil
},
},
expected: "certificate \"default\" updated in my-instance\n",
},

{
name: "enabling cert-manager integration",
args: []string{"./rpaasv2", "certificates", "add", "-i", "my-instance", "--cert-manager", "--issuer", "lets-encrypt", "--dns", "my-instance.example.com", "--dns", "foo.example.com", "--ip", "169.196.100.100", "--ip", "2001:db8:dead:beef::"},
Expand All @@ -112,6 +130,25 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
expected: "cert manager certificate was updated\n",
},

{
name: "enabling cert-manager integration with name definition",
args: []string{"./rpaasv2", "certificates", "add", "-i", "my-instance", "--cert-manager", "--name", "cert01", "--issuer", "lets-encrypt", "--dns", "my-instance.example.com"},
client: &fake.FakeClient{
FakeUpdateCertManager: func(args rpaasclient.UpdateCertManagerArgs) error {
assert.Equal(t, rpaasclient.UpdateCertManagerArgs{
Instance: "my-instance",
CertManager: types.CertManager{
Name: "cert01",
Issuer: "lets-encrypt",
DNSNames: []string{"my-instance.example.com"},
},
}, args)
return nil
},
},
expected: "cert manager certificate was updated\n",
},

{
name: "passing DNS names without cert manager flag",
args: []string{"./rpaasv2", "certificates", "add", "-i", "my-instance", "--dns", "my-instance.example.com"},
Expand Down
3 changes: 3 additions & 0 deletions pkg/rpaas/client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ type CertManager struct {
Issuer string `json:"issuer"`
DNSNames []string `json:"dnsNames,omitempty"`
IPAddresses []string `json:"ipAddresses,omitempty"`

// Name is used to multiple certificates in the same instance, and also to take over the manual certificates
Name string `json:"name,omitempty"`
}

type Metadata struct {
Expand Down
3 changes: 0 additions & 3 deletions pkg/web/extra_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ func deleteExtraFiles(c echo.Context) error {
}
var files []string
err = c.Bind(&files)
if err != nil {
return err
}
if err != nil {
return &echo.HTTPError{
Code: http.StatusBadRequest,
Expand Down

0 comments on commit 774cf34

Please sign in to comment.