-
Notifications
You must be signed in to change notification settings - Fork 20
/
serviceuser_controller.go
175 lines (141 loc) · 5.07 KB
/
serviceuser_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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/aiven/go-client-codegen/handler/service"
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"
)
// ServiceUserReconciler reconciles a ServiceUser object
type ServiceUserReconciler struct {
Controller
}
func newServiceUserReconciler(c Controller) reconcilerType {
return &ServiceUserReconciler{Controller: c}
}
type ServiceUserHandler struct{}
//+kubebuilder:rbac:groups=aiven.io,resources=serviceusers,verbs=update;get;list;watch;create;delete
//+kubebuilder:rbac:groups=aiven.io,resources=serviceusers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=serviceusers/finalizers,verbs=get;create;update
func (r *ServiceUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, ServiceUserHandler{}, &v1alpha1.ServiceUser{})
}
func (r *ServiceUserReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.ServiceUser{}).
Complete(r)
}
func (h ServiceUserHandler) createOrUpdate(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object, refs []client.Object) error {
user, err := h.convert(obj)
if err != nil {
return err
}
u, err := avn.ServiceUsers.Create(ctx, user.Spec.Project, user.Spec.ServiceName,
aiven.CreateServiceUserRequest{
Username: user.Name,
AccessControl: &aiven.AccessControl{
RedisACLCategories: []string{},
RedisACLCommands: []string{},
RedisACLChannels: []string{},
RedisACLKeys: []string{},
},
})
if err != nil && !isAlreadyExists(err) {
return fmt.Errorf("cannot createOrUpdate service user on aiven side: %w", err)
}
if u != nil {
user.Status.Type = u.Type
}
meta.SetStatusCondition(&user.Status.Conditions,
getInitializedCondition("Created",
"Successfully created or updated the instance in Aiven"))
metav1.SetMetaDataAnnotation(&user.ObjectMeta,
processedGenerationAnnotation, strconv.FormatInt(user.GetGeneration(), formatIntBaseDecimal))
return nil
}
func (h ServiceUserHandler) delete(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
user, err := h.convert(obj)
if err != nil {
return false, err
}
err = avn.ServiceUsers.Delete(ctx, user.Spec.Project, user.Spec.ServiceName, user.Name)
if !isNotFound(err) {
return false, err
}
return true, nil
}
func (h ServiceUserHandler) get(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (*corev1.Secret, error) {
user, err := h.convert(obj)
if err != nil {
return nil, err
}
u, err := avn.ServiceUsers.Get(ctx, user.Spec.Project, user.Spec.ServiceName, user.Name)
if err != nil {
return nil, err
}
s, err := avnGen.ServiceGet(ctx, user.Spec.Project, user.Spec.ServiceName)
if err != nil {
return nil, err
}
var component *service.ComponentOut
for _, c := range s.Components {
if c.Component == s.ServiceType {
component = &c
break
}
}
if component == nil {
return nil, fmt.Errorf("service component %q not found", s.ServiceType)
}
caCert, err := avn.CA.Get(ctx, user.Spec.Project)
if err != nil {
return nil, fmt.Errorf("aiven client error %w", err)
}
meta.SetStatusCondition(&user.Status.Conditions,
getRunningCondition(metav1.ConditionTrue, "CheckRunning",
"Instance is running on Aiven side"))
metav1.SetMetaDataAnnotation(&user.ObjectMeta, instanceIsRunningAnnotation, "true")
prefix := getSecretPrefix(user)
stringData := map[string]string{
prefix + "HOST": component.Host,
prefix + "PORT": fmt.Sprintf("%d", component.Port),
prefix + "USERNAME": u.Username,
prefix + "PASSWORD": u.Password,
prefix + "ACCESS_CERT": u.AccessCert,
prefix + "ACCESS_KEY": u.AccessKey,
prefix + "CA_CERT": caCert,
// todo: remove in future releases
"HOST": component.Host,
"PORT": fmt.Sprintf("%d", component.Port),
"USERNAME": u.Username,
"PASSWORD": u.Password,
"ACCESS_CERT": u.AccessCert,
"ACCESS_KEY": u.AccessKey,
"CA_CERT": caCert,
}
return newSecret(user, stringData, false), nil
}
func (h ServiceUserHandler) checkPreconditions(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
user, err := h.convert(obj)
if err != nil {
return false, err
}
meta.SetStatusCondition(&user.Status.Conditions,
getInitializedCondition("Preconditions", "Checking preconditions"))
return checkServiceIsOperational(ctx, avnGen, user.Spec.Project, user.Spec.ServiceName)
}
func (h ServiceUserHandler) convert(i client.Object) (*v1alpha1.ServiceUser, error) {
db, ok := i.(*v1alpha1.ServiceUser)
if !ok {
return nil, fmt.Errorf("cannot convert object to ServiceUser")
}
return db, nil
}