-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
96 lines (83 loc) · 2.87 KB
/
main.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
// Copyright 2022 Namespace Labs Inc; 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.
package main
import (
"context"
"encoding/json"
"fmt"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/provisioning"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/universe/development/localstack/s3"
)
const (
// Bucket type name provided by the aws/s3 package.
bucketTypeName = "Bucket"
localstackPackageName = "namespacelabs.dev/foundation/universe/development/localstack"
localstackServiceName = "api"
initContainerToConfigure = "namespacelabs.dev/foundation/universe/development/localstack/s3/internal/managebuckets/init"
)
type tool struct{}
func main() {
h := provisioning.NewHandlers()
henv := h.MatchEnv(&schema.Environment{Runtime: "kubernetes"})
henv.HandleStack(tool{})
provisioning.Handle(h)
}
func collectBuckets(server *schema.Server, owner string) ([]*s3.BucketConfig, error) {
buckets := []*s3.BucketConfig{}
for _, alloc := range server.Allocation {
for _, instance := range alloc.Instance {
for _, instantiated := range instance.Instantiated {
if instantiated.GetPackageName() == owner && instantiated.GetType() == bucketTypeName {
bucket := &s3.BucketConfig{}
if err := proto.Unmarshal(instantiated.Constructor.Value, bucket); err != nil {
return nil, err
}
buckets = append(buckets, bucket)
}
}
}
}
return buckets, nil
}
func getLocalstackEndpoint(s *schema.Stack) string {
for _, e := range s.Endpoint {
if e.ServiceName == localstackServiceName && e.ServerOwner == localstackPackageName {
return fmt.Sprintf("http://%s:%d", e.AllocatedName, e.Port.ContainerPort)
}
}
return ""
}
func (tool) Apply(ctx context.Context, r provisioning.StackRequest, out *provisioning.ApplyOutput) error {
localstackEndpoint := getLocalstackEndpoint(r.Stack)
if localstackEndpoint == "" {
return fmt.Errorf("localstack endpoint is required")
}
args := []string{fmt.Sprintf("--init_localstack_endpoint=%s", localstackEndpoint)}
bucketConfigs, err := collectBuckets(r.Focus.Server, r.PackageOwner())
if err != nil {
return err
}
// Append json-serialized bucket configs.
for _, bucketConfig := range bucketConfigs {
json, err := json.Marshal(bucketConfig)
if err != nil {
return nil
}
args = append(args, string(json))
}
out.Extensions = append(out.Extensions, kubedef.ExtendContainer{
With: &kubedef.ContainerExtension{
InitContainer: []*kubedef.ContainerExtension_InitContainer{{
PackageName: initContainerToConfigure,
Arg: args,
}},
}})
return nil
}
func (tool) Delete(ctx context.Context, r provisioning.StackRequest, out *provisioning.DeleteOutput) error {
return nil
}