This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.go
251 lines (227 loc) · 8.55 KB
/
plugin.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
package sqs
import (
"encoding/json"
smith_v1 "github.com/atlassian/smith/pkg/apis/smith/v1"
"github.com/atlassian/voyager"
orch_v1 "github.com/atlassian/voyager/pkg/apis/orchestration/v1"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringplugin"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil/knownshapes"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil/oap"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil/osb"
sc_v1b1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/pkg/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
const (
ResourceType voyager.ResourceType = "SQS"
ResourcePrefix = "SQS"
clusterServiceClassExternalName = "sqs"
clusterServiceClassExternalID = "06068066-7f66-4297-8683-a1ba0a2b7401"
clusterServicePlanExternalID = "56393d2c-d936-4634-a178-19f491a3551a"
)
type snsSubscription struct {
TopicArn string `json:"topicArn"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
type partialSqsAttributes struct {
// this is a pointer so '==' comparisons to the empty struct work properly.
Subscriptions *[]snsSubscription `json:"Subscriptions,omitempty"`
}
// WireUp has marked similarities to the aws autowiring functions, but because
// they're entangled with SvcCatEntangler it was unpleasant to reuse them without
// exposing too much. This is a separate function - for the moment - because
// it needs to understand how to wire the dependencies, which is atypical
// for aws-osb-provider resources.
func WireUp(stateResource *orch_v1.StateResource, context *wiringplugin.WiringContext) wiringplugin.WiringResult {
if stateResource.Type != ResourceType {
return &wiringplugin.WiringResultFailure{
Error: errors.Errorf("invalid resource type: %q", stateResource.Type),
}
}
var wiredResources []smith_v1.Resource
var snsSubscriptions []snsSubscription
var references []smith_v1.Reference
for _, dependency := range context.Dependencies {
snsShape, found, err := knownshapes.FindSnsSubscribableShape(dependency.Contract.Shapes)
if err != nil {
return &wiringplugin.WiringResultFailure{
Error: err,
}
}
if !found {
// user error caused by invalid specified dependency
return &wiringplugin.WiringResultFailure{
Error: errors.Errorf("sqs is allowed to depend only on sns resource, but SnsSubscribableShape was not found in %q", dependency.Name),
IsExternalError: true,
}
}
serviceBinding := wiringutil.ConsumerProducerServiceBinding(stateResource.Name, dependency.Name, snsShape.Data.ServiceInstanceName.ToReference())
wiredResources = append(wiredResources, serviceBinding)
topicArnRef := snsShape.Data.TopicARN.ToReference(serviceBinding.Name)
references = append(references, topicArnRef)
snsSubscriptions = append(snsSubscriptions, snsSubscription{
TopicArn: topicArnRef.Ref(),
Attributes: dependency.Attributes,
})
}
serviceInstance, external, retriable, err := constructServiceInstance(stateResource, context, references, snsSubscriptions)
if err != nil {
return &wiringplugin.WiringResultFailure{
Error: err,
IsExternalError: external,
IsRetriableError: retriable,
}
}
wiredResources = append(wiredResources, serviceInstance)
var hasDeadLetterQueue bool
if stateResource.Spec != nil {
var spec struct {
MaxReceiveCount int `json:"MaxReceiveCount"`
}
err := json.Unmarshal(stateResource.Spec.Raw, &spec)
if err != nil {
return &wiringplugin.WiringResultFailure{
Error: errors.WithStack(err),
}
}
hasDeadLetterQueue = spec.MaxReceiveCount > 0
}
envVars := map[string]string{
"QUEUE_URL": "data.queue-url",
"QUEUE_NAME": "data.queue-name",
"QUEUE_ARN": "data.queue-arn",
"QUEUE_REGION": "data.queue-region",
}
if hasDeadLetterQueue {
envVars["DEAD_QUEUE_URL"] = "data.dead-queue-url"
envVars["DEAD_QUEUE_NAME"] = "data.dead-queue-name"
envVars["DEAD_QUEUE_ARN"] = "data.dead-queue-arn"
}
return &wiringplugin.WiringResultSuccess{
Contract: wiringplugin.ResourceContract{
Shapes: []wiringplugin.Shape{
knownshapes.NewBindableEnvironmentVariables(serviceInstance.Name, ResourcePrefix, envVars),
knownshapes.NewBindableIamAccessible(serviceInstance.Name, "data.IamPolicySnippet"),
},
},
Resources: wiredResources,
}
}
func constructServiceInstance(resource *orch_v1.StateResource, context *wiringplugin.WiringContext, references []smith_v1.Reference, snsSubscriptions []snsSubscription) (smith_v1.Resource, bool /* external */, bool /* retriable */, error) {
instanceID, err := osb.InstanceID(resource.Spec)
if err != nil {
return smith_v1.Resource{}, false, false, err
}
serviceName := context.StateContext.ServiceName
userServiceName, err := oap.ServiceName(resource.Spec)
if err != nil {
return smith_v1.Resource{}, false, false, err
}
if userServiceName != "" {
serviceName = userServiceName
}
attributes, alarms, external, retriable, err := constructSqsAttributes(resource, snsSubscriptions)
if err != nil {
return smith_v1.Resource{}, external, retriable, err
}
resourceName, err := oap.ResourceName(resource.Spec)
if err != nil {
return smith_v1.Resource{}, false, false, err
}
if resourceName == "" {
resourceName = string(resource.Name)
}
serviceProperties := context.StateContext.ServiceProperties
serviceInstanceSpec := oap.ServiceInstanceSpec{
ServiceName: serviceName,
Resource: oap.RPSResource{
Name: resourceName,
Type: clusterServiceClassExternalName,
Attributes: attributes,
Alarms: alarms,
},
Environment: oap.ServiceEnvironment{
AlarmEndpoints: oap.PagerdutyAlarmEndpoints(
serviceProperties.Notifications.PagerdutyEndpoint.CloudWatch,
serviceProperties.Notifications.LowPriorityPagerdutyEndpoint.CloudWatch),
Tags: context.StateContext.Tags,
},
}
serviceInstanceSpecBytes, err := json.Marshal(&serviceInstanceSpec)
if err != nil {
return smith_v1.Resource{}, false, false, err
}
return smith_v1.Resource{
Name: wiringutil.ServiceInstanceResourceName(resource.Name),
References: references,
Spec: smith_v1.ResourceSpec{
Object: &sc_v1b1.ServiceInstance{
TypeMeta: meta_v1.TypeMeta{
Kind: "ServiceInstance",
APIVersion: sc_v1b1.SchemeGroupVersion.String(),
},
ObjectMeta: meta_v1.ObjectMeta{
Name: wiringutil.ServiceInstanceMetaName(resource.Name),
},
Spec: sc_v1b1.ServiceInstanceSpec{
PlanReference: sc_v1b1.PlanReference{
ClusterServiceClassExternalID: clusterServiceClassExternalID,
ClusterServicePlanExternalID: clusterServicePlanExternalID,
},
Parameters: &runtime.RawExtension{
Raw: serviceInstanceSpecBytes,
},
ExternalID: instanceID,
},
},
},
}, false, false, nil
}
func constructSqsAttributes(resource *orch_v1.StateResource, subscriptions []snsSubscription) (json.RawMessage /* attributes */, json.RawMessage /* alarms */, bool /* external */, bool /* retriable */, error) {
// The user shouldn't be setting anything in our 'partialSqsAttributes', since
// _we_ control it. So let's make sure they're not and fail ASAP.
if resource.Spec != nil {
var currentPartialSpec partialSqsAttributes
if err := json.Unmarshal(resource.Spec.Raw, ¤tPartialSpec); err != nil {
return nil, nil, false, false, errors.Wrap(err, "can't unmarshal state spec into JSON object")
}
if currentPartialSpec != (partialSqsAttributes{}) {
// user error caused by invalid spec
return nil, nil, true, false, errors.Errorf("at least one autowired value not empty: %+v", currentPartialSpec)
}
}
var subscriptionSpec map[string]interface{}
if subscriptions != nil {
var err error
subscriptionSpec, err = runtime.DefaultUnstructuredConverter.ToUnstructured(&partialSqsAttributes{
Subscriptions: &subscriptions,
})
if err != nil {
return nil, nil, false, false, errors.WithStack(err)
}
}
userSpec, err := oap.FilterAttributes(resource.Spec)
if err != nil {
return nil, nil, false, false, err
}
alarms, err := oap.Alarms(resource.Spec)
if err != nil {
return nil, nil, false, false, err
}
sqsAttributes, err := wiringutil.Merge(subscriptionSpec, userSpec)
if err != nil {
return nil, nil, false, false, err
}
var attributes []byte
if len(sqsAttributes) == 0 {
attributes = nil
} else {
attributes, err = json.Marshal(sqsAttributes)
if err != nil {
return nil, nil, false, false, errors.WithStack(err)
}
}
return attributes, alarms, false, false, nil
}