-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
185 lines (160 loc) · 5.18 KB
/
util.go
File metadata and controls
185 lines (160 loc) · 5.18 KB
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
177
178
179
180
181
182
183
184
185
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
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 pkg
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"time"
"stash.appscode.dev/apimachinery/apis"
"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/klog/v2"
"k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
"k8s.io/kubectl/pkg/scheme"
)
const (
PullInterval = time.Second * 2
WaitTimeOut = time.Minute * 10
)
func WaitUntilBackupSessionCompleted(name string, namespace string) error {
return wait.PollUntilContextTimeout(context.Background(), PullInterval, WaitTimeOut, true, func(ctx context.Context) (done bool, err error) {
backupSession, err := stashClient.StashV1beta1().BackupSessions(namespace).Get(ctx, name, metav1.GetOptions{})
if err == nil {
if backupSession.Status.Phase == v1beta1.BackupSessionSucceeded {
return true, nil
}
if backupSession.Status.Phase == v1beta1.BackupSessionFailed {
return true, fmt.Errorf("BackupSession has been failed")
}
}
return false, nil
})
}
func WaitUntilRestoreSessionCompleted(name string, namespace string) error {
return wait.PollUntilContextTimeout(context.Background(), PullInterval, WaitTimeOut, true, func(ctx context.Context) (done bool, err error) {
restoreSession, err := stashClient.StashV1beta1().RestoreSessions(namespace).Get(ctx, name, metav1.GetOptions{})
if err == nil {
if restoreSession.Status.Phase == v1beta1.RestoreSucceeded {
return true, nil
}
if restoreSession.Status.Phase == v1beta1.RestoreFailed {
return true, fmt.Errorf("RestoreSession has been failed")
}
}
return false, nil
})
}
func GetOperatorPod(aggrClient *clientset.Clientset, kubeClient *kubernetes.Clientset) (*core.Pod, error) {
apiSvc, err := aggrClient.ApiregistrationV1().APIServices().Get(context.TODO(), "v1alpha1.admission.stash.appscode.com", metav1.GetOptions{})
if err != nil {
return nil, err
}
podList, err := kubeClient.CoreV1().Pods(apiSvc.Spec.Service.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for i := range podList.Items {
if hasStashContainer(&podList.Items[i]) {
return &podList.Items[i], nil
}
}
return nil, fmt.Errorf("operator pod not found")
}
func hasStashContainer(pod *core.Pod) bool {
if strings.Contains(pod.Name, "stash") {
for _, c := range pod.Spec.Containers {
if c.Name == apis.OperatorContainer {
return true
}
}
}
return false
}
func execCommandOnPod(kubeClient *kubernetes.Clientset, config *rest.Config, pod *core.Pod, command []string) ([]byte, error) {
var (
execOut bytes.Buffer
execErr bytes.Buffer
)
klog.Infof("Executing command %v on pod %v", command, pod.Name)
req := kubeClient.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
Timeout(5 * time.Minute)
req.VersionedParams(&core.PodExecOptions{
Container: getContainerName(pod),
Command: command,
Stdout: true,
Stderr: true,
}, scheme.ParameterCodec)
executor, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
return nil, fmt.Errorf("failed to init executor: %v", err)
}
err = executor.StreamWithContext(context.Background(), remotecommand.StreamOptions{
Stdout: &execOut,
Stderr: &execErr,
Tty: true,
})
if err != nil {
return nil, fmt.Errorf("could not execute: %v, reason: %s", err, execErr.String())
}
return execOut.Bytes(), nil
}
func getContainerName(pod *core.Pod) string {
if hasStashContainer(pod) {
return apis.OperatorContainer
}
return apis.StashContainer
}
// nolint: unparam
func removeDirWithLogErr(dir string) {
if err := os.RemoveAll(dir); err != nil {
klog.Errorf("failed to remove directory %s. Reason: %v", dir, err)
}
}
func runCmdViaDocker(localDirs cliLocalDirectories, command string, extraArgs []string) error {
// get current user
currentUser, err := user.Current()
if err != nil {
return err
}
args := []string{
"run",
"--rm",
"-u", currentUser.Uid,
"-v", ScratchDir + ":" + ScratchDir,
"--env", "HTTP_PROXY=" + os.Getenv("HTTP_PROXY"),
"--env", "HTTPS_PROXY=" + os.Getenv("HTTPS_PROXY"),
"--env-file", filepath.Join(localDirs.configDir, ResticEnvs),
imgRestic.ToContainerImage(),
command,
}
args = append(args, extraArgs...)
klog.Infoln("Running docker with args:", args)
out, err := exec.Command("docker", args...).CombinedOutput()
klog.Infoln("Output:", string(out))
return err
}