Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhancement webhook validate rules #294

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions pkg/webhook/nebulacluster/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,24 @@ func validateNebulaClusterCreate(nc *v1alpha1.NebulaCluster) (allErrs field.Erro

// validateNebulaClusterGraphd validates a NebulaCluster for Graphd on update.
func validateNebulaClusterUpdateGraphd(nc, oldNC *v1alpha1.NebulaCluster) (allErrs field.ErrorList) {
_ = oldNC // unused
allErrs = append(allErrs, apivalidation.ValidateImmutableField(
nc.Spec.Graphd.Port,
oldNC.Spec.Graphd.Port,
field.NewPath("spec").Child("graphd").Child("port"),
)...)

allErrs = append(allErrs, validateNebulaClusterGraphd(nc)...)

return allErrs
}

// validateNebulaClusterMetad validates a NebulaCluster for Metad on Update.
func validateNebulaClusterUpdateMetad(nc, oldNC *v1alpha1.NebulaCluster) (allErrs field.ErrorList) {
allErrs = append(allErrs, apivalidation.ValidateImmutableField(
nc.Spec.Metad.Port,
oldNC.Spec.Metad.Port,
field.NewPath("spec").Child("metad").Child("port"),
)...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(
nc.Spec.Metad.Replicas,
oldNC.Spec.Metad.Replicas,
Expand All @@ -129,6 +139,13 @@ func validateNebulaClusterUpdateMetad(nc, oldNC *v1alpha1.NebulaCluster) (allErr

// validateNebulaClusterStoraged validates a NebulaCluster for Storaged on Update.
func validateNebulaClusterUpdateStoraged(nc, oldNC *v1alpha1.NebulaCluster) (allErrs field.ErrorList) {
if oldNC.Status.Storaged.Phase == v1alpha1.ScaleInPhase || oldNC.Status.Storaged.Phase == v1alpha1.ScaleOutPhase {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec", "storaged"),
fmt.Sprintf("field is immutable while in %s phase", oldNC.Status.Storaged.Phase),
))
}

if nc.Status.Storaged.Phase != v1alpha1.RunningPhase {
if *nc.Spec.Storaged.Replicas != *oldNC.Spec.Storaged.Replicas {
allErrs = append(allErrs, field.Invalid(
Expand All @@ -139,11 +156,39 @@ func validateNebulaClusterUpdateStoraged(nc, oldNC *v1alpha1.NebulaCluster) (all
}
}

allErrs = append(allErrs, apivalidation.ValidateImmutableField(
nc.Spec.Storaged.Port,
oldNC.Spec.Storaged.Port,
field.NewPath("spec").Child("storaged").Child("port"),
)...)

allErrs = append(allErrs, validateNebulaClusterUpdateStoragedDataVolume(nc, oldNC)...)
allErrs = append(allErrs, validateNebulaClusterStoraged(nc)...)

return allErrs
}

// validateNebulaClusterUpdateStoragedDataVolume validates a NebulaCluster for Storaged data volume on Update.
func validateNebulaClusterUpdateStoragedDataVolume(nc, oldNC *v1alpha1.NebulaCluster) (allErrs field.ErrorList) {
if len(nc.Spec.Storaged.DataVolumeClaims) != len(oldNC.Spec.Storaged.DataVolumeClaims) {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec", "storaged", "dataVolumeClaims"),
"storaged dataVolumeClaims len is immutable",
))
}
for i, pvc := range nc.Spec.Storaged.DataVolumeClaims {
if pvc.Resources.Requests.Storage().Cmp(*oldNC.Spec.Storaged.DataVolumeClaims[i].Resources.Requests.Storage()) == -1 {
allErrs = append(allErrs, field.Invalid(
field.NewPath("spec", "storaged", "dataVolumeClaims"),
pvc.Resources.Requests.Storage(),
"data volume size can only be increased",
))
}
}

return allErrs
}

// ValidateNebulaCluster validates a NebulaCluster on Update.
func validateNebulaClusterUpdate(nc, oldNC *v1alpha1.NebulaCluster) (allErrs field.ErrorList) {
name := nc.Name
Expand All @@ -158,12 +203,14 @@ func validateNebulaClusterUpdate(nc, oldNC *v1alpha1.NebulaCluster) (allErrs fie
field.NewPath("metadata"),
)...)

allErrs = append(allErrs, apivalidation.ValidateImmutableAnnotation(
nc.Annotations[annotation.AnnHaModeKey],
oldNC.Annotations[annotation.AnnHaModeKey],
annotation.AnnHaModeKey,
field.NewPath("metadata"),
)...)
if !validation.IsNebulaClusterHA(oldNC) {
allErrs = append(allErrs, apivalidation.ValidateImmutableAnnotation(
nc.Annotations[annotation.AnnHaModeKey],
oldNC.Annotations[annotation.AnnHaModeKey],
annotation.AnnHaModeKey,
field.NewPath("metadata"),
)...)
}

allErrs = append(allErrs, validateNebulaClusterUpdateGraphd(nc, oldNC)...)
allErrs = append(allErrs, validateNebulaClusterUpdateMetad(nc, oldNC)...)
Expand Down
15 changes: 15 additions & 0 deletions pkg/webhook/util/validation/nebulacluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package validation

import (
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1"
)

const (
Expand Down Expand Up @@ -72,3 +74,16 @@ func ValidateMinReplicasStoraged(fldPath *field.Path, replicas int, bHaMode bool

return allErrs
}

func IsNebulaClusterHA(nc *v1alpha1.NebulaCluster) bool {
if int(*nc.Spec.Graphd.Replicas) < minReplicasGraphdInHaMode {
return false
}
if int(*nc.Spec.Metad.Replicas) < minReplicasMetadInHaMode {
return false
}
if int(*nc.Spec.Storaged.Replicas) < minReplicasStoragedInHaMode {
return false
}
return true
}