-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustername.go
219 lines (177 loc) · 6.04 KB
/
clustername.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
/*
Copyright 2017 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 services
import (
"encoding/json"
"fmt"
"time"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
)
// ClusterName defines the name of the cluster. This is a configuration
// resource, never create more than one instance of it.
type ClusterName interface {
// Resource provides common resource properties.
Resource
// SetClusterName sets the name of the cluster.
SetClusterName(string)
// GetClusterName gets the name of the cluster.
GetClusterName() string
// CheckAndSetDefaults checks and set default values for missing fields.
CheckAndSetDefaults() error
}
// NewClusterName is a convenience wrapper to create a ClusterName resource.
func NewClusterName(spec ClusterNameSpecV2) (ClusterName, error) {
cn := ClusterNameV2{
Kind: KindClusterName,
Version: V2,
Metadata: Metadata{
Name: MetaNameClusterName,
Namespace: defaults.Namespace,
},
Spec: spec,
}
if err := cn.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &cn, nil
}
// ClusterNameV2 implements the ClusterName interface.
type ClusterNameV2 struct {
// Kind is a resource kind - always resource.
Kind string `json:"kind"`
// Version is a resource version.
Version string `json:"version"`
// Metadata is metadata about the resource.
Metadata Metadata `json:"metadata"`
// Spec is the specification of the resource.
Spec ClusterNameSpecV2 `json:"spec"`
}
// ClusterNameSpecV2 is the actual data we care about for ClusterName.
type ClusterNameSpecV2 struct {
// ClusterName is the name of the cluster. Changing this value once the
// cluster is setup can and will cause catastrophic problems.
ClusterName string `json:"cluster_name"`
}
// GetName returns the name of the cluster.
func (c *ClusterNameV2) GetName() string {
return c.Metadata.Name
}
// SetName sets the name of the cluster.
func (c *ClusterNameV2) SetName(e string) {
c.Metadata.Name = e
}
// Expires returns object expiry setting
func (c *ClusterNameV2) Expiry() time.Time {
return c.Metadata.Expiry()
}
// SetExpiry sets expiry time for the object
func (c *ClusterNameV2) SetExpiry(expires time.Time) {
c.Metadata.SetExpiry(expires)
}
// SetTTL sets Expires header using realtime clock
func (c *ClusterNameV2) SetTTL(clock clockwork.Clock, ttl time.Duration) {
c.Metadata.SetTTL(clock, ttl)
}
// GetMetadata returns object metadata
func (c *ClusterNameV2) GetMetadata() Metadata {
return c.Metadata
}
// SetClusterName sets the name of the cluster.
func (c *ClusterNameV2) SetClusterName(n string) {
c.Spec.ClusterName = n
}
// GetClusterName gets the name of the cluster.
func (c *ClusterNameV2) GetClusterName() string {
return c.Spec.ClusterName
}
// CheckAndSetDefaults checks validity of all parameters and sets defaults.
func (c *ClusterNameV2) CheckAndSetDefaults() error {
// make sure we have defaults for all metadata fields
err := c.Metadata.CheckAndSetDefaults()
if err != nil {
return trace.Wrap(err)
}
if c.Spec.ClusterName == "" {
return trace.BadParameter("cluster name is required")
}
return nil
}
// String represents a human readable version of the cluster name.
func (c *ClusterNameV2) String() string {
return fmt.Sprintf("ClusterName(%v)", c.Spec.ClusterName)
}
// ClusterNameSpecSchemaTemplate is a template for ClusterName schema.
const ClusterNameSpecSchemaTemplate = `{
"type": "object",
"additionalProperties": false,
"properties": {
"cluster_name": {
"type": "string"
}%v
}
}`
// GetClusterNameSchema returns the schema with optionally injected
// schema for extensions.
func GetClusterNameSchema(extensionSchema string) string {
var clusterNameSchema string
if clusterNameSchema == "" {
clusterNameSchema = fmt.Sprintf(ClusterNameSpecSchemaTemplate, "")
} else {
clusterNameSchema = fmt.Sprintf(ClusterNameSpecSchemaTemplate, ","+extensionSchema)
}
return fmt.Sprintf(V2SchemaTemplate, MetadataSchema, clusterNameSchema, DefaultDefinitions)
}
// ClusterNameMarshaler implements marshal/unmarshal of ClusterName implementations
// mostly adds support for extended versions.
type ClusterNameMarshaler interface {
Marshal(c ClusterName, opts ...MarshalOption) ([]byte, error)
Unmarshal(bytes []byte) (ClusterName, error)
}
var clusterNameMarshaler ClusterNameMarshaler = &TeleportClusterNameMarshaler{}
// SetClusterNameMarshaler sets the marshaler.
func SetClusterNameMarshaler(m ClusterNameMarshaler) {
marshalerMutex.Lock()
defer marshalerMutex.Unlock()
clusterNameMarshaler = m
}
// GetClusterNameMarshaler gets the marshaler.
func GetClusterNameMarshaler() ClusterNameMarshaler {
marshalerMutex.Lock()
defer marshalerMutex.Unlock()
return clusterNameMarshaler
}
// TeleportClusterNameMarshaler is used to marshal and unmarshal ClusterName.
type TeleportClusterNameMarshaler struct{}
// Unmarshal unmarshals ClusterName from JSON.
func (t *TeleportClusterNameMarshaler) Unmarshal(bytes []byte) (ClusterName, error) {
var clusterName ClusterNameV2
if len(bytes) == 0 {
return nil, trace.BadParameter("missing resource data")
}
err := utils.UnmarshalWithSchema(GetClusterNameSchema(""), &clusterName, bytes)
if err != nil {
return nil, trace.BadParameter(err.Error())
}
err = clusterName.CheckAndSetDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
return &clusterName, nil
}
// Marshal marshals ClusterName to JSON.
func (t *TeleportClusterNameMarshaler) Marshal(c ClusterName, opts ...MarshalOption) ([]byte, error) {
return json.Marshal(c)
}