/
type_setting.go
285 lines (248 loc) · 7.14 KB
/
type_setting.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// 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.
package v1
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
"github.com/altinity/clickhouse-operator/pkg/util"
)
// Settings value can be one of:
// 1. scalar value (string, int, bool, etc).
// Ex.:
// user1/networks/ip: "::/0"
// 2. vector of scalars
// Ex.:
// user1/networks/ip:
// - "127.0.0.1"
// - "192.168.1.2"
// We do not know types of these scalars in advance also
// Setting represents one settings, which can be either a sting or a vector of strings
type Setting struct {
_type SettingType
scalar string
vector []string
src *SettingSource
attributes map[string]string
}
type SettingType string
const (
SettingTypeUnknown SettingType = "unknown"
SettingTypeScalar SettingType = "scalar"
SettingTypeVector SettingType = "vector"
SettingTypeSource SettingType = "source"
)
// Ensure required interface implementation
var _ yaml.Marshaler = &Setting{}
// AsAny gets value of a setting as vector. ScalarString value is casted to vector
func (s *Setting) AsAny() any {
if s == nil {
return nil
}
switch s.Type() {
case SettingTypeScalar:
return s.scalarAsAny()
case SettingTypeVector:
return s.vectorAsAny()
case SettingTypeSource:
return s.sourceAsAny()
}
return nil
}
// Type gets type odf the setting
func (s *Setting) Type() SettingType {
if s == nil {
return SettingTypeUnknown
}
return s._type
}
// SetAttribute sets attribute of the setting
func (s *Setting) SetAttribute(name, value string) *Setting {
if s == nil {
return nil
}
if s.attributes == nil {
s.attributes = make(map[string]string)
}
s.attributes[name] = value
return s
}
// HasAttribute checks whether setting has specified attribute
func (s *Setting) HasAttribute(name string) bool {
if s == nil {
return false
}
if s.attributes == nil {
return false
}
_, ok := s.attributes[name]
return ok
}
// HasAttributes checks whether setting has attributes
func (s *Setting) HasAttributes() bool {
if s == nil {
return false
}
return len(s.attributes) > 0
}
// Attributes returns string form of attributes - used to config tag creation
func (s *Setting) Attributes() string {
if s == nil {
return ""
}
a := ""
for name, value := range s.attributes {
a += fmt.Sprintf(` %s="%s"`, name, value)
}
return a
}
// Len returns number of entries in the Setting (be it scalar or vector)
func (s *Setting) Len() int {
switch s.Type() {
case SettingTypeScalar:
return 1
case SettingTypeVector:
return len(s.vector)
case SettingTypeSource:
return 1
default:
return 0
}
}
// HasValue checks whether setting has a zero-value (no value)
func (s *Setting) HasValue() bool {
switch s.Type() {
case SettingTypeScalar:
return s.Len() > 0
case SettingTypeVector:
return s.Len() > 0
case SettingTypeSource:
return s.src.HasValue()
default:
return false
}
}
// MergeFrom merges from specified source
func (s *Setting) MergeFrom(from *Setting) *Setting {
// Need to have something to merge from
if from == nil {
return s
}
// Can merge from Vector only
from = from.CastToVector()
// Reasonable to merge values only
if !from.HasValue() {
return s
}
// In case recipient does not exist just copy values from source
if s == nil {
new := NewSettingVector(from.VectorOfStrings())
new.attributes = util.MergeStringMapsPreserve(new.attributes, from.attributes)
return new
}
s.vector = util.MergeStringArrays(s.vector, from.vector)
s.attributes = util.MergeStringMapsPreserve(s.attributes, from.attributes)
return s
}
// String gets string value of a setting. Vector is combined into one string
func (s *Setting) String() string {
if s == nil {
return ""
}
switch s.Type() {
case SettingTypeScalar:
return s.ScalarString()
case SettingTypeVector:
return "[" + strings.Join(s.vector, ", ") + "]"
case SettingTypeSource:
return "data source"
}
return ""
}
// String gets string value of a setting. Vector is combined into one string
func (s *Setting) StringFull() string {
if s == nil {
return ""
}
attributes := ""
if s.HasAttributes() {
attributes = ":[" + s.Attributes() + "]"
}
return s.String() + attributes
}
// MarshalYAML implements yaml.Marshaler interface
func (s *Setting) MarshalYAML() (interface{}, error) {
return s.String(), nil
}
// CastToVector returns either Setting in case it is vector or newly created Setting with value casted to VectorOfStrings
func (s *Setting) CastToVector() *Setting {
if s == nil {
return nil
}
switch s.Type() {
case SettingTypeScalar:
return NewSettingVector(s.AsVectorOfStrings())
}
return s
}
var ErrDataSourceAddressHasIncorrectFormat = fmt.Errorf("data source address has incorrect format")
// FetchDataSourceAddress fetches data source address from the setting.
// defaultNamespace specifies default namespace to be used in case there is no namespace specified in data source address.
func (s *Setting) FetchDataSourceAddress(defaultNamespace string, parseScalarString bool) (ObjectAddress, error) {
switch s.Type() {
case SettingTypeScalar:
if parseScalarString {
// Fetch k8s address of the field from the string
return s.parseDataSourceAddress(s.String(), defaultNamespace)
}
case SettingTypeSource:
// Fetch k8s address of the field from the source ref
name, key := s.GetNameKey()
return ObjectAddress{
Namespace: defaultNamespace,
Name: name,
Key: key,
}, nil
}
return ObjectAddress{}, fmt.Errorf("%w - unknown setting type", ErrDataSourceAddressHasIncorrectFormat)
}
// parseDataSourceAddress parses address into namespace, name, key triple
func (s *Setting) parseDataSourceAddress(dataSourceAddress, defaultNamespace string) (addr ObjectAddress, err error) {
// Extract data source's namespace and name and then field name within the data source,
// by splitting namespace/name/field (aka key) triple. Namespace can be omitted though
switch tags := strings.Split(dataSourceAddress, "/"); len(tags) {
case 3:
// All components are in place. Expect to have namespace/name/key triple
addr.Namespace = tags[0]
addr.Name = tags[1]
addr.Key = tags[2]
case 2:
// Assume namespace is omitted. Expect to have name/key pair
addr.Namespace = defaultNamespace
addr.Name = tags[0]
addr.Key = tags[1]
default:
// Skip incorrect entry
return ObjectAddress{}, fmt.Errorf("%w, dataSourceAddress: %s", ErrDataSourceAddressHasIncorrectFormat, dataSourceAddress)
}
// Sanity check for all address components being in place
if addr.AnyEmpty() {
return ObjectAddress{}, fmt.Errorf(
"%w, %s/%s/%s",
ErrDataSourceAddressHasIncorrectFormat,
addr.Namespace, addr.Name, addr.Key,
)
}
return addr, nil
}