-
Notifications
You must be signed in to change notification settings - Fork 20
/
clickhouserole_controller.go
161 lines (129 loc) · 5.14 KB
/
clickhouserole_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
// Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/
package controllers
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
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"
)
// default database used for statements that do not target a particular database
// think CREATE ROLE / GRANT / etc...
const defaultDatabase = "system"
func newClickhouseRoleReconciler(c Controller) reconcilerType {
return &ClickhouseRoleReconciler{Controller: c}
}
// ClickhouseRoleReconciler reconciles a ClickhouseRole object
type ClickhouseRoleReconciler struct {
Controller
}
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouseroles,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouseroles/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=aiven.io,resources=clickhouseroles/finalizers,verbs=get;create;update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *ClickhouseRoleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.reconcileInstance(ctx, req, &clickhouseRoleHandler{}, &v1alpha1.ClickhouseRole{})
}
// SetupWithManager sets up the controller with the Manager.
func (r *ClickhouseRoleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.ClickhouseRole{}).
Complete(r)
}
type clickhouseRoleHandler struct{}
func (h *clickhouseRoleHandler) createOrUpdate(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object, refs []client.Object) error {
role, err := h.convert(obj)
if err != nil {
return err
}
err = clickhouseRoleCreate(ctx, avn, role)
if err != nil {
return err
}
meta.SetStatusCondition(&role.Status.Conditions,
getInitializedCondition("Created",
"Successfully created or updated the instance in Aiven"))
metav1.SetMetaDataAnnotation(&role.ObjectMeta,
processedGenerationAnnotation, strconv.FormatInt(role.GetGeneration(), formatIntBaseDecimal))
return nil
}
func (h *clickhouseRoleHandler) delete(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
role, err := h.convert(obj)
if err != nil {
return false, err
}
err = clickhouseRoleDelete(ctx, avn, role)
return isDeleted(err)
}
func (h *clickhouseRoleHandler) get(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (*corev1.Secret, error) {
role, err := h.convert(obj)
if err != nil {
return nil, err
}
err = ClickhouseRoleExists(ctx, avn, role)
if err != nil {
return nil, err
}
meta.SetStatusCondition(&role.Status.Conditions,
getRunningCondition(metav1.ConditionTrue, "CheckRunning",
"Instance is running on Aiven side"))
metav1.SetMetaDataAnnotation(&role.ObjectMeta, instanceIsRunningAnnotation, "true")
return nil, nil
}
func (h *clickhouseRoleHandler) checkPreconditions(ctx context.Context, avn *aiven.Client, avnGen avngen.Client, obj client.Object) (bool, error) {
role, err := h.convert(obj)
if err != nil {
return false, err
}
meta.SetStatusCondition(&role.Status.Conditions,
getInitializedCondition("Preconditions", "Checking preconditions"))
return checkServiceIsOperational(ctx, avnGen, role.Spec.Project, role.Spec.ServiceName)
}
func (h *clickhouseRoleHandler) convert(i client.Object) (*v1alpha1.ClickhouseRole, error) {
role, ok := i.(*v1alpha1.ClickhouseRole)
if !ok {
return nil, fmt.Errorf("cannot convert object to ClickhouseRole")
}
return role, nil
}
// Escapes database identifiers like table or column names
func escape(identifier string) string {
// See https://github.com/ClickHouse/clickhouse-go/blob/8ad6ec6b95d8b0c96d00115bc2d69ff13083f94b/lib/column/column.go#L32
replacer := strings.NewReplacer("`", "\\`", "\\", "\\\\")
return "`" + replacer.Replace(identifier) + "`"
}
func runQuery(ctx context.Context, avn *aiven.Client, r *v1alpha1.ClickhouseRole, query string) error {
q := fmt.Sprintf("%s %s", query, escape(r.Spec.Role))
_, err := avn.ClickHouseQuery.Query(ctx, r.Spec.Project, r.Spec.ServiceName, defaultDatabase, q)
return err
}
func ClickhouseRoleExists(ctx context.Context, avn *aiven.Client, r *v1alpha1.ClickhouseRole) error {
err := runQuery(ctx, avn, r, "SHOW CREATE ROLE")
if isUnknownRole(err) {
return NewNotFound(fmt.Sprintf("ClickhouseRole %q not found", r.Name))
}
return err
}
func clickhouseRoleCreate(ctx context.Context, avn *aiven.Client, r *v1alpha1.ClickhouseRole) error {
return runQuery(ctx, avn, r, "CREATE ROLE IF NOT EXISTS")
}
func clickhouseRoleDelete(ctx context.Context, avn *aiven.Client, r *v1alpha1.ClickhouseRole) error {
err := runQuery(ctx, avn, r, "DROP ROLE IF EXISTS")
if isUnknownRole(err) {
return nil
}
return err
}
func isUnknownRole(err error) bool {
var e aiven.Error
return errors.As(err, &e) && strings.Contains(e.Message, "Code: 511")
}