Skip to content

Commit b4a9712

Browse files
authoredFeb 16, 2023
add TopologySpreadConstraints, add Database.Spec.Path, add skip-initialization annotation (#109)
* add TopologySpreadConstraints, add Database.Spec.Path, add Database skip-initialize annotation * fix style, split webhook config * use patch-validating * fix issues, fix make test and add make clean targets
1 parent 3779b04 commit b4a9712

21 files changed

+969
-230
lines changed
 

‎Makefile

+9-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ fmt: ## Run go fmt against code.
5959
vet: ## Run go vet against code.
6060
go vet ./...
6161

62-
test: manifests generate fmt vet envtest ## Run tests.
62+
test: manifests generate fmt vet envtest docker-build ## Run tests.
63+
kind create cluster --config e2e/kind-cluster-config.yaml --name kind-ydb-operator
64+
docker tag cr.yandex/yc/ydb-operator:latest kind/ydb-operator:current
65+
kind load docker-image cr.yandex/yc/ydb-operator:latest --name kind-ydb-operator
6366
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
6467

68+
.PHONY: clean
69+
clean:
70+
kind delete cluster --name kind-ydb-operator
71+
6572
##@ Build
6673

6774
build: generate fmt vet ## Build manager binary.
@@ -70,7 +77,7 @@ build: generate fmt vet ## Build manager binary.
7077
run: manifests generate fmt vet ## Run a controller from your host.
7178
go run ./cmd/ydb-kubernetes-operator/main.go
7279

73-
docker-build: test ## Build docker image with the manager.
80+
docker-build: ## Build docker image with the manager.
7481
docker build -t ${IMG} .
7582

7683
docker-push: ## Push docker image with the manager.

‎api/v1alpha1/const.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
DaemonBinaryName = "ydbd"
3333

3434
TenantNameFormat = "/%s/%s"
35+
36+
AnnotationSkipInitialization = "ydb.tech/skip-initialization"
3537
)
3638

3739
type ErasureType string

