forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pods.go
179 lines (158 loc) · 5.31 KB
/
pods.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
177
178
179
// Copyright 2018 The Gardener 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 kubernetesbase
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"time"
"github.com/gardener/gardener/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)
var podPath = []string{"api", "v1", "pods"}
// GetPod will return the Pod object for the given <name> in the given <namespace>.
func (c *Client) GetPod(namespace, name string) (*corev1.Pod, error) {
return c.
Clientset.
CoreV1().
Pods(namespace).
Get(name, metav1.GetOptions{})
}
// ListPods will list all the Pods in the given <namespace> for the given <listOptions>.
func (c *Client) ListPods(namespace string, listOptions metav1.ListOptions) (*corev1.PodList, error) {
pods, err := c.
Clientset.
CoreV1().
Pods(namespace).
List(listOptions)
if err != nil {
return nil, err
}
sort.Slice(pods.Items, func(i, j int) bool {
return pods.Items[i].ObjectMeta.CreationTimestamp.Before(&pods.Items[j].ObjectMeta.CreationTimestamp)
})
return pods, nil
}
// GetPodLogs will get the logs of all containers within the Pod for the given <name> in the given <namespace>
// for the given <podLogOptions>.
func (c *Client) GetPodLogs(namespace, name string, podLogOptions *corev1.PodLogOptions) (*bytes.Buffer, error) {
request := c.
Clientset.
CoreV1().
Pods(namespace).
GetLogs(name, podLogOptions)
stream, err := request.Stream()
if err != nil {
return nil, err
}
defer stream.Close()
buffer := bytes.NewBuffer(nil)
_, err = io.Copy(buffer, stream)
if err != nil {
return nil, err
}
return buffer, nil
}
// ForwardPodPort tries to forward the <remote> port of the pod with name <name> in namespace <namespace> to
// the <local> port. If <local> equals zero, a free port will be chosen randomly.
// It returns the stop channel which must be closed when the port forward connection should be terminated.
func (c *Client) ForwardPodPort(namespace, name string, local, remote int) (chan struct{}, error) {
fw, stopChan, err := c.setupForwardPodPort(namespace, name, local, remote)
if err != nil {
return nil, err
}
return stopChan, fw.ForwardPorts()
}
// CheckForwardPodPort tries to forward the <remote> port of the pod with name <name> in namespace <namespace> to
// the <local> port. If <local> equals zero, a free port will be chosen randomly.
// It returns true if the port forward connection has been established successfully or false otherwise.
func (c *Client) CheckForwardPodPort(namespace, name string, local, remote int) (bool, error) {
fw, stopChan, err := c.setupForwardPodPort(namespace, name, local, remote)
if err != nil {
return false, err
}
errChan := make(chan error)
go func() {
errChan <- fw.ForwardPorts()
}()
defer close(stopChan)
select {
case err = <-errChan:
return false, fmt.Errorf("forwarding ports: %v", err)
case <-fw.Ready:
return true, nil
case <-time.After(time.Second * 5):
return false, errors.New("port forward connection could not be established within five seconds")
}
}
// DeletePod will delete a Pod with the given <name> in the given <namespace>.
func (c *Client) DeletePod(namespace, name string) error {
return c.
Clientset.
CoreV1().
Pods(namespace).
Delete(name, &defaultDeleteOptions)
}
// CleanupPods deletes all the Pods in the cluster other than those stored in the
// exceptions map <exceptions>.
func (c *Client) CleanupPods(exceptions map[string]bool) error {
return c.CleanupResource(exceptions, true, podPath...)
}
// CheckPodCleanup will check whether all the Pods in the cluster other than those
// stored in the exceptions map <exceptions> have been deleted. It will return an error
// in case it has not finished yet, and nil if all resources are gone.
func (c *Client) CheckPodCleanup(exceptions map[string]bool) (bool, error) {
return c.CheckResourceCleanup(exceptions, true, podPath...)
}
func (c *Client) setupForwardPodPort(namespace, name string, local, remote int) (*portforward.PortForwarder, chan struct{}, error) {
var (
stopChan = make(chan struct{}, 1)
readyChan = make(chan struct{}, 1)
out = ioutil.Discard
localPort int
)
u := c.
Clientset.
Core().
RESTClient().
Post().
Resource("pods").
Namespace(namespace).
Name(name).
SubResource("portforward").
URL()
transport, upgrader, err := spdy.RoundTripperFor(c.Config)
if err != nil {
return nil, nil, err
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", u)
if local == 0 {
localPort, err = utils.FindFreePort()
if err != nil {
return nil, nil, err
}
}
fw, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", localPort, remote)}, stopChan, readyChan, out, out)
if err != nil {
return nil, nil, err
}
return fw, stopChan, nil
}