-
Notifications
You must be signed in to change notification settings - Fork 149
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
K8SPSMDB-491: Support existing cert-manager issuer #1479
Merged
+233
−4
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dcde16e
Add tls.issuerConf.
inelpandzic 7f94b4b
Use issuerConf values.
inelpandzic 60179c7
Add unit tests.
inelpandzic 6746951
Update pkg/apis/psmdb/v1/psmdb_types.go
inelpandzic f106310
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
inelpandzic 71a705b
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
inelpandzic 192cb43
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
inelpandzic fe783d9
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
hors 1371be3
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
inelpandzic 907db45
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
inelpandzic b7cc547
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
inelpandzic ba5765a
Merge branch 'main' into K8SPSMDB-491-cm-issuer-conf-support
hors File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package tls | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
cm "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" | ||
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" // nolint | ||
|
||
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" | ||
) | ||
|
||
func TestCreateIssuer(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
customIssuerName := "issuer-conf-name" | ||
|
||
cr := &api.PerconaServerMongoDB{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "psmdb-mock", Namespace: "psmdb"}, | ||
Spec: api.PerconaServerMongoDBSpec{ | ||
CRVersion: "1.16.0", | ||
TLS: &api.TLSSpec{ | ||
IssuerConf: &cmmeta.ObjectReference{ | ||
Name: customIssuerName, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
r := buildFakeClient(cr) | ||
|
||
issuer := &cm.Issuer{} | ||
|
||
t.Run("Create issuer with custom name", func(t *testing.T) { | ||
if err := r.CreateIssuer(ctx, cr); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err := r.cl.Get(ctx, types.NamespacedName{Namespace: "psmdb", Name: customIssuerName}, issuer) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if issuer.Name != customIssuerName { | ||
t.Fatalf("Expected issuer name %s, got %s", customIssuerName, issuer.Name) | ||
} | ||
}) | ||
|
||
t.Run("Create issuer with default name", func(t *testing.T) { | ||
cr.Spec.CRVersion = "1.15.0" | ||
if err := r.CreateIssuer(ctx, cr); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err := r.cl.Get(ctx, types.NamespacedName{Namespace: "psmdb", Name: issuerName(cr)}, issuer) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if issuer.Name != issuerName(cr) { | ||
t.Fatalf("Expected issuer name %s, got %s", issuerName(cr), issuer.Name) | ||
} | ||
}) | ||
} | ||
|
||
func TestCreateCertificate(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
customIssuerName := "issuer-conf-name" | ||
customIssuerKind := "issuer-conf-kind" | ||
customIssuerGroup := "issuer-conf-group" | ||
|
||
cr := &api.PerconaServerMongoDB{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "psmdb-mock", Namespace: "psmdb"}, | ||
Spec: api.PerconaServerMongoDBSpec{ | ||
CRVersion: "1.16.0", | ||
Secrets: &api.SecretsSpec{ | ||
SSL: "ssl", | ||
}, | ||
TLS: &api.TLSSpec{ | ||
IssuerConf: &cmmeta.ObjectReference{ | ||
Name: customIssuerName, | ||
Kind: customIssuerKind, | ||
Group: customIssuerGroup, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
r := buildFakeClient(cr) | ||
|
||
cert := &cm.Certificate{} | ||
|
||
t.Run("Create certificate with custom issuer name", func(t *testing.T) { | ||
if err := r.CreateCertificate(ctx, cr, false); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err := r.cl.Get(ctx, types.NamespacedName{Namespace: "psmdb", Name: certificateName(cr, false)}, cert) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cert.Spec.IssuerRef.Name != customIssuerName { | ||
t.Fatalf("Expected issuer name %s, got %s", customIssuerName, cert.Spec.IssuerRef.Name) | ||
} | ||
}) | ||
|
||
t.Run("Create certificate with default issuer name", func(t *testing.T) { | ||
cr.Name = "psmdb-mock-1" | ||
cr.Spec.CRVersion = "1.15.0" | ||
|
||
if err := r.CreateCertificate(ctx, cr, false); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err := r.cl.Get(ctx, types.NamespacedName{Namespace: "psmdb", Name: certificateName(cr, false)}, cert) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cert.Spec.IssuerRef.Name != issuerName(cr) { | ||
t.Fatalf("Expected issuer name %s, got %s", issuerName(cr), cert.Spec.IssuerRef.Name) | ||
} | ||
}) | ||
} | ||
|
||
// creates a fake client to mock API calls with the mock objects | ||
func buildFakeClient(objs ...client.Object) *CertManagerController { | ||
s := scheme.Scheme | ||
|
||
s.AddKnownTypes(api.SchemeGroupVersion, | ||
new(api.PerconaServerMongoDB), | ||
new(cm.Issuer), | ||
new(cm.Certificate), | ||
) | ||
|
||
cl := fake.NewClientBuilder().WithScheme(s).WithObjects(objs...).WithStatusSubresource(objs...).Build() | ||
|
||
return &CertManagerController{ | ||
cl: cl, | ||
scheme: s, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@inelpandzic maybe we need to have e2e test for it. We have it in PXC. Just copy a logic. @tplavcic what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hors I think the reason for have it in e2e in PXC is probably because we either couldn't test it easily with unit tests or just didn't consider it. I don't think we need to have it as a e2e test and that way just make our tests even longer.
Unit test for this case is perfectly good IMO.