‎api/v1alpha1/database_types.go

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ type DatabaseSpec struct {
4040
// +optional
4141
Domain string `json:"domain"`
4242

43+
// (Optional) Custom database path in schemeshard
44+
// Default: /<spec.domain>/<metadata.name>
45+
// +kubebuilder:validation:Pattern:=/[a-zA-Z0-9]([-_a-zA-Z0-9]*[a-zA-Z0-9])?/[a-zA-Z0-9]([-_a-zA-Z0-9]*[a-zA-Z0-9])?(/[a-zA-Z0-9]([-_a-zA-Z0-9]*[a-zA-Z0-9])?)*
46+
// +kubebuilder:validation:MaxLength:=255
47+
// +optional
48+
Path string `json:"path,omitempty"`
49+
4350
// (Optional) Database storage and compute resources
4451
// +optional
4552
Resources *DatabaseResources `json:"resources,omitempty"` // TODO: Add validation webhook: some resources must be specified
@@ -101,6 +108,16 @@ type DatabaseSpec struct {
101108
// +optional
102109
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
103110

111+
// (Optional) If specified, the pod's topologySpreadConstraints.
112+
// All topologySpreadConstraints are ANDed.
113+
// +optional
114+
// +patchMergeKey=topologyKey
115+
// +patchStrategy=merge
116+
// +listType=map
117+
// +listMapKey=topologyKey
118+
// +listMapKey=whenUnsatisfiable
119+
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty" patchStrategy:"merge" patchMergeKey:"topologyKey"`
120+
104121
// (Optional) Additional custom resource labels that are added to all resources
105122
// +optional
106123
AdditionalLabels map[string]string `json:"additionalLabels,omitempty"`

‎api/v1alpha1/database_webhook.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v1alpha1
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
v1 "k8s.io/api/core/v1"
89
"k8s.io/apimachinery/pkg/runtime"
@@ -24,6 +25,10 @@ func (r *Database) SetupWebhookWithManager(mgr ctrl.Manager) error {
2425

2526
var _ webhook.Defaulter = &Database{}
2627

28+
func GetDatabasePath(r *Database) string {
29+
return fmt.Sprintf(TenantNameFormat, r.Spec.Domain, r.Name) // FIXME: review later in context of multiple namespaces
30+
}
31+
2732
// Default implements webhook.Defaulter so a webhook will be registered for the type
2833
func (r *Database) Default() {
2934
databaselog.Info("default", "name", r.Name)
@@ -63,7 +68,11 @@ func (r *Database) Default() {
6368
}
6469

6570
if r.Spec.Domain == "" {
66-
r.Spec.Domain = DefaultDatabaseDomain // FIXME
71+
r.Spec.Domain = DefaultDatabaseDomain // FIXME: default domain should be "Root", not "root"
72+
}
73+
74+
if r.Spec.Path == "" {
75+
r.Spec.Path = GetDatabasePath(r)
6776
}
6877

6978
if r.Spec.Encryption == nil {
@@ -89,6 +98,12 @@ var _ webhook.Validator = &Database{}
8998
func (r *Database) ValidateCreate() error {
9099
databaselog.Info("validate create", "name", r.Name)
91100

101+
if r.Spec.Domain != "" && r.Spec.Path != "" {
102+
if !strings.HasPrefix(r.Spec.Path, fmt.Sprintf("/%s", r.Spec.Domain)) {
103+
return fmt.Errorf("incorrect database path, must start with domain: \"/%s\"", r.Spec.Domain)
104+
}
105+
}
106+
92107
if r.Spec.Resources == nil && r.Spec.SharedResources == nil && r.Spec.ServerlessResources == nil {
93108
return errors.New("incorrect database resources configuration, must be one of: Resources, SharedResources, ServerlessResources")
94109
}
@@ -101,7 +116,19 @@ func (r *Database) ValidateCreate() error {
101116
func (r *Database) ValidateUpdate(old runtime.Object) error {
102117
databaselog.Info("validate update", "name", r.Name)
103118

104-
// TODO(user): fill in your validation logic upon object update.
119+
oldDatabase, _ := old.(*Database)
120+
if r.Spec.Domain != oldDatabase.Spec.Domain {
121+
return errors.New("database domain cannot be changed")
122+
}
123+
124+
oldDatabasePath := oldDatabase.Spec.Path
125+
if oldDatabase.Spec.Path == "" {
126+
oldDatabasePath = GetDatabasePath(r)
127+
}
128+
if r.Spec.Path != oldDatabasePath {
129+
return errors.New("database path cannot be changed")
130+
}
131+
105132
return nil
106133
}
107134

‎api/v1alpha1/storage_types.go

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ type StorageSpec struct {
106106
// +optional
107107
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
108108

109+
// (Optional) If specified, the pod's topologySpreadConstraints.
110+
// All topologySpreadConstraints are ANDed.
111+
// +optional
112+
// +patchMergeKey=topologyKey
113+
// +patchStrategy=merge
114+
// +listType=map
115+
// +listMapKey=topologyKey
116+
// +listMapKey=whenUnsatisfiable
117+
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty" patchStrategy:"merge" patchMergeKey:"topologyKey"`
118+
109119
// (Optional) Additional custom resource labels that are added to all resources
110120
// +optional
111121
AdditionalLabels map[string]string `json:"additionalLabels,omitempty"`

‎api/v1alpha1/zz_generated.deepcopy.go

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

‎deploy/ydb-operator/Chart.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ type: application
1515
# This is the chart version. This version number should be incremented each time you make changes
1616
# to the chart and its templates, including the app version.
1717
# Versions are expected to follow Semantic Versioning (https://semver.org/)
18-
version: 0.4.23
18+
version: 0.4.24
1919

2020
# This is the version number of the application being deployed. This version number should be
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "0.4.23"
24+
appVersion: "0.4.24"

0 commit comments

Comments
 (0)