@@ -17,12 +17,14 @@ limitations under the License.
17
17
package v1beta1
18
18
19
19
import (
20
+ "encoding/json"
20
21
"fmt"
21
22
"net"
22
23
23
24
"github.com/k0sproject/k0s/internal/pkg/iface"
24
25
"github.com/k0sproject/k0s/internal/pkg/stringslice"
25
26
27
+ "k8s.io/apimachinery/pkg/util/validation"
26
28
"k8s.io/apimachinery/pkg/util/validation/field"
27
29
28
30
"github.com/asaskevich/govalidator"
@@ -32,37 +34,43 @@ var _ Validateable = (*APISpec)(nil)
32
34
33
35
// APISpec defines the settings for the K0s API
34
36
type APISpec struct {
35
- // Local address on which to bind an API
36
- Address string `json:"address"`
37
+ // Address on which to connect to the API server.
38
+ // +optional
39
+ Address string `json:"address,omitempty"`
37
40
38
41
// The loadbalancer address (for k0s controllers running behind a loadbalancer)
42
+ // +optional
39
43
ExternalAddress string `json:"externalAddress,omitempty"`
44
+
40
45
// Map of key-values (strings) for any extra arguments to pass down to Kubernetes api-server process
46
+ // +optional
41
47
ExtraArgs map [string ]string `json:"extraArgs,omitempty"`
48
+
42
49
// Custom port for k0s-api server to listen on (default: 9443)
50
+ // +kubebuilder:validation:Minimum=1
51
+ // +kubebuilder:validation:Maximum=65535
52
+ // +kubebuilder:default=9443
53
+ // +optional
43
54
K0sAPIPort int `json:"k0sApiPort,omitempty"`
44
55
45
56
// Custom port for kube-api server to listen on (default: 6443)
46
- Port int `json:"port"`
57
+ // +kubebuilder:validation:Minimum=1
58
+ // +kubebuilder:validation:Maximum=65535
59
+ // +kubebuilder:default=6443
60
+ // +optional
61
+ Port int `json:"port,omitempty"`
47
62
48
63
// List of additional addresses to push to API servers serving the certificate
49
- SANs []string `json:"sans"`
64
+ // +optional
65
+ SANs []string `json:"sans,omitempty"`
50
66
}
51
67
52
- const defaultKasPort = 6443
53
-
54
68
// DefaultAPISpec default settings for api
55
69
func DefaultAPISpec () * APISpec {
56
- // Collect all nodes addresses for sans
57
- addresses , _ := iface .AllAddresses ()
58
- publicAddress , _ := iface .FirstPublicAddress ()
59
- return & APISpec {
60
- Port : defaultKasPort ,
61
- K0sAPIPort : 9443 ,
62
- SANs : addresses ,
63
- Address : publicAddress ,
64
- ExtraArgs : make (map [string ]string ),
65
- }
70
+ a := new (APISpec )
71
+ a .setDefaults ()
72
+ a .SANs , _ = iface .AllAddresses ()
73
+ return a
66
74
}
67
75
68
76
// APIAddress ...
@@ -131,13 +139,47 @@ func (a *APISpec) Validate() []error {
131
139
errors = append (errors , field .Invalid (path , san , "invalid IP address / DNS name" ))
132
140
}
133
141
142
+ if a .ExternalAddress != "" {
143
+ validateIPAddressOrDNSName (field .NewPath ("externalAddress" ), a .ExternalAddress )
144
+ }
145
+
146
+ for _ , msg := range validation .IsValidPortNum (a .K0sAPIPort ) {
147
+ errors = append (errors , field .Invalid (field .NewPath ("k0sApiPort" ), a .K0sAPIPort , msg ))
148
+ }
149
+
150
+ for _ , msg := range validation .IsValidPortNum (a .Port ) {
151
+ errors = append (errors , field .Invalid (field .NewPath ("port" ), a .Port , msg ))
152
+ }
153
+
134
154
sansPath := field .NewPath ("sans" )
135
155
for idx , san := range a .SANs {
136
156
validateIPAddressOrDNSName (sansPath .Index (idx ), san )
137
157
}
138
158
139
- if a .ExternalAddress != "" {
140
- validateIPAddressOrDNSName (field .NewPath ("externalAddress" ), a .ExternalAddress )
141
- }
142
159
return errors
143
160
}
161
+
162
+ // Sets in some sane defaults when unmarshaling the data from JSON.
163
+ func (a * APISpec ) UnmarshalJSON (data []byte ) error {
164
+ type apiSpec APISpec
165
+ jc := (* apiSpec )(a )
166
+
167
+ if err := json .Unmarshal (data , jc ); err != nil {
168
+ return err
169
+ }
170
+
171
+ a .setDefaults ()
172
+ return nil
173
+ }
174
+
175
+ func (a * APISpec ) setDefaults () {
176
+ if a .Address == "" {
177
+ a .Address , _ = iface .FirstPublicAddress ()
178
+ }
179
+ if a .K0sAPIPort == 0 {
180
+ a .K0sAPIPort = 9443
181
+ }
182
+ if a .Port == 0 {
183
+ a .Port = 6443
184
+ }
185
+ }
0 commit comments