Skip to content

Conversation

@zijiren233
Copy link
Member

Description

This PR introduces a new admission webhook for Pod resources that implements intelligent resource request auto-adjustment to optimize cluster resource utilization. The webhook automatically adjusts pod resource requests based on configurable ratios while preventing invalid resource configurations.

Key Features

🚀 Resource Request Auto-Adjustment

  • Automatic Request Optimization: Automatically adjusts pod resource requests based on limits and configurable ratios to improve cluster resource utilization
  • Differentiated Strategies:
    • Default pods: 1:10 limit-to-request ratio (customizable)
    • Database pods (KubeBlocks managed): 1:5 limit-to-request ratio (customizable)
  • Smart Thresholds: Skip adjustment for pods with limits below configurable thresholds (default: 100m CPU, 128Mi memory)

🛡️ Resource Validation

  • Prevents pods from setting CPU or memory limits to "0"
  • Validates both regular containers and init containers
  • Special handling for database pods (only validates first container)

🎯 Targeted Application

  • Only applies to user namespaces (namespaces starting with "ns-")
  • Respects existing low resource requests (won't increase requests)
  • Database pod detection via KubeBlocks labels

@zijiren233 zijiren233 requested a review from a team as a code owner September 24, 2025 09:31
@pull-request-size
Copy link

Whoa! Easy there, Partner!

This PR is too big. Please break it up into smaller PRs.

Complete()
}

//+kubebuilder:webhook:path=/mutate--v1-pod,mutating=true,failurePolicy=fail,sideEffects=None,groups=core,resources=pods,verbs=create;update,versions=v1,name=mpod.sealos.io,admissionReviewVersions=v1
Copy link
Collaborator

Choose a reason for hiding this comment

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

mutate--v1-pod The name is too simple. If there are more webhooks in the future, it may not be clear what it is.

use mutate-qota-v1-pod is better?


const (
// Labels to identify KubeBlocks database pods
KubeBlocksManagedByLabel = "app.kubernetes.io/managed-by"
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better not to use this label. It's more common. I'm not sure whether to use helm to install this value is kubeblocks or helm.

Copy link
Member Author

Choose a reason for hiding this comment

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

When using helm, this will be Helm, and the purpose of this label is to obtain ratios; for example, the ratio for ordinary Pods is 1:10, while for kubeblock it is 1:5.

@cuisongliu cuisongliu requested a review from Copilot September 24, 2025 15:06
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

This PR introduces a new admission webhook for Pod resources that implements intelligent resource request auto-adjustment to optimize cluster resource utilization while also validating resource configurations to prevent invalid settings.

Key changes:

  • Automatic resource request optimization based on configurable ratios (1:10 for regular pods, 1:5 for database pods by default)
  • Resource validation preventing zero CPU/memory limits
  • Differentiated handling for database pods (KubeBlocks managed) vs regular pods

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
webhooks/admission/deploy/manifests/deploy.yaml.tmpl Adds webhook configuration for pod mutating and validating webhooks with new command-line arguments
webhooks/admission/deploy/Kubefile Sets default environment variables for pod webhook configuration
webhooks/admission/config/webhook/manifests.yaml Defines webhook registration for development/testing environments
webhooks/admission/config/manager/kustomization.yaml Updates container image name reference
webhooks/admission/config/default/manager_auth_proxy_patch.yaml Adds new command-line arguments for pod webhook configuration
webhooks/admission/cmd/main.go Integrates pod webhook setup with command-line flag parsing and manager registration
webhooks/admission/api/v1/pod_webhook_test.go Comprehensive test coverage for pod mutation logic including edge cases
webhooks/admission/api/v1/pod_webhook.go Core implementation of pod mutating and validating webhook logic
webhooks/admission/api/v1/pod_validator_test.go Test coverage for pod validation logic including database pod special handling

Comment on lines 89 to 90
// Only apply oversell to namespaces starting with "ns-"
if !isUserNamespace(pod.Namespace) {
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

The function isUserNamespace is referenced but not defined in this file. This will cause a compilation error.

Copilot uses AI. Check for mistakes.
"os"
"strings"

v1 "github.com/labring/sealos/webhook/admission/api/v1"
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

The import path contains 'webhook' but the actual directory structure shows 'webhooks' (plural). This import path may be incorrect and could cause build issues.

Suggested change
v1 "github.com/labring/sealos/webhook/admission/api/v1"
v1 "github.com/labring/sealos/webhooks/admission/api/v1"

Copilot uses AI. Check for mistakes.
- Refactor PodMutator to use configurable oversell ratios with constructor functions
- Replace generic database pod detection with specific KubeBlocks label checking
- Optimize database pod mutation to only process first container (main workload)
- Improve resource adjustment logic to only modify requests that exceed oversell limits
- Add comprehensive test coverage for new functionality including edge cases
- Handle zero resource limits gracefully without unnecessary mutations
@cuisongliu cuisongliu modified the milestones: v5.1, v5.2 Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants