Skip to content

Commit 88bca90

Browse files
authored
Merge pull request #54 from MbolotSuse/test-ids-2.7
Adding a dev mode
2 parents 7968bbb + a83ad16 commit 88bca90

File tree

5 files changed

+100
-27
lines changed

5 files changed

+100
-27
lines changed

charts/rancher-csp-adapter/templates/deployment.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ spec:
1616
- env:
1717
- name: CATTLE_DEBUG
1818
value: {{ .Values.debug | quote }}
19+
- name: CATTLE_DEV_MODE
20+
value: {{ .Values.devMode | quote }}
1921
- name: K8S_OUTPUT_CONFIGMAP
2022
value: '{{ template "csp-adapter.outputConfigMap" }}'
2123
- name: K8S_OUTPUT_NOTIFICATION

charts/rancher-csp-adapter/values.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
debug: false
2+
# used for development only - not supported in production
3+
devMode: false
24

35
image:
46
repository: rancher/rancher-csp-adapter

main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ func main() {
2828
}
2929

3030
const (
31-
debugEnv = "CATTLE_DEBUG"
32-
awsCSP = "aws"
31+
debugEnv = "CATTLE_DEBUG"
32+
devModeEnv = "CATTLE_DEV_MODE"
33+
awsCSP = "aws"
3334
)
3435

3536
func run() error {
@@ -55,7 +56,9 @@ func run() error {
5556
return err
5657
}
5758

58-
awsClient, err := aws.NewClient(ctx)
59+
devMode := os.Getenv(devModeEnv) == "true"
60+
61+
awsClient, err := aws.NewClient(ctx, devMode)
5962
if err != nil {
6063
registerErr := registerStartupError(k8sClients, createCSPInfo(awsCSP, "unknown"), err)
6164
if registerErr != nil {

pkg/clients/aws/client.go

+33-15
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ type stsClient interface {
4242
}
4343

4444
type client struct {
45-
acctNum string
46-
sts stsClient
47-
lm licenseManagerClient
45+
acctNum string
46+
sts stsClient
47+
lm licenseManagerClient
48+
useTestProducts bool
4849
}
4950

50-
func NewClient(ctx context.Context) (Client, error) {
51+
func NewClient(ctx context.Context, useTestProducts bool) (Client, error) {
5152
cfg, err := config.LoadDefaultConfig(ctx)
5253
if err != nil {
5354
return nil, err
@@ -66,6 +67,7 @@ func NewClient(ctx context.Context) (Client, error) {
6667
}
6768

6869
c.acctNum = acctNum
70+
c.useTestProducts = useTestProducts
6971

7072
logrus.Debugf("account number: %s", acctNum)
7173

@@ -92,23 +94,39 @@ func (c *client) getAccountNumber(ctx context.Context) (string, error) {
9294
}
9395

9496
var (
95-
productSKUField = "ProductSKU"
96-
rancherProductSKUNonEmea = "0b87d4fa-d1fe-41d8-830b-67d4ec381549"
97-
rancherProductSKUEmea = "a303097d-1dc2-4548-8ea6-f46bb9842e21"
98-
maxResults int32 = 1
97+
productSKUField = "ProductSKU"
98+
rancherProductSKUNonEmea = "0b87d4fa-d1fe-41d8-830b-67d4ec381549"
99+
rancherProductSKUEmea = "a303097d-1dc2-4548-8ea6-f46bb9842e21"
100+
// test skus - should not be enabled at the same time as prod skus
101+
rancherProductTestSKUNonEmea = "83929a73-7c49-4511-aa45-8854a4f001d4"
102+
rancherProductTestSKUEmea = "e001cf36-9e45-496e-be2c-b48749bf7dd2"
103+
maxResults int32 = 1
99104
)
100105

101106
func (c *client) GetRancherLicense(ctx context.Context) (*types.GrantedLicense, error) {
102-
license, err := c.getLicenseForProductID(ctx, rancherProductSKUNonEmea)
103-
if err != nil {
104-
// if we could not get the original license, attempt to retrieve the license for Emea countries
105-
license, newErr := c.getLicenseForProductID(ctx, rancherProductSKUEmea)
106-
if newErr != nil {
107-
return nil, fmt.Errorf("unable to get license for non-emea: %s, unable to get license for emea: %s", err.Error(), newErr.Error())
107+
// only attempt to retrieve the Emea licenses if we can't get the standard license
108+
productSKUs := []string{rancherProductSKUNonEmea, rancherProductSKUEmea}
109+
// test product IDs should only be used specifically when requested
110+
if c.useTestProducts {
111+
productSKUs = []string{rancherProductTestSKUNonEmea, rancherProductTestSKUEmea}
112+
}
113+
var errors []error
114+
for _, productSKU := range productSKUs {
115+
license, err := c.getLicenseForProductID(ctx, productSKU)
116+
if err != nil {
117+
errors = append(errors, fmt.Errorf("unable to get license for sku %s: %w", productSKU, err))
118+
continue
108119
}
120+
// if we found a valid license, return the first one that we find
109121
return license, nil
110122
}
111-
return license, nil
123+
// if we got to this point, then we never found a valid license
124+
err := fmt.Errorf("unable to get a valid rancher license")
125+
// aggregate all individual errors into one message so we can see each error for each product sku
126+
for _, productError := range errors {
127+
err = fmt.Errorf("%s, %s", err.Error(), productError.Error())
128+
}
129+
return nil, err
112130
}
113131

114132
func (c *client) getLicenseForProductID(ctx context.Context, productID string) (*types.GrantedLicense, error) {

pkg/clients/aws/client_test.go

+57-9
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ const fakeAccountNum = "123456789101"
1111

1212
func TestGetRancherLicense(t *testing.T) {
1313
tests := []struct {
14-
name string // name of the test, to be displayed on failure
15-
hasNonEmeaLicense bool // if the account has a license for the non-EMEA product sku
16-
hasEmeaLicense bool // if the account has a licensed for the EMEA product sku
17-
includeProductSku bool // if the return from aws should include or exclude a product sku
18-
desiredLicense string // which license our client should pick - emea, non-emea, or nothing
19-
errDesired bool // if we wanted an error for this test case
14+
name string // name of the test, to be displayed on failure
15+
hasNonEmeaLicense bool // if the account has a license for the non-EMEA product sku
16+
hasEmeaLicense bool // if the account has a licensed for the EMEA product sku
17+
hasTestNonEmeaLicense bool // if the account has a license for the test non-EMEA product sku
18+
hasTestEmeaLicense bool // if the account has a license for the test EMEA product sku
19+
usesTestIds bool // if the account is using a test product id
20+
includeProductSku bool // if the return from aws should include or exclude a product sku
21+
desiredLicense string // which license our client should pick - emea, non-emea, or nothing
22+
errDesired bool // if we wanted an error for this test case
2023
}{
2124
{
2225
name: "test non-emea license",
@@ -66,22 +69,67 @@ func TestGetRancherLicense(t *testing.T) {
6669
desiredLicense: rancherProductSKUEmea,
6770
errDesired: false,
6871
},
72+
{
73+
name: "test non-emea test license",
74+
hasTestNonEmeaLicense: true,
75+
usesTestIds: true,
76+
includeProductSku: true,
77+
desiredLicense: rancherProductTestSKUNonEmea,
78+
},
79+
{
80+
name: "test emea test license",
81+
hasTestEmeaLicense: true,
82+
usesTestIds: true,
83+
includeProductSku: true,
84+
desiredLicense: rancherProductTestSKUEmea,
85+
},
86+
{
87+
name: "test non-emea + emea test license",
88+
hasTestNonEmeaLicense: true,
89+
hasTestEmeaLicense: true,
90+
usesTestIds: true,
91+
includeProductSku: true,
92+
desiredLicense: rancherProductTestSKUNonEmea,
93+
},
94+
{
95+
name: "test non-emea test + prod skus, test requested, test used",
96+
hasNonEmeaLicense: true,
97+
hasTestNonEmeaLicense: true,
98+
usesTestIds: true,
99+
includeProductSku: true,
100+
desiredLicense: rancherProductTestSKUNonEmea,
101+
},
102+
{
103+
name: "test non-emea test + prod skus, prod requested, prod used",
104+
hasNonEmeaLicense: true,
105+
hasTestNonEmeaLicense: true,
106+
usesTestIds: false,
107+
includeProductSku: true,
108+
desiredLicense: rancherProductSKUNonEmea,
109+
},
69110
}
70111
for _, test := range tests {
71112
test := test
72113
t.Run(test.name, func(t *testing.T) {
73114
mockLMClient := mockLicenseManagerClient{}
74115
client := &client{
75-
acctNum: fakeAccountNum,
76-
lm: &mockLMClient,
77-
sts: &mockSTSClient{accountNumber: fakeAccountNum},
116+
acctNum: fakeAccountNum,
117+
lm: &mockLMClient,
118+
sts: &mockSTSClient{accountNumber: fakeAccountNum},
119+
useTestProducts: test.usesTestIds,
78120
}
79121
if test.hasNonEmeaLicense {
80122
mockLMClient.AddLicenseForSku(rancherProductSKUNonEmea, fakeAccountNum, test.includeProductSku)
81123
}
124+
if test.hasTestNonEmeaLicense {
125+
mockLMClient.AddLicenseForSku(rancherProductTestSKUNonEmea, fakeAccountNum, test.includeProductSku)
126+
}
82127
if test.hasEmeaLicense {
83128
mockLMClient.AddLicenseForSku(rancherProductSKUEmea, fakeAccountNum, test.includeProductSku)
84129
}
130+
if test.hasTestEmeaLicense {
131+
mockLMClient.AddLicenseForSku(rancherProductTestSKUEmea, fakeAccountNum, test.includeProductSku)
132+
}
85133

86134
license, err := client.GetRancherLicense(context.Background())
87135
if test.errDesired {

0 commit comments

Comments
 (0)