@@ -29,13 +29,18 @@ type SysctlError struct {
29
29
additionalInfo string
30
30
err error
31
31
option string
32
+ hasValue bool
32
33
value int
33
34
fatal bool
34
35
}
35
36
36
37
// Error return the error as string
37
38
func (e * SysctlError ) Error () string {
38
- return fmt .Sprintf ("Sysctl %s=%d : %s (%s)" , e .option , e .value , e .err , e .additionalInfo )
39
+ value := ""
40
+ if e .hasValue {
41
+ value = fmt .Sprintf ("=%d" , e .value )
42
+ }
43
+ return fmt .Sprintf ("Sysctl %s%s : %s (%s)" , e .option , value , e .err , e .additionalInfo )
39
44
}
40
45
41
46
// IsFatal was the error fatal and reason to exit kube-router
@@ -48,6 +53,39 @@ func (e *SysctlError) Unwrap() error {
48
53
return e .err
49
54
}
50
55
56
+ func sysctlStat (path string , hasValue bool , value int ) (string , * SysctlError ) {
57
+ sysctlPath := fmt .Sprintf ("/proc/sys/%s" , path )
58
+ if _ , err := os .Stat (sysctlPath ); err != nil {
59
+ if os .IsNotExist (err ) {
60
+ return sysctlPath , & SysctlError {
61
+ "option not found, Does your kernel version support this feature?" ,
62
+ err , path , hasValue , value , false }
63
+ }
64
+ return sysctlPath , & SysctlError {"path existed, but could not be stat'd" , err , path , hasValue , value , true }
65
+ }
66
+ return sysctlPath , nil
67
+ }
68
+
69
+ // GetSysctlSingleTemplate gets a sysctl value by first formatting the PathTemplate parameter with the substitute string
70
+ // and then getting the sysctl value and converting it into a string
71
+ func GetSysctlSingleTemplate (pathTemplate string , substitute string ) (string , * SysctlError ) {
72
+ actualPath := fmt .Sprintf (pathTemplate , substitute )
73
+ return GetSysctl (actualPath )
74
+ }
75
+
76
+ // GetSysctl gets a sysctl value
77
+ func GetSysctl (path string ) (string , * SysctlError ) {
78
+ sysctlPath , err := sysctlStat (path , false , 0 )
79
+ if err != nil {
80
+ return "" , err
81
+ }
82
+ buf , readErr := os .ReadFile (sysctlPath )
83
+ if readErr != nil {
84
+ return "" , & SysctlError {"path could not be read" , err , path , false , 0 , true }
85
+ }
86
+ return string (buf ), nil
87
+ }
88
+
51
89
// SetSysctlSingleTemplate sets a sysctl value by first formatting the PathTemplate parameter with the substitute string
52
90
// and then setting the sysctl to the value parameter
53
91
func SetSysctlSingleTemplate (pathTemplate string , substitute string , value int ) * SysctlError {
@@ -57,18 +95,13 @@ func SetSysctlSingleTemplate(pathTemplate string, substitute string, value int)
57
95
58
96
// SetSysctl sets a sysctl value
59
97
func SetSysctl (path string , value int ) * SysctlError {
60
- sysctlPath := fmt .Sprintf ("/proc/sys/%s" , path )
61
- if _ , err := os .Stat (sysctlPath ); err != nil {
62
- if os .IsNotExist (err ) {
63
- return & SysctlError {
64
- "option not found, Does your kernel version support this feature?" ,
65
- err , path , value , false }
66
- }
67
- return & SysctlError {"path existed, but could not be stat'd" , err , path , value , true }
68
- }
69
- err := os .WriteFile (sysctlPath , []byte (strconv .Itoa (value )), 0640 )
98
+ sysctlPath , err := sysctlStat (path , true , value )
70
99
if err != nil {
71
- return & SysctlError {"path could not be set" , err , path , value , true }
100
+ return err
101
+ }
102
+ writeErr := os .WriteFile (sysctlPath , []byte (strconv .Itoa (value )), 0640 )
103
+ if writeErr != nil {
104
+ return & SysctlError {"path could not be set" , err , path , true , value , true }
72
105
}
73
106
return nil
74
107
}
0 commit comments