Skip to content

Commit 054f2c8

Browse files
authored
Merge pull request #11 from MbolotSuse/message-fix
Fixing bug and correcting error messages
2 parents f026dc8 + 792ced8 commit 054f2c8

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

pkg/manager/aws.go

+24-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"math"
88
"strconv"
9+
"strings"
910
"time"
1011

1112
"github.com/rancher/csp-adapter/pkg/clients/aws"
@@ -39,10 +40,11 @@ const (
3940
// same as RFC3339 from time.time without the Z7:00 indicating timezone. Some AWS timestamps have this format
4041
rfc3339NoTZ = "2006-01-02T15:04:05"
4142
// keys for the consumption token secret's data. Can't do a straight marshal because we need all values to be strings
42-
tokenKey = "consumptionToken"
43-
nodeKey = "entitledNodes"
44-
expiryKey = "expiry"
45-
awsCSP = "aws"
43+
tokenKey = "consumptionToken"
44+
nodeKey = "entitledNodes"
45+
expiryKey = "expiry"
46+
awsCSP = "aws"
47+
statusPrefix = "AWS Marketplace Adapter:"
4648
)
4749

4850
type licenseCheckoutInfo struct {
@@ -55,7 +57,8 @@ func (m *AWS) start(ctx context.Context, errs chan<- error) {
5557
for range ticker(ctx, managerInterval) {
5658
err := m.runComplianceCheck(ctx)
5759
if err != nil {
58-
updError := m.updateAdapterOutput(false, fmt.Sprintf("unable to run compliance check with error: %v", err), "unable to run adapter, check adapter logs")
60+
updError := m.updateAdapterOutput(false, fmt.Sprintf("unable to run compliance check with error: %v", err),
61+
fmt.Sprintf("%s Unable to run the adapter, please check the adapter logs", statusPrefix))
5962
if updError != nil {
6063
errs <- err
6164
}
@@ -72,7 +75,6 @@ func (m *AWS) start(ctx context.Context, errs chan<- error) {
7275
func (m *AWS) runComplianceCheck(ctx context.Context) error {
7376
license, err := m.aws.GetRancherLicense(ctx)
7477
if err != nil {
75-
// TODO: No license is an error state
7678
return fmt.Errorf("unable to get rancher license, err: %v", err)
7779
}
7880
nodeCounts, err := m.scraper.ScrapeAndParse()
@@ -116,14 +118,17 @@ func (m *AWS) runComplianceCheck(ctx context.Context) error {
116118
// only checkout what we actually have available to us
117119
checkoutAmount = availableLicenses
118120
}
119-
resp, err := m.aws.CheckoutRancherLicense(ctx, *license, checkoutAmount)
120-
if err != nil {
121-
return fmt.Errorf("unable to checkout rancher licenses %v", err)
121+
if checkoutAmount > 0 {
122+
// it's possible that we have no licenses available - don't attempt checkout in this case
123+
resp, err := m.aws.CheckoutRancherLicense(ctx, *license, checkoutAmount)
124+
if err != nil {
125+
return fmt.Errorf("unable to checkout rancher licenses %v", err)
126+
}
127+
logrus.Debugf("successfully checked out license")
128+
currentCheckoutInfo.ConsumptionToken = *resp.LicenseConsumptionToken
129+
currentCheckoutInfo.EntitledLicenses = checkoutAmount
130+
currentCheckoutInfo.Expiry = parseExpirationTimestamp(*resp.Expiration)
122131
}
123-
logrus.Debugf("successfully checked out license")
124-
currentCheckoutInfo.ConsumptionToken = *resp.LicenseConsumptionToken
125-
currentCheckoutInfo.EntitledLicenses = checkoutAmount
126-
currentCheckoutInfo.Expiry = parseExpirationTimestamp(*resp.Expiration)
127132
} else {
128133
newCheckoutInfo, err := m.extendCheckout(ctx, 5*managerInterval, currentCheckoutInfo)
129134
if err != nil {
@@ -141,12 +146,14 @@ func (m *AWS) runComplianceCheck(ctx context.Context) error {
141146

142147
var statusMessage string
143148
if currentCheckoutInfo.EntitledLicenses == requiredLicenses {
144-
statusMessage = "Rancher server has the required amount of licenses"
149+
statusMessage = fmt.Sprintf("%s Rancher server has the required amount of licenses", statusPrefix)
145150
} else {
146-
statusMessage = fmt.Sprintf("server is not in compliance, wanted %d, but got %d", requiredLicenses, currentCheckoutInfo.EntitledLicenses)
151+
statusMessage = fmt.Sprintf("%s You have exceeded your licensed node count. At least %d more licens(es) are required in %s to become compliant.",
152+
statusPrefix, requiredLicenses-currentCheckoutInfo.EntitledLicenses, strings.ToUpper(awsCSP))
147153
}
154+
configMessage := fmt.Sprintf("Rancher server required %d licens(es) and was able to check out %d licens(es)", requiredLicenses, currentCheckoutInfo.EntitledLicenses)
148155

149-
return m.updateAdapterOutput(currentCheckoutInfo.EntitledLicenses == requiredLicenses, statusMessage, statusMessage)
156+
return m.updateAdapterOutput(currentCheckoutInfo.EntitledLicenses == requiredLicenses, configMessage, statusMessage)
150157
}
151158

152159
// extendCheckout extends the checkout of the licenses in info if info.Expiry is within minTimeTillExpiry
@@ -206,6 +213,7 @@ func (m *AWS) saveCheckoutInfo(info *licenseCheckoutInfo) error {
206213
}
207214

208215
// updateAdapterOutput uses the k8s client to update the status objects signaling compliance/non-compliance to other apps
216+
// configMessage is used to update the supportConfig configmap, and notificationMessage is created in a user-facing object
209217
func (m *AWS) updateAdapterOutput(inCompliance bool, configMessage string, notificationMessage string) error {
210218
config := GetDefaultSupportConfig(m.k8s)
211219
config.CSP = CSPInfo{

0 commit comments

Comments
 (0)