This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Add basic ElasticsearchCluster resource validation #199
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8716bd
Add basic ElasticsearchCluster resource validation
munnerz eaeb7bc
Run validation on ESC updates
munnerz 99a05a0
Make PullPolicy field use upstream type
munnerz 724a647
Validate image pull policy. Validate node pool has role.
munnerz 75e081e
Add unit tests for validation functions
munnerz 9554779
Switch persistence.size field to be resource.Quantity and add validation
munnerz 520322d
Expand elasticsearch persistence validation test
munnerz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package validation | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
|
||
"github.com/jetstack/navigator/pkg/apis/navigator" | ||
) | ||
|
||
var supportedPullPolicies = []string{ | ||
string(corev1.PullNever), | ||
string(corev1.PullIfNotPresent), | ||
string(corev1.PullAlways), | ||
"", | ||
} | ||
|
||
func ValidateImageSpec(img *navigator.ImageSpec, fldPath *field.Path) field.ErrorList { | ||
el := field.ErrorList{} | ||
if img.Tag == "" { | ||
el = append(el, field.Required(fldPath.Child("tag"), "")) | ||
} | ||
if img.Repository == "" { | ||
el = append(el, field.Required(fldPath.Child("repository"), "")) | ||
} | ||
if img.PullPolicy != corev1.PullNever && | ||
img.PullPolicy != corev1.PullIfNotPresent && | ||
img.PullPolicy != corev1.PullAlways && | ||
img.PullPolicy != "" { | ||
el = append(el, field.NotSupported(fldPath.Child("pullPolicy"), img.PullPolicy, supportedPullPolicies)) | ||
} | ||
return el | ||
} | ||
|
||
var supportedESClusterRoles = []string{ | ||
string(navigator.ElasticsearchRoleData), | ||
string(navigator.ElasticsearchRoleIngest), | ||
string(navigator.ElasticsearchRoleMaster), | ||
} | ||
|
||
func ValidateElasticsearchClusterRole(r navigator.ElasticsearchClusterRole, fldPath *field.Path) field.ErrorList { | ||
el := field.ErrorList{} | ||
switch r { | ||
case navigator.ElasticsearchRoleData: | ||
case navigator.ElasticsearchRoleIngest: | ||
case navigator.ElasticsearchRoleMaster: | ||
default: | ||
el = append(el, field.NotSupported(fldPath, r, supportedESClusterRoles)) | ||
} | ||
return el | ||
} | ||
|
||
func ValidateElasticsearchClusterNodePool(np *navigator.ElasticsearchClusterNodePool, fldPath *field.Path) field.ErrorList { | ||
el := ValidateDNS1123Subdomain(np.Name, fldPath.Child("name")) | ||
el = append(el, ValidateElasticsearchPersistence(&np.Persistence, fldPath.Child("persistence"))...) | ||
rolesPath := fldPath.Child("roles") | ||
if len(np.Roles) == 0 { | ||
el = append(el, field.Required(rolesPath, "at least one role must be specified")) | ||
} | ||
for i, r := range np.Roles { | ||
idxPath := rolesPath.Index(i) | ||
el = append(el, ValidateElasticsearchClusterRole(r, idxPath)...) | ||
} | ||
if np.Replicas < 0 { | ||
el = append(el, field.Invalid(fldPath.Child("replicas"), np.Replicas, "must be greater than zero")) | ||
} | ||
// TODO: call k8s.io/kubernetes/pkg/apis/core/validation.ValidateResourceRequirements on np.Resources | ||
// this will require vendoring kubernetes/kubernetes and switching to use the corev1 ResourceRequirements | ||
// struct | ||
return el | ||
} | ||
|
||
func ValidateElasticsearchPersistence(cfg *navigator.ElasticsearchClusterPersistenceConfig, fldPath *field.Path) field.ErrorList { | ||
el := field.ErrorList{} | ||
if cfg.Enabled && cfg.Size.IsZero() { | ||
el = append(el, field.Required(fldPath.Child("size"), "")) | ||
} | ||
if cfg.Size.Sign() == -1 { | ||
el = append(el, field.Invalid(fldPath.Child("size"), cfg.Size, "must be greater than zero")) | ||
} | ||
return el | ||
} | ||
|
||
func ValidateElasticsearchClusterSpec(spec *navigator.ElasticsearchClusterSpec, fldPath *field.Path) field.ErrorList { | ||
allErrs := ValidateImageSpec(&spec.Image.ImageSpec, fldPath.Child("image")) | ||
allErrs = append(allErrs, ValidateImageSpec(&spec.Pilot.ImageSpec, fldPath.Child("pilot"))...) | ||
npPath := fldPath.Child("nodePools") | ||
allNames := sets.String{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh, |
||
for i, np := range spec.NodePools { | ||
idxPath := npPath.Index(i) | ||
if allNames.Has(np.Name) { | ||
allErrs = append(allErrs, field.Duplicate(idxPath.Child("name"), np.Name)) | ||
} else { | ||
allNames.Insert(np.Name) | ||
} | ||
allErrs = append(allErrs, ValidateElasticsearchClusterNodePool(&np, idxPath)...) | ||
} | ||
return allErrs | ||
} | ||
|
||
func ValidateElasticsearchCluster(esc *navigator.ElasticsearchCluster) field.ErrorList { | ||
allErrs := ValidateObjectMeta(&esc.ObjectMeta, true, apimachineryvalidation.NameIsDNSSubdomain, field.NewPath("metadata")) | ||
allErrs = append(allErrs, ValidateElasticsearchClusterSpec(&esc.Spec, field.NewPath("spec"))...) | ||
return allErrs | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps instead check that the value is >0 and this could all be replaced with,
if cgf.Size.Sign() != 1
The docs say:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in my other comment, there are a few cases to cover:
I've expanded our test suite to explicitly cover all the cases above, and they are still passing 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't see the case for
3 Persistence disabled, size = 0 (VALID)
Unless that's how we determine that persistence is not required.
In which case, would that be better expressed as
Persistence *ElasticsearchClusterPersistenceConfig
andPersistence = nil
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see you just explained below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if I set:
I have implicitly also said the size=0. It would we weird/awkward to require users to write: