-
Notifications
You must be signed in to change notification settings - Fork 92
/
cloudflowapplication.go
339 lines (289 loc) · 12.1 KB
/
cloudflowapplication.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cfapp
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/lightbend/cloudflow/kubectl-cloudflow/fileutil"
. "github.com/lightbend/cloudflow/kubectl-cloudflow/version"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Import additional authentication methods
)
// PortMapping maps outlets
type PortMapping struct {
ID string `json:"id"`
Config json.RawMessage `json:"config"`
Cluster string `json:"cluster,omitempty"`
}
// Endpoint contains deployment endpoint information
type Endpoint struct {
AppID string `json:"app_id,omitempty"`
Streamlet string `json:"streamlet,omitempty"`
ContainerPort int `json:"container_port,omitempty"`
}
// Deployment contains a streamlet deployment
type Deployment struct {
ClassName string `json:"class_name"`
Config json.RawMessage `json:"config"`
Image string `json:"image"`
Name string `json:"name"`
PortMappings map[string]PortMapping `json:"port_mappings"`
VolumeMounts []VolumeMountDescriptor `json:"volume_mounts"`
Runtime string `json:"runtime"`
StreamletName string `json:"streamlet_name"`
SecretName string `json:"secret_name"`
Endpoint *Endpoint `json:"endpoint,omitempty"`
Replicas int `json:"replicas,omitempty"`
}
// InOutletSchema contains the schema of a in/out-let
type InOutletSchema struct {
Fingerprint string `json:"fingerprint"`
Schema string `json:"schema"`
Name string `json:"name"`
Format string `json:"format"`
}
// InOutlet defines a in/out-let and its schema
type InOutlet struct {
Name string `json:"name"`
Schema InOutletSchema `json:"schema"`
}
// Attribute describes that a streamlet requires specific configuration by the cloudflow platform
type Attribute struct {
AttributeName string `json:"attribute_name"`
ConfigPath string `json:"config_path"`
}
// ConfigParameterDescriptor describes an argument that has to be supplied when deploying a Cloudflow application
type ConfigParameterDescriptor struct {
Key string `json:"key"`
Description string `json:"description"`
Type string `json:"validation_type"`
Pattern string `json:"validation_pattern"`
DefaultValue string `json:"default_value"`
}
// ReadWriteMany is a name of a VolumeMount access mode
const ReadWriteMany = "ReadWriteMany"
// ReadOnlyMany is a name of a VolumeMount access mode
const ReadOnlyMany = "ReadOnlyMany"
// VolumeMountDescriptor describes a volume mount that a streamlet will use
type VolumeMountDescriptor struct {
Name string `json:"name"`
Path string `json:"path"`
AccessMode string `json:"access_mode"`
PVCName string `json:"pvc_name,omitempty"`
}
// Descriptor describes a streamlet
type Descriptor struct {
Attributes []Attribute `json:"attributes"`
ClassName string `json:"class_name"`
ConfigParameters []ConfigParameterDescriptor `json:"config_parameters"`
VolumeMounts []VolumeMountDescriptor `json:"volume_mounts"`
Inlets []InOutlet `json:"inlets"`
Labels []string `json:"labels"`
Outlets []InOutlet `json:"outlets"`
Runtime string `json:"runtime"`
Description string `json:"description"`
}
// Streamlet is a streamlet instance in the cloudflow application
type Streamlet struct {
Descriptor Descriptor `json:"descriptor"`
Name string `json:"name"`
}
// CloudflowApplicationSpec contains the CR spec for a cloudflow application
type CloudflowApplicationSpec struct {
AppID string `json:"app_id"`
AppVersion string `json:"app_version"`
Deployments []Deployment `json:"deployments"`
Streamlets []Streamlet `json:"streamlets"`
AgentPaths map[string]string `json:"agent_paths"`
Version string `json:"version,omitempty"`
LibraryVersion string `json:"library_version,omitempty"`
}
// PodStatus contains the status of the pod
type PodStatus struct {
Name string `json:"name"`
Ready string `json:"ready"`
NrOfContainersReady int `json:"nr_of_containers_ready"`
NrOfContainers int `json:"nr_of_containers"`
Restarts int `json:"restarts"`
Status string `json:"status"`
}
// StreamletStatus contains the status of the streamlet
type StreamletStatus struct {
StreamletName string `json:"streamlet_name"`
PodStatuses []PodStatus `json:"pod_statuses"`
}
// EndpointStatus contains the status of the endpoint
type EndpointStatus struct {
StreamletName string `json:"streamlet_name"`
URL string `json:"url"`
}
// CloudflowApplicationStatus contains the status of the application
type CloudflowApplicationStatus struct {
AppID string `json:"app_id"`
AppVersion string `json:"app_version"`
AppStatus string `json:"app_status"`
AppMessage string `json:"app_message"`
EndpointStatuses []EndpointStatus `json:"endpoint_statuses"`
StreamletStatuses []StreamletStatus `json:"streamlet_statuses"`
}
// CloudflowApplication is the complete resource object sent to the API endpoint
type CloudflowApplication struct {
metav1.TypeMeta
metav1.ObjectMeta `json:"metadata"`
Spec CloudflowApplicationSpec `json:"spec"`
Status *CloudflowApplicationStatus `json:"status,omitempty"`
}
// CloudflowApplicationList is a list of CloudflowApplications
type CloudflowApplicationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []CloudflowApplication `json:"items"`
}
// CloudflowApplicationDescriptorDigestPair is a pair of app descriptor and image digest
type CloudflowApplicationDescriptorDigestPair struct {
AppDescriptor string `json:"appDescriptor"`
ImageDigest string `json:"imageDigest"`
}
// APIVersion formats the correct APIVersion from group name and version
func APIVersion() string {
return fmt.Sprintf("%s/%s", GroupName, GroupVersion)
}
// LoadCloudflowApplicationSpec loads the spec from file
func LoadCloudflowApplicationSpec(crFile string) (CloudflowApplicationSpec, error) {
crString, err := fileutil.GetFileContents(crFile)
if err != nil {
return CloudflowApplicationSpec{}, fmt.Errorf("Failed to read the file contents for the CR - please check if the file exists or it has a bad formatting (%s)", err.Error())
}
bytes := []byte(crString)
var cr CloudflowApplication
if json.Unmarshal(bytes, &cr); err != nil {
return CloudflowApplicationSpec{}, err
}
applicationSpec := cr.Spec
err = checkApplicationDescriptorVersion(applicationSpec)
return applicationSpec, err
}
// UpdateCloudflowApplication creates a CloudflowApplication struct that can be used with a Update call
func UpdateCloudflowApplication(spec CloudflowApplicationSpec, currentAppCR CloudflowApplication, releaseTag string, buildNumber string) CloudflowApplication {
app := CloudflowApplication{}
app.APIVersion = APIVersion()
app.Kind = "CloudflowApplication"
app.ObjectMeta = metav1.ObjectMeta{
Name: spec.AppID,
ResourceVersion: currentAppCR.ObjectMeta.ResourceVersion,
Labels: CreateLabels(spec.AppID),
Annotations: LastModifiedByCLIAnnotation(releaseTag, buildNumber, currentAppCR.ObjectMeta.Annotations),
}
app.Spec = spec
return app
}
// NewCloudflowApplication creates a CloudflowApplication strcut that can be used with a Create call
func NewCloudflowApplication(spec CloudflowApplicationSpec, releaseTag string, buildNumber string) CloudflowApplication {
app := CloudflowApplication{}
app.APIVersion = APIVersion()
app.Kind = Kind
app.ObjectMeta = metav1.ObjectMeta{
Name: spec.AppID,
Labels: CreateLabels(spec.AppID),
Annotations: CreatedByCliAnnotation(releaseTag, buildNumber),
}
app.Spec = spec
return app
}
// NewCloudflowApplicationNamespace creates a Namespace struct
func NewCloudflowApplicationNamespace(spec CloudflowApplicationSpec, releaseTag string, buildNumber string) v1.Namespace {
return v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: spec.AppID,
Labels: CreateLabels(spec.AppID),
},
}
}
// CreatedByCliAnnotation creates a map for use as an annotation when a resource is created by the CLI
func CreatedByCliAnnotation(releaseTag string, buildNumber string) map[string]string {
return map[string]string{
"com.lightbend.cloudflow/created-by-cli-version": fmt.Sprintf("%s (%s)", releaseTag, buildNumber),
}
}
// LastModifiedByCLIAnnotation creates a map for use as an annotation when a resource is updated by the CLI
func LastModifiedByCLIAnnotation(releaseTag string, buildNumber string, previousAnnotations map[string]string) map[string]string {
newAnnotations := make(map[string]string)
for k, v := range previousAnnotations {
newAnnotations[k] = v
}
newAnnotations["com.lightbend.cloudflow/last-modified-by-cli-version"] = fmt.Sprintf("%s (%s)", releaseTag, buildNumber)
return newAnnotations
}
// CreateLabels creates cloudflow application labels
func CreateLabels(appID string) map[string]string {
return map[string]string{
"app.kubernetes.io/part-of": appID,
"app.kubernetes.io/managed-by": "cloudflow",
"com.lightbend.cloudflow/app-id": appID,
}
}
// DeepCopyInto copies all properties of this object into another object of the
// same type that is provided as a pointer.
func (in *CloudflowApplication) DeepCopyInto(out *CloudflowApplication) {
out.TypeMeta = in.TypeMeta
out.ObjectMeta = in.ObjectMeta
out.Kind = in.Kind
out.Spec = CloudflowApplicationSpec{
AppID: in.Spec.AppID,
AppVersion: in.Spec.AppVersion,
Deployments: in.Spec.Deployments,
Streamlets: in.Spec.Streamlets,
}
}
// DeepCopyObject returns a generically typed copy of an object
func (in *CloudflowApplication) DeepCopyObject() runtime.Object {
out := CloudflowApplication{}
in.DeepCopyInto(&out)
return &out
}
// DeepCopyObject returns a generically typed copy of an object
func (in *CloudflowApplicationList) DeepCopyObject() runtime.Object {
out := CloudflowApplicationList{}
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
out.Items = make([]CloudflowApplication, len(in.Items))
for i := range in.Items {
in.Items[i].DeepCopyInto(&out.Items[i])
}
}
return &out
}
// GenerateOwnerReference creates an ownerReference pointing towards this CR
func (in *CloudflowApplication) GenerateOwnerReference() metav1.OwnerReference {
managingController := true
blockOwnerDeletion := true
return metav1.OwnerReference{
APIVersion: APIVersion(),
Kind: Kind,
Name: in.GetName(),
UID: in.GetUID(),
Controller: &managingController,
BlockOwnerDeletion: &blockOwnerDeletion}
}
//CheckApplicationDescriptorVersion checks the version, logs and exits if not supported
func checkApplicationDescriptorVersion(spec CloudflowApplicationSpec) error {
if len(strings.TrimSpace(spec.Version)) == 0 {
return fmt.Errorf("Application file parse error: spec.version is missing or empty")
}
if len(strings.TrimSpace(spec.LibraryVersion)) == 0 {
return fmt.Errorf("Application file parse error: spec.library_version is missing or empty")
}
version, convErr := strconv.Atoi(spec.Version)
if convErr != nil {
return fmt.Errorf("Application file parse error: spec.version is invalid")
}
if version < SupportedApplicationDescriptorVersion {
return fmt.Errorf("Application built with sbt-cloudflow version '%s', is incompatible and no longer supported. Please upgrade sbt-cloudflow and rebuild the application with 'sbt buildApp'", spec.LibraryVersion)
} else if version > SupportedApplicationDescriptorVersion {
return fmt.Errorf("Application built with sbt-cloudflow version '%s', is incompatible and requires a newer version of the kubectl cloudflow plugin. Please upgrade and try again", spec.LibraryVersion)
}
return nil
}