Skip to content

Commit 2af40ca

Browse files
authored
Merge pull request #68 from arangodb/store-accepted-spec
Store accepted spec
2 parents bb57fc5 + 0909a77 commit 2af40ca

Some content is hidden

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

43 files changed

+1023
-406
lines changed

pkg/apis/deployment/v1alpha/authentication_spec.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,29 @@ package v1alpha
2525
import (
2626
"github.com/pkg/errors"
2727

28+
"github.com/arangodb/kube-arangodb/pkg/util"
2829
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
2930
)
3031

3132
// AuthenticationSpec holds authentication specific configuration settings
3233
type AuthenticationSpec struct {
33-
JWTSecretName string `json:"jwtSecretName,omitempty"`
34+
JWTSecretName *string `json:"jwtSecretName,omitempty"`
3435
}
3536

3637
const (
3738
// JWTSecretNameDisabled is the value of JWTSecretName to use for disabling authentication.
3839
JWTSecretNameDisabled = "None"
3940
)
4041

42+
// GetJWTSecretName returns the value of jwtSecretName.
43+
func (s AuthenticationSpec) GetJWTSecretName() string {
44+
return util.StringOrDefault(s.JWTSecretName)
45+
}
46+
4147
// IsAuthenticated returns true if authentication is enabled.
4248
// Returns false other (when JWTSecretName == "None").
4349
func (s AuthenticationSpec) IsAuthenticated() bool {
44-
return s.JWTSecretName != JWTSecretNameDisabled
50+
return s.GetJWTSecretName() != JWTSecretNameDisabled
4551
}
4652

4753
// Validate the given spec
@@ -50,7 +56,7 @@ func (s AuthenticationSpec) Validate(required bool) error {
5056
return maskAny(errors.Wrap(ValidationError, "JWT secret is required"))
5157
}
5258
if s.IsAuthenticated() {
53-
if err := k8sutil.ValidateResourceName(s.JWTSecretName); err != nil {
59+
if err := k8sutil.ValidateResourceName(s.GetJWTSecretName()); err != nil {
5460
return maskAny(err)
5561
}
5662
}
@@ -59,8 +65,17 @@ func (s AuthenticationSpec) Validate(required bool) error {
5965

6066
// SetDefaults fills in missing defaults
6167
func (s *AuthenticationSpec) SetDefaults(defaultJWTSecretName string) {
62-
if s.JWTSecretName == "" {
63-
s.JWTSecretName = defaultJWTSecretName
68+
if s.GetJWTSecretName() == "" {
69+
// Note that we don't check for nil here, since even a specified, but empty
70+
// string should result in the default value.
71+
s.JWTSecretName = util.NewString(defaultJWTSecretName)
72+
}
73+
}
74+
75+
// SetDefaultsFrom fills unspecified fields with a value from given source spec.
76+
func (s *AuthenticationSpec) SetDefaultsFrom(source AuthenticationSpec) {
77+
if s.JWTSecretName == nil {
78+
s.JWTSecretName = util.NewStringOrNil(source.JWTSecretName)
6479
}
6580
}
6681

@@ -71,7 +86,7 @@ func (s AuthenticationSpec) ResetImmutableFields(fieldPrefix string, target *Aut
7186
var resetFields []string
7287
if s.IsAuthenticated() != target.IsAuthenticated() {
7388
// Note: You can change the name, but not from empty to non-empty (or reverse).
74-
target.JWTSecretName = s.JWTSecretName
89+
target.JWTSecretName = util.NewStringOrNil(s.JWTSecretName)
7590
resetFields = append(resetFields, fieldPrefix+".jwtSecretName")
7691
}
7792
return resetFields

pkg/apis/deployment/v1alpha/authentication_spec_test.go

+25-24
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,24 @@ package v1alpha
2525
import (
2626
"testing"
2727

28+
"github.com/arangodb/kube-arangodb/pkg/util"
2829
"github.com/stretchr/testify/assert"
2930
)
3031

3132
func TestAuthenticationSpecValidate(t *testing.T) {
3233
// Valid
33-
assert.Nil(t, AuthenticationSpec{JWTSecretName: "None"}.Validate(false))
34-
assert.Nil(t, AuthenticationSpec{JWTSecretName: "foo"}.Validate(false))
35-
assert.Nil(t, AuthenticationSpec{JWTSecretName: "foo"}.Validate(true))
34+
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("None")}.Validate(false))
35+
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.Validate(false))
36+
assert.Nil(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.Validate(true))
3637

3738
// Not valid
38-
assert.Error(t, AuthenticationSpec{JWTSecretName: "Foo"}.Validate(false))
39+
assert.Error(t, AuthenticationSpec{JWTSecretName: util.NewString("Foo")}.Validate(false))
3940
}
4041

4142
func TestAuthenticationSpecIsAuthenticated(t *testing.T) {
42-
assert.False(t, AuthenticationSpec{JWTSecretName: "None"}.IsAuthenticated())
43-
assert.True(t, AuthenticationSpec{JWTSecretName: "foo"}.IsAuthenticated())
44-
assert.True(t, AuthenticationSpec{JWTSecretName: ""}.IsAuthenticated())
43+
assert.False(t, AuthenticationSpec{JWTSecretName: util.NewString("None")}.IsAuthenticated())
44+
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewString("foo")}.IsAuthenticated())
45+
assert.True(t, AuthenticationSpec{JWTSecretName: util.NewString("")}.IsAuthenticated())
4546
}
4647

