forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
292 lines (262 loc) · 9.69 KB
/
rest.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package buildconfiginstantiate
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/httpstream/spdy"
knet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/wait"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
kapi "k8s.io/kubernetes/pkg/api"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
kubeletremotecommand "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
"k8s.io/kubernetes/pkg/registry/core/pod"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/generator"
"github.com/openshift/origin/pkg/build/registry"
buildutil "github.com/openshift/origin/pkg/build/util"
)
var (
cancelPollInterval = 500 * time.Millisecond
cancelPollDuration = 30 * time.Second
)
// NewStorage creates a new storage object for build generation
func NewStorage(generator *generator.BuildGenerator) *InstantiateREST {
return &InstantiateREST{generator: generator}
}
// InstantiateREST is a RESTStorage implementation for a BuildGenerator which supports only
// the Create operation (as the generator has no underlying storage object).
type InstantiateREST struct {
generator *generator.BuildGenerator
}
// New creates a new build generation request
func (s *InstantiateREST) New() runtime.Object {
return &buildapi.BuildRequest{}
}
// Create instantiates a new build from a build configuration
func (s *InstantiateREST) Create(ctx apirequest.Context, obj runtime.Object) (runtime.Object, error) {
if err := rest.BeforeCreate(Strategy, ctx, obj); err != nil {
return nil, err
}
request := obj.(*buildapi.BuildRequest)
if request.TriggeredBy == nil {
buildTriggerCauses := []buildapi.BuildTriggerCause{}
request.TriggeredBy = append(buildTriggerCauses,
buildapi.BuildTriggerCause{
Message: buildapi.BuildTriggerCauseManualMsg,
},
)
}
return s.generator.Instantiate(ctx, request)
}
func NewBinaryStorage(generator *generator.BuildGenerator, watcher rest.Watcher, podClient kcoreclient.PodsGetter, info kubeletclient.ConnectionInfoGetter) *BinaryInstantiateREST {
return &BinaryInstantiateREST{
Generator: generator,
Watcher: watcher,
PodGetter: &podGetter{podClient},
ConnectionInfo: info,
Timeout: 5 * time.Minute,
}
}
type BinaryInstantiateREST struct {
Generator *generator.BuildGenerator
Watcher rest.Watcher
PodGetter pod.ResourceGetter
ConnectionInfo kubeletclient.ConnectionInfoGetter
Timeout time.Duration
}
// New creates a new build generation request
func (s *BinaryInstantiateREST) New() runtime.Object {
return &buildapi.BinaryBuildRequestOptions{}
}
// Connect returns a ConnectHandler that will handle the request/response for a request
func (r *BinaryInstantiateREST) Connect(ctx apirequest.Context, name string, options runtime.Object, responder rest.Responder) (http.Handler, error) {
return &binaryInstantiateHandler{
r: r,
responder: responder,
ctx: ctx,
name: name,
options: options.(*buildapi.BinaryBuildRequestOptions),
}, nil
}
// NewConnectOptions prepares a binary build request.
func (r *BinaryInstantiateREST) NewConnectOptions() (runtime.Object, bool, string) {
return &buildapi.BinaryBuildRequestOptions{}, false, ""
}
// ConnectMethods returns POST, the only supported binary method.
func (r *BinaryInstantiateREST) ConnectMethods() []string {
return []string{"POST"}
}
// binaryInstantiateHandler responds to upload requests
type binaryInstantiateHandler struct {
r *BinaryInstantiateREST
responder rest.Responder
ctx apirequest.Context
name string
options *buildapi.BinaryBuildRequestOptions
}
var _ http.Handler = &binaryInstantiateHandler{}
func (h *binaryInstantiateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
build, err := h.handle(r.Body)
if err != nil {
h.responder.Error(err)
return
}
h.responder.Object(http.StatusCreated, build)
}
func (h *binaryInstantiateHandler) handle(r io.Reader) (runtime.Object, error) {
h.options.Name = h.name
if err := rest.BeforeCreate(BinaryStrategy, h.ctx, h.options); err != nil {
glog.Infof("failed to validate binary: %#v", h.options)
return nil, err
}
request := &buildapi.BuildRequest{}
request.Name = h.name
if len(h.options.Commit) > 0 {
request.Revision = &buildapi.SourceRevision{
Git: &buildapi.GitSourceRevision{
Committer: buildapi.SourceControlUser{
Name: h.options.CommitterName,
Email: h.options.CommitterEmail,
},
Author: buildapi.SourceControlUser{
Name: h.options.AuthorName,
Email: h.options.AuthorEmail,
},
Message: h.options.Message,
Commit: h.options.Commit,
},
}
}
request.Binary = &buildapi.BinaryBuildSource{
AsFile: h.options.AsFile,
}
var build *buildapi.Build
start := time.Now()
if err := wait.Poll(time.Second, h.r.Timeout, func() (bool, error) {
result, err := h.r.Generator.Instantiate(h.ctx, request)
if err != nil {
if errors.IsNotFound(err) {
if s, ok := err.(errors.APIStatus); ok {
if s.Status().Kind == "imagestreamtags" {
return false, nil
}
}
}
glog.V(2).Infof("failed to instantiate: %#v", request)
return false, err
}
build = result
return true, nil
}); err != nil {
return nil, err
}
remaining := h.r.Timeout - time.Now().Sub(start)
// Attempt to cancel the build if it did not start running
// before we gave up.
cancel := true
defer func() {
if !cancel {
return
}
h.cancelBuild(build)
}()
latest, ok, err := registry.WaitForRunningBuild(h.r.Watcher, h.ctx, build, remaining)
switch {
case latest.Status.Phase == buildapi.BuildPhaseError:
// don't cancel the build if it reached a terminal state on its own
cancel = false
return nil, errors.NewBadRequest(fmt.Sprintf("build %s encountered an error: %s", build.Name, buildutil.NoBuildLogsMessage))
case latest.Status.Phase == buildapi.BuildPhaseFailed:
// don't cancel the build if it reached a terminal state on its own
cancel = false
return nil, errors.NewBadRequest(fmt.Sprintf("build %s failed: %s: %s", build.Name, build.Status.Reason, build.Status.Message))
case latest.Status.Phase == buildapi.BuildPhaseCancelled:
// don't cancel the build if it reached a terminal state on its own
cancel = false
return nil, errors.NewBadRequest(fmt.Sprintf("build %s was cancelled: %s", build.Name, buildutil.NoBuildLogsMessage))
case latest.Status.Phase != buildapi.BuildPhaseRunning:
return nil, errors.NewBadRequest(fmt.Sprintf("cannot upload file to build %s with status %s", build.Name, latest.Status.Phase))
case err == registry.ErrBuildDeleted:
return nil, errors.NewBadRequest(fmt.Sprintf("build %s was deleted before it started: %s", build.Name, buildutil.NoBuildLogsMessage))
case err != nil:
return nil, errors.NewBadRequest(fmt.Sprintf("unable to wait for build %s to run: %v", build.Name, err))
case !ok:
return nil, errors.NewTimeoutError(fmt.Sprintf("timed out waiting for build %s to start after %s", build.Name, h.r.Timeout), 0)
}
// The container should be the default build container, so setting it to blank
buildPodName := buildapi.GetBuildPodName(build)
opts := &kapi.PodAttachOptions{
Stdin: true,
// TODO remove Stdout and Stderr once https://github.com/kubernetes/kubernetes/issues/44448 is
// fixed
Stdout: true,
Stderr: true,
}
location, transport, err := pod.AttachLocation(h.r.PodGetter, h.r.ConnectionInfo, h.ctx, buildPodName, opts)
if err != nil {
if errors.IsNotFound(err) {
return nil, errors.NewNotFound(kapi.Resource("pod"), buildPodName)
}
return nil, errors.NewBadRequest(err.Error())
}
tlsClientConfig, err := knet.TLSClientConfig(transport)
if err != nil {
return nil, errors.NewInternalError(fmt.Errorf("unable to connect to node, could not retrieve TLS client config: %v", err))
}
upgrader := spdy.NewRoundTripper(tlsClientConfig)
exec, err := remotecommand.NewStreamExecutor(upgrader, nil, "POST", location)
if err != nil {
return nil, errors.NewInternalError(fmt.Errorf("unable to connect to server: %v", err))
}
streamOptions := remotecommand.StreamOptions{
SupportedProtocols: kubeletremotecommand.SupportedStreamingProtocols,
Stdin: r,
// TODO remove Stdout and Stderr once https://github.com/kubernetes/kubernetes/issues/44448 is
// fixed
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
if err := exec.Stream(streamOptions); err != nil {
return nil, errors.NewInternalError(err)
}
cancel = false
return latest, nil
}
// cancelBuild will mark a build for cancellation unless
// cancel is false in which case it is a no-op.
func (h *binaryInstantiateHandler) cancelBuild(build *buildapi.Build) {
build.Status.Cancelled = true
h.r.Generator.Client.UpdateBuild(h.ctx, build)
wait.Poll(cancelPollInterval, cancelPollDuration, func() (bool, error) {
build.Status.Cancelled = true
err := h.r.Generator.Client.UpdateBuild(h.ctx, build)
switch {
case err != nil && errors.IsConflict(err):
build, err = h.r.Generator.Client.GetBuild(h.ctx, build.Name, &metav1.GetOptions{})
return false, err
default:
return true, err
}
})
}
type podGetter struct {
podsNamespacer kcoreclient.PodsGetter
}
func (g *podGetter) Get(ctx apirequest.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
ns, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, errors.NewBadRequest("namespace parameter required.")
}
return g.podsNamespacer.Pods(ns).Get(name, *options)
}