-
Notifications
You must be signed in to change notification settings - Fork 17
/
api_buildimage.go
172 lines (143 loc) · 5.67 KB
/
api_buildimage.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
/*
Copyright © 2022 - 2023 SUSE LLC
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 server
import (
"encoding/json"
"fmt"
"io"
"net/http"
"path"
"time"
"github.com/sirupsen/logrus"
)
const (
jobStatusStarted = "Started"
// jobStatusCompleted = "Completed"
jobStatusFailed = "Failed"
jobStatusNotStarted = "Not Started"
)
type buildImageJobStatus struct {
Status string `json:"status"`
URL string `json:"url"`
}
type buildImageJob struct {
Token string `json:"token"`
URL string `json:"url"`
}
func (i *InventoryServer) apiBuildImage(resp http.ResponseWriter, req *http.Request) error {
if req.Method == http.MethodPost {
return i.apiBuildImagePostStart(resp, req)
} else if req.Method == http.MethodGet {
return i.apiBuildImageGetStatus(resp, req)
}
err := fmt.Errorf("method '%s' is not supported", req.Method)
http.Error(resp, err.Error(), http.StatusMethodNotAllowed)
return err
}
func (i *InventoryServer) apiBuildImageGetStatus(resp http.ResponseWriter, req *http.Request) error {
escapedToken := sanitizeUserInput(path.Base(req.URL.Path))
logrus.Debugf("Get build-image job status for %s", escapedToken)
jobStatus := jobStatusNotStarted
jobDownloadURL := ""
if reg, err := i.registrationCache.getRegistrationData(escapedToken); err != nil {
if _, err := i.getMachineRegistration(escapedToken); err != nil {
logrus.Warnf("Requested build-image status for unexistent MachineRegistration token: %s", escapedToken)
http.Error(resp, err.Error(), http.StatusBadRequest)
return err
}
logrus.Debug("build-image job was ever started")
} else {
jobStatus = reg.buildImageStatus
jobDownloadURL = reg.downloadURL
}
data := buildImageJobStatus{Status: jobStatus, URL: jobDownloadURL}
if err := json.NewEncoder(resp).Encode(data); err != nil {
errMsg := fmt.Errorf("cannot marshal build-image data: %w", err)
http.Error(resp, errMsg.Error(), http.StatusInternalServerError)
return errMsg
}
return nil
}
func sanitizeBuildImageJob(job buildImageJob) buildImageJob {
job.Token = sanitizeUserInput(job.Token)
job.URL = sanitizeUserInput(job.URL)
return job
}
func (i *InventoryServer) apiBuildImagePostStart(resp http.ResponseWriter, req *http.Request) error {
body, err := io.ReadAll(req.Body)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return err
}
var job buildImageJob
if err := json.Unmarshal(body, &job); err != nil {
errMsg := fmt.Errorf("cannot unmarshal build-image POST data: %w", err)
http.Error(resp, errMsg.Error(), http.StatusBadRequest)
return errMsg
}
sanitizedJob := sanitizeBuildImageJob(job)
if _, err := i.getMachineRegistration(sanitizedJob.Token); err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)
return err
}
if reg, err := i.registrationCache.getRegistrationData(sanitizedJob.Token); err != nil {
if reg.buildImageStatus == jobStatusStarted {
if reg.buildImageURL == sanitizedJob.URL {
logrus.Debugf("Same job already started, nothing to do")
if err := json.NewEncoder(resp).Encode(sanitizedJob); err != nil {
errMsg := fmt.Errorf("cannot marshal build-image data: %w", err)
http.Error(resp, errMsg.Error(), http.StatusInternalServerError)
return errMsg
}
return nil
}
errMsg := fmt.Errorf("a build image task is already running for the MachineRegistration")
http.Error(resp, errMsg.Error(), http.StatusBadRequest)
return errMsg
}
}
i.registrationCache.setRegistrationData(sanitizedJob.Token, registrationData{
buildImageURL: sanitizedJob.URL,
buildImageStatus: jobStatusStarted,
downloadURL: "",
})
logrus.Infof("New build-image job queued: seed image:'%s', reg token:'%s'", sanitizedJob.URL, sanitizedJob.Token)
// start the actual build job here
go i.draftDoBuildImage(sanitizedJob)
if err := json.NewEncoder(resp).Encode(sanitizedJob); err != nil {
errMsg := fmt.Errorf("cannot marshal build-image data: %w", err)
http.Error(resp, errMsg.Error(), http.StatusInternalServerError)
return errMsg
}
return nil
}
func (i *InventoryServer) draftDoBuildImage(job buildImageJob) {
// This should spawn a pod that will:
// - download the base image (job.Url)
// - retrieve the registration YAML from the registrationToken (job.Token)
// - generate the seed image (base image + registration yaml)
// - return the seed image (url? shared volume?)
// Here we should just update the status then.
// For now, it will always fail.
logrus.Infof("build-image for token %s failed (build job not supported yet).", job.Token)
// let's add a small waiting time before reporting the job failure. We need this till we don't implement
// the actual build job to let the tests check that the build job is marked as started (otherwise this empty
// job may update the status to Failed before the test is able to check for the Started state).
time.Sleep(5 * time.Millisecond)
if err := i.registrationCache.setDownloadURL(job.Token, ""); err != nil {
logrus.Errorf("Cannot update build-image download URL for job with token %s: %s", job.Token, err.Error())
}
if err := i.registrationCache.setBuildImageStatus(job.Token, jobStatusFailed); err != nil {
logrus.Errorf("Cannot update build-image status for job with token %s: %s", job.Token, err.Error())
}
}