Skip to content

Commit

Permalink
get preflight result endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell committed Jun 17, 2024
1 parent 289b309 commit ae26b69
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
3 changes: 3 additions & 0 deletions pkg/upgradeservice/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func RegisterRoutes(r *mux.Router, handler UpgradeServiceHandler) {
subRouter.Path("/liveconfig").Methods("POST").HandlerFunc(handler.LiveAppConfig)
subRouter.Path("/config").Methods("PUT").HandlerFunc(handler.SaveAppConfig)
subRouter.Path("/config/{filename}/download").Methods("GET").HandlerFunc(handler.DownloadFileFromConfig)

// TODO: subRouter.Path("/preflight/run").Methods("POST").HandlerFunc(handler.StartPreflightChecks)
subRouter.Path("/preflight/result").Methods("GET").HandlerFunc(handler.GetPreflightResult)
}

func JSON(w http.ResponseWriter, code int, payload interface{}) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/upgradeservice/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ type UpgradeServiceHandler interface {
LiveAppConfig(w http.ResponseWriter, r *http.Request)
SaveAppConfig(w http.ResponseWriter, r *http.Request)
DownloadFileFromConfig(w http.ResponseWriter, r *http.Request)

GetPreflightResult(w http.ResponseWriter, r *http.Request)
}
40 changes: 40 additions & 0 deletions pkg/upgradeservice/handlers/preflight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handlers

import (
"net/http"

"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
preflighttypes "github.com/replicatedhq/kots/pkg/preflight/types"
upgradepreflight "github.com/replicatedhq/kots/pkg/upgradeservice/preflight"
)

type GetPreflightResultResponse struct {
PreflightProgress string `json:"preflightProgress,omitempty"`
PreflightResult preflighttypes.PreflightResult `json:"preflightResult"`
}

func (h *Handler) GetPreflightResult(w http.ResponseWriter, r *http.Request) {
params := GetContextParams(r)
appSlug := mux.Vars(r)["appSlug"]

if params.AppSlug != appSlug {
logger.Error(errors.Errorf("app slug in path %s does not match app slug in context %s", appSlug, params.AppSlug))
w.WriteHeader(http.StatusForbidden)
return
}

preflightData, err := upgradepreflight.GetPreflightData()
if err != nil {
logger.Error(errors.Wrap(err, "failed to get preflight data"))
w.WriteHeader(http.StatusInternalServerError)
return
}

response := GetPreflightResultResponse{
PreflightResult: *preflightData.Result,
PreflightProgress: preflightData.Progress,
}
JSON(w, 200, response)
}
53 changes: 43 additions & 10 deletions pkg/upgradeservice/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
Expand All @@ -24,8 +25,8 @@ import (
)

type PreflightData struct {
Progress map[string]interface{} `json:"progress"`
Results *types.PreflightResults `json:"results"`
Progress string `json:"progress,omitempty"`
Result *types.PreflightResult `json:"result"`
}

var PreflightDataFilepath string
Expand Down Expand Up @@ -107,7 +108,7 @@ func Run(app *apptypes.App, archiveDir string, sequence int64, registrySettings
},
},
}
if err := setPreflightResults(preflightResults); err != nil {
if err := setPreflightResults(app.Slug, preflightResults); err != nil {
logger.Error(errors.Wrap(err, "failed to set preflight results"))
return
}
Expand All @@ -126,7 +127,11 @@ func Run(app *apptypes.App, archiveDir string, sequence int64, registrySettings
zap.String("appID", app.ID),
zap.Int64("sequence", sequence))

_, err := preflightpkg.Execute(preflight, ignoreRBAC, setPreflightProgress, setPreflightResults)
setResults := func(results *types.PreflightResults) error {
return setPreflightResults(app.Slug, results)
}

_, err := preflightpkg.Execute(preflight, ignoreRBAC, setPreflightProgress, setResults)
if err != nil {
logger.Error(errors.Wrap(err, "failed to run preflight checks"))
return
Expand All @@ -142,31 +147,59 @@ func Run(app *apptypes.App, archiveDir string, sequence int64, registrySettings
return nil
}

func setPreflightResults(results *types.PreflightResults) error {
preflightData, err := getPreflightData()
func setPreflightResults(appSlug string, results *types.PreflightResults) error {
preflightData, err := GetPreflightData()
if err != nil {
return errors.Wrap(err, "failed to get preflight data")
}
preflightData.Results = results
resultsBytes, err := json.Marshal(results)
if err != nil {
return errors.Wrap(err, "failed to marshal preflight results")
}
createdAt := time.Now()
preflightData.Result = &types.PreflightResult{
Result: string(resultsBytes),
CreatedAt: &createdAt,
AppSlug: appSlug,
ClusterSlug: "this-cluster",
Skipped: false,
HasFailingStrictPreflights: hasFailingStrictPreflights(results),
}
if err := setPreflightData(preflightData); err != nil {
return errors.Wrap(err, "failed to set preflight results")
}
return nil
}

func hasFailingStrictPreflights(results *types.PreflightResults) bool {
// convert to troubleshoot type so we can use the existing function
uploadResults := &troubleshootpreflight.UploadPreflightResults{}
uploadResults.Results = results.Results
for _, e := range results.Errors {
uploadResults.Errors = append(uploadResults.Errors, &troubleshootpreflight.UploadPreflightError{
Error: e.Error,
})
}
return troubleshootpreflight.HasStrictAnalyzersFailed(uploadResults)
}

func setPreflightProgress(progress map[string]interface{}) error {
preflightData, err := getPreflightData()
preflightData, err := GetPreflightData()
if err != nil {
return errors.Wrap(err, "failed to get preflight data")
}
preflightData.Progress = progress
progressBytes, err := json.Marshal(progress)
if err != nil {
return errors.Wrap(err, "failed to marshal preflight progress")
}
preflightData.Progress = string(progressBytes)
if err := setPreflightData(preflightData); err != nil {
return errors.Wrap(err, "failed to set preflight progress")
}
return nil
}

func getPreflightData() (*PreflightData, error) {
func GetPreflightData() (*PreflightData, error) {
var preflightData *PreflightData
if _, err := os.Stat(PreflightDataFilepath); err != nil {
if !os.IsNotExist(err) {
Expand Down

0 comments on commit ae26b69

Please sign in to comment.