/
exec.go
176 lines (149 loc) · 5.44 KB
/
exec.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright 2023 The Rook Authors. All rights reserved.
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 exec
import (
"bytes"
"context"
"fmt"
"io"
"os"
"github.com/rook/kubectl-rook-ceph/pkg/k8sutil"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
var (
OperatorNamespace string // operator namespae
CephClusterNamespace string // Cephcluster namespace
)
func RunCommandInOperatorPod(ctx context.Context, clientsets *k8sutil.Clientsets, cmd string, args []string, operatorNamespace, clusterNamespace string, returnOutput bool) (string, error) {
var pod v1.Pod
var err error
pod, err = k8sutil.WaitForPodToRun(ctx, clientsets.Kube, operatorNamespace, "app=rook-ceph-operator")
if err != nil {
return "", fmt.Errorf("failed to wait for operator pod to run. %w", err)
}
var stdout, stderr bytes.Buffer
err = execCmdInPod(ctx, clientsets, cmd, pod.Name, "rook-ceph-operator", pod.Namespace, clusterNamespace, args, &stdout, &stderr, returnOutput)
if err != nil {
err = fmt.Errorf("%s. %w", stderr.String(), err)
}
var out string
if returnOutput {
out = stdout.String()
}
return out, err
}
func RunCommandInToolboxPod(ctx context.Context, clientsets *k8sutil.Clientsets, cmd string, args []string, clusterNamespace string, returnOutput bool) (string, error) {
var pod v1.Pod
var err error
pod, err = k8sutil.WaitForPodToRun(ctx, clientsets.Kube, clusterNamespace, "app=rook-ceph-tools")
if err != nil {
return "", fmt.Errorf("failed to wait for operator pod to run. %w", err)
}
var stdout, stderr bytes.Buffer
err = execCmdInPod(ctx, clientsets, cmd, pod.Name, "rook-ceph-tools", pod.Namespace, clusterNamespace, args, &stdout, &stderr, returnOutput)
if err != nil {
err := fmt.Errorf("failed to run command. %w", err)
if !returnOutput {
return "", err
}
return stdout.String(), err
}
if !returnOutput {
return "", nil
}
fmt.Println(stderr.String())
return stdout.String(), nil
}
func RunCommandInLabeledPod(ctx context.Context, clientsets *k8sutil.Clientsets, label, container, cmd string, args []string, clusterNamespace string, returnOutput bool) (string, error) {
var list *v1.PodList
var err error
opts := metav1.ListOptions{LabelSelector: label}
list, err = clientsets.Kube.CoreV1().Pods(clusterNamespace).List(ctx, opts)
if err != nil || len(list.Items) == 0 {
return "", fmt.Errorf("failed to get rook mon pod where the command could be executed. %w", err)
}
var stdout, stderr bytes.Buffer
err = execCmdInPod(ctx, clientsets, cmd, list.Items[0].Name, container, list.Items[0].Namespace, clusterNamespace, args, &stdout, &stderr, returnOutput)
if err != nil {
err := fmt.Errorf("failed to run command. %w", err)
if !returnOutput {
return "", err
}
return stdout.String(), err
}
if !returnOutput {
return "", nil
}
fmt.Println(stderr.String())
return stdout.String(), nil
}
// execCmdInPod exec command on specific pod and wait the command's output.
func execCmdInPod(ctx context.Context, clientsets *k8sutil.Clientsets,
command, podName, containerName, podNamespace, clusterNamespace string,
args []string, stdout, stderr io.Writer, returnOutput bool) error {
if len(args) < 1 {
return fmt.Errorf("no arg passed to exec with %q command", command)
}
cmd := []string{}
cmd = append(cmd, command)
cmd = append(cmd, args...)
if containerName == "rook-ceph-tools" {
cmd = append(cmd, "--connect-timeout=10")
} else if cmd[0] == "ceph" && args[0] != "daemon" {
cmd = append(cmd, "--connect-timeout=10", fmt.Sprintf("--conf=/var/lib/rook/%s/%s.config", clusterNamespace, clusterNamespace))
} else if cmd[0] == "rbd" {
cmd = append(cmd, fmt.Sprintf("--conf=/var/lib/rook/%s/%s.config", clusterNamespace, clusterNamespace))
}
// Prepare the API URL used to execute another process within the Pod. In
// this case, we'll run a remote shell.
req := clientsets.Kube.CoreV1().RESTClient().
Post().
Namespace(podNamespace).
Resource("pods").
Name(podName).
SubResource("exec").
VersionedParams(&v1.PodExecOptions{
Container: containerName,
Command: cmd,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(clientsets.KubeConfig, "POST", req.URL())
if err != nil {
return fmt.Errorf("failed to create SPDYExecutor. %w", err)
}
// returnOutput is true, the command's output will be print on shell directly with os.Stdout or os.Stderr
if !returnOutput {
// Connect this process' std{in,out,err} to the remote shell process.
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
} else {
// Connect this process' std{in,out,err} to the remote shell process.
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: stdout,
Stderr: stderr,
Tty: false,
})
}
if err != nil {
return fmt.Errorf("failed to run command. %w", err)
}
return nil
}