Skip to content

Commit 71b9114

Browse files
committed
kubelet: Migrate pkg/kubelet/sysctl to contextual logging
1 parent 814e2c8 commit 71b9114

8 files changed

+20
-9
lines changed

hack/golangci-hints.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ linters-settings: # please keep this alphabetized
178178
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
179179
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
180180
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
181+
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*
181182
182183
# As long as contextual logging is alpha or beta, all WithName, WithValues,
183184
# NewContext calls have to go through klog. Once it is GA, we can lift

hack/golangci-strict.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ linters-settings: # please keep this alphabetized
224224
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
225225
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
226226
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
227+
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*
227228
228229
# As long as contextual logging is alpha or beta, all WithName, WithValues,
229230
# NewContext calls have to go through klog. Once it is GA, we can lift

hack/golangci.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ linters-settings: # please keep this alphabetized
226226
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
227227
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
228228
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
229+
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*
229230
230231
# As long as contextual logging is alpha or beta, all WithName, WithValues,
231232
# NewContext calls have to go through klog. Once it is GA, we can lift

hack/logcheck.conf

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ contextual k8s.io/kubernetes/pkg/kubelet/clustertrustbundle/.*
5555
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
5656
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
5757
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
58+
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*
5859

5960
# As long as contextual logging is alpha or beta, all WithName, WithValues,
6061
# NewContext calls have to go through klog. Once it is GA, we can lift

pkg/kubelet/kubelet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration,
948948

949949
// Safe, allowed sysctls can always be used as unsafe sysctls in the spec.
950950
// Hence, we concatenate those two lists.
951-
safeAndUnsafeSysctls := append(sysctl.SafeSysctlAllowlist(), allowedUnsafeSysctls...)
951+
safeAndUnsafeSysctls := append(sysctl.SafeSysctlAllowlist(ctx), allowedUnsafeSysctls...)
952952
sysctlsAllowlist, err := sysctl.NewAllowlist(safeAndUnsafeSysctls)
953953
if err != nil {
954954
return nil, err

pkg/kubelet/sysctl/allowlist_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424

2525
v1 "k8s.io/api/core/v1"
2626
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
27+
"k8s.io/kubernetes/test/utils/ktesting"
2728
)
2829

2930
func TestNewAllowlist(t *testing.T) {
31+
tCtx := ktesting.Init(t)
3032
type Test struct {
3133
sysctls []string
3234
err bool
@@ -42,7 +44,7 @@ func TestNewAllowlist(t *testing.T) {
4244
{sysctls: []string{"foo"}, err: true},
4345
{sysctls: []string{"foo*"}, err: true},
4446
} {
45-
_, err := NewAllowlist(append(SafeSysctlAllowlist(), test.sysctls...))
47+
_, err := NewAllowlist(append(SafeSysctlAllowlist(tCtx), test.sysctls...))
4648
if test.err && err == nil {
4749
t.Errorf("expected an error creating a allowlist for %v", test.sysctls)
4850
} else if !test.err && err != nil {
@@ -52,6 +54,7 @@ func TestNewAllowlist(t *testing.T) {
5254
}
5355

5456
func TestAllowlist(t *testing.T) {
57+
tCtx := ktesting.Init(t)
5558
type Test struct {
5659
sysctl string
5760
hostNet, hostIPC bool
@@ -78,7 +81,7 @@ func TestAllowlist(t *testing.T) {
7881
pod.Spec.SecurityContext = &v1.PodSecurityContext{}
7982
attrs := &lifecycle.PodAdmitAttributes{Pod: pod}
8083

81-
w, err := NewAllowlist(append(SafeSysctlAllowlist(), "kernel.msg*", "kernel.sem", "net.b.*"))
84+
w, err := NewAllowlist(append(SafeSysctlAllowlist(tCtx), "kernel.msg*", "kernel.sem", "net.b.*"))
8285
if err != nil {
8386
t.Fatalf("failed to create allowlist: %v", err)
8487
}

pkg/kubelet/sysctl/safe_sysctls.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package sysctl
1818

1919
import (
20+
"context"
2021
goruntime "runtime"
2122

2223
"k8s.io/apimachinery/pkg/util/version"
@@ -75,18 +76,19 @@ var safeSysctls = []sysctl{
7576
// A sysctl is called safe iff
7677
// - it is namespaced in the container or the pod
7778
// - it is isolated, i.e. has no influence on any other pod on the same node.
78-
func SafeSysctlAllowlist() []string {
79+
func SafeSysctlAllowlist(ctx context.Context) []string {
7980
if goruntime.GOOS != "linux" {
8081
return nil
8182
}
8283

83-
return getSafeSysctlAllowlist(utilkernel.GetVersion)
84+
return getSafeSysctlAllowlist(ctx, utilkernel.GetVersion)
8485
}
8586

86-
func getSafeSysctlAllowlist(getVersion func() (*version.Version, error)) []string {
87+
func getSafeSysctlAllowlist(ctx context.Context, getVersion func() (*version.Version, error)) []string {
88+
logger := klog.FromContext(ctx)
8789
kernelVersion, err := getVersion()
8890
if err != nil {
89-
klog.ErrorS(err, "failed to get kernel version, unable to determine which sysctls are available")
91+
logger.Error(err, "failed to get kernel version, unable to determine which sysctls are available")
9092
}
9193

9294
var safeSysctlAllowlist []string
@@ -99,7 +101,7 @@ func getSafeSysctlAllowlist(getVersion func() (*version.Version, error)) []strin
99101
if kernelVersion != nil && kernelVersion.AtLeast(version.MustParseGeneric(sc.kernel)) {
100102
safeSysctlAllowlist = append(safeSysctlAllowlist, sc.name)
101103
} else {
102-
klog.InfoS("kernel version is too old, dropping the sysctl from safe sysctl list", "kernelVersion", kernelVersion, "sysctl", sc.name)
104+
logger.Info("kernel version is too old, dropping the sysctl from safe sysctl list", "kernelVersion", kernelVersion, "sysctl", sc.name)
103105
}
104106
}
105107
return safeSysctlAllowlist

pkg/kubelet/sysctl/safe_sysctls_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"testing"
2323

2424
"k8s.io/apimachinery/pkg/util/version"
25+
"k8s.io/kubernetes/test/utils/ktesting"
2526
)
2627

2728
func Test_getSafeSysctlAllowlist(t *testing.T) {
29+
tCtx := ktesting.Init(t)
2830
tests := []struct {
2931
name string
3032
getVersion func() (*version.Version, error)
@@ -82,7 +84,7 @@ func Test_getSafeSysctlAllowlist(t *testing.T) {
8284
}
8385
for _, tt := range tests {
8486
t.Run(tt.name, func(t *testing.T) {
85-
if got := getSafeSysctlAllowlist(tt.getVersion); !reflect.DeepEqual(got, tt.want) {
87+
if got := getSafeSysctlAllowlist(tCtx, tt.getVersion); !reflect.DeepEqual(got, tt.want) {
8688
t.Errorf("getSafeSysctlAllowlist() = %v, want %v", got, tt.want)
8789
}
8890
})

0 commit comments

Comments
 (0)