-
Notifications
You must be signed in to change notification settings - Fork 92
/
sysctl.go
105 lines (89 loc) · 2.94 KB
/
sysctl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package collect
import (
"bytes"
"context"
"path/filepath"
"time"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)
type CollectSysctl struct {
Collector *troubleshootv1beta2.Sysctl
BundlePath string
Namespace string
ClientConfig *rest.Config
Client kubernetes.Interface
Context context.Context
RBACErrors
}
func (c *CollectSysctl) Title() string {
return getCollectorName(c)
}
func (c *CollectSysctl) IsExcluded() (bool, error) {
return isExcluded(c.Collector.Exclude)
}
func (c *CollectSysctl) Collect(progressChan chan<- interface{}) (CollectorResult, error) {
if c.Collector.Timeout != "" {
timeout, err := time.ParseDuration(c.Collector.Timeout)
if err != nil {
return nil, errors.Wrap(err, "parse timeout")
}
if timeout == 0 {
timeout = time.Minute
}
childCtx, cancel := context.WithTimeout(c.Context, timeout)
defer cancel()
c.Context = childCtx
}
if c.Collector.Namespace == "" {
c.Collector.Namespace = c.Namespace
}
if c.Collector.Namespace == "" {
kubeconfig := k8sutil.GetKubeconfig()
namespace, _, _ := kubeconfig.Namespace()
c.Collector.Namespace = namespace
}
runPodOptions := RunPodOptions{
Image: c.Collector.Image,
ImagePullPolicy: c.Collector.ImagePullPolicy,
Namespace: c.Collector.Namespace,
HostNetwork: true,
}
command := `
find /proc/sys/net/ipv4 -type f | while read f; do v=$(cat $f 2>/dev/null); echo "$f = $v"; done
find /proc/sys/net/bridge -type f | while read f; do v=$(cat $f 2>/dev/null); echo "$f = $v"; done
find /proc/sys/vm -type f | while read f; do v=$(cat $f 2>/dev/null); echo "$f = $v"; done
`
runPodOptions.Command = []string{"sh", "-c", command}
if c.Collector.ImagePullSecret != nil {
runPodOptions.ImagePullSecretName = c.Collector.ImagePullSecret.Name
if c.Collector.ImagePullSecret.Data != nil {
secretName, err := createSecret(c.Context, c.Client, c.Collector.Namespace, c.Collector.ImagePullSecret)
if err != nil {
return nil, errors.Wrap(err, "create image pull secret")
}
defer func() {
err := c.Client.CoreV1().Secrets(c.Collector.Namespace).Delete(context.Background(), c.Collector.ImagePullSecret.Name, metav1.DeleteOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
klog.Errorf("Failed to delete secret %s: %v", c.Collector.ImagePullSecret.Name, err)
}
}()
runPodOptions.ImagePullSecretName = secretName
}
}
results, err := RunPodsReadyNodes(c.Context, c.Client.CoreV1(), runPodOptions)
if err != nil {
return nil, err
}
output := NewResult()
for k, v := range results {
output.SaveResult(c.BundlePath, filepath.Join("sysctl", k), bytes.NewBuffer(v))
}
return output, nil
}