forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
169 lines (149 loc) · 6.5 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
package deploylog
import (
"fmt"
"time"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/rest"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
genericrest "k8s.io/kubernetes/pkg/registry/generic/rest"
"k8s.io/kubernetes/pkg/registry/pod"
"k8s.io/kubernetes/pkg/runtime"
"github.com/openshift/origin/pkg/client"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/deploy/api/validation"
"github.com/openshift/origin/pkg/deploy/registry"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
// defaultTimeout is the default time to wait for the logs of a deployment
const defaultTimeout time.Duration = 10 * time.Second
// REST is an implementation of RESTStorage for the api server.
type REST struct {
ConfigGetter client.DeploymentConfigsNamespacer
DeploymentGetter kclient.ReplicationControllersNamespacer
PodGetter pod.ResourceGetter
ConnectionInfo kclient.ConnectionInfoGetter
Timeout time.Duration
}
// REST implements GetterWithOptions
var _ = rest.GetterWithOptions(&REST{})
// NewREST creates a new REST for DeploymentLogs. It uses three clients: one for configs,
// one for deployments (replication controllers) and one for pods to get the necessary
// attributes to assemble the URL to which the request shall be redirected in order to
// get the deployment logs.
func NewREST(dn client.DeploymentConfigsNamespacer, rn kclient.ReplicationControllersNamespacer, pn kclient.PodsNamespacer, connectionInfo kclient.ConnectionInfoGetter) *REST {
return &REST{
ConfigGetter: dn,
DeploymentGetter: rn,
PodGetter: &podGetter{pn},
ConnectionInfo: connectionInfo,
Timeout: defaultTimeout,
}
}
// NewGetOptions returns a new options object for deployment logs
func (r *REST) NewGetOptions() (runtime.Object, bool, string) {
return &deployapi.DeploymentLogOptions{}, false, ""
}
// New creates an empty DeploymentLog resource
func (r *REST) New() runtime.Object {
return &deployapi.DeploymentLog{}
}
// Get returns a streamer resource with the contents of the deployment log
func (r *REST) Get(ctx kapi.Context, name string, opts runtime.Object) (runtime.Object, error) {
// Ensure we have a namespace in the context
namespace, ok := kapi.NamespaceFrom(ctx)
if !ok {
return nil, errors.NewBadRequest("namespace parameter required.")
}
// Validate DeploymentLogOptions
deployLogOpts, ok := opts.(*deployapi.DeploymentLogOptions)
if !ok {
return nil, errors.NewBadRequest("did not get an expected options.")
}
if errs := validation.ValidateDeploymentLogOptions(deployLogOpts); len(errs) > 0 {
return nil, errors.NewInvalid("deploymentLogOptions", "", errs)
}
// Fetch deploymentConfig and check latest version; if 0, there are no deployments
// for this config
config, err := r.ConfigGetter.DeploymentConfigs(namespace).Get(name)
if err != nil {
return nil, errors.NewNotFound("deploymentConfig", name)
}
desiredVersion := config.LatestVersion
if desiredVersion == 0 {
return nil, errors.NewBadRequest(fmt.Sprintf("no deployment exists for deploymentConfig %q", config.Name))
}
// Support retrieving logs for older deployments
switch {
case deployLogOpts.Version == nil:
// Latest
case *deployLogOpts.Version <= 0 || *deployLogOpts.Version > config.LatestVersion:
// Invalid version
return nil, errors.NewBadRequest(fmt.Sprintf("invalid version for deploymentConfig %q: %d", config.Name, *deployLogOpts.Version))
default:
desiredVersion = *deployLogOpts.Version
}
// Get desired deployment
targetName := deployutil.DeploymentNameForConfigVersion(config.Name, desiredVersion)
target, err := r.DeploymentGetter.ReplicationControllers(namespace).Get(targetName)
if err != nil {
return nil, err
}
// Check for deployment status; if it is new or pending, we will wait for it. If it is complete,
// the deployment completed successfully and the deployer pod will be deleted so we will return a
// success message. If it is running or failed, retrieve the log from the deployer pod.
status := deployutil.DeploymentStatusFor(target)
switch status {
case deployapi.DeploymentStatusNew, deployapi.DeploymentStatusPending:
if deployLogOpts.NoWait {
glog.V(4).Infof("Deployment %s is in %s state. No logs to retrieve yet.", deployutil.LabelForDeployment(target), status)
return &genericrest.LocationStreamer{}, nil
}
glog.V(4).Infof("Deployment %s is in %s state, waiting for it to start...", deployutil.LabelForDeployment(target), status)
latest, ok, err := registry.WaitForRunningDeployment(r.DeploymentGetter, target, r.Timeout)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("unable to wait for deployment %s to run: %v", deployutil.LabelForDeployment(target), err))
}
if !ok {
return nil, errors.NewTimeoutError(fmt.Sprintf("timed out waiting for deployment %s to start after %s", deployutil.LabelForDeployment(target), r.Timeout), 1)
}
if deployutil.DeploymentStatusFor(latest) == deployapi.DeploymentStatusComplete {
// Deployer pod has been deleted, no logs to retrieve
glog.V(4).Infof("Deployment %s was successful so the deployer pod is deleted. No logs to retrieve.", deployutil.LabelForDeployment(target))
return &genericrest.LocationStreamer{}, nil
}
case deployapi.DeploymentStatusComplete:
// Deployer pod has been deleted, no logs to retrieve
glog.V(4).Infof("Deployment %s was successful so the deployer pod is deleted. No logs to retrieve.", deployutil.LabelForDeployment(target))
return &genericrest.LocationStreamer{}, nil
}
// Setup url of the deployer pod
deployPodName := deployutil.DeployerPodNameForDeployment(target.Name)
logOpts := &kapi.PodLogOptions{
Follow: deployLogOpts.Follow,
}
location, transport, err := pod.LogLocation(r.PodGetter, r.ConnectionInfo, ctx, deployPodName, logOpts)
if err != nil {
return nil, errors.NewBadRequest(err.Error())
}
return &genericrest.LocationStreamer{
Location: location,
Transport: transport,
ContentType: "text/plain",
Flush: deployLogOpts.Follow,
}, nil
}
// podGetter implements the ResourceGetter interface. Used by LogLocation to
// retrieve the deployer pod
type podGetter struct {
podsNamespacer kclient.PodsNamespacer
}
// Get is responsible for retrieving the deployer pod
func (g *podGetter) Get(ctx kapi.Context, name string) (runtime.Object, error) {
namespace, ok := kapi.NamespaceFrom(ctx)
if !ok {
return nil, errors.NewBadRequest("namespace parameter required.")
}
return g.podsNamespacer.Pods(namespace).Get(name)
}