diff --git a/pkg/controller/common/license/match_test.go b/pkg/controller/common/license/match_test.go index 26ab45a2f9..a394f1ce29 100644 --- a/pkg/controller/common/license/match_test.go +++ b/pkg/controller/common/license/match_test.go @@ -225,14 +225,13 @@ func Test_bestMatchAt(t *testing.T) { Type: LicenseTypeEnterpriseTrial, Issuer: "API", ClusterLicenses: []ElasticsearchLicense{ - {License: license(twelveMonth, platinum)}, - {License: license(twoMonth, gold)}, + {License: license(twelveMonth, trial)}, }, }, }, }, }, - want: license(twelveMonth, platinum), + want: license(twelveMonth, trial), wantFound: true, }, { diff --git a/pkg/controller/elasticsearch/license/apply.go b/pkg/controller/elasticsearch/license/apply.go index 2035b6f036..86af49e317 100644 --- a/pkg/controller/elasticsearch/license/apply.go +++ b/pkg/controller/elasticsearch/license/apply.go @@ -28,6 +28,13 @@ func isTrial(l esclient.License) bool { return l.Type == string(esclient.ElasticsearchLicenseTypeTrial) } +// isECKManagedTrial returns true if this is a trial started via the internal trial mechanism in the operator. We use an +// empty license with only the type field populated to indicate to the Elasticsearch controller that a self-generated +// trial in Elasticsearch should be started. This can be done only once. +func isECKManagedTrial(l esclient.License) bool { + return isTrial(l) && l.Signature == "" && l.UID == "" && l.ExpiryDateInMillis == 0 && l.StartDateInMillis == 0 +} + // isBasic returns true if an Elasticsearch license is of the basic type func isBasic(l esclient.License) bool { return l.Type == string(esclient.ElasticsearchLicenseTypeBasic) @@ -117,7 +124,8 @@ func updateLicense( }, } - if isTrial(desired) { + if isECKManagedTrial(desired) { + // start a self-generated trial in Elasticsearch, this can only be done once. return pkgerrors.Wrap(startTrial(ctx, updater, esCluster), "failed to start trial") } diff --git a/test/e2e/es/license_test.go b/test/e2e/es/license_test.go index 5832ce357d..e48b5c37c0 100644 --- a/test/e2e/es/license_test.go +++ b/test/e2e/es/license_test.go @@ -165,8 +165,8 @@ func TestEnterpriseTrialExtension(t *testing.T) { licenseTestContext.Init(), licenseTestContext.CheckElasticsearchLicense(client.ElasticsearchLicenseTypeTrial), licenseTestContext.CheckEnterpriseTrialLicenseValid(trialSecretName), - // simulate a trial extension - licenseTestContext.CreateTrialExtension(licenseSecretName, privateKey.(*rsa.PrivateKey)), + // simulate a trial extension: old style with Enterprise Elasticsearch license + licenseTestContext.CreateTrialExtension(licenseSecretName, privateKey.(*rsa.PrivateKey), client.ElasticsearchLicenseTypeEnterprise), licenseTestContext.CheckElasticsearchLicense( client.ElasticsearchLicenseTypePlatinum, // depends on ES version client.ElasticsearchLicenseTypeEnterprise, @@ -174,11 +174,10 @@ func TestEnterpriseTrialExtension(t *testing.T) { // revert to basic again licenseTestContext.DeleteAllEnterpriseLicenseSecrets(), licenseTestContext.CheckElasticsearchLicense(client.ElasticsearchLicenseTypeBasic), - // repeatedly extending a trial is possible - licenseTestContext.CreateTrialExtension(licenseSecretName, privateKey.(*rsa.PrivateKey)), + // repeatedly extending a trial is possible: new style with Elasticsearch trial license + licenseTestContext.CreateTrialExtension(licenseSecretName, privateKey.(*rsa.PrivateKey), client.ElasticsearchLicenseTypeTrial), licenseTestContext.CheckElasticsearchLicense( - client.ElasticsearchLicenseTypePlatinum, // depends on ES version - client.ElasticsearchLicenseTypeEnterprise, + client.ElasticsearchLicenseTypeTrial, ), // cleanup license for the next tests licenseTestContext.DeleteAllEnterpriseLicenseSecrets(), diff --git a/test/e2e/test/elasticsearch/license_fixture.go b/test/e2e/test/elasticsearch/license_fixture.go index 0175a17121..e6f28e9b2f 100644 --- a/test/e2e/test/elasticsearch/license_fixture.go +++ b/test/e2e/test/elasticsearch/license_fixture.go @@ -32,33 +32,43 @@ type licenseSpec struct { } func (e ESLicense) SignableContentBytes() ([]byte, error) { - return json.Marshal(licenseSpec{ + spec := licenseSpec{ UID: e.UID, LicenseType: e.Type, IssueDateInMillis: e.IssueDateInMillis, ExpiryDateInMillis: e.ExpiryDateInMillis, - MaxNodes: nil, // assume v5 license w/ ERU + MaxNodes: &e.MaxNodes, MaxResourceUnits: e.MaxResourceUnits, IssuedTo: e.IssuedTo, Issuer: e.Issuer, StartDateInMillis: e.StartDateInMillis, - }) + } + + // v3 and v5 are handling maxNodes differently: v3 requires max_nodes v5 does not tolerate any other value than null + if e.MaxNodes == 0 { + spec.MaxNodes = nil + } + + return json.Marshal(spec) } func (e ESLicense) Version() int { - return 5 // we only test the new enterprise capable license version + if e.Type == string(client.ElasticsearchLicenseTypeTrial) { + return 3 // we either test trials + } + return 5 // or the new enterprise capable license version } var _ license.Signable = &ESLicense{} -func GenerateTestLicense(signer *license.Signer) (client.License, error) { +func GenerateTestLicense(signer *license.Signer, typ client.ElasticsearchLicenseType) (client.License, error) { uuid, err := uuid.NewUUID() if err != nil { return client.License{}, err } licenseSpec := client.License{ UID: uuid.String(), - Type: "enterprise", + Type: string(typ), IssueDateInMillis: chrono.ToMillis(time.Now().Add(-3 * 24 * time.Hour)), ExpiryDateInMillis: chrono.ToMillis(time.Now().Add(30 * 24 * time.Hour)), MaxResourceUnits: 100, @@ -66,6 +76,11 @@ func GenerateTestLicense(signer *license.Signer) (client.License, error) { Issuer: "ECK e2e job", StartDateInMillis: chrono.ToMillis(time.Now().Add(-3 * 24 * time.Hour)), } + if typ == client.ElasticsearchLicenseTypeTrial { + licenseSpec.MaxResourceUnits = 0 + licenseSpec.MaxNodes = 100 + } + sign, err := signer.Sign(ESLicense{License: licenseSpec}) if err != nil { return client.License{}, err diff --git a/test/e2e/test/elasticsearch/steps_license.go b/test/e2e/test/elasticsearch/steps_license.go index a44df5d226..c6b53b1d8e 100644 --- a/test/e2e/test/elasticsearch/steps_license.go +++ b/test/e2e/test/elasticsearch/steps_license.go @@ -84,13 +84,13 @@ func (ltctx *LicenseTestContext) CreateEnterpriseLicenseSecret(secretName string } } -func (ltctx *LicenseTestContext) CreateTrialExtension(secretName string, privateKey *rsa.PrivateKey) test.Step { +func (ltctx *LicenseTestContext) CreateTrialExtension(secretName string, privateKey *rsa.PrivateKey, esLicenseType client.ElasticsearchLicenseType) test.Step { //nolint:thelper return test.Step{ Name: "Creating a trial extension secret", Test: func(t *testing.T) { signer := license.NewSigner(privateKey) - clusterLicense, err := GenerateTestLicense(signer) + clusterLicense, err := GenerateTestLicense(signer, esLicenseType) require.NoError(t, err) trialExtension := license.EnterpriseLicense{ License: license.LicenseSpec{