4748
func TestAuthenticationSpecSetDefaults(t *testing.T) {
@@ -50,8 +51,8 @@ func TestAuthenticationSpecSetDefaults(t *testing.T) {
5051
return spec
5152
}
5253

53-
assert.Equal(t, "test-jwt", def(AuthenticationSpec{}).JWTSecretName)
54-
assert.Equal(t, "foo", def(AuthenticationSpec{JWTSecretName: "foo"}).JWTSecretName)
54+
assert.Equal(t, "test-jwt", def(AuthenticationSpec{}).GetJWTSecretName())
55+
assert.Equal(t, "foo", def(AuthenticationSpec{JWTSecretName: util.NewString("foo")}).GetJWTSecretName())
5556
}
5657

5758
func TestAuthenticationSpecResetImmutableFields(t *testing.T) {
@@ -63,35 +64,35 @@ func TestAuthenticationSpecResetImmutableFields(t *testing.T) {
6364
}{
6465
// Valid "changes"
6566
{
66-
AuthenticationSpec{JWTSecretName: "None"},
67-
AuthenticationSpec{JWTSecretName: "None"},
68-
AuthenticationSpec{JWTSecretName: "None"},
67+
AuthenticationSpec{JWTSecretName: util.NewString("None")},
68+
AuthenticationSpec{JWTSecretName: util.NewString("None")},
69+
AuthenticationSpec{JWTSecretName: util.NewString("None")},
6970
nil,
7071
},
7172
{
72-
AuthenticationSpec{JWTSecretName: "foo"},
73-
AuthenticationSpec{JWTSecretName: "foo"},
74-
AuthenticationSpec{JWTSecretName: "foo"},
73+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
74+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
75+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
7576
nil,
7677
},
7778
{
78-
AuthenticationSpec{JWTSecretName: "foo"},
79-
AuthenticationSpec{JWTSecretName: "foo2"},
80-
AuthenticationSpec{JWTSecretName: "foo2"},
79+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
80+
AuthenticationSpec{JWTSecretName: util.NewString("foo2")},
81+
AuthenticationSpec{JWTSecretName: util.NewString("foo2")},
8182
nil,
8283
},
8384

8485
// Invalid changes
8586
{
86-
AuthenticationSpec{JWTSecretName: "foo"},
87-
AuthenticationSpec{JWTSecretName: "None"},
88-
AuthenticationSpec{JWTSecretName: "foo"},
87+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
88+
AuthenticationSpec{JWTSecretName: util.NewString("None")},
89+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
8990
[]string{"test.jwtSecretName"},
9091
},
9192
{
92-
AuthenticationSpec{JWTSecretName: "None"},
93-
AuthenticationSpec{JWTSecretName: "foo"},
94-
AuthenticationSpec{JWTSecretName: "None"},
93+
AuthenticationSpec{JWTSecretName: util.NewString("None")},
94+
AuthenticationSpec{JWTSecretName: util.NewString("foo")},
95+
AuthenticationSpec{JWTSecretName: util.NewString("None")},
9596
[]string{"test.jwtSecretName"},
9697
},
9798
}

pkg/apis/deployment/v1alpha/deployment_mode.go

+24
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,27 @@ func (m DeploymentMode) HasCoordinators() bool {
7373
func (m DeploymentMode) SupportsSync() bool {
7474
return m == DeploymentModeCluster
7575
}
76+
77+
// NewMode returns a reference to a string with given value.
78+
func NewMode(input DeploymentMode) *DeploymentMode {
79+
return &input
80+
}
81+
82+
// NewModeOrNil returns nil if input is nil, otherwise returns a clone of the given value.
83+
func NewModeOrNil(input *DeploymentMode) *DeploymentMode {
84+
if input == nil {
85+
return nil
86+
}
87+
return NewMode(*input)
88+
}
89+
90+
// ModeOrDefault returns the default value (or empty string) if input is nil, otherwise returns the referenced value.
91+
func ModeOrDefault(input *DeploymentMode, defaultValue ...DeploymentMode) DeploymentMode {
92+
if input == nil {
93+
if len(defaultValue) > 0 {
94+
return defaultValue[0]
95+
}
96+
return ""
97+
}
98+
return *input
99+
}

0 commit comments

Comments
 (0)