pkg/proxy: move get kernel version out of ipvs proxier
Signed-off-by: Daman Arora <aroradaman@gmail.com>
This commit is contained in:
8
pkg/util/kernel/OWNERS
Normal file
8
pkg/util/kernel/OWNERS
Normal file
@@ -0,0 +1,8 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
reviewers:
|
||||
- sig-network-reviewers
|
||||
- sig-node-reviewers
|
||||
approvers:
|
||||
- sig-network-approvers
|
||||
- sig-node-approvers
|
45
pkg/util/kernel/constants.go
Normal file
45
pkg/util/kernel/constants.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Copyright 2023 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kernel
|
||||
|
||||
// IPLocalReservedPortsNamespacedKernelVersion is the kernel version in which net.ipv4.ip_local_reserved_ports was namespaced(netns).
|
||||
// (ref: https://github.com/torvalds/linux/commit/122ff243f5f104194750ecbc76d5946dd1eec934)
|
||||
const IPLocalReservedPortsNamespacedKernelVersion = "3.16"
|
||||
|
||||
// IPVSConnReuseModeMinSupportedKernelVersion is the minium kernel version supporting net.ipv4.vs.conn_reuse_mode.
|
||||
// (ref: https://github.com/torvalds/linux/commit/d752c364571743d696c2a54a449ce77550c35ac5)
|
||||
const IPVSConnReuseModeMinSupportedKernelVersion = "4.1"
|
||||
|
||||
// TCPKeepAliveTimeNamespacedKernelVersion is the kernel version in which net.ipv4.tcp_keepalive_time was namespaced(netns).
|
||||
// (ref: https://github.com/torvalds/linux/commit/13b287e8d1cad951634389f85b8c9b816bd3bb1e)
|
||||
const TCPKeepAliveTimeNamespacedKernelVersion = "4.5"
|
||||
|
||||
// TCPKeepAliveIntervalNamespacedKernelVersion is the kernel version in which net.ipv4.tcp_keepalive_intvl was namespaced(netns).
|
||||
// (ref: https://github.com/torvalds/linux/commit/b840d15d39128d08ed4486085e5507d2617b9ae1)
|
||||
const TCPKeepAliveIntervalNamespacedKernelVersion = "4.5"
|
||||
|
||||
// TCPKeepAliveProbesNamespacedKernelVersion is the kernel version in which net.ipv4.tcp_keepalive_probes was namespaced(netns).
|
||||
// (ref: https://github.com/torvalds/linux/commit/9bd6861bd4326e3afd3f14a9ec8a723771fb20bb)
|
||||
const TCPKeepAliveProbesNamespacedKernelVersion = "4.5"
|
||||
|
||||
// TCPFinTimeoutNamespacedKernelVersion is the kernel version in which net.ipv4.tcp_fin_timeout was namespaced(netns).
|
||||
// (ref: https://github.com/torvalds/linux/commit/1e579caa18b96f9eb18f4f5416658cd15f37c062)
|
||||
const TCPFinTimeoutNamespacedKernelVersion = "4.6"
|
||||
|
||||
// IPVSConnReuseModeFixedKernelVersion is the kernel version in which net.ipv4.vs.conn_reuse_mode was fixed.
|
||||
// (ref: https://github.com/torvalds/linux/commit/35dfb013149f74c2be1ff9c78f14e6a3cd1539d1)
|
||||
const IPVSConnReuseModeFixedKernelVersion = "5.9"
|
48
pkg/util/kernel/version.go
Normal file
48
pkg/util/kernel/version.go
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright 2023 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kernel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/version"
|
||||
)
|
||||
|
||||
type readFileFunc func(string) ([]byte, error)
|
||||
|
||||
// GetVersion returns currently running kernel version.
|
||||
func GetVersion() (*version.Version, error) {
|
||||
return getVersion(os.ReadFile)
|
||||
}
|
||||
|
||||
// getVersion reads os release file from the give readFile function.
|
||||
func getVersion(readFile readFileFunc) (*version.Version, error) {
|
||||
kernelVersionFile := "/proc/sys/kernel/osrelease"
|
||||
fileContent, err := readFile(kernelVersionFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read os-release file: %s", err.Error())
|
||||
}
|
||||
|
||||
kernelVersion, err := version.ParseGeneric(strings.TrimSpace(string(fileContent)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse kernel version: %s", err.Error())
|
||||
}
|
||||
|
||||
return kernelVersion, nil
|
||||
}
|
82
pkg/util/kernel/version_test.go
Normal file
82
pkg/util/kernel/version_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
Copyright 2023 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kernel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/version"
|
||||
)
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
readFileFunc readFileFunc
|
||||
expected *version.Version
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "valid os-release file",
|
||||
readFileFunc: func(_ string) ([]byte, error) {
|
||||
return []byte("5.15.0-84-generic"), nil
|
||||
},
|
||||
expected: version.MajorMinor(5, 15),
|
||||
},
|
||||
{
|
||||
name: "valid os-release file",
|
||||
readFileFunc: func(_ string) ([]byte, error) {
|
||||
return []byte("5.4.0-128-generic"), nil
|
||||
},
|
||||
expected: version.MajorMinor(5, 4),
|
||||
},
|
||||
{
|
||||
name: "failed to read os-release file",
|
||||
readFileFunc: func(_ string) ([]byte, error) {
|
||||
return nil, errors.New("open /proc/sys/kernel/osrelease: failed to read file")
|
||||
},
|
||||
err: errors.New("failed to read os-release file: open /proc/sys/kernel/osrelease: failed to read file"),
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "version not parsable",
|
||||
readFileFunc: func(_ string) ([]byte, error) {
|
||||
return []byte("5-15-0"), nil
|
||||
},
|
||||
err: errors.New("failed to parse kernel version: illegal version string \"5-15-0\""),
|
||||
expected: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
kernelVersion, err := getVersion(tc.readFileFunc)
|
||||
|
||||
if tc.err != nil {
|
||||
assert.Equal(t, tc.err.Error(), err.Error())
|
||||
assert.Nil(t, kernelVersion)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expected.Major(), kernelVersion.Major())
|
||||
assert.Equal(t, tc.expected.Minor(), kernelVersion.Minor())
|
||||
assert.Equal(t, tc.expected.Patch(), kernelVersion.Patch())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user