6
6
"fmt"
7
7
"math"
8
8
"strconv"
9
+ "strings"
9
10
"time"
10
11
11
12
"github.com/rancher/csp-adapter/pkg/clients/aws"
@@ -39,10 +40,11 @@ const (
39
40
// same as RFC3339 from time.time without the Z7:00 indicating timezone. Some AWS timestamps have this format
40
41
rfc3339NoTZ = "2006-01-02T15:04:05"
41
42
// 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:"
46
48
)
47
49
48
50
type licenseCheckoutInfo struct {
@@ -55,7 +57,8 @@ func (m *AWS) start(ctx context.Context, errs chan<- error) {
55
57
for range ticker (ctx , managerInterval ) {
56
58
err := m .runComplianceCheck (ctx )
57
59
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 ))
59
62
if updError != nil {
60
63
errs <- err
61
64
}
@@ -72,7 +75,6 @@ func (m *AWS) start(ctx context.Context, errs chan<- error) {
72
75
func (m * AWS ) runComplianceCheck (ctx context.Context ) error {
73
76
license , err := m .aws .GetRancherLicense (ctx )
74
77
if err != nil {
75
- // TODO: No license is an error state
76
78
return fmt .Errorf ("unable to get rancher license, err: %v" , err )
77
79
}
78
80
nodeCounts , err := m .scraper .ScrapeAndParse ()
@@ -116,14 +118,17 @@ func (m *AWS) runComplianceCheck(ctx context.Context) error {
116
118
// only checkout what we actually have available to us
117
119
checkoutAmount = availableLicenses
118
120
}
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 )
122
131
}
123
- logrus .Debugf ("successfully checked out license" )
124
- currentCheckoutInfo .ConsumptionToken = * resp .LicenseConsumptionToken
125
- currentCheckoutInfo .EntitledLicenses = checkoutAmount
126
- currentCheckoutInfo .Expiry = parseExpirationTimestamp (* resp .Expiration )
127
132
} else {
128
133
newCheckoutInfo , err := m .extendCheckout (ctx , 5 * managerInterval , currentCheckoutInfo )
129
134
if err != nil {
@@ -141,12 +146,14 @@ func (m *AWS) runComplianceCheck(ctx context.Context) error {
141
146
142
147
var statusMessage string
143
148
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 )
145
150
} 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 ))
147
153
}
154
+ configMessage := fmt .Sprintf ("Rancher server required %d licens(es) and was able to check out %d licens(es)" , requiredLicenses , currentCheckoutInfo .EntitledLicenses )
148
155
149
- return m .updateAdapterOutput (currentCheckoutInfo .EntitledLicenses == requiredLicenses , statusMessage , statusMessage )
156
+ return m .updateAdapterOutput (currentCheckoutInfo .EntitledLicenses == requiredLicenses , configMessage , statusMessage )
150
157
}
151
158
152
159
// 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 {
206
213
}
207
214
208
215
// 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
209
217
func (m * AWS ) updateAdapterOutput (inCompliance bool , configMessage string , notificationMessage string ) error {
210
218
config := GetDefaultSupportConfig (m .k8s )
211
219
config .CSP = CSPInfo {
0 commit comments