-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the common etcd client in etos-iut #83
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,9 @@ import ( | |
|
||
eiffelevents "github.com/eiffel-community/eiffelevents-sdk-go" | ||
config "github.com/eiffel-community/etos-api/internal/configs/iut" | ||
"github.com/eiffel-community/etos-api/internal/database" | ||
"github.com/eiffel-community/etos-api/pkg/application" | ||
packageurl "github.com/package-url/packageurl-go" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
|
||
"github.com/google/uuid" | ||
"github.com/julienschmidt/httprouter" | ||
|
@@ -38,14 +38,14 @@ import ( | |
type V1Alpha1Application struct { | ||
logger *logrus.Entry | ||
cfg config.Config | ||
database *clientv3.Client | ||
database database.Opener | ||
wg *sync.WaitGroup | ||
} | ||
|
||
type V1Alpha1Handler struct { | ||
logger *logrus.Entry | ||
cfg config.Config | ||
database *clientv3.Client | ||
database database.Opener | ||
wg *sync.WaitGroup | ||
} | ||
|
||
|
@@ -72,11 +72,11 @@ func (a *V1Alpha1Application) Close() { | |
} | ||
|
||
// New returns a new V1Alpha1Application object/struct | ||
func New(cfg config.Config, log *logrus.Entry, ctx context.Context, cli *clientv3.Client) application.Application { | ||
func New(cfg config.Config, log *logrus.Entry, ctx context.Context, db database.Opener) application.Application { | ||
return &V1Alpha1Application{ | ||
logger: log, | ||
cfg: cfg, | ||
database: cli, | ||
database: db, | ||
wg: &sync.WaitGroup{}, | ||
} | ||
} | ||
|
@@ -127,19 +127,27 @@ type StopRequest struct { | |
|
||
// Start creates a number of IUTs and stores them in the ETCD database returning a checkout ID. | ||
func (h V1Alpha1Handler) Start(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
identifier, err := uuid.Parse(r.Header.Get("X-Etos-Id")) | ||
logger := h.logger.WithField("identifier", identifier).WithContext(r.Context()) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
} | ||
checkOutID := uuid.New() | ||
|
||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
var startReq StartRequest | ||
if err := json.NewDecoder(r.Body).Decode(&startReq); err != nil { | ||
logger.Errorf("Failed to decode request body: %s", r.Body) | ||
RespondWithError(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
defer r.Body.Close() | ||
purl, err := packageurl.FromString(startReq.ArtifactIdentity) | ||
|
||
if err != nil { | ||
logger.Errorf("Failed to create a purl struct from artifact identity: %s", startReq.ArtifactIdentity) | ||
RespondWithError(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
@@ -150,11 +158,14 @@ func (h V1Alpha1Handler) Start(w http.ResponseWriter, r *http.Request, ps httpro | |
} | ||
iuts, err := json.Marshal(purls) | ||
if err != nil { | ||
logger.Errorf("Failed to marshal purls: %s", purls) | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
_, err = h.database.Put(r.Context(), fmt.Sprintf("/iut/%s", checkOutID.String()), string(iuts)) | ||
client := h.database.Open(r.Context(), identifier) | ||
_, err = client.Write([]byte(string(iuts))) | ||
if err != nil { | ||
logger.Errorf("Failed to write to database: %s", string(iuts)) | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
@@ -166,43 +177,50 @@ func (h V1Alpha1Handler) Start(w http.ResponseWriter, r *http.Request, ps httpro | |
|
||
// Status creates a simple DONE Status response with IUTs. | ||
func (h V1Alpha1Handler) Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
identifier := r.Header.Get("X-Etos-Id") | ||
identifier, err := uuid.Parse(r.Header.Get("X-Etos-Id")) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
} | ||
logger := h.logger.WithField("identifier", identifier).WithContext(r.Context()) | ||
|
||
id, err := uuid.Parse(r.URL.Query().Get("id")) | ||
client := h.database.Open(r.Context(), identifier) | ||
|
||
data := make([]byte, 4096) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is 4096 always goign to be enough here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally it is around 130-150 bytes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make([]byte) should be fine here since we cannot know the size There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I would recommend using something I.e. |
||
byteCount, err := client.Read(data) | ||
data = data[:byteCount] | ||
|
||
key := fmt.Sprintf("/iut/%s", id) | ||
dbResp, err := h.database.Get(r.Context(), key) | ||
if err != nil { | ||
logger.Errorf("Failed to look up status request id: %s", id) | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
if len(dbResp.Kvs) == 0 { | ||
err = fmt.Errorf("No key found: %s", key) | ||
logger.Errorf("Failed to look up status request id: %s, %s", identifier, err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we really doing a lookup here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In line 190 we do a look up by reading from the database. |
||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
statusResp := StatusResponse{ | ||
Id: id, | ||
Status: "DONE", | ||
} | ||
if err = json.Unmarshal(dbResp.Kvs[0].Value, &statusResp.Iuts); err != nil { | ||
if err = json.Unmarshal(data, &statusResp.Iuts); err != nil { | ||
logger.Errorf("Failed to unmarshal data: %s", data) | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
response, err := json.Marshal(statusResp) | ||
if err != nil { | ||
logger.Errorf("Failed to marshal status response: %s", statusResp) | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(response) | ||
|
||
} | ||
|
||
// Stop deletes the given IUTs from the database and returns an empty response. | ||
func (h V1Alpha1Handler) Stop(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
identifier := r.Header.Get("X-Etos-Id") | ||
identifier, err := uuid.Parse(r.Header.Get("X-Etos-Id")) | ||
if err != nil { | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
} | ||
logger := h.logger.WithField("identifier", identifier).WithContext(r.Context()) | ||
|
||
var stopReq StopRequest | ||
|
@@ -212,7 +230,8 @@ func (h V1Alpha1Handler) Stop(w http.ResponseWriter, r *http.Request, ps httprou | |
RespondWithError(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
_, err := h.database.Delete(r.Context(), fmt.Sprintf("/iut/%s", stopReq.Id)) | ||
client := h.database.Open(r.Context(), identifier) | ||
_, err = client.Write(nil) | ||
if err != nil { | ||
logger.Errorf("Etcd delete failed: %s", err.Error()) | ||
RespondWithError(w, http.StatusInternalServerError, err.Error()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this added? and why in the write function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This client shall implement the io.ReadWriter interface. The ETOS API applications are designed in a way that a database client shall be easily swapped with any other client which implements this common interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the question is why are we adding delete in the write function. A question I also have
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all potential database clients are supposed to conform to io.ReadWriter interface and be swappable, we have no other choice.
The io.ReadWriter interface seems to be more-suitable for a storage like a binary file, but here it requires workarounds to fit it into a key-value pair storage like etcd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather we write a null value into the database rather than having this function do things that it shouldn't do.
If deletion is required then we should have a separate interface for that and in the Stop function cast the database to that interface to see if it is possible to delete.