Skip to content

K8SPXC-1683: add validation for PiTR fields#2398

Open
nmarukovich wants to merge 11 commits intomainfrom
K8SPXC-1683_add_validation
Open

K8SPXC-1683: add validation for PiTR fields#2398
nmarukovich wants to merge 11 commits intomainfrom
K8SPXC-1683_add_validation

Conversation

@nmarukovich
Copy link
Copy Markdown
Contributor

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

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?
  • Are OpenShift compare files changed for E2E tests (compare/*-oc.yml)?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported PXC version?
  • Does the change support oldest and newest supported Kubernetes version?

@egegunes egegunes changed the title K8SPXC-1683 add validation K8SPXC-1683: add validation for PiTR fields Mar 16, 2026
// +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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this is the right format? in other places we do +kubebuilder:validation:Enum={latest,date,transaction,skip}

@nmarukovich nmarukovich requested a review from egegunes March 16, 2026 16:35
Copy link
Copy Markdown
Contributor

@egegunes egegunes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nmarukovich please check pitr tests

Comment on lines +32 to +34
// +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'"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see we can't use it. We need format const format = "2006-01-02 15:04:05"

const format = "2006-01-02 15:04:05"

and built-in datetime format validates RFC3339 (YYYY-MM-DDTHH:MM:SSZ).

@pull-request-size pull-request-size bot added size/L 100-499 lines and removed size/M 30-99 lines labels Mar 18, 2026
Comment on lines +107 to +133
// 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
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we duplicate validation rules?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkech asked to add unit test. and we use this in unit test (I can add integration one instead)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this mean that we test what we don't use?

Copy link
Copy Markdown
Contributor Author

@nmarukovich nmarukovich Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we discussed the kubebuilder validation testing through envtest, are we going to update that test in this PR?

@nmarukovich nmarukovich requested review from egegunes and gkech March 20, 2026 16:03
Entry("type transaction with gtid", "valid-transaction", &pxcv1.PITR{Type: "transaction", GTID: "abc123:1-10"}),
)

DescribeTable("invalid PITR configurations",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

til DescribeTable nice

@egegunes egegunes added this to the v1.20.0 milestone Apr 8, 2026
@JNKPercona
Copy link
Copy Markdown
Collaborator

Test Name Result Time
auto-tuning-8-0 passed 00:00:00
allocator-8-0 passed 00:00:00
allocator-8-4 passed 00:00:00
backup-storage-tls-8-0 passed 00:00:00
cross-site-8-0 passed 00:00:00
custom-users-8-0 passed 00:00:00
demand-backup-cloud-8-0 passed 00:00:00
demand-backup-cloud-8-4 passed 00:00:00
demand-backup-cloud-pxb-8-0 passed 00:00:00
demand-backup-encrypted-with-tls-5-7 passed 00:00:00
demand-backup-encrypted-with-tls-8-0 passed 00:00:00
demand-backup-encrypted-with-tls-8-4 passed 00:00:00
demand-backup-encrypted-with-tls-pxb-5-7 passed 00:00:00
demand-backup-encrypted-with-tls-pxb-8-0 passed 00:00:00
demand-backup-encrypted-with-tls-pxb-8-4 passed 00:00:00
demand-backup-8-0 passed 00:00:00
demand-backup-flow-control-8-0 passed 00:00:00
demand-backup-flow-control-8-4 passed 00:00:00
demand-backup-parallel-8-0 passed 00:00:00
demand-backup-parallel-8-4 passed 00:00:00
demand-backup-without-passwords-8-0 passed 00:00:00
demand-backup-without-passwords-8-4 passed 00:00:00
extra-pvc-8-0 passed 00:00:00
haproxy-5-7 passed 00:00:00
haproxy-8-0 passed 00:00:00
haproxy-8-4 passed 00:00:00
init-deploy-5-7 passed 00:00:00
init-deploy-8-0 passed 00:00:00
limits-8-0 passed 00:00:00
monitoring-2-0-8-0 passed 00:00:00
monitoring-pmm3-8-0 passed 00:00:00
monitoring-pmm3-8-4 passed 00:00:00
one-pod-5-7 passed 00:00:00
one-pod-8-0 passed 00:00:00
pitr-8-0 passed 00:00:00
pitr-8-4 passed 00:00:00
pitr-pxb-8-0 passed 00:00:00
pitr-pxb-8-4 passed 00:00:00
pitr-gap-errors-8-0 passed 00:00:00
pitr-gap-errors-8-4 passed 00:00:00
proxy-protocol-8-0 passed 00:00:00
proxy-switch-8-0 passed 00:00:00
proxysql-sidecar-res-limits-8-0 passed 00:00:00
proxysql-scheduler-8-0 passed 00:00:00
pvc-resize-5-7 passed 00:00:00
pvc-resize-8-0 passed 00:00:00
recreate-8-0 passed 00:00:00
restore-to-encrypted-cluster-8-0 passed 00:00:00
restore-to-encrypted-cluster-8-4 passed 00:00:00
restore-to-encrypted-cluster-pxb-8-0 passed 00:00:00
restore-to-encrypted-cluster-pxb-8-4 passed 00:00:00
scaling-proxysql-8-0 passed 00:00:00
scaling-8-0 passed 00:00:00
scheduled-backup-5-7 passed 00:00:00
scheduled-backup-8-0 passed 00:00:00
scheduled-backup-8-4 passed 00:00:00
security-context-8-0 passed 00:00:00
smart-update1-8-0 passed 00:00:00
smart-update1-8-4 passed 00:00:00
smart-update2-8-0 passed 00:00:00
smart-update2-8-4 passed 00:00:00
smart-update3-8-0 passed 00:00:00
storage-8-0 passed 00:00:00
tls-issue-cert-manager-ref-8-0 passed 00:08:50
tls-issue-cert-manager-8-0 passed 00:00:00
tls-issue-self-8-0 passed 00:13:51
upgrade-consistency-8-0 passed 00:00:00
upgrade-consistency-8-4 passed 00:00:00
upgrade-haproxy-5-7 passed 00:00:00
upgrade-haproxy-8-0 passed 00:00:00
upgrade-proxysql-5-7 passed 00:00:00
upgrade-proxysql-8-0 passed 00:00:00
users-5-7 passed 00:00:00
users-8-0 passed 00:29:16
users-scheduler-8-4 passed 00:28:09
validation-hook-8-0 passed 00:00:00
Summary Value
Tests Run 76/76
Job Duration 00:55:09
Total Test Time 01:20:08

commit: fa6f4d5
image: perconalab/percona-xtradb-cluster-operator:PR-2398-fa6f4d5d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L 100-499 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants