-
Notifications
You must be signed in to change notification settings - Fork 20
/
projectvpc_controller.go
158 lines (124 loc) · 4.51 KB
/
projectvpc_controller.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
// Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/
package controllers
import (
"context"
"fmt"
"strconv"
"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/aiven/aiven-operator/api/v1alpha1"
)
var isDependencyError = v1alpha1.ErrorSubstrChecker(
"VPC cannot be deleted while there are services in it",
"VPC cannot be deleted while there are services migrating from it",
)
// ProjectVPCReconciler reconciles a ProjectVPC object
type ProjectVPCReconciler struct {
Controller
}
func newProjectVPCReconciler(c Controller) reconcilerType {
return &ProjectVPCReconciler{Controller: c}
}
type ProjectVPCHandler struct {
log logr.Logger
}
//+kubebuilder:rbac:groups=aiven.io,resources=projectvpcs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=projectvpcs/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=projectvpcs/finalizers,verbs=get;create;update
func (r *ProjectVPCReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, &ProjectVPCHandler{log: r.Log}, &v1alpha1.ProjectVPC{})
}
func (r *ProjectVPCReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.ProjectVPC{}).
Complete(r)
}
func (h *ProjectVPCHandler) createOrUpdate(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object, refs []client.Object) error {
projectVPC, err := h.convert(obj)
if err != nil {
return err
}
vpc, err := avn.VPCs.Create(ctx, projectVPC.Spec.Project, aiven.CreateVPCRequest{
CloudName: projectVPC.Spec.CloudName,
NetworkCIDR: projectVPC.Spec.NetworkCidr,
})
if err != nil {
return err
}
projectVPC.Status.ID = vpc.ProjectVPCID
meta.SetStatusCondition(&projectVPC.Status.Conditions,
getInitializedCondition("Created",
"Successfully created or updated the instance in Aiven"))
meta.SetStatusCondition(&projectVPC.Status.Conditions,
getRunningCondition(metav1.ConditionUnknown, "Created",
"Successfully created or updated the instance in Aiven, status remains unknown"))
metav1.SetMetaDataAnnotation(&projectVPC.ObjectMeta,
processedGenerationAnnotation, strconv.FormatInt(projectVPC.GetGeneration(), formatIntBaseDecimal))
return nil
}
func (h *ProjectVPCHandler) delete(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
projectVPC, err := h.convert(obj)
if err != nil {
return false, err
}
vpc, err := avn.VPCs.Get(ctx, projectVPC.Spec.Project, projectVPC.Status.ID)
if isNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
switch vpc.State {
case "DELETING", "DELETED":
return true, nil
}
services, err := avn.Services.List(ctx, projectVPC.Spec.Project)
if err != nil {
return false, err
}
for _, s := range services {
if s.ProjectVPCID != nil && *s.ProjectVPCID == projectVPC.Status.ID {
h.log.Info(fmt.Sprintf("vpc has dependent service %q in status %q", s.Name, s.State))
return false, nil
}
}
err = avn.VPCs.Delete(ctx, projectVPC.Spec.Project, projectVPC.Status.ID)
if isDependencyError(err) {
return false, fmt.Errorf("%w: %s", v1alpha1.ErrDeleteDependencies, err)
}
return false, nil
}
func (h *ProjectVPCHandler) get(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (*corev1.Secret, error) {
projectVPC, err := h.convert(obj)
if err != nil {
return nil, err
}
vpc, err := avn.VPCs.Get(ctx, projectVPC.Spec.Project, projectVPC.Status.ID)
if err != nil {
return nil, err
}
projectVPC.Status.State = vpc.State
if vpc.State == "ACTIVE" {
meta.SetStatusCondition(&projectVPC.Status.Conditions,
getRunningCondition(metav1.ConditionTrue, "CheckRunning",
"Instance is running on Aiven side"))
metav1.SetMetaDataAnnotation(&projectVPC.ObjectMeta, instanceIsRunningAnnotation, "true")
}
return nil, nil
}
func (h *ProjectVPCHandler) checkPreconditions(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
return true, nil
}
func (h *ProjectVPCHandler) convert(i client.Object) (*v1alpha1.ProjectVPC, error) {
vpc, ok := i.(*v1alpha1.ProjectVPC)
if !ok {
return nil, fmt.Errorf("cannot convert object to ProjectVPC")
}
return vpc, nil
}