This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_repository.go
298 lines (260 loc) · 8.44 KB
/
app_repository.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
package repositories
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
workloadsv1alpha1 "code.cloudfoundry.org/cf-k8s-controllers/apis/workloads/v1alpha1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
_ "k8s.io/client-go/plugin/pkg/client/auth"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//+kubebuilder:rbac:groups=workloads.cloudfoundry.org,resources=cfapps,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=workloads.cloudfoundry.org,resources=cfapps/status,verbs=get
type AppRepo struct{}
const (
StartedState DesiredState = "STARTED"
StoppedState DesiredState = "STOPPED"
Kind string = "CFApp"
APIVersion string = "workloads.cloudfoundry.org/v1alpha1"
TimestampFormat string = time.RFC3339
CFAppGUIDLabel string = "apps.cloudfoundry.org/appGuid"
)
type AppRecord struct {
Name string
GUID string
SpaceGUID string
DropletGUID string
Labels map[string]string
Annotations map[string]string
State DesiredState
Lifecycle Lifecycle
EnvSecretName string
CreatedAt string
UpdatedAt string
}
type DesiredState string
type Lifecycle struct {
Type string
Data LifecycleData
}
type LifecycleData struct {
Buildpacks []string
Stack string
}
type AppEnvVarsRecord struct {
Name string
AppGUID string
SpaceGUID string
EnvironmentVariables map[string]string
}
type CurrentDropletRecord struct {
AppGUID string
DropletGUID string
}
func (f *AppRepo) FetchApp(ctx context.Context, client client.Client, appGUID string) (AppRecord, error) {
// TODO: Could look up namespace from guid => namespace cache to do Get
appList := &workloadsv1alpha1.CFAppList{}
err := client.List(ctx, appList)
if err != nil { // untested
return AppRecord{}, err
}
allApps := appList.Items
matches := filterAppsByMetadataName(allApps, appGUID)
return returnApp(matches)
}
func (f *AppRepo) CreateApp(ctx context.Context, client client.Client, appRecord AppRecord) (AppRecord, error) {
cfApp := appRecordToCFApp(appRecord)
err := client.Create(ctx, &cfApp)
if err != nil {
return AppRecord{}, err
}
return cfAppToAppRecord(cfApp), err
}
func (f *AppRepo) FetchAppList(ctx context.Context, client client.Client) ([]AppRecord, error) {
// TODO: add checks for allowed namespaces with completion of initial auth work
// Currently we assume the client has full cluster access and naively returns all records
appList := &workloadsv1alpha1.CFAppList{}
err := client.List(ctx, appList)
if err != nil {
return []AppRecord{}, err
}
allApps := appList.Items
appRecordList := make([]AppRecord, 0, len(allApps))
for _, app := range allApps {
appRecordList = append(appRecordList, cfAppToAppRecord(app))
}
return appRecordList, nil
}
func (f *AppRepo) FetchNamespace(ctx context.Context, client client.Client, nsGUID string) (SpaceRecord, error) {
namespace := &v1.Namespace{}
err := client.Get(ctx, types.NamespacedName{Name: nsGUID}, namespace)
if err != nil {
switch errtype := err.(type) {
case *k8serrors.StatusError:
reason := errtype.Status().Reason
if reason == metav1.StatusReasonNotFound || reason == metav1.StatusReasonUnauthorized {
return SpaceRecord{}, PermissionDeniedOrNotFoundError{Err: err}
}
}
return SpaceRecord{}, err
}
return v1NamespaceToSpaceRecord(namespace), nil
}
func (f *AppRepo) CreateAppEnvironmentVariables(ctx context.Context, client client.Client, envVariables AppEnvVarsRecord) (AppEnvVarsRecord, error) {
secretObj := appEnvVarsRecordToSecret(envVariables)
err := client.Create(ctx, &secretObj)
if err != nil {
return AppEnvVarsRecord{}, err
}
return appEnvVarsSecretToRecord(secretObj), nil
}
type SetCurrentDropletMessage struct {
AppGUID string
DropletGUID string
SpaceGUID string
}
func (f *AppRepo) SetCurrentDroplet(ctx context.Context, c client.Client, message SetCurrentDropletMessage) (CurrentDropletRecord, error) {
baseCFApp := &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: message.AppGUID,
Namespace: message.SpaceGUID,
},
}
cfApp := baseCFApp.DeepCopy()
cfApp.Spec.CurrentDropletRef = corev1.LocalObjectReference{Name: message.DropletGUID}
err := c.Patch(ctx, cfApp, client.MergeFrom(baseCFApp))
if err != nil {
return CurrentDropletRecord{}, fmt.Errorf("err in client.Patch: %w", err)
}
return CurrentDropletRecord{
AppGUID: message.AppGUID,
DropletGUID: message.DropletGUID,
}, nil
}
type SetAppDesiredStateMessage struct {
AppGUID string
SpaceGUID string
Value string
}
func (f *AppRepo) SetAppDesiredState(ctx context.Context, c client.Client, message SetAppDesiredStateMessage) (AppRecord, error) {
baseCFApp := &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: message.AppGUID,
Namespace: message.SpaceGUID,
},
}
cfApp := baseCFApp.DeepCopy()
cfApp.Spec.DesiredState = workloadsv1alpha1.DesiredState(StartedState)
err := c.Patch(ctx, cfApp, client.MergeFrom(baseCFApp))
if err != nil {
return AppRecord{}, fmt.Errorf("err in client.Patch: %w", err)
}
return cfAppToAppRecord(*cfApp), nil
}
func appRecordToCFApp(appRecord AppRecord) workloadsv1alpha1.CFApp {
return workloadsv1alpha1.CFApp{
TypeMeta: metav1.TypeMeta{
Kind: Kind,
APIVersion: APIVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: appRecord.GUID,
Namespace: appRecord.SpaceGUID,
Labels: appRecord.Labels,
Annotations: appRecord.Annotations,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: appRecord.Name,
DesiredState: workloadsv1alpha1.DesiredState(appRecord.State),
EnvSecretName: appRecord.EnvSecretName,
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: workloadsv1alpha1.LifecycleType(appRecord.Lifecycle.Type),
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: appRecord.Lifecycle.Data.Buildpacks,
Stack: appRecord.Lifecycle.Data.Stack,
},
},
},
}
}
func cfAppToAppRecord(cfApp workloadsv1alpha1.CFApp) AppRecord {
updatedAtTime, _ := getTimeLastUpdatedTimestamp(&cfApp.ObjectMeta)
return AppRecord{
GUID: cfApp.Name,
Name: cfApp.Spec.Name,
SpaceGUID: cfApp.Namespace,
DropletGUID: cfApp.Spec.CurrentDropletRef.Name,
Labels: cfApp.Labels,
Annotations: cfApp.Annotations,
State: DesiredState(cfApp.Spec.DesiredState),
Lifecycle: Lifecycle{
Data: LifecycleData{
Buildpacks: cfApp.Spec.Lifecycle.Data.Buildpacks,
Stack: cfApp.Spec.Lifecycle.Data.Stack,
},
},
CreatedAt: cfApp.CreationTimestamp.UTC().Format(TimestampFormat),
UpdatedAt: updatedAtTime,
}
}
func returnApp(apps []workloadsv1alpha1.CFApp) (AppRecord, error) {
if len(apps) == 0 {
return AppRecord{}, NotFoundError{}
}
if len(apps) > 1 {
return AppRecord{}, errors.New("duplicate apps exist")
}
return cfAppToAppRecord(apps[0]), nil
}
func filterAppsByMetadataName(apps []workloadsv1alpha1.CFApp, name string) []workloadsv1alpha1.CFApp {
var filtered []workloadsv1alpha1.CFApp
for i, app := range apps {
if app.ObjectMeta.Name == name {
filtered = append(filtered, apps[i])
}
}
return filtered
}
func v1NamespaceToSpaceRecord(namespace *v1.Namespace) SpaceRecord {
// TODO How do we derive Organization GUID here?
return SpaceRecord{
Name: namespace.Name,
OrganizationGUID: "",
}
}
func appEnvVarsRecordToSecret(envVars AppEnvVarsRecord) corev1.Secret {
labels := make(map[string]string, 1)
labels[CFAppGUIDLabel] = envVars.AppGUID
envSecretName := envVars.AppGUID + "-env"
return corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: envSecretName,
Namespace: envVars.SpaceGUID,
Labels: labels,
},
StringData: envVars.EnvironmentVariables,
}
}
func appEnvVarsSecretToRecord(envVars corev1.Secret) AppEnvVarsRecord {
appGUID := strings.TrimSuffix(envVars.Name, "-env")
return AppEnvVarsRecord{
Name: envVars.Name,
AppGUID: appGUID,
SpaceGUID: envVars.Namespace,
EnvironmentVariables: convertByteSliceValuesToStrings(envVars.Data),
}
}
func convertByteSliceValuesToStrings(inputMap map[string][]byte) map[string]string {
// StringData is a write-only field of a corev1.Secret, the real data lives in .Data and is []byte & base64 encoded
marshalledData, _ := json.Marshal(inputMap)
outputMap := make(map[string]string)
json.Unmarshal(marshalledData, &outputMap)
return outputMap
}