forked from crossplane-contrib/provider-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globaladdress.go
151 lines (125 loc) · 5.66 KB
/
globaladdress.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
/*
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 compute
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/option"
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/compute/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/globaladdress"
)
// Error strings.
const (
errNotGlobalAddress = "managed resource is not a GlobalAddress"
errGetAddress = "cannot get external Address resource"
errCreateAddress = "cannot create external Address resource"
errDeleteAddress = "cannot delete external Address resource"
errUpdateManaged = "cannot update managed resource"
)
// GlobalAddressController is the controller for GlobalAddress CRD.
type GlobalAddressController 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 *GlobalAddressController) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Named(strings.ToLower(fmt.Sprintf("%s.%s", v1alpha2.GlobalAddressKindAPIVersion, v1alpha2.Group))).
For(&v1alpha2.GlobalAddress{}).
Complete(resource.NewManagedReconciler(mgr,
resource.ManagedKind(v1alpha2.GlobalAddressGroupVersionKind),
resource.WithExternalConnecter(&gaConnector{client: mgr.GetClient(), newCompute: compute.NewService}),
resource.WithManagedConnectionPublishers()))
}
type gaConnector struct {
client client.Client
newCompute func(ctx context.Context, opts ...option.ClientOption) (*compute.Service, error)
}
func (c *gaConnector) Connect(ctx context.Context, mg resource.Managed) (resource.ExternalClient, error) {
ga, ok := mg.(*v1alpha2.GlobalAddress)
if !ok {
return nil, errors.New(errNotGlobalAddress)
}
p := &gcpv1alpha2.Provider{}
if err := c.client.Get(ctx, meta.NamespacedNameOf(ga.Spec.ProviderReference), p); err != nil {
return nil, errors.Wrap(err, errProviderNotRetrieved)
}
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, errProviderSecretNotRetrieved)
}
cmp, err := c.newCompute(ctx,
option.WithCredentialsJSON(s.Data[p.Spec.Secret.Key]),
option.WithScopes(compute.ComputeScope))
return &gaExternal{client: c.client, compute: cmp, projectID: p.Spec.ProjectID}, errors.Wrap(err, errNewClient)
}
type gaExternal struct {
client client.Client
compute *compute.Service
projectID string
}
func (e *gaExternal) Observe(ctx context.Context, mg resource.Managed) (resource.ExternalObservation, error) {
ga, ok := mg.(*v1alpha2.GlobalAddress)
if !ok {
return resource.ExternalObservation{}, errors.New(errNotGlobalAddress)
}
observed, err := e.compute.GlobalAddresses.Get(e.projectID, ga.Spec.Name).Context(ctx).Do()
if gcp.IsErrorNotFound(err) {
return resource.ExternalObservation{ResourceExists: false}, nil
}
if err != nil {
return resource.ExternalObservation{}, errors.Wrap(err, errGetAddress)
}
// Global addresses are always "up to date" because they can't be updated. ¯\_(ツ)_/¯
eo := resource.ExternalObservation{ResourceExists: true, ResourceUpToDate: true}
// NOTE(negz): We must update our parameters before our status to avoid
// client.Update overwriting our newly updated status with that most
// recently persisted to the API server.
globaladdress.UpdateParameters(&ga.Spec.GlobalAddressParameters, observed)
err = e.client.Update(ctx, ga)
globaladdress.UpdateStatus(&ga.Status, observed)
return eo, errors.Wrap(err, errUpdateManaged)
}
func (e *gaExternal) Create(ctx context.Context, mg resource.Managed) (resource.ExternalCreation, error) {
ga, ok := mg.(*v1alpha2.GlobalAddress)
if !ok {
return resource.ExternalCreation{}, errors.New(errNotGlobalAddress)
}
ga.Status.SetConditions(runtimev1alpha1.Creating())
address := globaladdress.FromParameters(ga.Spec.GlobalAddressParameters)
_, err := e.compute.GlobalAddresses.Insert(e.projectID, address).Context(ctx).Do()
return resource.ExternalCreation{}, errors.Wrap(resource.Ignore(gcp.IsErrorAlreadyExists, err), errCreateAddress)
}
func (e *gaExternal) Update(_ context.Context, _ resource.Managed) (resource.ExternalUpdate, error) {
// Global addresses cannot be updated.
return resource.ExternalUpdate{}, nil
}
func (e *gaExternal) Delete(ctx context.Context, mg resource.Managed) error {
ga, ok := mg.(*v1alpha2.GlobalAddress)
if !ok {
return errors.New(errNotGlobalAddress)
}
ga.Status.SetConditions(runtimev1alpha1.Deleting())
_, err := e.compute.GlobalAddresses.Delete(e.projectID, ga.Spec.Name).Context(ctx).Do()
return errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errDeleteAddress)
}