-
Notifications
You must be signed in to change notification settings - Fork 14
/
handlers.go
212 lines (176 loc) · 6.06 KB
/
handlers.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
/*
* Copyright (C) 2022 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package apiserver
import (
"bufio"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
terraformv1alpha1 "github.com/appvia/terranetes-controller/pkg/apis/terraform/v1alpha1"
"github.com/appvia/terranetes-controller/pkg/utils"
"github.com/appvia/terranetes-controller/pkg/utils/filters"
"github.com/appvia/terranetes-controller/pkg/utils/kubernetes"
)
var sanitizeRegEx = regexp.MustCompile(`^[a-zA-Z0-9\-\.\:]{1,64}$`)
// validateInput checks the inputs parameter is valid
func validateInput(name, value string) error {
switch {
case value == "":
return fmt.Errorf("%s is empty", name)
case !sanitizeRegEx.MatchString(value):
return fmt.Errorf("%s does not match input regex: %s", name, sanitizeRegEx)
}
return nil
}
// handleHealth is http handler for the health endpoint
func (s *Server) handleHealth(w http.ResponseWriter, req *http.Request) {
_, _ = w.Write([]byte("OK\n"))
}
// handleBuilds is http handler for the logs endpoint
//
//nolint:errcheck
func (s *Server) handleBuilds(w http.ResponseWriter, req *http.Request) {
params := []string{
"generation",
"name",
"namespace",
"stage",
"uid",
}
values := make(map[string]string)
for _, key := range params {
value := req.URL.Query().Get(key)
if err := validateInput(key, value); err != nil {
log.WithError(err).Error("received an invalid request")
w.WriteHeader(http.StatusBadRequest)
return
}
values[key] = value
}
fields := log.Fields{
"generation": values["generation"],
"name": values["name"],
"namespace": values["namespace"],
"stage": values["stage"],
"uid": values["uid"],
}
log.WithFields(fields).Debug("received request for builds")
labels := []string{
terraformv1alpha1.ConfigurationGenerationLabel + "=" + values["generation"],
terraformv1alpha1.ConfigurationNameLabel + "=" + values["name"],
terraformv1alpha1.ConfigurationNamespaceLabel + "=" + values["namespace"],
terraformv1alpha1.ConfigurationStageLabel + "=" + values["stage"],
terraformv1alpha1.ConfigurationUIDLabel + "=" + values["uid"],
}
var pod *v1.Pod
// @step: try and find the pod running the terraform job: We have to assume also
// the pods hasn't been scheduled yet
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("[info] waiting for the job to be scheduled\n"))
w.Write([]byte(fmt.Sprintf("[info] watching build: %s, generation: %s for the job to be scheduled\n", values["name"], values["generation"])))
// @step: we query the jobs using the labels and find the latest job for the configuration at stage x, generation y. We then
// find the associated pods and stream the logs back to the caller
err := utils.RetryWithTimeout(req.Context(), 3*time.Minute, 2*time.Second, func() (bool, error) {
w.Write([]byte("."))
// @step: find the matching job
list, err := s.Client.BatchV1().Jobs(s.Namespace).List(req.Context(), metav1.ListOptions{
LabelSelector: strings.Join(labels, ","),
})
if err != nil {
log.WithFields(fields).WithError(err).Error("failed to list the jobs")
return false, nil
}
if len(list.Items) == 0 {
log.WithFields(fields).Warn("no jobs found")
return false, nil
}
latest, found := filters.Jobs(list).
WithGeneration(values["generation"]).
WithName(values["name"]).
WithNamespace(values["namespace"]).
WithStage(values["stage"]).
WithUID(values["uid"]).
Latest()
if !found || latest == nil {
log.WithFields(fields).Debug("no matching job found")
return false, nil
}
log.WithFields(fields).Warn("found zero matching jobs for the build")
// @step: find the latest pod associated to the job
pods, err := s.Client.CoreV1().Pods(s.Namespace).List(req.Context(), metav1.ListOptions{
LabelSelector: "job-name=" + latest.Name,
})
if err != nil {
log.WithFields(fields).WithError(err).Error("failed to list the pods")
return false, nil
}
if len(pods.Items) == 0 {
log.WithFields(fields).Warn("no pods associated to the job")
return false, nil
}
pod = kubernetes.FindLatestPod(pods)
switch pod.Status.Phase {
case v1.PodRunning, v1.PodSucceeded, v1.PodFailed:
return true, nil
}
return false, nil
})
if err != nil {
log.WithFields(fields).WithError(err).Error("failed to find the pod")
w.Write([]byte("[error] failed to find associated pod in time\n"))
return
}
log.WithFields(fields).WithField("pod", pod.Name).Debug("found the pod")
err = func() error {
for _, container := range append(pod.Spec.InitContainers, pod.Spec.Containers...) {
stream, err := s.Client.CoreV1().Pods(s.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{
Container: container.Name,
Follow: true,
}).Stream(req.Context())
if err != nil {
return err
}
// @step: we either copy or flush line by line the output
if flush, ok := w.(http.Flusher); !ok {
if _, err := io.Copy(w, stream); err != nil {
return err
}
} else {
scanner := bufio.NewScanner(stream)
for scanner.Scan() {
w.Write([]byte(fmt.Sprintf("%s\n", scanner.Text())))
flush.Flush()
}
}
stream.Close()
}
return nil
}()
if err != nil {
log.WithFields(fields).WithError(err).Error("failed to stream the logs")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("[error] failed to retrieve the logs\n"))
return
}
w.Write([]byte("[build] completed\n"))
}