Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubelet: ensure secret pulled image #114847

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/features/kube_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ const (
// fallback to using it's cgroupDriver option.
KubeletCgroupDriverFromCRI featuregate.Feature = "KubeletCgroupDriverFromCRI"

// owner: @mikebrow @pacoxu
// kep: http://kep.k8s.io/2535
// alpha: v1.30
//
// Enables kubelet to ensure images pulled with pod imagePullSecrets are authenticated
// by other pods that do not have the same credentials.
KubeletEnsureSecretPulledImages featuregate.Feature = "KubeletEnsureSecretPulledImages"

// owner: @AkihiroSuda
// alpha: v1.22
//
Expand Down Expand Up @@ -1058,6 +1066,8 @@ var defaultKubernetesFeatureGates = map[featuregate.Feature]featuregate.FeatureS

KubeletInUserNamespace: {Default: false, PreRelease: featuregate.Alpha},

KubeletEnsureSecretPulledImages: {Default: false, PreRelease: featuregate.Alpha},

KubeletPodResourcesDynamicResources: {Default: false, PreRelease: featuregate.Alpha},

KubeletPodResourcesGet: {Default: false, PreRelease: featuregate.Alpha},
Expand Down
8 changes: 7 additions & 1 deletion pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/kubelet/apis/config/fuzzer/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
obj.EnableSystemLogHandler = true
obj.MemoryThrottlingFactor = utilpointer.Float64(rand.Float64())
obj.LocalStorageCapacityIsolation = true
obj.PullImageSecretRecheckPeriod = metav1.Duration{}
},
}
}
1 change: 1 addition & 0 deletions pkg/kubelet/apis/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ var (
"Port",
"ProtectKernelDefaults",
"ProviderID",
"PullImageSecretRecheckPeriod.Duration",
"ReadOnlyPort",
"RegisterNode",
"RegistryBurst",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ oomScoreAdj: -999
podLogsDir: /var/log/pods
podPidsLimit: -1
port: 10250
pullImageSecretRecheckPeriod: 0s
registerNode: true
registryBurst: 10
registryPullQPS: 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ oomScoreAdj: -999
podLogsDir: /var/log/pods
podPidsLimit: -1
port: 10250
pullImageSecretRecheckPeriod: 0s
registerNode: true
registryBurst: 10
registryPullQPS: 5
Expand Down
5 changes: 5 additions & 0 deletions pkg/kubelet/apis/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ type KubeletConfiguration struct {
// If not specified the default value is ContainerRuntimeEndpoint
// +optional
ImageServiceEndpoint string

// PullImageSecretRecheckPeriod defines the duration to recheck the pull image secret.
// By default, the kubelet will not recheck the pull image secret.
// For security reasons, we recommend rechecking the pull image secret, ideally every 24 hours (1d).
PullImageSecretRecheckPeriod metav1.Duration
}

// KubeletAuthorizationMode denotes the authorization mode for the kubelet
Expand Down
3 changes: 3 additions & 0 deletions pkg/kubelet/apis/config/v1beta1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,7 @@ func SetDefaults_KubeletConfiguration(obj *kubeletconfigv1beta1.KubeletConfigura
if obj.PodLogsDir == "" {
obj.PodLogsDir = DefaultPodLogsDir
}
if obj.PullImageSecretRecheckPeriod == nil {
obj.PullImageSecretRecheckPeriod = &metav1.Duration{}
}
}
61 changes: 35 additions & 26 deletions pkg/kubelet/apis/config/v1beta1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/kubernetes/pkg/kubelet/qos"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
utilpointer "k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)

func TestSetDefaultsKubeletConfiguration(t *testing.T) {
Expand Down Expand Up @@ -129,6 +130,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
RegisterNode: utilpointer.Bool(true),
LocalStorageCapacityIsolation: utilpointer.Bool(true),
PodLogsDir: DefaultPodLogsDir,
PullImageSecretRecheckPeriod: &metav1.Duration{},
},
},
{
Expand Down Expand Up @@ -259,6 +261,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
RegisterNode: utilpointer.Bool(false),
LocalStorageCapacityIsolation: utilpointer.Bool(false),
PodLogsDir: "",
PullImageSecretRecheckPeriod: nil,
},
&v1beta1.KubeletConfiguration{
EnableServer: utilpointer.Bool(false),
Expand Down Expand Up @@ -360,6 +363,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
RegisterNode: utilpointer.Bool(false),
LocalStorageCapacityIsolation: utilpointer.Bool(false),
PodLogsDir: DefaultPodLogsDir,
PullImageSecretRecheckPeriod: &metav1.Duration{},
},
},
{
Expand Down Expand Up @@ -512,6 +516,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
RegisterNode: utilpointer.Bool(true),
LocalStorageCapacityIsolation: utilpointer.Bool(true),
PodLogsDir: "/custom/path",
PullImageSecretRecheckPeriod: &metav1.Duration{Duration: 12 * time.Hour},
},
&v1beta1.KubeletConfiguration{
EnableServer: utilpointer.Bool(true),
Expand Down Expand Up @@ -592,7 +597,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
KubeAPIQPS: utilpointer.Int32(1),
KubeAPIBurst: 1,
SerializeImagePulls: utilpointer.Bool(true),
MaxParallelImagePulls: utilpointer.Int32Ptr(5),
MaxParallelImagePulls: ptr.To[int32](5),
EvictionHard: map[string]string{
"memory.available": "1Mi",
"nodefs.available": "1%",
Expand Down Expand Up @@ -661,6 +666,7 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
RegisterNode: utilpointer.Bool(true),
LocalStorageCapacityIsolation: utilpointer.Bool(true),
PodLogsDir: "/custom/path",
PullImageSecretRecheckPeriod: &metav1.Duration{Duration: 12 * time.Hour},
},
},
{
Expand Down Expand Up @@ -731,8 +737,8 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
EvictionPressureTransitionPeriod: metav1.Duration{Duration: 5 * time.Minute},
EnableControllerAttachDetach: utilpointer.Bool(true),
MakeIPTablesUtilChains: utilpointer.Bool(true),
IPTablesMasqueradeBit: utilpointer.Int32Ptr(DefaultIPTablesMasqueradeBit),
IPTablesDropBit: utilpointer.Int32Ptr(DefaultIPTablesDropBit),
IPTablesMasqueradeBit: ptr.To[int32](DefaultIPTablesMasqueradeBit),
IPTablesDropBit: ptr.To[int32](DefaultIPTablesDropBit),
FailSwapOn: utilpointer.Bool(true),
ContainerLogMaxSize: "10Mi",
ContainerLogMaxFiles: utilpointer.Int32(5),
Expand All @@ -749,10 +755,11 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
EnableProfilingHandler: utilpointer.Bool(true),
EnableDebugFlagsHandler: utilpointer.Bool(true),
SeccompDefault: utilpointer.Bool(false),
MemoryThrottlingFactor: utilpointer.Float64Ptr(DefaultMemoryThrottlingFactor),
MemoryThrottlingFactor: ptr.To(DefaultMemoryThrottlingFactor),
RegisterNode: utilpointer.Bool(true),
LocalStorageCapacityIsolation: utilpointer.Bool(true),
PodLogsDir: DefaultPodLogsDir,
PullImageSecretRecheckPeriod: &metav1.Duration{},
},
},
{
Expand Down Expand Up @@ -781,22 +788,22 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
CacheUnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
},
},
RegistryPullQPS: utilpointer.Int32Ptr(5),
RegistryPullQPS: ptr.To[int32](5),
RegistryBurst: 10,
EventRecordQPS: utilpointer.Int32Ptr(50),
EventRecordQPS: ptr.To[int32](50),
EventBurst: 100,
EnableDebuggingHandlers: utilpointer.Bool(true),
HealthzPort: utilpointer.Int32Ptr(10248),
HealthzPort: ptr.To[int32](10248),
HealthzBindAddress: "127.0.0.1",
OOMScoreAdj: utilpointer.Int32Ptr(int32(qos.KubeletOOMScoreAdj)),
OOMScoreAdj: ptr.To[int32](int32(qos.KubeletOOMScoreAdj)),
StreamingConnectionIdleTimeout: metav1.Duration{Duration: 4 * time.Hour},
NodeStatusUpdateFrequency: metav1.Duration{Duration: 10 * time.Second},
NodeStatusReportFrequency: metav1.Duration{Duration: 5 * time.Minute},
NodeLeaseDurationSeconds: 40,
ContainerRuntimeEndpoint: "unix:///run/containerd/containerd.sock",
ImageMinimumGCAge: metav1.Duration{Duration: 2 * time.Minute},
ImageGCHighThresholdPercent: utilpointer.Int32Ptr(85),
ImageGCLowThresholdPercent: utilpointer.Int32Ptr(80),
ImageGCHighThresholdPercent: ptr.To[int32](85),
ImageGCLowThresholdPercent: ptr.To[int32](80),
VolumeStatsAggPeriod: metav1.Duration{Duration: time.Minute},
CgroupsPerQOS: utilpointer.Bool(true),
CgroupDriver: "cgroupfs",
Expand All @@ -812,19 +819,19 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
ResolverConfig: utilpointer.String(kubetypes.ResolvConfDefault),
CPUCFSQuota: utilpointer.Bool(true),
CPUCFSQuotaPeriod: &metav1.Duration{Duration: 100 * time.Millisecond},
NodeStatusMaxImages: utilpointer.Int32Ptr(50),
NodeStatusMaxImages: ptr.To[int32](50),
MaxOpenFiles: 1000000,
ContentType: "application/vnd.kubernetes.protobuf",
KubeAPIQPS: utilpointer.Int32Ptr(50),
KubeAPIQPS: ptr.To[int32](50),
KubeAPIBurst: 100,
SerializeImagePulls: utilpointer.Bool(false),
MaxParallelImagePulls: utilpointer.Int32(5),
EvictionHard: nil,
EvictionPressureTransitionPeriod: metav1.Duration{Duration: 5 * time.Minute},
EnableControllerAttachDetach: utilpointer.Bool(true),
MakeIPTablesUtilChains: utilpointer.Bool(true),
IPTablesMasqueradeBit: utilpointer.Int32Ptr(DefaultIPTablesMasqueradeBit),
IPTablesDropBit: utilpointer.Int32Ptr(DefaultIPTablesDropBit),
IPTablesMasqueradeBit: ptr.To[int32](DefaultIPTablesMasqueradeBit),
IPTablesDropBit: ptr.To[int32](DefaultIPTablesDropBit),
FailSwapOn: utilpointer.Bool(true),
ContainerLogMaxSize: "10Mi",
ContainerLogMaxFiles: utilpointer.Int32(5),
Expand All @@ -841,10 +848,11 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
EnableProfilingHandler: utilpointer.Bool(true),
EnableDebugFlagsHandler: utilpointer.Bool(true),
SeccompDefault: utilpointer.Bool(false),
MemoryThrottlingFactor: utilpointer.Float64Ptr(DefaultMemoryThrottlingFactor),
MemoryThrottlingFactor: ptr.To(DefaultMemoryThrottlingFactor),
RegisterNode: utilpointer.Bool(true),
LocalStorageCapacityIsolation: utilpointer.Bool(true),
PodLogsDir: DefaultPodLogsDir,
PullImageSecretRecheckPeriod: &metav1.Duration{},
},
},
{
Expand Down Expand Up @@ -873,22 +881,22 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
CacheUnauthorizedTTL: metav1.Duration{Duration: 30 * time.Second},
},
},
RegistryPullQPS: utilpointer.Int32Ptr(5),
RegistryPullQPS: ptr.To[int32](5),
RegistryBurst: 10,
EventRecordQPS: utilpointer.Int32Ptr(50),
EventRecordQPS: ptr.To[int32](50),
EventBurst: 100,
EnableDebuggingHandlers: utilpointer.Bool(true),
HealthzPort: utilpointer.Int32Ptr(10248),
HealthzPort: ptr.To[int32](10248),
HealthzBindAddress: "127.0.0.1",
OOMScoreAdj: utilpointer.Int32Ptr(int32(qos.KubeletOOMScoreAdj)),
OOMScoreAdj: ptr.To[int32](int32(qos.KubeletOOMScoreAdj)),
StreamingConnectionIdleTimeout: metav1.Duration{Duration: 4 * time.Hour},
NodeStatusUpdateFrequency: metav1.Duration{Duration: 10 * time.Second},
NodeStatusReportFrequency: metav1.Duration{Duration: 5 * time.Minute},
NodeLeaseDurationSeconds: 40,
ContainerRuntimeEndpoint: "unix:///run/containerd/containerd.sock",
ImageMinimumGCAge: metav1.Duration{Duration: 2 * time.Minute},
ImageGCHighThresholdPercent: utilpointer.Int32Ptr(85),
ImageGCLowThresholdPercent: utilpointer.Int32Ptr(80),
ImageGCHighThresholdPercent: ptr.To[int32](85),
ImageGCLowThresholdPercent: ptr.To[int32](80),
VolumeStatsAggPeriod: metav1.Duration{Duration: time.Minute},
CgroupsPerQOS: utilpointer.Bool(true),
CgroupDriver: "cgroupfs",
Expand All @@ -904,19 +912,19 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
ResolverConfig: utilpointer.String(kubetypes.ResolvConfDefault),
CPUCFSQuota: utilpointer.Bool(true),
CPUCFSQuotaPeriod: &metav1.Duration{Duration: 100 * time.Millisecond},
NodeStatusMaxImages: utilpointer.Int32Ptr(50),
NodeStatusMaxImages: ptr.To[int32](50),
MaxOpenFiles: 1000000,
ContentType: "application/vnd.kubernetes.protobuf",
KubeAPIQPS: utilpointer.Int32Ptr(50),
KubeAPIQPS: ptr.To[int32](50),
KubeAPIBurst: 100,
SerializeImagePulls: utilpointer.Bool(true),
MaxParallelImagePulls: utilpointer.Int32(1),
EvictionHard: nil,
EvictionPressureTransitionPeriod: metav1.Duration{Duration: 5 * time.Minute},
EnableControllerAttachDetach: utilpointer.Bool(true),
MakeIPTablesUtilChains: utilpointer.Bool(true),
IPTablesMasqueradeBit: utilpointer.Int32(DefaultIPTablesMasqueradeBit),
IPTablesDropBit: utilpointer.Int32(DefaultIPTablesDropBit),
IPTablesMasqueradeBit: ptr.To[int32](DefaultIPTablesMasqueradeBit),
IPTablesDropBit: ptr.To[int32](DefaultIPTablesDropBit),
FailSwapOn: utilpointer.Bool(true),
ContainerLogMaxSize: "10Mi",
ContainerLogMaxFiles: utilpointer.Int32(5),
Expand All @@ -933,10 +941,11 @@ func TestSetDefaultsKubeletConfiguration(t *testing.T) {
EnableProfilingHandler: utilpointer.Bool(true),
EnableDebugFlagsHandler: utilpointer.Bool(true),
SeccompDefault: utilpointer.Bool(false),
MemoryThrottlingFactor: utilpointer.Float64(DefaultMemoryThrottlingFactor),
MemoryThrottlingFactor: ptr.To(DefaultMemoryThrottlingFactor),
RegisterNode: utilpointer.Bool(true),
LocalStorageCapacityIsolation: utilpointer.Bool(true),
PodLogsDir: DefaultPodLogsDir,
PullImageSecretRecheckPeriod: &metav1.Duration{},
},
},
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/kubelet/apis/config/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/kubelet/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/kubelet/container/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"hash/fnv"
"strconv"
"strings"

"k8s.io/klog/v2"
Expand Down Expand Up @@ -134,6 +135,17 @@ func HashContainerWithoutResources(container *v1.Container) uint64 {
return uint64(hashWithoutResources.Sum32())
}

// HashAuth - returns a hash code for a CRI pull image auth, and an error if the hash cannot be calculated.
func HashAuth(auth *runtimeapi.AuthConfig) (string, error) {
hash := fnv.New64a()
authJSON, err := json.Marshal(auth)
if err != nil {
return "", fmt.Errorf("failed to marshal auth config: %w", err)
}
hashutil.DeepHashObject(hash, authJSON)
return strconv.FormatUint(hash.Sum64(), 16), nil
}

// envVarsToMap constructs a map of environment name to value from a slice
// of env vars.
func envVarsToMap(envs []EnvVar) map[string]string {
Expand Down
Loading