Add negative validation for imageMinimumGCAge

This commit is contained in:
Neeraj Krishna Gopalakrishna
2026-01-02 15:34:46 +05:30
parent 6af6361e3b
commit 92129d36bf
5 changed files with 23 additions and 1 deletions

View File

@@ -70225,7 +70225,7 @@ func schema_k8sio_kubelet_config_v1beta1_KubeletConfiguration(ref common.Referen
},
"imageMinimumGCAge": {
SchemaProps: spec.SchemaProps{
Description: "imageMinimumGCAge is the minimum age for an unused image before it is garbage collected. Default: \"2m\"",
Description: "imageMinimumGCAge is the minimum age for an unused image before it is garbage collected. The field value must be greater than 0. If unset or 0, defaults to 2m. Default: \"2m\"",
Ref: ref(metav1.Duration{}.OpenAPIModelName()),
},
},

View File

@@ -219,6 +219,8 @@ type KubeletConfiguration struct {
NodeLeaseDurationSeconds int32
// ImageMinimumGCAge is the minimum age for an unused image before it is
// garbage collected.
// The field value must be greater than 0.
// If unset or 0, defaults to 2m.
ImageMinimumGCAge metav1.Duration
// ImageMaximumGCAge is the maximum age an image can be unused before it is garbage collected.
// The default of this field is "0s", which disables this field--meaning images won't be garbage

View File

@@ -96,6 +96,9 @@ func ValidateKubeletConfiguration(kc *kubeletconfig.KubeletConfiguration, featur
if kc.ImageMaximumGCAge.Duration != 0 && !localFeatureGate.Enabled(features.ImageMaximumGCAge) {
allErrors = append(allErrors, fmt.Errorf("invalid configuration: ImageMaximumGCAge feature gate is required for Kubelet configuration option imageMaximumGCAge"))
}
if kc.ImageMinimumGCAge.Duration < 0 {
allErrors = append(allErrors, fmt.Errorf("invalid configuration: imageMinimumGCAge %v must not be negative", kc.ImageMinimumGCAge.Duration))
}
if kc.ImageMaximumGCAge.Duration < 0 {
allErrors = append(allErrors, fmt.Errorf("invalid configuration: imageMaximumGCAge %v must not be negative", kc.ImageMaximumGCAge.Duration))
}

View File

@@ -48,6 +48,7 @@ var (
HealthzPort: 10248,
ImageGCHighThresholdPercent: 85,
ImageGCLowThresholdPercent: 80,
ImageMinimumGCAge: metav1.Duration{Duration: 2 * time.Minute},
ImagePullCredentialsVerificationPolicy: "NeverVerifyPreloadedImages",
IPTablesDropBit: 15,
IPTablesMasqueradeBit: 14,
@@ -751,6 +752,20 @@ func TestValidateKubeletConfiguration(t *testing.T) {
return conf
},
errMsg: "unrecognized feature gate: invalid",
}, {
name: "invalid configuration: invalid ImageMinimumGCAge",
configure: func(conf *kubeletconfig.KubeletConfiguration) *kubeletconfig.KubeletConfiguration {
conf.ImageMinimumGCAge = metav1.Duration{Duration: -1}
return conf
},
errMsg: "invalid configuration: imageMinimumGCAge -1ns must not be negative",
}, {
name: "valid ImageMinimumGCAge set to default 1ns",
configure: func(conf *kubeletconfig.KubeletConfiguration) *kubeletconfig.KubeletConfiguration {
// Verify that values other than the default are accepted.
conf.ImageMinimumGCAge = metav1.Duration{Duration: 1 * time.Nanosecond}
return conf
},
},
}

View File

@@ -335,6 +335,8 @@ type KubeletConfiguration struct {
NodeLeaseDurationSeconds int32 `json:"nodeLeaseDurationSeconds,omitempty"`
// imageMinimumGCAge is the minimum age for an unused image before it is
// garbage collected.
// The field value must be greater than 0.
// If unset or 0, defaults to 2m.
// Default: "2m"
// +optional
ImageMinimumGCAge metav1.Duration `json:"imageMinimumGCAge,omitempty"`