K8SPXC-1683: add validation for PiTR fields#2398
Conversation
| // +kubebuilder:validation:XValidation:rule="self.type != 'latest' || (self.date == '' && self.gtid == '')",message="Date and GTID should not be set when type is 'latest'" | ||
| type PITR struct { | ||
| BackupSource *PXCBackupStatus `json:"backupSource"` | ||
| // +kubebuilder:validation:Enum=latest;date;transaction;skip |
There was a problem hiding this comment.
are you sure this is the right format? in other places we do +kubebuilder:validation:Enum={latest,date,transaction,skip}
…ator into K8SPXC-1683_add_validation
egegunes
left a comment
There was a problem hiding this comment.
@nmarukovich please check pitr tests
| // +kubebuilder:validation:XValidation:rule="self.type != 'date' || (self.date != '' && self.date.matches('^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$'))",message="Date is required for type 'date' and should be in format YYYY-MM-DD HH:MM:SS with valid ranges (MM: 01-12, DD: 01-31, HH: 00-23, MM/SS: 00-59)" | ||
| // +kubebuilder:validation:XValidation:rule="(self.type != 'transaction' && self.type != 'skip') || self.gtid != ''",message="GTID is required for types 'transaction' and 'skip'" | ||
| // +kubebuilder:validation:XValidation:rule="self.type != 'latest' || (self.date == '' && self.gtid == '')",message="Date and GTID should not be set when type is 'latest'" |
There was a problem hiding this comment.
We should create unit tests for these validations to ensure them
| Unsafe UnsafeFlags `json:"unsafeFlags,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:validation:XValidation:rule="self.type != 'date' || (self.date != '' && self.date.matches('^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$'))",message="Date is required for type 'date' and should be in format YYYY-MM-DD HH:MM:SS with valid ranges (MM: 01-12, DD: 01-31, HH: 00-23, MM/SS: 00-59)" |
There was a problem hiding this comment.
is it possible to use one of built-ins for date validation, described here? https://kubernetes.io/docs/reference/using-api/cel/#kubernetes-format-library
There was a problem hiding this comment.
As I can see we can't use it. We need format const format = "2006-01-02 15:04:05"
and built-in datetime format validates RFC3339 (YYYY-MM-DDTHH:MM:SSZ).
…ator into K8SPXC-1683_add_validation
| // Validate checks PITR fields consistency. | ||
| // The same rules are enforced at CRD level via x-kubernetes-validations (CEL). | ||
| func (p *PITR) Validate() error { | ||
| switch p.Type { | ||
| case "latest": | ||
| if p.Date != "" { | ||
| return errors.New("date should not be set when type is 'latest'") | ||
| } | ||
| if p.GTID != "" { | ||
| return errors.New("gtid should not be set when type is 'latest'") | ||
| } | ||
| case "date": | ||
| if p.Date == "" { | ||
| return errors.New("date is required for type 'date'") | ||
| } | ||
| if !pitrDateRegexp.MatchString(p.Date) { | ||
| return errors.New("date should be in format YYYY-MM-DD HH:MM:SS with valid ranges (MM: 01-12, DD: 01-31, HH: 00-23, MM/SS: 00-59)") | ||
| } | ||
| case "transaction", "skip": | ||
| if p.GTID == "" { | ||
| return fmt.Errorf("gtid is required for type %q", p.Type) | ||
| } | ||
| default: | ||
| return fmt.Errorf("unknown type %q: must be one of latest, date, transaction, skip", p.Type) | ||
| } | ||
| return nil | ||
| } |
There was a problem hiding this comment.
why do we duplicate validation rules?
There was a problem hiding this comment.
@gkech asked to add unit test. and we use this in unit test (I can add integration one instead)
There was a problem hiding this comment.
doesn't this mean that we test what we don't use?
There was a problem hiding this comment.
it means, that if we want to have unit test we need to actually duplicate the behaviour.
If we can test it in any other way and you ok with integration test, I will update PR.
| Unsafe UnsafeFlags `json:"unsafeFlags,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:validation:XValidation:rule="self.type != 'date' || (has(self.date) && self.date.matches('^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$'))",message="Date is required for type 'date' and should be in format YYYY-MM-DD HH:MM:SS with valid ranges (MM: 01-12, DD: 01-31, HH: 00-23, MM/SS: 00-59)" |
There was a problem hiding this comment.
Since we discussed the kubebuilder validation testing through envtest, are we going to update that test in this PR?
| Entry("type transaction with gtid", "valid-transaction", &pxcv1.PITR{Type: "transaction", GTID: "abc123:1-10"}), | ||
| ) | ||
|
|
||
| DescribeTable("invalid PITR configurations", |
commit: fa6f4d5 |
CHANGE DESCRIPTION
Problem:
Short explanation of the problem.
Add validation for PITR fields.
Cause:
Short explanation of the root cause of the issue if applicable.
Solution:
Short explanation of the solution we are providing with this PR.
CHECKLIST
Jira
Needs Doc) and QA (Needs QA)?Tests
compare/*-oc.yml)?Config/Logging/Testability