Skip to content

Commit 1f889a7

Browse files
committed
aiven: Support all possible Kafka pools
Instead of maintaining a hardcoded list of possible pools, we loosen the check a bit. We now enforce <tenant>-<env> format but allow any tenant and any env. Fixes nais/system#118
1 parent 2fb7bde commit 1f889a7

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ nais.crt
3333

3434
.direnv
3535
result
36+
cli

cmd/aivencmd/createcmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func createCommand() *cli.Command {
108108
pool, err := aiven_services.KafkaPoolFromString(context.String("pool"))
109109
if err != nil {
110110
metrics.AddOne("aiven_create_pool_values_error_total")
111-
return fmt.Errorf("valid values for pool: %v", strings.Join(aiven_services.KafkaPools, ", "))
111+
return fmt.Errorf("valid values for pool should specify tenant and environment separated by a dash (-): %v", err)
112112
}
113113

114114
access, err := aiven_services.OpenSearchAccessFromString(context.String("access"))

pkg/aiven/aiven_services/kafka.go

+5-22
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,17 @@ import (
77
aiven_nais_io_v1 "github.com/nais/liberator/pkg/apis/aiven.nais.io/v1"
88
)
99

10-
type KafkaPool int64
11-
12-
const (
13-
NavDev KafkaPool = iota
14-
NavProd
15-
NavInfrastructure
16-
DevNaisDev
17-
)
18-
19-
var KafkaPools = []string{"nav-dev", "nav-prod", "nav-infrastructure", "dev-nais-dev"}
10+
type KafkaPool string
2011

2112
func KafkaPoolFromString(pool string) (KafkaPool, error) {
22-
switch strings.ToLower(pool) {
23-
case "nav-dev":
24-
return NavDev, nil
25-
case "nav-prod":
26-
return NavProd, nil
27-
case "nav-infrastructure":
28-
return NavInfrastructure, nil
29-
case "dev-nais-dev":
30-
return DevNaisDev, nil
31-
default:
32-
return -1, fmt.Errorf("unknown pool: %v", pool)
13+
if !strings.Contains(pool, "-") {
14+
return "", fmt.Errorf("invalid pool: %v", pool)
3315
}
16+
return KafkaPool(pool), nil
3417
}
3518

3619
func (p KafkaPool) String() string {
37-
return KafkaPools[p]
20+
return string(p)
3821
}
3922

4023
type Kafka struct {

pkg/aiven/aiven_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
team = "team"
2222
secretName = "secret-name"
2323
expiry = 1
24+
pool = "nav-dev"
2425
)
2526

2627
func buildWithScheme(objects ...runtime.Object) *fake.ClientBuilder {
@@ -42,14 +43,14 @@ func TestGenerateAivenApplicationCreated(t *testing.T) {
4243

4344
fakeClient := buildWithScheme(&namespace).Build()
4445
kafka := &aiven_services.Kafka{}
45-
aiven := Setup(fakeClient, kafka, username, team, secretName, "", aiven_services.NavDev, aiven_services.Read, expiry)
46+
aiven := Setup(fakeClient, kafka, username, team, secretName, "", pool, aiven_services.Read, expiry)
4647
currentAivenApp, err := aiven.GenerateApplication()
4748
assert.NoError(t, err)
4849

4950
assert.Equal(t, username, currentAivenApp.Name, "Name has the same value")
5051
assert.Equal(t, team, currentAivenApp.Namespace, "Namespace has the same value")
5152
assert.Equal(t, secretName, currentAivenApp.Spec.SecretName, "SecretName has the same value")
52-
assert.Equal(t, aiven_services.NavDev.String(), currentAivenApp.Spec.Kafka.Pool, "Pool has the same value")
53+
assert.Equal(t, pool, currentAivenApp.Spec.Kafka.Pool, "Pool has the same value")
5354

5455
assert.True(t, currentAivenApp.Spec.ExpiresAt.After(time.Now()), "Parsed date is still valid")
5556
}
@@ -74,14 +75,14 @@ func TestGenerateAivenApplicationUpdated(t *testing.T) {
7475

7576
fakeClient := buildWithScheme(&namespace, &aivenApp).Build()
7677
kafka := &aiven_services.Kafka{}
77-
aiven := Setup(fakeClient, kafka, username, team, secretName, "", aiven_services.NavDev, aiven_services.Read, expiry)
78+
aiven := Setup(fakeClient, kafka, username, team, secretName, "", pool, aiven_services.Read, expiry)
7879
currentAivenApp, err := aiven.GenerateApplication()
7980
assert.NoError(t, err)
8081

8182
assert.Equal(t, username, currentAivenApp.Name, "Name has the same value")
8283
assert.Equal(t, team, currentAivenApp.Namespace, "Namespace has the same value")
8384
assert.Equal(t, secretName, currentAivenApp.Spec.SecretName, "SecretName has the same value")
84-
assert.Equal(t, aiven_services.NavDev.String(), currentAivenApp.Spec.Kafka.Pool, "Pool has the same value")
85+
assert.Equal(t, pool, currentAivenApp.Spec.Kafka.Pool, "Pool has the same value")
8586

8687
assert.True(t, currentAivenApp.Spec.ExpiresAt.After(time.Now()), "Parsed date is still valid")
8788
}
@@ -114,7 +115,7 @@ func TestGenerateAivenApplicationUpdated_HasOwnerReference(t *testing.T) {
114115

115116
fakeClient := buildWithScheme(&namespace, &aivenApp).Build()
116117
kafka := &aiven_services.Kafka{}
117-
aiven := Setup(fakeClient, kafka, username, team, secretName, "", aiven_services.NavDev, aiven_services.Read, expiry)
118+
aiven := Setup(fakeClient, kafka, username, team, secretName, "", pool, aiven_services.Read, expiry)
118119
_, err := aiven.GenerateApplication()
119120
assert.EqualError(t, err, "create/update: username 'user' is owned by another resource; overwrite is not allowed")
120121
}

0 commit comments

Comments
 (0)