Skip to content

Commit

Permalink
feat: validate the data directory (#448)
Browse files Browse the repository at this point in the history
* refactor: use external ctrlkit lib and bump dependencies

Signed-off-by: arkbriar <[email protected]>

* feat: validate the data directory

Signed-off-by: arkbriar <[email protected]>

* Update the regex

Signed-off-by: arkbriar <[email protected]>

---------

Signed-off-by: arkbriar <[email protected]>
  • Loading branch information
arkbriar authored May 30, 2023
1 parent b62d781 commit 957c93f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis/risingwave/v1alpha1/risingwave_state_store_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type RisingWaveStateStoreBackend struct {
// DataDirectory is the directory to store the data in the object storage.
// Defaults to hummock.
// +kubebuilder:default=hummock
// +kubebuilder:validation:Pattern="^[0-9a-zA-Z_/-]{1,}$"
DataDirectory string `json:"dataDirectory,omitempty"`

// Memory indicates to store the data in memory. It's only for test usage and strongly discouraged to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44108,6 +44108,7 @@ spec:
default: hummock
description: DataDirectory is the directory to store the data
in the object storage. Defaults to hummock.
pattern: ^[0-9a-zA-Z_/-]{1,}$
type: string
gcs:
description: GCS storage spec.
Expand Down Expand Up @@ -44465,6 +44466,7 @@ spec:
default: hummock
description: DataDirectory is the directory to store the data
in the object storage. Defaults to hummock.
pattern: ^[0-9a-zA-Z_/-]{1,}$
type: string
gcs:
description: GCS storage spec.
Expand Down
2 changes: 2 additions & 0 deletions config/risingwave-operator-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51268,6 +51268,7 @@ spec:
default: hummock
description: DataDirectory is the directory to store the data
in the object storage. Defaults to hummock.
pattern: ^[0-9a-zA-Z_/-]{1,}$
type: string
gcs:
description: GCS storage spec.
Expand Down Expand Up @@ -51625,6 +51626,7 @@ spec:
default: hummock
description: DataDirectory is the directory to store the data
in the object storage. Defaults to hummock.
pattern: ^[0-9a-zA-Z_/-]{1,}$
type: string
gcs:
description: GCS storage spec.
Expand Down
2 changes: 2 additions & 0 deletions config/risingwave-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51268,6 +51268,7 @@ spec:
default: hummock
description: DataDirectory is the directory to store the data
in the object storage. Defaults to hummock.
pattern: ^[0-9a-zA-Z_/-]{1,}$
type: string
gcs:
description: GCS storage spec.
Expand Down Expand Up @@ -51625,6 +51626,7 @@ spec:
default: hummock
description: DataDirectory is the directory to store the data
in the object storage. Defaults to hummock.
pattern: ^[0-9a-zA-Z_/-]{1,}$
type: string
gcs:
description: GCS storage spec.
Expand Down
3 changes: 3 additions & 0 deletions pkg/webhook/risingwave_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package webhook

import (
"context"
"strings"

"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand All @@ -32,6 +33,8 @@ type RisingWaveMutatingWebhook struct{}
// Default implements admission.CustomDefaulter.
func (m *RisingWaveMutatingWebhook) Default(ctx context.Context, obj runtime.Object) error {
ConvertToV1alpha2Features(obj.(*risingwavev1alpha1.RisingWave))
risingwave := obj.(*risingwavev1alpha1.RisingWave)
risingwave.Spec.StateStore.DataDirectory = strings.TrimRight(strings.TrimSpace(risingwave.Spec.StateStore.DataDirectory), "/")
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/webhook/risingwave_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ func (v *RisingWaveValidatingWebhook) validateMetaStoreAndStateStore(path *field
fieldErrs = append(fieldErrs, field.Invalid(path.Child("stateStore"), stateStore, "multiple state store types"))
}

if strings.HasSuffix(stateStore.DataDirectory, "/") ||
strings.HasPrefix(stateStore.DataDirectory, "/") ||
strings.Contains(stateStore.DataDirectory, "//") ||
len(stateStore.DataDirectory) > 800 {
fieldErrs = append(fieldErrs, field.Invalid(path.Child("stateStore", "dataDirectory"), stateStore.DataDirectory, "must be a valid path"))
}

return fieldErrs
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/webhook/risingwave_validating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package webhook

import (
"context"
"strings"
"testing"

kruisepubs "github.com/openkruise/kruise-api/apps/pub"
Expand Down Expand Up @@ -743,6 +744,48 @@ func Test_RisingWaveValidatingWebhook_ValidateCreate(t *testing.T) {
},
pass: true,
},
"invalid-data-dir-1": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = "/"
},
pass: false,
},
"invalid-data-dir-2": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = "/a"
},
pass: false,
},
"invalid-data-dir-3": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = "a/"
},
pass: false,
},
"invalid-data-dir-4": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = "a//b"
},
pass: false,
},
"invalid-data-dir-5": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = strings.Repeat("a", 801)
},
pass: false,
},
"valid-data-dir-1": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = "a"
},
pass: true,
},
"valid-data-dir-2": {
patch: func(r *risingwavev1alpha1.RisingWave) {
r.Spec.StateStore.DataDirectory = "a/b"
},
pass: true,
},
}

for name, tc := range testcases {
Expand Down

0 comments on commit 957c93f

Please sign in to comment.