/
server_info.go
243 lines (202 loc) · 6.21 KB
/
server_info.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
/*
Copyright 2023 Gravitational, Inc.
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 types
import (
"fmt"
"strings"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/utils"
)
// ServerInfo represents info that should be applied to joining Nodes.
type ServerInfo interface {
// ResourceWithLabels provides common resource headers
ResourceWithLabels
// GetNewLabels gets the labels to apply to matched Nodes.
GetNewLabels() map[string]string
// SetNewLabels sets the labels to apply to matched Nodes.
SetNewLabels(map[string]string)
}
// NewServerInfo creates an instance of ServerInfo.
func NewServerInfo(meta Metadata, spec ServerInfoSpecV1) (ServerInfo, error) {
si := &ServerInfoV1{
Metadata: meta,
Spec: spec,
}
if err := si.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return si, nil
}
// GetKind returns resource kind
func (s *ServerInfoV1) GetKind() string {
return s.Kind
}
// GetSubKind returns resource subkind
func (s *ServerInfoV1) GetSubKind() string {
return s.SubKind
}
// SetSubKind sets resource subkind
func (s *ServerInfoV1) SetSubKind(subkind string) {
s.SubKind = subkind
}
// GetVersion returns resource version
func (s *ServerInfoV1) GetVersion() string {
return s.Version
}
// GetName returns the name of the resource
func (s *ServerInfoV1) GetName() string {
return s.Metadata.Name
}
// SetName sets the name of the resource
func (s *ServerInfoV1) SetName(name string) {
s.Metadata.Name = name
}
// Expiry returns object expiry setting
func (s *ServerInfoV1) Expiry() time.Time {
return s.Metadata.Expiry()
}
// SetExpiry sets object expiry
func (s *ServerInfoV1) SetExpiry(expiry time.Time) {
s.Metadata.SetExpiry(expiry)
}
// GetMetadata returns object metadata
func (s *ServerInfoV1) GetMetadata() Metadata {
return s.Metadata
}
// GetResourceID returns resource ID
func (s *ServerInfoV1) GetResourceID() int64 {
return s.Metadata.ID
}
// SetResourceID sets resource ID
func (s *ServerInfoV1) SetResourceID(id int64) {
s.Metadata.ID = id
}
// GetRevision returns the revision
func (s *ServerInfoV1) GetRevision() string {
return s.Metadata.GetRevision()
}
// SetRevision sets the revision
func (s *ServerInfoV1) SetRevision(rev string) {
s.Metadata.SetRevision(rev)
}
// Origin returns the origin value of the resource.
func (s *ServerInfoV1) Origin() string {
return s.Metadata.Origin()
}
// SetOrigin sets the origin value of the resource.
func (s *ServerInfoV1) SetOrigin(o string) {
s.Metadata.SetOrigin(o)
}
// GetLabel retrieves the label with the provided key.
func (s *ServerInfoV1) GetLabel(key string) (string, bool) {
value, ok := s.Metadata.Labels[key]
return value, ok
}
// GetAllLabels returns all resource's labels.
func (s *ServerInfoV1) GetAllLabels() map[string]string {
return s.Metadata.Labels
}
// GetStaticLabels returns the resource's static labels.
func (s *ServerInfoV1) GetStaticLabels() map[string]string {
return s.Metadata.Labels
}
// SetStaticLabels sets the resource's static labels.
func (s *ServerInfoV1) SetStaticLabels(sl map[string]string) {
s.Metadata.Labels = sl
}
// MatchSearch goes through select field values of a resource
// and tries to match against the list of search values.
func (s *ServerInfoV1) MatchSearch(searchValues []string) bool {
fieldVals := append(
utils.MapToStrings(s.GetAllLabels()),
s.GetName(),
)
return MatchSearch(fieldVals, searchValues, nil)
}
// GetNewLabels gets the labels to apply to matched Nodes.
func (s *ServerInfoV1) GetNewLabels() map[string]string {
return s.Spec.NewLabels
}
// SetNewLabels sets the labels to apply to matched Nodes.
func (s *ServerInfoV1) SetNewLabels(labels map[string]string) {
s.Spec.NewLabels = labels
s.fixLabels()
}
// fixLabels sets the namespace of this ServerInfo's labels to match the
// matching scheme indicated by the name.
func (s *ServerInfoV1) fixLabels() {
// Determine which prefix the labels need, if any.
namePrefix, _, found := strings.Cut(s.GetName(), "-")
if !found {
return
}
var labelPrefix string
switch namePrefix {
case "aws":
labelPrefix = "aws/"
case "si":
labelPrefix = TeleportDynamicLabelPrefix
default:
return
}
// Replace the prefix on existing labels.
for k, v := range s.Spec.NewLabels {
prefix, name, _ := strings.Cut(k, "/")
if name == "" {
name = prefix
}
delete(s.Spec.NewLabels, k)
s.Spec.NewLabels[labelPrefix+name] = v
}
}
func (s *ServerInfoV1) setStaticFields() {
s.Kind = KindServerInfo
s.Version = V1
s.SubKind = SubKindCloudInfo
}
// CheckAndSetDefaults validates the Resource and sets any empty fields to
// default values.
func (s *ServerInfoV1) CheckAndSetDefaults() error {
s.setStaticFields()
s.fixLabels()
return trace.Wrap(s.Metadata.CheckAndSetDefaults())
}
// ServerInfoNameFromAWS gets the name of the ServerInfo that matches the node
// with the given AWS account ID and instance ID.
func ServerInfoNameFromAWS(accountID, instanceID string) string {
return fmt.Sprintf("aws-%v-%v", accountID, instanceID)
}
// ServerInfoNameFromNodeName gets the name of the ServerInfo that matches the
// node with the given name.
func ServerInfoNameFromNodeName(name string) string {
return fmt.Sprintf("si-%v", name)
}
// ServerInfoForServer returns a ServerInfo from a Server
func ServerInfoForServer(server Server) (ServerInfo, error) {
return NewServerInfo(
Metadata{
Name: serverInfoNameFromServer(server),
},
ServerInfoSpecV1{},
)
}
// serverInfoNameFromServer returns the ServerInfo name for this Server.
func serverInfoNameFromServer(s Server) string {
awsAccountID := s.GetAWSAccountID()
awsInstanceID := s.GetAWSInstanceID()
if awsAccountID != "" && awsInstanceID != "" {
return ServerInfoNameFromAWS(awsAccountID, awsInstanceID)
}
return ServerInfoNameFromNodeName(s.GetName())
}