Skip to content

feat: add CEL validation rules to MemberCluster taint API types#478

Open
ytimocin wants to merge 1 commit intokubefleet-dev:mainfrom
ytimocin:feat/cel-validation-membercluster
Open

feat: add CEL validation rules to MemberCluster taint API types#478
ytimocin wants to merge 1 commit intokubefleet-dev:mainfrom
ytimocin:feat/cel-validation-membercluster

Conversation

@ytimocin
Copy link
Collaborator

@ytimocin ytimocin commented Mar 4, 2026

Description of your changes

Adds CRD CEL validation for MemberCluster taints in both API versions (v1 and v1beta1) to match existing webhook validation behavior (defense-in-depth).

What changed

  • API type updates:
    • Added CEL rules on MemberClusterSpec.Taints and Taint in:
      • apis/cluster/v1/membercluster_types.go
      • apis/cluster/v1beta1/membercluster_types.go
    • Rules cover:
      • taint key name segment format and max length
      • taint key prefix DNS-subdomain format and max length
      • taint value label-value format and max length
      • taint uniqueness by (key, value, effect)
  • Regenerated CRD schema:
    • config/crd/bases/cluster.kubernetes-fleet.io_memberclusters.yaml
  • Added integration validation coverage:
    • test/apis/cluster/v1/api_validation_integration_test.go
    • test/apis/cluster/v1beta1/api_validation_integration_test.go
    • Added comprehensive valid/invalid taint CEL cases for both versions.
  • Updated webhook validator unit tests:
    • pkg/utils/validator/membercluster_test.go
    • Added coverage to confirm same key+effect with different value is valid.
  • Updated e2e assertion:
    • test/e2e/webhook_test.go
    • Adjusted expected error message to match CEL validation output.
  • Webhook logic is unchanged.

Why

  • Enforces invalid taints earlier at CRD admission time.
  • Keeps behavior consistent with the current webhook validator.

How has this code been tested

  • go test ./pkg/utils/validator -run TestValidateTaints -count=1
  • go test ./pkg/webhook/membercluster -count=1
  • go test -c ./test/apis/cluster/v1 -o /tmp/test-apis-cluster-v1.test
  • go test -c ./test/apis/cluster/v1beta1 -o /tmp/test-apis-cluster-v1beta1.test
  • Focused API integration commands:
    • go test ./test/apis/cluster/v1 -run TestAPIs -count=1 -- -ginkgo.focus="MemberCluster taint CEL validation" -ginkgo.v
    • go test ./test/apis/cluster/v1beta1 -run TestAPIs -count=1 -- -ginkgo.focus="MemberCluster taint CEL validation" -ginkgo.v

Special notes for your reviewer

  • Requests failing CEL validation are rejected before reaching the webhook, so some error messages now come from CEL/CRD validation.

I have:

  • Run make reviewable to ensure this PR is ready for review.

@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ytimocin ytimocin force-pushed the feat/cel-validation-membercluster branch 12 times, most recently from 6dfd0c7 to 90e59c0 Compare March 5, 2026 22:58
@ytimocin ytimocin requested a review from Copilot March 6, 2026 00:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds CRD-level CEL validations for MemberCluster taints (v1 + v1beta1) so invalid taints are rejected at schema validation time, aligning with existing webhook validation for defense-in-depth.

Changes:

  • Added CEL rules and length constraints to MemberClusterSpec.Taints / Taint in both API versions (format, prefix/name/value limits, and uniqueness by (key,value,effect)).
  • Regenerated the MemberCluster CRD schema to include the new validations.
  • Expanded validation coverage via API integration tests (v1 + v1beta1), updated validator unit tests, and adjusted an e2e assertion to match CEL error output.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
apis/cluster/v1/membercluster_types.go Adds CEL validations + length constraints for taints in v1 API types.
apis/cluster/v1beta1/membercluster_types.go Adds the same CEL validations + constraints for v1beta1 API types.
config/crd/bases/cluster.kubernetes-fleet.io_memberclusters.yaml CRD schema regeneration to include taint CEL validations and max/min length constraints.
test/apis/cluster/v1/api_validation_integration_test.go Adds integration coverage for valid/invalid taint CEL cases (v1).
test/apis/cluster/v1beta1/api_validation_integration_test.go Adds integration coverage for valid/invalid taint CEL cases (v1beta1).
pkg/utils/validator/membercluster_test.go Adds a unit test covering “same key+effect, different value” as valid (uniqueness is by key+value+effect).
test/e2e/webhook_test.go Updates expected error text to match CEL/CRD validation output.

var statusErr *k8sErrors.StatusError
g.Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Update MC call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8sErrors.StatusError{})))
g.Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character"))
g.Expect(statusErr.ErrStatus.Message).Should(MatchRegexp("taint key name segment must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character"))
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

This assertion uses MatchRegexp with a literal string that contains regex metacharacters (notably the unescaped "."), which can make the test pass for unintended messages. Consider using ContainSubstring for this check, or wrapping the expected message with regexp.QuoteMeta() before passing it to MatchRegexp to ensure the regex matches the literal text.

Copilot uses AI. Check for mistakes.
@ytimocin ytimocin force-pushed the feat/cel-validation-membercluster branch from 90e59c0 to d486583 Compare March 6, 2026 18:37
Signed-off-by: Yetkin Timocin <ytimocin@microsoft.com>
@ytimocin ytimocin force-pushed the feat/cel-validation-membercluster branch from d486583 to d20fcc6 Compare March 6, 2026 21:14
@sjwaight sjwaight linked an issue Mar 8, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: improve reliability by moving away from using webhook

2 participants