forked from crossplane-contrib/provider-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
210 lines (179 loc) · 8.06 KB
/
connection.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
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package servicenetworking
import (
"context"
"fmt"
"path"
"strings"
"github.com/pkg/errors"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/option"
servicenetworking "google.golang.org/api/servicenetworking/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimev1alpha1 "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1"
"github.com/crossplaneio/crossplane-runtime/pkg/meta"
"github.com/crossplaneio/crossplane-runtime/pkg/resource"
"github.com/crossplaneio/stack-gcp/gcp/apis/servicenetworking/v1alpha2"
gcpv1alpha2 "github.com/crossplaneio/stack-gcp/gcp/apis/v1alpha2"
"github.com/crossplaneio/stack-gcp/pkg/clients/gcp"
"github.com/crossplaneio/stack-gcp/pkg/clients/gcp/connection"
)
// Error strings.
const (
errGetProvider = "cannot get provider"
errGetProviderSecret = "cannot get provider secret"
errNewClient = "cannot create new Compute Service"
errNotConnection = "managed resource is not a Connection"
errListConnections = "cannot list external Connection resources"
errGetNetwork = "cannot get VPC Network"
errCreateConnection = "cannot create external Connection resource"
errUpdateConnection = "cannot update external Connection resource"
errDeleteConnection = "cannot delete external Connection resource"
)
// NOTE(negz): There is no 'Get' method for connections, only 'List', and the
// behaviour of the API is not well documented. I am assuming based on the docs
// and my observations of the API, Console, and Terraform implementation of this
// resource that:
//
// * You can only create connections for service
// 'services/servicenetworking.googleapis.com' via the API.
// * You cannot create multiple connections for service
// 'services/servicenetworking.googleapis.com' via the API.
// * Connections created via the API for service
// 'services/servicenetworking.googleapis.com' always produce a peering named
// 'servicenetworking-googleapis-com'.
//
// I note that when I create a MySQL instance with a private IP via the console
// I am prompted to create a new connection if one does not exist. This creates
// a connection for service 'services/servicenetworking.googleapis.com' with a
// peering (to a different VPC network) named 'cloudsql-mysql-googleapis-com'. I
// presume this is dark Google magic that is not exposed to API callers.
// https://cloud.google.com/service-infrastructure/docs/service-networking/reference/rest/v1/services.connections/list
const peeringName = "servicenetworking-googleapis-com"
// ConnectionController is the controller for Connection CRD.
type ConnectionController struct{}
// SetupWithManager creates a new Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func (c *ConnectionController) SetupWithManager(mgr ctrl.Manager) error {
conn := &connector{
client: mgr.GetClient(),
newCompute: compute.NewService,
newServiceNetworking: servicenetworking.NewService,
}
return ctrl.NewControllerManagedBy(mgr).
Named(strings.ToLower(fmt.Sprintf("%s.%s", v1alpha2.ConnectionKindAPIVersion, v1alpha2.Group))).
For(&v1alpha2.Connection{}).
Complete(resource.NewManagedReconciler(mgr,
resource.ManagedKind(v1alpha2.ConnectionGroupVersionKind),
resource.WithExternalConnecter(conn),
resource.WithManagedConnectionPublishers()))
}
type connector struct {
client client.Client
newCompute func(ctx context.Context, opts ...option.ClientOption) (*compute.Service, error)
newServiceNetworking func(ctx context.Context, opts ...option.ClientOption) (*servicenetworking.APIService, error)
}
func (c *connector) Connect(ctx context.Context, mg resource.Managed) (resource.ExternalClient, error) {
ga, ok := mg.(*v1alpha2.Connection)
if !ok {
return nil, errors.New(errNotConnection)
}
p := &gcpv1alpha2.Provider{}
if err := c.client.Get(ctx, meta.NamespacedNameOf(ga.Spec.ProviderReference), p); err != nil {
return nil, errors.Wrap(err, errGetProvider)
}
s := &v1.Secret{}
if err := c.client.Get(ctx, types.NamespacedName{Namespace: p.Namespace, Name: p.Spec.Secret.Name}, s); err != nil {
return nil, errors.Wrap(err, errGetProviderSecret)
}
cmp, err := c.newCompute(ctx,
option.WithCredentialsJSON(s.Data[p.Spec.Secret.Key]),
option.WithScopes(compute.ComputeScope))
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
sn, err := c.newServiceNetworking(ctx,
option.WithCredentialsJSON(s.Data[p.Spec.Secret.Key]),
option.WithScopes(servicenetworking.ServiceManagementScope))
return &external{sn: sn, compute: cmp, projectID: p.Spec.ProjectID}, errors.Wrap(err, errNewClient)
}
type external struct {
compute *compute.Service
sn *servicenetworking.APIService
projectID string
}
func (e *external) Observe(ctx context.Context, mg resource.Managed) (resource.ExternalObservation, error) {
cn, ok := mg.(*v1alpha2.Connection)
if !ok {
return resource.ExternalObservation{}, errors.New(errNotConnection)
}
r, err := e.sn.Services.Connections.List(cn.Spec.Parent).Network(cn.Spec.Network).Context(ctx).Do()
if err != nil {
return resource.ExternalObservation{}, errors.Wrap(err, errListConnections)
}
o := connection.Observation{Connection: findConnection(r.Connections)}
if o.Connection == nil {
return resource.ExternalObservation{ResourceExists: false}, nil
}
if o.Network, err = e.compute.Networks.Get(e.projectID, path.Base(o.Connection.Network)).Context(ctx).Do(); err != nil {
return resource.ExternalObservation{}, errors.Wrap(err, errGetNetwork)
}
eo := resource.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: connection.UpToDate(cn.Spec.ConnectionParameters, o.Connection),
}
connection.UpdateStatus(&cn.Status, o)
return eo, nil
}
func findConnection(conns []*servicenetworking.Connection) *servicenetworking.Connection {
for _, c := range conns {
if c.Peering == peeringName {
return c
}
}
return nil
}
func (e *external) Create(ctx context.Context, mg resource.Managed) (resource.ExternalCreation, error) {
cn, ok := mg.(*v1alpha2.Connection)
if !ok {
return resource.ExternalCreation{}, errors.New(errNotConnection)
}
cn.Status.SetConditions(runtimev1alpha1.Creating())
conn := connection.FromParameters(cn.Spec.ConnectionParameters)
_, err := e.sn.Services.Connections.Create(cn.Spec.Parent, conn).Context(ctx).Do()
return resource.ExternalCreation{}, errors.Wrap(resource.Ignore(gcp.IsErrorAlreadyExists, err), errCreateConnection)
}
func (e *external) Update(ctx context.Context, mg resource.Managed) (resource.ExternalUpdate, error) {
cn, ok := mg.(*v1alpha2.Connection)
if !ok {
return resource.ExternalUpdate{}, errors.New(errNotConnection)
}
name := fmt.Sprintf("%s/connections/%s", cn.Spec.Parent, peeringName)
conn := connection.FromParameters(cn.Spec.ConnectionParameters)
_, err := e.sn.Services.Connections.Patch(name, conn).Context(ctx).Do()
return resource.ExternalUpdate{}, errors.Wrap(err, errUpdateConnection)
}
func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
cn, ok := mg.(*v1alpha2.Connection)
if !ok {
return errors.New(errNotConnection)
}
cn.Status.SetConditions(runtimev1alpha1.Deleting())
rm := &compute.NetworksRemovePeeringRequest{Name: cn.Status.Peering}
_, err := e.compute.Networks.RemovePeering(e.projectID, path.Base(cn.Spec.Network), rm).Context(ctx).Do()
return errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errDeleteConnection)
}