Skip to content
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

Use enterprise_trial as the trial license type #2934

Merged
merged 12 commits into from
Apr 23, 2020
8 changes: 6 additions & 2 deletions cmd/licensing-info/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"log"

Expand All @@ -25,7 +26,7 @@ import (
//
// Example of use:
//
// > go run cmd/licensing-info/main.go
// > go run cmd/licensing-info/main.go -operator-namespace <operator-namespace>
// {
// "timestamp": "2019-12-17T11:56:02+01:00",
// "license_level": "basic",
Expand All @@ -35,7 +36,10 @@ import (
//

func main() {
licensingInfo, err := license.NewResourceReporter(newK8sClient()).Get()
var operatorNamespace string
flag.StringVar(&operatorNamespace, "operator-namespace", "elastic-system", "indicates the namespace where the operator is deployed")
flag.Parse()
licensingInfo, err := license.NewResourceReporter(newK8sClient(), operatorNamespace).Get()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we need still need this tool.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, maybe open an issue to revisit keeping it in there? I'm not sure how valuable it is

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it because it was handy for testing the license reporting and in case someone wanted to get it in real time but it is not documented. So I don't really see any good reason to keep it.

if err != nil {
log.Fatal(err, "Failed to get licensing info")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ func execute() {
go func() {
time.Sleep(10 * time.Second) // wait some arbitrary time for the manager to start
mgr.GetCache().WaitForCacheSync(nil) // wait until k8s client cache is initialized
r := licensing.NewResourceReporter(mgr.GetClient())
r.Start(operatorNamespace, licensing.ResourceReporterFrequency)
r := licensing.NewResourceReporter(mgr.GetClient(), operatorNamespace)
r.Start(licensing.ResourceReporterFrequency)
}()

log.Info("Starting the manager", "uuid", operatorInfo.OperatorUUID,
Expand Down
6 changes: 3 additions & 3 deletions pkg/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ func (r LicensingResolver) ToInfo(totalMemory resource.Quantity) (LicensingInfo,
}

// Save updates or creates licensing information in a config map
func (r LicensingResolver) Save(info LicensingInfo, operatorNs string) error {
func (r LicensingResolver) Save(info LicensingInfo) error {
data, err := info.toMap()
if err != nil {
return err
}

log.V(1).Info("Saving", "namespace", operatorNs, "configmap_name", licensingCfgMapName, "license_info", info)
log.V(1).Info("Saving", "namespace", r.operatorNs, "configmap_name", licensingCfgMapName, "license_info", info)
cm := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: operatorNs,
Namespace: r.operatorNs,
Name: licensingCfgMapName,
Labels: map[string]string{
common.TypeLabelName: Type,
Expand Down
15 changes: 8 additions & 7 deletions pkg/license/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,44 @@ type ResourceReporter struct {
}

// NewResourceReporter returns a new ResourceReporter
func NewResourceReporter(client client.Client) ResourceReporter {
func NewResourceReporter(client client.Client, operatorNs string) ResourceReporter {
c := k8s.WrapClient(client)
return ResourceReporter{
aggregator: Aggregator{
client: c,
},
licensingResolver: LicensingResolver{
client: c,
client: c,
operatorNs: operatorNs,
Copy link
Collaborator Author

@pebrc pebrc Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was always "" up until now, which lead to errors now that we actually do trial license checks.

},
}
}

// Start starts to report the licensing information repeatedly at regular intervals
func (r ResourceReporter) Start(operatorNs string, refreshPeriod time.Duration) {
func (r ResourceReporter) Start(refreshPeriod time.Duration) {
// report once as soon as possible to not wait the first tick
err := r.Report(operatorNs)
err := r.Report()
if err != nil {
log.Error(err, "Failed to report licensing information")
}

ticker := time.NewTicker(refreshPeriod)
for range ticker.C {
err := r.Report(operatorNs)
err := r.Report()
if err != nil {
log.Error(err, "Failed to report licensing information")
}
}
}

// Report reports the licensing information in a config map
func (r ResourceReporter) Report(operatorNs string) error {
func (r ResourceReporter) Report() error {
licensingInfo, err := r.Get()
if err != nil {
return err
}

return r.licensingResolver.Save(licensingInfo, operatorNs)
return r.licensingResolver.Save(licensingInfo)
}

// Get aggregates managed resources and returns the licensing information
Expand Down
19 changes: 10 additions & 9 deletions pkg/license/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"k8s.io/apimachinery/pkg/types"
)

const operatorNs = "test-system"

func Test_Get(t *testing.T) {
es := esv1.Elasticsearch{
Spec: esv1.ElasticsearchSpec{
Expand All @@ -30,7 +32,7 @@ func Test_Get(t *testing.T) {
}},
},
}
licensingInfo, err := NewResourceReporter(k8s.FakeClient(&es)).Get()
licensingInfo, err := NewResourceReporter(k8s.FakeClient(&es), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "21.47GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "1", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -56,7 +58,7 @@ func Test_Get(t *testing.T) {
}},
},
}
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&es)).Get()
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&es), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "644.25GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "11", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -80,7 +82,7 @@ func Test_Get(t *testing.T) {
}},
},
}
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&es)).Get()
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&es), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "171.80GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "3", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -104,7 +106,7 @@ func Test_Get(t *testing.T) {
}},
},
}
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&es)).Get()
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&es), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "171.80GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "3", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -114,7 +116,7 @@ func Test_Get(t *testing.T) {
Count: 100,
},
}
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&kb)).Get()
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&kb), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "107.37GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "2", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -138,7 +140,7 @@ func Test_Get(t *testing.T) {
},
},
}
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&kb)).Get()
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&kb), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "214.75GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "4", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -160,7 +162,7 @@ func Test_Get(t *testing.T) {
},
},
}
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&kb)).Get()
licensingInfo, err = NewResourceReporter(k8s.FakeClient(&kb), operatorNs).Get()
assert.NoError(t, err)
assert.Equal(t, "204.80GB", licensingInfo.TotalManagedMemory)
assert.Equal(t, "4", licensingInfo.EnterpriseResourceUnits)
Expand All @@ -175,13 +177,12 @@ func Test_Start(t *testing.T) {
kb := kbv1.Kibana{Spec: kbv1.KibanaSpec{Count: 2}}
apm := apmv1.ApmServer{Spec: apmv1.ApmServerSpec{Count: 2}}
k8sClient := k8s.FakeClient(&es, &kb, &apm)
operatorNs := "test-system"
refreshPeriod := 1 * time.Second
waitFor := 10 * refreshPeriod
tick := refreshPeriod / 2

// start the resource reporter
go NewResourceReporter(k8sClient).Start(operatorNs, refreshPeriod)
go NewResourceReporter(k8sClient, operatorNs).Start(refreshPeriod)

// check that the licensing config map exists
assert.Eventually(t, func() bool {
Expand Down