/
reconcile.go
195 lines (170 loc) · 5.37 KB
/
reconcile.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package server
import (
"context"
"fmt"
"github.com/Azure/azure-service-operator/api/v1alpha1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
"github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
func (m *MySQLFirewallRuleClient) Ensure(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) {
instance, err := m.convert(obj)
if err != nil {
return true, err
}
client := getMySQLFirewallRulesClient(m.creds)
instance.Status.Provisioning = true
// Check if this server already exists and its state if it does. This is required
// to overcome the issue with the lack of idempotence of the Create call
firewallrule, err := m.GetFirewallRule(ctx, instance.Spec.ResourceGroup, instance.Spec.Server, instance.Name)
if err == nil {
instance.Status.Provisioned = true
instance.Status.Provisioning = false
instance.Status.State = firewallrule.Status
instance.Status.Message = resourcemanager.SuccessMsg
return true, nil
}
future, err := m.CreateFirewallRule(
ctx,
instance.Spec.ResourceGroup,
instance.Spec.Server,
instance.Name,
instance.Spec.StartIPAddress,
instance.Spec.EndIPAddress,
)
if err != nil {
// let the user know what happened
instance.Status.Message = err.Error()
instance.Status.Provisioning = false
// errors we expect might happen that we are ok with waiting for
catch := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.AsyncOpIncompleteError,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
// most of these error technically mean the resource is actually not provisioning
switch azerr.Type {
case errhelp.AsyncOpIncompleteError:
instance.Status.Provisioning = true
}
// reconciliation is not done but error is acceptable
return false, nil
}
// reconciliation not done and we don't know what happened
return false, err
}
instance.Status.State = future.Status()
firewallrule, err = future.Result(client)
if err != nil {
// let the user know what happened
instance.Status.Message = err.Error()
instance.Status.Provisioning = false
// errors we expect might happen that we are ok with waiting for
catch := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
errhelp.AsyncOpIncompleteError,
}
azerr := errhelp.NewAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
// most of these error technically mean the resource is actually not provisioning
switch azerr.Type {
case errhelp.AsyncOpIncompleteError:
instance.Status.Provisioning = true
}
// reconciliation is not done but error is acceptable
return false, nil
}
// reconciliation not done and we don't know what happened
return false, err
}
instance.Status.State = firewallrule.Status
if instance.Status.Provisioning {
instance.Status.Provisioned = true
instance.Status.Provisioning = false
instance.Status.Message = resourcemanager.SuccessMsg
} else {
instance.Status.Provisioned = false
instance.Status.Provisioning = true
}
return true, nil
}
func (m *MySQLFirewallRuleClient) Delete(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) {
instance, err := m.convert(obj)
if err != nil {
return true, err
}
status, err := m.DeleteFirewallRule(ctx, instance.Spec.ResourceGroup, instance.Spec.Server, instance.Name)
if err != nil {
catch := []string{
errhelp.AsyncOpIncompleteError,
}
gone := []string{
errhelp.ResourceGroupNotFoundErrorCode,
errhelp.ParentNotFoundErrorCode,
errhelp.NotFoundErrorCode,
errhelp.ResourceNotFound,
}
azerr := errhelp.NewAzureError(err)
if helpers.ContainsString(catch, azerr.Type) {
return true, nil
} else if helpers.ContainsString(gone, azerr.Type) {
return false, nil
}
return true, err
}
instance.Status.State = status
if err == nil {
if status != "InProgress" {
return false, nil
}
}
return true, nil
}
func (m *MySQLFirewallRuleClient) GetParents(obj runtime.Object) ([]resourcemanager.KubeParent, error) {
instance, err := m.convert(obj)
if err != nil {
return nil, err
}
return []resourcemanager.KubeParent{
{
Key: types.NamespacedName{
Namespace: instance.Namespace,
Name: instance.Spec.Server,
},
Target: &azurev1alpha1.MySQLServer{},
},
{
Key: types.NamespacedName{
Namespace: instance.Namespace,
Name: instance.Spec.ResourceGroup,
},
Target: &azurev1alpha1.ResourceGroup{},
},
}, nil
}
func (m *MySQLFirewallRuleClient) GetStatus(obj runtime.Object) (*v1alpha1.ASOStatus, error) {
instance, err := m.convert(obj)
if err != nil {
return nil, err
}
return &instance.Status, nil
}
func (m *MySQLFirewallRuleClient) convert(obj runtime.Object) (*v1alpha1.MySQLFirewallRule, error) {
local, ok := obj.(*v1alpha1.MySQLFirewallRule)
if !ok {
return nil, fmt.Errorf("failed type assertion on kind: %s", obj.GetObjectKind().GroupVersionKind().String())
}
return local, nil
}