Skip to content

Commit d71f687

Browse files
authored
Add Database.Spec CABundle and Secrets options, fix Database.Spec.Path getters (#112)
* add CABundle * rearrange fields * add Secrets * fix database path getters * fix * fix goconst linter, comment prealloc linter * fix null pointer dereference
1 parent 9ecd352 commit d71f687

13 files changed

+282
-42
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ fabric.properties
3636

3737
bin/
3838
config/
39-
vendor/
39+
vendor/
40+
.envrc

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ linters:
170170
# - nilnil
171171
# - nlreturn
172172
- nolintlint
173-
- prealloc
173+
# - prealloc
174174
- predeclared
175175
- rowserrcheck
176176
- revive

api/v1alpha1/const.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ const (
3131
BinariesDir = "/opt/ydb/bin"
3232
DaemonBinaryName = "ydbd"
3333

34-
TenantNameFormat = "/%s/%s"
35-
3634
AnnotationSkipInitialization = "ydb.tech/skip-initialization"
35+
36+
legacyTenantNameFormat = "/%s/%s"
3737
)
3838

3939
type ErasureType string

api/v1alpha1/database_types.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ type DatabaseSpec struct {
6464
// +optional
6565
PublicHost string `json:"publicHost,omitempty"`
6666

67+
// (Optional) YDBVersion sets the explicit version of the YDB image
68+
// Default: ""
69+
// +optional
70+
YDBVersion string `json:"version,omitempty"`
71+
72+
// (Optional) YDB Image
73+
// +optional
74+
Image PodImage `json:"image,omitempty"`
75+
6776
// List of initialization containers belonging to the pod.
6877
// Init containers are executed in order prior to containers being started. If any
6978
// init container fails, the pod is considered to have failed and is handled according
@@ -85,14 +94,15 @@ type DatabaseSpec struct {
8594
// +optional
8695
Monitoring *MonitoringOptions `json:"monitoring,omitempty"`
8796

88-
// (Optional) YDBVersion sets the explicit version of the YDB image
89-
// Default: ""
97+
// User-defined root certificate authority that is added to system trust
98+
// store of Storage pods on startup.
9099
// +optional
91-
YDBVersion string `json:"version,omitempty"`
100+
CABundle []byte `json:"caBundle,omitempty"`
92101

93-
// (Optional) YDB Image
102+
// Secret names that will be mounted into the well-known directory of
103+
// every storage pod. Directory: `/opt/ydb/secrets/<secret_name>/<secret_key>`
94104
// +optional
95-
Image PodImage `json:"image,omitempty"`
105+
Secrets []*corev1.LocalObjectReference `json:"secrets,omitempty"`
96106

97107
// NodeSelector is a selector which must be true for the pod to fit on a node.
98108
// Selector which must match a node's labels for the pod to be scheduled on that node.

api/v1alpha1/database_webhook.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ func (r *Database) SetupWebhookWithManager(mgr ctrl.Manager) error {
2626
var _ webhook.Defaulter = &Database{}
2727

2828
func GetDatabasePath(r *Database) string {
29-
return fmt.Sprintf(TenantNameFormat, r.Spec.Domain, r.Name) // FIXME: review later in context of multiple namespaces
29+
if r.Spec.Path != "" {
30+
return r.Spec.Path
31+
}
32+
return GetLegacyDatabasePath(r)
33+
}
34+
35+
func GetLegacyDatabasePath(r *Database) string {
36+
return fmt.Sprintf(legacyTenantNameFormat, r.Spec.Domain, r.Name) // FIXME: review later in context of multiple namespaces
3037
}
3138

3239
// Default implements webhook.Defaulter so a webhook will be registered for the type
@@ -72,7 +79,7 @@ func (r *Database) Default() {
7279
}
7380

7481
if r.Spec.Path == "" {
75-
r.Spec.Path = GetDatabasePath(r)
82+
r.Spec.Path = GetLegacyDatabasePath(r)
7683
}
7784

7885
if r.Spec.Encryption == nil {
@@ -121,11 +128,7 @@ func (r *Database) ValidateUpdate(old runtime.Object) error {
121128
return errors.New("database domain cannot be changed")
122129
}
123130

124-
oldDatabasePath := oldDatabase.Spec.Path
125-
if oldDatabase.Spec.Path == "" {
126-
oldDatabasePath = GetDatabasePath(r)
127-
}
128-
if r.Spec.Path != oldDatabasePath {
131+
if GetDatabasePath(oldDatabase) != GetDatabasePath(r) {
129132
return errors.New("database path cannot be changed")
130133
}
131134

api/v1alpha1/storage_types.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ type StorageSpec struct {
4646
// +optional
4747
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
4848

49+
// (Optional) YDBVersion sets the explicit version of the YDB image
50+
// Default: ""
51+
// +optional
52+
YDBVersion string `json:"version,omitempty"`
53+
4954
// Container image information
5055
// +required
5156
Image PodImage `json:"image,omitempty"`
@@ -66,11 +71,6 @@ type StorageSpec struct {
6671
// +optional
6772
InitContainers []corev1.Container `json:"initContainers,omitempty"`
6873

69-
// (Optional) YDBVersion sets the explicit version of the YDB image
70-
// Default: ""
71-
// +optional
72-
YDBVersion string `json:"version,omitempty"`
73-
7474
// (Optional) Monitoring sets configuration options for YDB observability
7575
// Default: ""
7676
// +optional

api/v1alpha1/zz_generated.deepcopy.go

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deploy/ydb-operator/crds/database.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ spec:
873873
type: array
874874
type: object
875875
type: object
876+
caBundle:
877+
description: User-defined root certificate authority that is added
878+
to system trust store of Storage pods on startup.
879+
format: byte
880+
type: string
876881
configuration:
877882
description: YDB configuration in YAML format. Will be applied on
878883
top of generated one in internal/configuration
@@ -2357,6 +2362,19 @@ spec:
23572362
type: object
23582363
type: array
23592364
type: object
2365+
secrets:
2366+
description: 'Secret names that will be mounted into the well-known
2367+
directory of every storage pod. Directory: `/opt/ydb/secrets/<secret_name>/<secret_key>`'
2368+
items:
2369+
description: LocalObjectReference contains enough information to
2370+
let you locate the referenced object inside the same namespace.
2371+
properties:
2372+
name:
2373+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
2374+
TODO: Add other useful fields. apiVersion, kind, uid?'
2375+
type: string
2376+
type: object
2377+
type: array
23602378
serverlessResources:
23612379
description: (Optional) If specified, created database will be "serverless".
23622380
properties:

internal/controllers/database/sync.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (r *Reconciler) handleResourcesSync(
214214
) (bool, ctrl.Result, error) {
215215
r.Log.Info("running step handleResourcesSync")
216216

217-
for _, builder := range database.GetResourceBuilders() {
217+
for _, builder := range database.GetResourceBuilders(r.Config) {
218218
newResource := builder.Placeholder(database)
219219

220220
result, err := resources.CreateOrUpdateIgnoreStatus(ctx, r.Client, newResource, func() error {
@@ -349,7 +349,7 @@ func (r *Reconciler) handleTenantCreation(
349349
) (bool, ctrl.Result, error) {
350350
r.Log.Info("running step handleTenantCreation")
351351

352-
path := database.GetPath()
352+
path := v1alpha1.GetDatabasePath(database.Database)
353353
var storageUnits []v1alpha1.StorageUnit
354354
var shared bool
355355
var sharedDatabasePath string
@@ -408,7 +408,7 @@ func (r *Reconciler) handleTenantCreation(
408408
)
409409
return Stop, ctrl.Result{RequeueAfter: SharedDatabaseAwaitRequeueDelay}, err
410410
}
411-
sharedDatabasePath = fmt.Sprintf(v1alpha1.TenantNameFormat, sharedDatabaseCr.Spec.Domain, sharedDatabaseCr.Name)
411+
sharedDatabasePath = v1alpha1.GetDatabasePath(sharedDatabaseCr)
412412
default:
413413
// TODO: move this logic to webhook
414414
r.Recorder.Event(

internal/resources/database.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
corev1 "k8s.io/api/core/v1"
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/client-go/rest"
89

910
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
1011
"github.com/ydb-platform/ydb-kubernetes-operator/internal/configuration"
@@ -56,14 +57,7 @@ func (b *DatabaseBuilder) GetStorageEndpoint() string {
5657
return fmt.Sprintf("%s:%d", host, api.GRPCPort)
5758
}
5859

59-
func (b *DatabaseBuilder) GetPath() string {
60-
if b.Spec.Path == "" {
61-
return api.GetDatabasePath(b.Database)
62-
}
63-
return b.Spec.Path
64-
}
65-
66-
func (b *DatabaseBuilder) GetResourceBuilders() []ResourceBuilder {
60+
func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {
6761
if b.Spec.ServerlessResources != nil {
6862
return []ResourceBuilder{}
6963
}
@@ -196,7 +190,12 @@ func (b *DatabaseBuilder) GetResourceBuilders() []ResourceBuilder {
196190

197191
optionalBuilders = append(
198192
optionalBuilders,
199-
&DatabaseStatefulSetBuilder{Database: b.Unwrap(), Labels: databaseLabels, Storage: b.Storage},
193+
&DatabaseStatefulSetBuilder{
194+
Database: b.Unwrap(),
195+
Labels: databaseLabels,
196+
RestConfig: restConfig,
197+
Storage: b.Storage,
198+
},
200199
)
201200

202201
return optionalBuilders

0 commit comments

Comments
 (0)