/
utils.go
172 lines (139 loc) · 4.5 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright © 2020, 2021 Cisco
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// All rights reserved.
package utils
import (
"fmt"
"github.com/CloudNativeSDWAN/cnwan-operator/internal/types"
"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var (
log = zap.New(zap.UseDevMode(false))
)
// ParseAndValidateSettings parses the settings and validates them.
//
// In case of any errors, the settings returned is nil and the error
// occurred is returned.
// TODO: remove this in favor a independent ConfigMaps/Secrets validation.
func ParseAndValidateSettings(settings *types.Settings) (*types.Settings, error) {
if settings == nil {
return nil, fmt.Errorf("no settings provided")
}
finalSettings := &types.Settings{WatchNamespacesByDefault: settings.WatchNamespacesByDefault}
if settings.CloudMetadata != nil {
clCfg := settings.CloudMetadata
finalCfg := &types.CloudMetadata{}
if clCfg.Network != nil && *clCfg.Network != "" {
finalCfg.Network = clCfg.Network
}
if clCfg.SubNetwork != nil && *clCfg.SubNetwork != "" {
finalCfg.SubNetwork = clCfg.SubNetwork
}
if finalCfg.Network != nil || finalCfg.SubNetwork != nil {
finalSettings.CloudMetadata = finalCfg
}
}
if len(settings.Service.Annotations) == 0 {
log.V(int(zapcore.WarnLevel)).Info("no allowed annotations provided: no service will be registered")
}
finalSettings.Service = settings.Service
if settings.ServiceRegistrySettings == nil {
return nil, fmt.Errorf("no service registry provided")
}
finalSettings.ServiceRegistrySettings = &types.ServiceRegistrySettings{}
// Make sure only one service registry is provided.
// TODO: this won't be necessary in future anymore as the CLI will take
// care of it.
srs := func() int {
n := 0
if settings.EtcdSettings != nil {
n++
}
if settings.ServiceDirectorySettings != nil {
n++
}
if settings.CloudMapSettings != nil {
n++
}
return n
}()
if srs == 0 {
return nil, fmt.Errorf("no service registry provided")
}
if srs > 1 {
return nil, fmt.Errorf("UNSUPPORTED: multiple service registries have been provided")
}
if settings.EtcdSettings != nil {
parsedSettings, err := parseEtcdSettings(settings.EtcdSettings)
if err != nil {
return nil, err
}
finalSettings.EtcdSettings = parsedSettings
// Nothing else to check
return finalSettings, nil
}
if settings.ServiceDirectorySettings != nil {
// This validation is performed elsewhere
finalSettings.ServiceDirectorySettings = settings.ServiceDirectorySettings
}
if settings.CloudMapSettings != nil {
// This validation is performed elsewhere
finalSettings.CloudMapSettings = settings.CloudMapSettings
}
return finalSettings, nil
}
func parseEtcdSettings(settings *types.EtcdSettings) (*types.EtcdSettings, error) {
if len(settings.Endpoints) == 0 {
return nil, fmt.Errorf("no etcd endpoints provided")
}
if settings.Authentication != types.EtcdAuthWithNothing &&
settings.Authentication != types.EtcdAuthWithUsernamePassw &&
settings.Authentication != types.EtcdAuthWithTLS {
return nil, fmt.Errorf("unrecognized authentication method for etcd")
}
if settings.Authentication == types.EtcdAuthWithTLS {
return nil, fmt.Errorf("etcd authentication with TLS is not supported yet")
}
finalSettings := &types.EtcdSettings{
Authentication: settings.Authentication,
Prefix: settings.Prefix,
Endpoints: []*types.EtcdEndpoint{},
}
dups := map[string]int{}
for i, endp := range settings.Endpoints {
if len(endp.Host) == 0 {
continue
}
// https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
port := 2379
if endp.Port != nil {
port = *settings.Endpoints[i].Port
}
if val, exists := dups[endp.Host]; exists && val == port {
// skip this
continue
}
newEndp := &types.EtcdEndpoint{
Host: endp.Host,
Port: &port,
}
finalSettings.Endpoints = append(finalSettings.Endpoints, newEndp)
dups[endp.Host] = port
}
return finalSettings, nil
}