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
158 lines (135 loc) · 5.18 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
package platformdns
import (
"encoding/json"
"github.com/atlassian/voyager/pkg/orchestration/wiring/wiringutil/osb"
smith_v1 "github.com/atlassian/smith/pkg/apis/smith/v1"
"github.com/atlassian/voyager"
orch_v1 "github.com/atlassian/voyager/pkg/apis/orchestration/v1"
apiplatformdns "github.com/atlassian/voyager/pkg/orchestration/wiring/platformdns/api"
"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/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
)
type autowiringOnlySpec struct {
Target string `json:"target"`
ServiceName voyager.ServiceName `json:"serviceName"`
EnvironmentType string `json:"environmentType"`
}
type WiringPlugin struct {
}
func New() *WiringPlugin {
return &WiringPlugin{}
}
func (p *WiringPlugin) WireUp(resource *orch_v1.StateResource, context *wiringplugin.WiringContext) wiringplugin.WiringResult {
if resource.Type != apiplatformdns.ResourceType {
return &wiringplugin.WiringResultFailure{
Error: errors.Errorf("invalid resource type: %q", resource.Type),
}
}
serviceInstance, err := osb.ConstructServiceInstance(resource, apiplatformdns.ClusterServiceClassExternalID, apiplatformdns.ClusterServicePlanExternalID)
if err != nil {
return &wiringplugin.WiringResultFailure{
Error: err,
}
}
instanceParameters, external, retriable, err := instanceParameters(resource, context)
if err != nil {
return &wiringplugin.WiringResultFailure{
Error: err,
IsExternalError: external,
IsRetriableError: retriable,
}
}
serviceInstance.Spec.Parameters = &runtime.RawExtension{
Raw: instanceParameters,
}
references, external, retriable, err := getReferences(resource, context)
if err != nil {
return &wiringplugin.WiringResultFailure{
Error: err,
IsExternalError: external,
IsRetriableError: retriable,
}
}
instanceResourceName := wiringutil.ServiceInstanceResourceName(resource.Name)
smithResource := smith_v1.Resource{
Name: instanceResourceName,
References: references,
Spec: smith_v1.ResourceSpec{
Object: serviceInstance,
},
}
return &wiringplugin.WiringResultSuccess{
Contract: wiringplugin.ResourceContract{
Shapes: nil, // no shapes
},
Resources: []smith_v1.Resource{smithResource},
}
}
func instanceParameters(resource *orch_v1.StateResource, context *wiringplugin.WiringContext) ([]byte, bool /* externalErr */, bool /* retriable */, error) {
// Don't allow user to set anything they shouldn't
if resource.Spec != nil {
var ourSpec autowiringOnlySpec
if err := json.Unmarshal(resource.Spec.Raw, &ourSpec); err != nil {
return nil, false, false, errors.WithStack(err)
}
if ourSpec != (autowiringOnlySpec{}) {
return nil, true, false, errors.Errorf("unsupported parameters were provided: %+v", ourSpec)
}
}
references, external, retriable, err := getReferences(resource, context)
if err != nil {
// User error from providing invalid dependency
return nil, external, retriable, err
}
autowiringSpec, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&autowiringOnlySpec{
Target: references[0].Ref(),
EnvironmentType: mapEnvironmentType(context.StateContext.Location.EnvType),
ServiceName: context.StateContext.ServiceName,
})
if err != nil {
return nil, false, false, errors.WithStack(err)
}
var userSpec map[string]interface{}
if resource.Spec != nil {
if err = json.Unmarshal(resource.Spec.Raw, &userSpec); err != nil {
return nil, false, false, errors.WithStack(err)
}
}
finalSpec, err := wiringutil.Merge(userSpec, autowiringSpec)
if err != nil {
return nil, false, false, errors.WithStack(err)
}
bytes, err := json.Marshal(&finalSpec)
return bytes, false, false, err
}
func mapEnvironmentType(envType voyager.EnvType) string {
// DNS provider expects full string of production, rather than "prod"
if envType == voyager.EnvTypeProduction {
return "production"
}
// currently the other environment types match
return string(envType)
}
// svccatentangler plugin expects reference function to return a slice of references, in the case of platformdns it will
// always be a single reference.
func getReferences(_ *orch_v1.StateResource, context *wiringplugin.WiringContext) ([]smith_v1.Reference, bool /* externalErr */, bool /* retriable */, error) {
var references []smith_v1.Reference
// Ensure we only depend on one resource, as we can only bind to a single ingress
if len(context.Dependencies) != 1 {
return nil, true, false, errors.Errorf("%s resources must depend on only one ingress resource", apiplatformdns.ResourceType)
}
dependency := context.Dependencies[0]
ingressShape, found, err := knownshapes.FindIngressEndpointShape(dependency.Contract.Shapes)
if err != nil {
return nil, false, false, err
}
if !found {
return nil, true, false, errors.Errorf("shape %q is required to create ServiceBinding for %q but was not found",
knownshapes.IngressEndpointShape, dependency.Name)
}
references = append(references, ingressShape.Data.IngressEndpoint.ToReference())
return references, false, false, nil
}