Skip to content

Commit cf09ebd

Browse files
authored
Merge pull request #103 from arangodb/duration-type
Changed TLSSpec.TTL to new string based `Duration` type
2 parents 33ef204 + 28c6cda commit cf09ebd

File tree

6 files changed

+97
-15
lines changed

6 files changed

+97
-15
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package v1alpha
24+
25+
import (
26+
"time"
27+
28+
"github.com/pkg/errors"
29+
)
30+
31+
// Duration is a period of time, specified in go time.Duration format.
32+
// This is intended to allow human friendly TTL's to be specified.
33+
type Duration string
34+
35+
// Validate the duration.
36+
// Return errors when validation fails, nil on success.
37+
func (d Duration) Validate() error {
38+
if d != "" {
39+
if _, err := time.ParseDuration(string(d)); err != nil {
40+
return maskAny(errors.Wrapf(ValidationError, "Invalid duration: '%s': %s", string(d), err.Error()))
41+
}
42+
}
43+
return nil
44+
}
45+
46+
// AsDuration parses the duration to a time.Duration value.
47+
// In case of a parse error, 0 is returned.
48+
func (d Duration) AsDuration() time.Duration {
49+
if d == "" {
50+
return 0
51+
}
52+
result, err := time.ParseDuration(string(d))
53+
if err != nil {
54+
return 0
55+
}
56+
return result
57+
}
58+
59+
// NewDuration returns a reference to a Duration with given value.
60+
func NewDuration(input Duration) *Duration {
61+
return &input
62+
}
63+
64+
// NewDurationOrNil returns nil if input is nil, otherwise returns a clone of the given value.
65+
func NewDurationOrNil(input *Duration) *Duration {
66+
if input == nil {
67+
return nil
68+
}
69+
return NewDuration(*input)
70+
}
71+
72+
// DurationOrDefault returns the default value (or empty string) if input is nil, otherwise returns the referenced value.
73+
func DurationOrDefault(input *Duration, defaultValue ...Duration) Duration {
74+
if input == nil {
75+
if len(defaultValue) > 0 {
76+
return defaultValue[0]
77+
}
78+
return ""
79+
}
80+
return *input
81+
}

pkg/apis/deployment/v1alpha/tls_spec.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ package v1alpha
2525
import (
2626
"fmt"
2727
"net"
28-
"time"
2928

3029
"github.com/arangodb/kube-arangodb/pkg/util"
3130
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
3231
"github.com/arangodb/kube-arangodb/pkg/util/validation"
3332
)
3433

3534
const (
36-
defaultTLSTTL = time.Hour * 2160 // About 3 month
35+
defaultTLSTTL = Duration("2610h") // About 3 month
3736
)
3837

3938
// TLSSpec holds TLS specific configuration settings
4039
type TLSSpec struct {
41-
CASecretName *string `json:"caSecretName,omitempty"`
42-
AltNames []string `json:"altNames,omitempty"`
43-
TTL *time.Duration `json:"ttl,omitempty"`
40+
CASecretName *string `json:"caSecretName,omitempty"`
41+
AltNames []string `json:"altNames,omitempty"`
42+
TTL *Duration `json:"ttl,omitempty"`
4443
}
4544

4645
const (
@@ -59,8 +58,8 @@ func (s TLSSpec) GetAltNames() []string {
5958
}
6059

6160
// GetTTL returns the value of ttl.
62-
func (s TLSSpec) GetTTL() time.Duration {
63-
return util.DurationOrDefault(s.TTL)
61+
func (s TLSSpec) GetTTL() Duration {
62+
return DurationOrDefault(s.TTL)
6463
}
6564

6665
// IsSecure returns true when a CA secret has been set, false otherwise.
@@ -94,6 +93,9 @@ func (s TLSSpec) Validate() error {
9493
if _, _, _, err := s.GetParsedAltNames(); err != nil {
9594
return maskAny(err)
9695
}
96+
if err := s.GetTTL().Validate(); err != nil {
97+
return maskAny(err)
98+
}
9799
}
98100
return nil
99101
}
@@ -105,10 +107,10 @@ func (s *TLSSpec) SetDefaults(defaultCASecretName string) {
105107
// string should result in the default value.
106108
s.CASecretName = util.NewString(defaultCASecretName)
107109
}
108-
if s.GetTTL() == 0 {
110+
if s.GetTTL() == "" {
109111
// Note that we don't check for nil here, since even a specified, but zero
110112
// should result in the default value.
111-
s.TTL = util.NewDuration(defaultTLSTTL)
113+
s.TTL = NewDuration(defaultTLSTTL)
112114
}
113115
}
114116

@@ -121,6 +123,6 @@ func (s *TLSSpec) SetDefaultsFrom(source TLSSpec) {
121123
s.AltNames = source.AltNames
122124
}
123125
if s.TTL == nil {
124-
s.TTL = util.NewDurationOrNil(source.TTL)
126+
s.TTL = NewDurationOrNil(source.TTL)
125127
}
126128
}

pkg/apis/deployment/v1alpha/tls_spec_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ func TestTLSSpecSetDefaults(t *testing.T) {
6262
assert.Len(t, def(TLSSpec{}).GetAltNames(), 0)
6363
assert.Len(t, def(TLSSpec{AltNames: []string{"foo.local"}}).GetAltNames(), 1)
6464
assert.Equal(t, defaultTLSTTL, def(TLSSpec{}).GetTTL())
65-
assert.Equal(t, time.Hour, def(TLSSpec{TTL: util.NewDuration(time.Hour)}).GetTTL())
65+
assert.Equal(t, time.Hour, def(TLSSpec{TTL: NewDuration("1h")}).GetTTL().AsDuration())
6666
}

pkg/apis/deployment/v1alpha/zz_generated.deepcopy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ func (in *TLSSpec) DeepCopyInto(out *TLSSpec) {
627627
if *in == nil {
628628
*out = nil
629629
} else {
630-
*out = new(time.Duration)
630+
*out = new(Duration)
631631
**out = **in
632632
}
633633
}

pkg/deployment/resources/tls.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func createServerCertificate(log zerolog.Logger, cli v1.CoreV1Interface, serverN
105105
Hosts: append(append(serverNames, dnsNames...), ipAddresses...),
106106
EmailAddresses: emailAddress,
107107
ValidFrom: time.Now(),
108-
ValidFor: spec.GetTTL(),
108+
ValidFor: spec.GetTTL().AsDuration(),
109109
IsCA: false,
110110
ECDSACurve: tlsECDSACurve,
111111
}

tests/scale_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tests
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/dchest/uniuri"
98

@@ -24,7 +23,7 @@ func TestScaleClusterNonTLS(t *testing.T) {
2423
// Prepare deployment config
2524
depl := newDeployment("test-scale-non-tls" + uniuri.NewLen(4))
2625
depl.Spec.Mode = api.NewMode(api.DeploymentModeCluster)
27-
depl.Spec.TLS = api.TLSSpec{util.NewString("None"), nil, util.NewDuration(time.Second * 50)}
26+
depl.Spec.TLS = api.TLSSpec{CASecretName: util.NewString("None")}
2827
depl.Spec.SetDefaults(depl.GetName()) // this must be last
2928

3029
// Create deployment

0 commit comments

Comments
 (0)