Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/api/server/handlers_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func (h *handlers) bindContainer(ctx *fiber.Ctx) error {
}

if err == nil {
klog.Errorf("devcontainer %s already exists", devContainer.Name)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("devcontainer %s already exists", devContainer.Name),
Expand Down Expand Up @@ -442,6 +443,7 @@ func (h *handlers) listAppContainersInChart(ctx *fiber.Ctx) error {

manifest, err := helm.DryRun(ctx.Context(), h.kubeConfig, testNamespace, appName, getAppPath(username, app), values)
if err != nil {
klog.Errorf("failed to dry run %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Dry run failed: %v", err),
Expand All @@ -450,6 +452,7 @@ func (h *handlers) listAppContainersInChart(ctx *fiber.Ctx) error {

resources, err := helm.DecodeManifest(manifest)
if err != nil {
klog.Errorf("failed to decode manifest %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Decode manifest failed: %v", err),
Expand Down Expand Up @@ -768,6 +771,7 @@ func (h *handlers) updateDevContainer(ctx *fiber.Ctx) error {
app := make(map[string]string)
err := ctx.BodyParser(&app)
if err != nil {
klog.Errorf("failed to parse body %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Parse body failed: %v", err),
Expand All @@ -784,6 +788,7 @@ func (h *handlers) updateDevContainer(ctx *fiber.Ctx) error {

err = h.db.DB.Model(&model.DevContainers{}).Where("name = ?", name).Update("name", newName).Error
if err != nil {
klog.Errorf("failed to update dev container name=%s, err=%v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Update dev conainter failed: %v", err),
Expand Down Expand Up @@ -873,16 +878,19 @@ func GetAppContainersInChart(owner, app string) ([]*helm.ContainerInfo, error) {
values["domain"] = entries
kubeConfig, err := ctrl.GetConfig()
if err != nil {
klog.Errorf("failed to get kube config %v", err)
return nil, err
}

manifest, err := helm.DryRun(context.TODO(), kubeConfig, testNamespace, appName, getAppPath(owner, app), values)
if err != nil {
klog.Errorf("failed to parse manifest %v", err)
return nil, err
}

resources, err := helm.DecodeManifest(manifest)
if err != nil {
klog.Errorf("failed to decode manifest %v", err)
return nil, err
}
op := db.NewDbOperator()
Expand Down
21 changes: 19 additions & 2 deletions pkg/api/server/handlers_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {

token := ctx.Locals("auth_token").(string)


name, ok := app["name"]
if !ok {
klog.Error("app name is empty, ", app)
Expand All @@ -257,6 +256,7 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {
}()
err = UpdateDevAppState(username, name, deploying)
if err != nil {
klog.Errorf("failed to update dev app state name=%s,err=%v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("update app state err %v", err),
Expand All @@ -269,7 +269,7 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {

err = command.Lint().WithDir(BaseDir).Run(context.TODO(), username, name)
if err != nil {

klog.Errorf("failed to lint app=%s, err=%v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": err.Error(),
Expand Down Expand Up @@ -299,6 +299,7 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {
klog.Info("preinstall, create a labeled namespace for webhook")
_, err = container.CreateOrUpdateDevNamespace(ctx.Context(), h.kubeConfig, username, devName)
if err != nil {
klog.Errorf("failed to check namespace %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Check namespace failed: %v", err),
Expand Down Expand Up @@ -329,6 +330,7 @@ func (h *handlers) installDevApp(ctx *fiber.Ctx) error {

err = UpdateDevAppState(username, name, deployed)
if err != nil {
klog.Errorf("failed to update app=%s state to deployed %v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("update app state to deployed err %v", err),
Expand All @@ -354,6 +356,7 @@ func (h *handlers) downloadDevAppChart(ctx *fiber.Ctx) error {

buf, err := command.PackageChart().WithDir(BaseDir).Run(app)
if err != nil {
klog.Errorf("failed to package app=%s chart %v", app, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Package chart Failed: %v", err),
Expand Down Expand Up @@ -515,6 +518,7 @@ func (h *handlers) deleteDevApp(ctx *fiber.Ctx) error {

err = command.DeleteChart().WithDir(BaseDir).Run(name)
if err != nil {
klog.Errorf("failed to delete chart %s, err=%v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Delete Chart Failed: %v", err),
Expand Down Expand Up @@ -610,6 +614,7 @@ func (h *handlers) lintDevAppChart(ctx *fiber.Ctx) error {

err := command.Lint().WithDir(BaseDir).Run(ctx.Context(), username, app)
if err != nil {
klog.Errorf("failed to lint app %s, err=%v", app, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Lint Failed: %v", err),
Expand Down Expand Up @@ -643,6 +648,7 @@ func (h *handlers) uninstall(ctx *fiber.Ctx) error {
devName := name + "-dev"
res, err := uninstall(devName, token)
if err != nil {
klog.Errorf("failed to uninstall %s, err=%v", devName, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Uninstall Failed: %v", err),
Expand Down Expand Up @@ -684,6 +690,7 @@ func (h *handlers) createAppByArchive(ctx *fiber.Ctx) error {
uniqueId := strings.ReplaceAll(uuid.NewString(), "-", "")
err = UnArchive(filepath.Join("/tmp", file.Filename), filepath.Join("/tmp", uniqueId))
if err != nil {
klog.Errorf("failed to unarchive file %s, err=%v", filepath.Join("/tmp", file.Filename), err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("UnArchive failed: %v", err),
Expand All @@ -692,6 +699,7 @@ func (h *handlers) createAppByArchive(ctx *fiber.Ctx) error {

cfg, err := readCfgFromFile(username, filepath.Join("/tmp", uniqueId))
if err != nil {
klog.Errorf("failed to read cfg from file %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Read cfg frome file failed: %v", err),
Expand All @@ -706,6 +714,7 @@ func (h *handlers) createAppByArchive(ctx *fiber.Ctx) error {

err = command.Lint().WithDir(filepath.Dir(chartDir)).Run(context.TODO(), username, filepath.Base(chartDir))
if err != nil {
klog.Errorf("lint failed %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Lint failed: %v", err),
Expand Down Expand Up @@ -753,6 +762,7 @@ func (h *handlers) createAppByArchive(ctx *fiber.Ctx) error {
}
appID, err = InsertDevApp(&appData)
if err != nil {
klog.Errorf("failed to insert app %s,err=%v", cfg.Metadata.Name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Insert app failed: %v", err),
Expand All @@ -767,6 +777,7 @@ func (h *handlers) createAppByArchive(ctx *fiber.Ctx) error {
if err != nil {
klog.Error(e)
}
klog.Errorf("failed to copy app dir %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Copy app withdir failed: %v", err),
Expand Down Expand Up @@ -1035,6 +1046,7 @@ func (h *handlers) fillApp(ctx *fiber.Ctx) error {
}
appId, err := UpdateDevApp(username, cfg.Name, updates)
if err != nil {
klog.Errorf("failed to update dev app %s, err=%v", cfg.Name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("update app err %v", err),
Expand Down Expand Up @@ -1077,6 +1089,7 @@ func (h *handlers) fillAppWithExample(ctx *fiber.Ctx) error {
var app App
err := ctx.BodyParser(&app)
if err != nil {
klog.Errorf("failed to parse body %v", err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Bad Request: %v", err),
Expand Down Expand Up @@ -1104,6 +1117,7 @@ func (h *handlers) fillAppWithExample(ctx *fiber.Ctx) error {

appId, err := UpdateDevApp(username, name, updates)
if err != nil {
klog.Errorf("failed to update app %s, err=%v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("update app err %v", err),
Expand Down Expand Up @@ -1169,6 +1183,7 @@ func (h *handlers) fillAppWithDevContainer(ctx *fiber.Ctx) error {

appId, err := UpdateDevApp(username, name, updates)
if err != nil {
klog.Errorf("failed to update dev app %w,err=%v", name, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("update app err %v", err),
Expand All @@ -1177,6 +1192,7 @@ func (h *handlers) fillAppWithDevContainer(ctx *fiber.Ctx) error {

containers, err := GetAppContainersInChart(username, name)
if err != nil || len(containers) == 0 {
klog.Errorf("failed to get app containers in chart err=%v, len(containers)=%d", err, len(containers))
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("get bind containers err %v", err),
Expand All @@ -1194,6 +1210,7 @@ func (h *handlers) fillAppWithDevContainer(ctx *fiber.Ctx) error {
}
err = BindContainer(bindData)
if err != nil {
klog.Errorf("failed to bind container app=%s,err=%v", name, err)
e := h.db.DB.Where("app_id = ?", appId).Delete(&model.DevAppContainers{}).Error
if e != nil && !errors.Is(e, gorm.ErrRecordNotFound) {
klog.Errorf("delete devAppContainer app_id=%d err %v", appId, e)
Expand Down
15 changes: 12 additions & 3 deletions pkg/api/server/handlers_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (h *handlers) saveFile(ctx *fiber.Ctx) error {
appName := pathParts[0]
file, err := WriteFileAndLint(ctx.Context(), username, path, appName, bytes.NewReader(content), command.Lint().WithDir(BaseDir).Run)
if err != nil {
klog.Errorf("failed to write app=%s file path=%s", appName, path)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": err.Error(),
Expand All @@ -81,40 +82,46 @@ func WriteFileAndLint(ctx context.Context, owner, originFilePath, name string, c
if !exists {
err := os.MkdirAll("/charts/tmp", 0755)
if err != nil {
klog.Errorf("failed to mkdir dir=%s,err=%v", "/charts/tmp", err)
return nil, err
}
}

tempFile, err := os.CreateTemp("/charts/tmp", "bak-*"+filepath.Base(originFilePath))
if err != nil {
klog.Infof("failed to crate temp file %v", err)
return nil, fmt.Errorf("create bak temp file failed %v", tempFile)
}

bakContent, err := os.ReadFile(filepath.Join(BaseDir, originFilePath))
bakContent, err := os.ReadFile(filepath.Join(BaseDir, owner, originFilePath))
if err != nil {
klog.Errorf("failed to read origin file path=%s,err=%v", originFilePath, err)
return nil, fmt.Errorf("read origin file %s failed %v", originFilePath, err)
}

_, err = tempFile.Write(bakContent)
if err != nil {
klog.Errorf("failed to write bak content to temp file %v", err)
return nil, err
}

file, err := files.WriteFile(afero.NewBasePathFs(afero.NewOsFs(), BaseDir), originFilePath, content)
if err != nil {
klog.Infof("failed to write file path=%s, err=%v", originFilePath, err)
return nil, err
}

if err = lintFunc(ctx, owner, name); err != nil {
if restoreErr := os.Rename(tempFile.Name(), filepath.Join(BaseDir, owner, originFilePath)); restoreErr != nil {
klog.Errorf("failed to lint and restore, path=%s,err=%v", filepath.Join(BaseDir, owner, originFilePath), restoreErr)
return nil, fmt.Errorf("lint failed: %v, and restore bak failed: %v", err, restoreErr)
}
return nil, fmt.Errorf("lint failed: %v", err)
}
if _, err = os.Stat(tempFile.Name()); err == nil {
e := os.Remove(tempFile.Name())
if e != nil {
klog.Infof("remove temp file failed %v", e)
klog.Infof("remove temp file path=%s failed %v", tempFile.Name(), e)
}
}

Expand All @@ -129,6 +136,7 @@ func (h *handlers) resourcePostHandler(ctx *fiber.Ctx) error {
klog.Infof("resourcePostHandler mkdir: %s", filepath.Join(BaseDir, path))
err := os.MkdirAll(filepath.Join(BaseDir, path), 0755)
if err != nil {
klog.Errorf("failed to mkdir dir=%s, err=%v", filepath.Join(BaseDir, path), err)
return ctx.JSON(fiber.Map{
"code": errToStatus(err),
"message": err.Error(),
Expand All @@ -151,7 +159,7 @@ func (h *handlers) resourcePostHandler(ctx *fiber.Ctx) error {
}
file, err := files.WriteFile(afero.NewBasePathFs(afero.NewOsFs(), BaseDir), path, bytes.NewReader(ctx.Body()))
if err != nil {
klog.Error("write file error, ", err, ", ", path)
klog.Infof("failed to write file path=%s, err=%v", path, err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Write file failed: %v path: %s", err, path),
Expand Down Expand Up @@ -180,6 +188,7 @@ func (h *handlers) resourceDeleteHandler(ctx *fiber.Ctx) error {
}
err = os.RemoveAll(filepath.Join(BaseDir, path))
if err != nil {
klog.Errorf("failed to remove dir=%s, err=%v", filepath.Join(BaseDir, path), err)
return ctx.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Delete file failed: %v", err),
Expand Down
10 changes: 9 additions & 1 deletion pkg/development/command/chartmuseum.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package command

import (
"errors"
"fmt"
"k8s.io/klog/v2"
"net/http"
"time"

Expand All @@ -16,12 +18,15 @@ func getChartVersions(owner, name string) (helm_repo.ChartVersions, error) {
url := fmt.Sprintf("http://127.0.0.1:8888/%s/api/charts/%s", owner, name)
resp, err := client.R().Get(url)
if err != nil {
klog.Errorf("failed to send request to url=%s,err=%v", url, err)
return chartVersions, err
}
if resp.StatusCode() != http.StatusOK {
klog.Errorf("get chart versions from chartmuseum return unexpected status code %d,err=%v", resp.StatusCode(), resp.String())
return chartVersions, fmt.Errorf("get chart versions from chartmuseum return unexpected status code, %d", resp.StatusCode())
}
if err = yaml.Unmarshal(resp.Body(), &chartVersions); err != nil {
klog.Errorf("failed to unmarshal body to chartVersions %v", err)
return chartVersions, err
}
return chartVersions, nil
Expand All @@ -32,10 +37,13 @@ func deleteChartVersion(owner, name, version string) error {
url := fmt.Sprintf("http://127.0.0.1:8888/%s/api/charts/%s/%s", owner, name, version)
resp, err := client.R().Delete(url)
if err != nil {
klog.Errorf("failed to send request to url=%s,err=%v", url, err)
return err
}
if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("delete chart %s, version %s from chartmuseum return unexpected status code", name, version)
msg := fmt.Sprintf("failed to delete chart %s, version %s from chart repo return unexpected status code %v,err=%v", name, version, resp.StatusCode(), resp.String())
klog.Error(msg)
return errors.New(msg)
}
return nil
}
6 changes: 3 additions & 3 deletions pkg/development/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestCreateApp(t *testing.T) {
err := CreateApp().WithDir("/tmp").Run(context.Background(), &CreateConfig{Name: "testdev"})
err := CreateApp().WithDir("/tmp").Run(context.Background(), &CreateConfig{Name: "testdev"}, "")
if err != nil {
klog.Error(err)
t.Fail()
Expand All @@ -20,7 +20,7 @@ func TestCreateApp(t *testing.T) {
}

func TestInstall(t *testing.T) {
_, err := Install().Run(context.Background(), "newapp", "test", "0.0.1")
_, err := Install().Run(context.Background(), "newapp", "test", "", "0.0.1")
if err != nil {
klog.Error(err)
t.Fail()
Expand All @@ -30,7 +30,7 @@ func TestInstall(t *testing.T) {
}

func TestUpdateRepo(t *testing.T) {
_, err := UpdateRepo().WithDir("/tmp").Run(context.Background(), "newapp", false)
_, err := UpdateRepo().WithDir("/tmp").Run(context.Background(), "newapp", "app", false)
if err != nil {
klog.Error(err)
t.Fail()
Expand Down
Loading