Skip to content

Commit 0bbf335

Browse files
authored
[Feature] Generics for type handling (#1313)
1 parent fcc86c5 commit 0bbf335

File tree

113 files changed

+1012
-1024
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1012
-1024
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- (Bugfix) Discover Arango image during ID phase
88
- (Feature) PV Unschedulable condition
99
- (Feature) Features startup logging
10+
- (Maintenance) Generics for type handling
1011

1112
## [1.2.27](https://github.com/arangodb/kube-arangodb/tree/1.2.27) (2023-04-27)
1213
- (Feature) Add InSync Cache

internal/readme.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ func GenerateReadme(root string) error {
8383
for _, v := range p.Versions {
8484
if err := t.AddRow(map[md.Column]string{
8585
platform: p.Name,
86-
kVersion: util.StringOrDefault(v.KubernetesVersion, ""),
87-
aVersion: util.StringOrDefault(v.ArangoDBVersion, ""),
88-
state: util.StringOrDefault(v.State, ""),
89-
remarks: util.StringOrDefault(v.Remarks, ""),
90-
pRemarks: util.StringOrDefault(v.ProviderRemarks, ""),
86+
kVersion: util.TypeOrDefault[string](v.KubernetesVersion, ""),
87+
aVersion: util.TypeOrDefault[string](v.ArangoDBVersion, ""),
88+
state: util.TypeOrDefault[string](v.State, ""),
89+
remarks: util.TypeOrDefault[string](v.Remarks, ""),
90+
pRemarks: util.TypeOrDefault[string](v.ProviderRemarks, ""),
9191
}); err != nil {
9292
return err
9393
}

pkg/apis/backup/v1/backup_spec_backoff_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -43,9 +43,9 @@ func TestArangoBackupSpecBackOff_Backoff(t *testing.T) {
4343
})
4444
t.Run("Custom", func(t *testing.T) {
4545
b := &ArangoBackupSpecBackOff{
46-
MinDelay: util.NewInt(20),
47-
MaxDelay: util.NewInt(120),
48-
Iterations: util.NewInt(10),
46+
MinDelay: util.NewType[int](20),
47+
MaxDelay: util.NewType[int](120),
48+
Iterations: util.NewType[int](10),
4949
}
5050

5151
assert.Equal(t, 20*time.Second, b.Backoff(0))
@@ -64,9 +64,9 @@ func TestArangoBackupSpecBackOff_Backoff(t *testing.T) {
6464

6565
t.Run("Invalid", func(t *testing.T) {
6666
b := &ArangoBackupSpecBackOff{
67-
MinDelay: util.NewInt(-1),
68-
MaxDelay: util.NewInt(-1),
69-
Iterations: util.NewInt(0),
67+
MinDelay: util.NewType[int](-1),
68+
MaxDelay: util.NewType[int](-1),
69+
Iterations: util.NewType[int](0),
7070
}
7171

7272
assert.Equal(t, 0, b.GetMinDelay())
@@ -78,8 +78,8 @@ func TestArangoBackupSpecBackOff_Backoff(t *testing.T) {
7878

7979
t.Run("Max < Min", func(t *testing.T) {
8080
b := &ArangoBackupSpecBackOff{
81-
MinDelay: util.NewInt(50),
82-
MaxDelay: util.NewInt(20),
81+
MinDelay: util.NewType[int](50),
82+
MaxDelay: util.NewType[int](20),
8383
}
8484

8585
assert.Equal(t, 20, b.GetMinDelay())

pkg/apis/backup/v1/backup_status_backoff_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func TestArangoBackupStatusBackOff_Backoff(t *testing.T) {
4242

4343
t.Run("Test MaxIterations", func(t *testing.T) {
4444
var spec = &ArangoBackupSpecBackOff{
45-
Iterations: util.NewInt(2),
46-
MaxIterations: util.NewInt(3),
45+
Iterations: util.NewType[int](2),
46+
MaxIterations: util.NewType[int](3),
4747
}
4848
var status *ArangoBackupStatusBackOff
4949

pkg/apis/deployment/v1/authentication_spec.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@ const (
3838

3939
// GetJWTSecretName returns the value of jwtSecretName.
4040
func (s AuthenticationSpec) GetJWTSecretName() string {
41-
return util.StringOrDefault(s.JWTSecretName)
41+
return util.TypeOrDefault[string](s.JWTSecretName)
4242
}
4343

4444
// IsAuthenticated returns true if authentication is enabled.
@@ -65,14 +65,14 @@ func (s *AuthenticationSpec) SetDefaults(defaultJWTSecretName string) {
6565
if s.GetJWTSecretName() == "" {
6666
// Note that we don't check for nil here, since even a specified, but empty
6767
// string should result in the default value.
68-
s.JWTSecretName = util.NewString(defaultJWTSecretName)
68+
s.JWTSecretName = util.NewType[string](defaultJWTSecretName)
6969
}
7070
}
7171

7272
// SetDefaultsFrom fills unspecified fields with a value from given source spec.
7373
func (s *AuthenticationSpec) SetDefaultsFrom(source AuthenticationSpec) {
7474
if s.JWTSecretName == nil {
75-
s.JWTSecretName = util.NewStringOrNil(source.JWTSecretName)
75+
s.JWTSecretName = util.NewTypeOrNil[string](source.JWTSecretName)
7676
}
7777
}
7878

@@ -83,7 +83,7 @@ func (s AuthenticationSpec) ResetImmutableFields(fieldPrefix string, target *Aut
8383
var resetFields []string
8484
if s.IsAuthenticated() != target.IsAuthenticated() {
8585
// Note: You can change the name, but not from empty to non-empty (or reverse).
86-
target.JWTSecretName = util.NewStringOrNil(s.JWTSecretName)
86+
target.JWTSecretName = util.NewTypeOrNil[string](s.JWTSecretName)
8787
resetFields = append(resetFields, fieldPrefix+".jwtSecretName")
8888
}
8989
return resetFields

pkg/apis/deployment/v1/authentication_spec_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -30,18 +30,18 @@ import (
3030

3131
func TestAuthenticationSpecValidate(t *testing.T) {
3232
// Valid
33-
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("None")}.Validate(false))
34-
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.Validate(false))
35-
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.Validate(true))
33+
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("None")}.Validate(false))
34+
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")}.Validate(false))
35+
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")}.Validate(true))
3636

3737
// Not valid
38-
assert.Error(t, AuthenticationSpec{JWTSecretName: util.NewString("Foo")}.Validate(false))
38+
assert.Error(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("Foo")}.Validate(false))
3939
}
4040

4141
func TestAuthenticationSpecIsAuthenticated(t *testing.T) {
42-
assert.False(t, AuthenticationSpec{JWTSecretName: util.NewString("None")}.IsAuthenticated())
43-
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.IsAuthenticated())
44-
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewString("")}.IsAuthenticated())
42+
assert.False(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("None")}.IsAuthenticated())
43+
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")}.IsAuthenticated())
44+
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewType[string]("")}.IsAuthenticated())
4545
}
4646

4747
func TestAuthenticationSpecSetDefaults(t *testing.T) {
@@ -51,7 +51,7 @@ func TestAuthenticationSpecSetDefaults(t *testing.T) {
5151
}
5252

5353
assert.Equal(t, "test-jwt", def(AuthenticationSpec{}).GetJWTSecretName())
54-
assert.Equal(t, "foo", def(AuthenticationSpec{JWTSecretName: util.NewString("foo")}).GetJWTSecretName())
54+
assert.Equal(t, "foo", def(AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")}).GetJWTSecretName())
5555
}
5656

5757
func TestAuthenticationSpecResetImmutableFields(t *testing.T) {
@@ -63,35 +63,35 @@ func TestAuthenticationSpecResetImmutableFields(t *testing.T) {
6363
}{
6464
// Valid "changes"
6565
{
66-
AuthenticationSpec{JWTSecretName: util.NewString("None")},
67-
AuthenticationSpec{JWTSecretName: util.NewString("None")},
68-
AuthenticationSpec{JWTSecretName: util.NewString("None")},
66+
AuthenticationSpec{JWTSecretName: util.NewType[string]("None")},
67+
AuthenticationSpec{JWTSecretName: util.NewType[string]("None")},
68+
AuthenticationSpec{JWTSecretName: util.NewType[string]("None")},
6969
nil,
7070
},
7171
{
72-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
73-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
74-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
72+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
73+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
74+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
7575
nil,
7676
},
7777
{
78-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
79-
AuthenticationSpec{JWTSecretName: util.NewString("foo2")},
80-
AuthenticationSpec{JWTSecretName: util.NewString("foo2")},
78+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
79+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo2")},
80+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo2")},
8181
nil,
8282
},
8383

8484
// Invalid changes
8585
{
86-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
87-
AuthenticationSpec{JWTSecretName: util.NewString("None")},
88-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
86+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
87+
AuthenticationSpec{JWTSecretName: util.NewType[string]("None")},
88+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
8989
[]string{"test.jwtSecretName"},
9090
},
9191
{
92-
AuthenticationSpec{JWTSecretName: util.NewString("None")},
93-
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
94-
AuthenticationSpec{JWTSecretName: util.NewString("None")},
92+
AuthenticationSpec{JWTSecretName: util.NewType[string]("None")},
93+
AuthenticationSpec{JWTSecretName: util.NewType[string]("foo")},
94+
AuthenticationSpec{JWTSecretName: util.NewType[string]("None")},
9595
[]string{"test.jwtSecretName"},
9696
},
9797
}

pkg/apis/deployment/v1/chaos_spec.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -39,12 +39,12 @@ type ChaosSpec struct {
3939

4040
// IsEnabled returns the value of enabled.
4141
func (s ChaosSpec) IsEnabled() bool {
42-
return util.BoolOrDefault(s.Enabled)
42+
return util.TypeOrDefault[bool](s.Enabled)
4343
}
4444

4545
// GetInterval returns the value of interval.
4646
func (s ChaosSpec) GetInterval() time.Duration {
47-
return util.DurationOrDefault(s.Interval)
47+
return util.TypeOrDefault[time.Duration](s.Interval)
4848
}
4949

5050
// GetKillPodProbability returns the value of kill-pod-probability.
@@ -68,7 +68,7 @@ func (s ChaosSpec) Validate() error {
6868
// SetDefaults fills in missing defaults
6969
func (s *ChaosSpec) SetDefaults() {
7070
if s.GetInterval() == 0 {
71-
s.Interval = util.NewDuration(time.Minute)
71+
s.Interval = util.NewType[time.Duration](time.Minute)
7272
}
7373
if s.GetKillPodProbability() == 0 {
7474
s.KillPodProbability = NewPercent(50)
@@ -78,10 +78,10 @@ func (s *ChaosSpec) SetDefaults() {
7878
// SetDefaultsFrom fills unspecified fields with a value from given source spec.
7979
func (s *ChaosSpec) SetDefaultsFrom(source ChaosSpec) {
8080
if s.Enabled == nil {
81-
s.Enabled = util.NewBoolOrNil(source.Enabled)
81+
s.Enabled = util.NewTypeOrNil[bool](source.Enabled)
8282
}
8383
if s.Interval == nil {
84-
s.Interval = util.NewDurationOrNil(source.Interval)
84+
s.Interval = util.NewTypeOrNil[time.Duration](source.Interval)
8585
}
8686
if s.KillPodProbability == nil {
8787
s.KillPodProbability = NewPercentOrNil(source.KillPodProbability)

pkg/apis/deployment/v1/deployment_metrics_spec.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -106,7 +106,7 @@ func (s *MetricsSpec) GetPort() uint16 {
106106

107107
// IsEnabled returns whether metrics are enabled or not
108108
func (s *MetricsSpec) IsEnabled() bool {
109-
return util.BoolOrDefault(s.Enabled, false)
109+
return util.TypeOrDefault[bool](s.Enabled, false)
110110
}
111111

112112
// HasImage returns whether a image was specified or not
@@ -118,22 +118,22 @@ func (s *MetricsSpec) HasImage() bool {
118118
// GetImage returns the Image or empty string
119119
// Deprecated
120120
func (s *MetricsSpec) GetImage() string {
121-
return util.StringOrDefault(s.Image)
121+
return util.TypeOrDefault[string](s.Image)
122122
}
123123

124124
// SetDefaults sets default values
125125
func (s *MetricsSpec) SetDefaults(defaultTokenName string, isAuthenticated bool) {
126126
if s.Enabled == nil {
127-
s.Enabled = util.NewBool(false)
127+
s.Enabled = util.NewType[bool](false)
128128
}
129129
if s.GetJWTTokenSecretName() == "" {
130-
s.Authentication.JWTTokenSecretName = util.NewString(defaultTokenName)
130+
s.Authentication.JWTTokenSecretName = util.NewType[string](defaultTokenName)
131131
}
132132
}
133133

134134
// GetJWTTokenSecretName returns the token secret name or empty string
135135
func (s *MetricsSpec) GetJWTTokenSecretName() string {
136-
return util.StringOrDefault(s.Authentication.JWTTokenSecretName)
136+
return util.TypeOrDefault[string](s.Authentication.JWTTokenSecretName)
137137
}
138138

139139
// HasJWTTokenSecretName returns true if a secret name was specified
@@ -144,13 +144,13 @@ func (s *MetricsSpec) HasJWTTokenSecretName() bool {
144144
// SetDefaultsFrom fills unspecified fields with a value from given source spec.
145145
func (s *MetricsSpec) SetDefaultsFrom(source MetricsSpec) {
146146
if s.Enabled == nil {
147-
s.Enabled = util.NewBoolOrNil(source.Enabled)
147+
s.Enabled = util.NewTypeOrNil[bool](source.Enabled)
148148
}
149149
if s.Image == nil {
150-
s.Image = util.NewStringOrNil(source.Image)
150+
s.Image = util.NewTypeOrNil[string](source.Image)
151151
}
152152
if s.Authentication.JWTTokenSecretName == nil {
153-
s.Authentication.JWTTokenSecretName = util.NewStringOrNil(source.Authentication.JWTTokenSecretName)
153+
s.Authentication.JWTTokenSecretName = util.NewTypeOrNil[string](source.Authentication.JWTTokenSecretName)
154154
}
155155
setStorageDefaultsFromResourceList(&s.Resources.Limits, source.Resources.Limits)
156156
setStorageDefaultsFromResourceList(&s.Resources.Requests, source.Resources.Requests)

0 commit comments

Comments
 (0)