Skip to content

Commit

Permalink
Validate lenght of cell name
Browse files Browse the repository at this point in the history
We need to limit the lenght of the name of the nova-cell
as the operator generates names for the sub CRs from this name as a prefix.
E.g. <nova-CR-name>-<cell name>-metadata-internal
K8s limits the name of a CR to 63 chars.
  • Loading branch information
mrkisaolamb committed Jun 6, 2023
1 parent 5289d6d commit 1cca508
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
37 changes: 35 additions & 2 deletions api/v1beta1/novacell_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/runtime/schema"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

// NovaCellDefaults -
Expand Down Expand Up @@ -85,19 +88,49 @@ func (spec *NovaCellSpec) Default() {

var _ webhook.Validator = &NovaCell{}

// ValidateName validate cell name lenght
func (r *NovaCell) ValidateName() *field.Error{
if len(r.Spec.CellName) > 35{
return field.Forbidden(field.NewPath("spec").Child("CellName"), "should be shorter than 35 signs")
}
return nil
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *NovaCell) ValidateCreate() error {
novacelllog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
var allErrs field.ErrorList

if err := r.ValidateName(); err != nil {
allErrs = append(allErrs, err)
}

if len(allErrs) != 0 {
novacelllog.Info("validation failed", "name", r.Name)
return apierrors.NewInvalid(
schema.GroupKind{Group: "nova.openstack.org", Kind: "Nova"},
r.Name, allErrs)
}
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *NovaCell) ValidateUpdate(old runtime.Object) error {
novacelllog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
var allErrs field.ErrorList

if err := r.ValidateName(); err != nil {
allErrs = append(allErrs, err)
}

if len(allErrs) != 0 {
novacelllog.Info("validation failed", "name", r.Name)
return apierrors.NewInvalid(
schema.GroupKind{Group: "nova.openstack.org", Kind: "Nova"},
r.Name, allErrs)
}
return nil
}

Expand Down
29 changes: 29 additions & 0 deletions test/functional/novacell_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/openstack-k8s-operators/lib-common/modules/test/helpers"
"github.com/google/uuid"

corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
novav1 "github.com/openstack-k8s-operators/nova-operator/api/v1beta1"
Expand Down Expand Up @@ -216,3 +219,29 @@ var _ = Describe("NovaCell controller", func() {
})
})
})

var _ = Describe("NovaCell controller webhook", func() {
It("name is too long", func() {
DeferCleanup(
k8sClient.Delete, ctx, CreateNovaConductorSecret(cell1.CellName.Namespace, SecretName))
DeferCleanup(
k8sClient.Delete, ctx, CreateNovaMessageBusSecret(cell1.CellName.Namespace, MessageBusSecretName))

spec := GetDefaultNovaCellSpec()
spec["cellName"] = uuid.New().String()
rawObj := map[string]interface{}{
"apiVersion": "nova.openstack.org/v1beta1",
"kind": "NovaCell",
"metadata": map[string]interface{}{
"name": cell1.CellName.Name,
"namespace": cell1.CellName.Namespace,
},
"spec": spec,
}
th.Logger.Info("Creating", "raw", rawObj)
unstructuredObj := &unstructured.Unstructured{Object: rawObj}
_, err := controllerutil.CreateOrPatch(
th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil })
Expect(err).Should(HaveOccurred())
})
})
2 changes: 1 addition & 1 deletion test/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ var _ = BeforeEach(func() {

novaName := types.NamespacedName{
Namespace: namespace,
Name: uuid.New().String(),
Name: uuid.New().String()[:25],
}

novaNames = GetNovaNames(novaName, []string{"cell0", "cell1", "cell2"})
Expand Down

0 comments on commit 1cca508

Please sign in to comment.