/
mariadbdatabase_helpers.go
182 lines (154 loc) · 5.36 KB
/
mariadbdatabase_helpers.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
/*
Copyright AppsCode Inc. and Contributors
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 v1alpha1
import (
"context"
dbapi "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"kubedb.dev/apimachinery/crds"
"kubedb.dev/apimachinery/pkg/double_optin"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"kmodules.xyz/client-go/apiextensions"
"kmodules.xyz/client-go/meta"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
MariaDBSuffix string = dbapi.ResourceSingularMariaDB
)
func (_ MariaDBDatabase) CustomResourceDefinition() *apiextensions.CustomResourceDefinition {
return crds.MustCustomResourceDefinition(SchemeGroupVersion.WithResource(ResourceMariaDBDatabases))
}
var _ Interface = &MariaDBDatabase{}
func (in *MariaDBDatabase) GetInit() *InitSpec {
return in.Spec.Init
}
func (in *MariaDBDatabase) GetStatus() DatabaseStatus {
return in.Status
}
// GetAppBindingMeta returns meta info of the appbinding which has been created by schema manager
func (in *MariaDBDatabase) GetAppBindingMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-apbng"),
Namespace: in.Namespace,
}
return meta
}
// GetVaultSecretEngineMeta returns meta info of the secret engine which has been created by schema manager
func (in *MariaDBDatabase) GetVaultSecretEngineMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-engine"),
Namespace: in.Namespace,
}
return meta
}
// GetMySQLRoleMeta returns meta info of the MySQL role which has been created by schema manager
func (in *MariaDBDatabase) GetMySQLRoleMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-role"),
Namespace: in.Namespace,
}
return meta
}
// GetSecretAccessRequestMeta returns meta info of the secret access request which has been created by schema manager
func (in *MariaDBDatabase) GetSecretAccessRequestMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-req"),
Namespace: in.Namespace,
}
return meta
}
// GetInitJobMeta returns meta info of the init job which has been created by schema manager
func (in *MariaDBDatabase) GetInitJobMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-job"),
Namespace: in.Namespace,
}
return meta
}
// GetMySQLAuthSecretMeta returns meta info of the mysql auth secret
func (in *MariaDBDatabase) GetMySQLAuthSecretMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: in.Spec.Database.ServerRef.Name + "-auth",
Namespace: in.Spec.Database.ServerRef.Namespace,
}
return meta
}
// GetRestoreSessionMeta returns meta info of the restore session which has been created by schema manager
func (in *MariaDBDatabase) GetRestoreSessionMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-rs"),
Namespace: in.Namespace,
}
return meta
}
// GetRepositoryMeta returns meta info of the repository which has been created by schema manager
func (in *MariaDBDatabase) GetRepositoryMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-rp"),
Namespace: in.Namespace,
}
return meta
}
// GetRepositorySecretMeta returns meta info of the repository which has been created by schema manager
func (in *MariaDBDatabase) GetRepositorySecretMeta() metav1.ObjectMeta {
meta := metav1.ObjectMeta{
Name: meta.NameWithSuffix(in.Name, MariaDBSuffix+"-rp-sec"),
Namespace: in.Namespace,
}
return meta
}
func (in *MariaDBDatabase) CheckDoubleOptIn(ctx context.Context, client client.Client) (bool, error) {
// Get updated MariaDBDatabase object
var schema MariaDBDatabase
err := client.Get(ctx, types.NamespacedName{
Namespace: in.GetNamespace(),
Name: in.GetName(),
}, &schema)
if err != nil {
return false, err
}
// Get the database server
var maria dbapi.MariaDB
err = client.Get(ctx, types.NamespacedName{
Namespace: schema.Spec.Database.ServerRef.Namespace,
Name: schema.Spec.Database.ServerRef.Name,
}, &maria)
if err != nil {
return false, err
}
if maria.Spec.AllowedSchemas == nil {
return false, nil
}
// Get namespace object of the schema
var nsSchema core.Namespace
err = client.Get(ctx, types.NamespacedName{
Name: schema.GetNamespace(),
}, &nsSchema)
if err != nil {
return false, err
}
// Get namespace object of the Database server
var nsDB core.Namespace
err = client.Get(ctx, types.NamespacedName{
Name: schema.Spec.Database.ServerRef.Namespace,
}, &nsDB)
if err != nil {
return false, err
}
possible, err := double_optin.CheckIfDoubleOptInPossible(schema.ObjectMeta, nsSchema.ObjectMeta, nsDB.ObjectMeta, maria.Spec.AllowedSchemas)
if err != nil {
return false, err
}
return possible, nil
}