forked from crossplane/crossplane
/
gcp_handler.go
107 lines (91 loc) · 3.7 KB
/
gcp_handler.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
/*
Copyright 2018 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 mysql
import (
"fmt"
"strings"
corev1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/core/v1alpha1"
gcpdbv1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/gcp/database/v1alpha1"
mysqlv1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/storage/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// CloudSQLServerHandler is a dynamic provisioning handler for CloudSQL instance
type CloudSQLServerHandler struct{}
// find CloudSQL instance
func (h *CloudSQLServerHandler) find(name types.NamespacedName, c client.Client) (corev1alpha1.Resource, error) {
cloudsqlInstance := &gcpdbv1alpha1.CloudsqlInstance{}
err := c.Get(ctx, name, cloudsqlInstance)
return cloudsqlInstance, err
}
// provision (create) a new CloudSQL instance
func (h *CloudSQLServerHandler) provision(class *corev1alpha1.ResourceClass, instance *mysqlv1alpha1.MySQLInstance, c client.Client) (corev1alpha1.Resource, error) {
// construct CloudSQL instance spec from class definition/parameters
cloudsqlInstanceSpec := gcpdbv1alpha1.NewCloudSQLInstanceSpec(class.Parameters)
// translate and validate engine version
var err error
cloudsqlInstanceSpec.DatabaseVersion, err = resolveClassInstanceValues(cloudsqlInstanceSpec.DatabaseVersion, translateVersion(instance.Spec.EngineVersion))
if err != nil {
return nil, err
}
// assign provider reference and reclaim policy from the resource class
cloudsqlInstanceSpec.ProviderRef = class.ProviderRef
cloudsqlInstanceSpec.ReclaimPolicy = class.ReclaimPolicy
// set class and claim references
cloudsqlInstanceSpec.ClassRef = class.ObjectReference()
cloudsqlInstanceSpec.ClaimRef = instance.ObjectReference()
// create and save CloudSQL instance instance
cloudsqlInstance := &gcpdbv1alpha1.CloudsqlInstance{
TypeMeta: metav1.TypeMeta{
APIVersion: gcpdbv1alpha1.APIVersion,
Kind: gcpdbv1alpha1.CloudsqlInstanceKind,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: class.Namespace,
Name: fmt.Sprintf("mysql-%s", instance.UID),
OwnerReferences: []metav1.OwnerReference{instance.OwnerReference()},
},
Spec: *cloudsqlInstanceSpec,
}
err = c.Create(ctx, cloudsqlInstance)
return cloudsqlInstance, err
}
// bind updates resource state binding phase
// - state = true: bound
// - state = false: unbound
// TODO: this setBindStatus function could be refactored to 1 common implementation for all providers
func (h *CloudSQLServerHandler) setBindStatus(name types.NamespacedName, c client.Client, state bool) error {
cloudsqlInstance := &gcpdbv1alpha1.CloudsqlInstance{}
err := c.Get(ctx, name, cloudsqlInstance)
if err != nil {
// TODO: the CRD is not found and the binding state is supposed to be unbound. is this OK?
if errors.IsNotFound(err) && !state {
return nil
}
return err
}
if state {
cloudsqlInstance.Status.SetBound()
} else {
cloudsqlInstance.Status.SetUnbound()
}
return c.Update(ctx, cloudsqlInstance)
}
func translateVersion(version string) string {
if version == "" {
return ""
}
return fmt.Sprintf("MYSQL_%s", strings.Replace(version, ".", "_", -1))
}