Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
samson-samson committed Aug 30, 2023
1 parent b2aea3b commit 24fba01
Show file tree
Hide file tree
Showing 46 changed files with 1,275 additions and 814 deletions.
8 changes: 5 additions & 3 deletions chaosmeta-platform/conf/app.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
secretkey: chaosmeta1234567
argoWorkflowNamespace: chaosmeta
workflowNamespace: chaosmeta
db:
name: chaosmeta
user: root
name: chaosmeta_platform
user: chaosmeta
passwd: chaosmeta
url: chaosmeta-platform:3306
url: 127.0.0.1:3306
maxidle: 30
maxconn: 30
debug: false
Expand Down
12 changes: 10 additions & 2 deletions chaosmeta-platform/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func (r RunMode) Int() int {
}

type Config struct {
SecretKey string `yaml:"secretkey"`
DB struct {
SecretKey string `yaml:"secretkey"`
ArgoWorkflowNamespace string `yaml:"argoWorkflowNamespace"`
WorkflowNamespace string `yaml:"workflowNamespace"`
DB struct {
Name string `yaml:"name"`
User string `yaml:"user"`
Passwd string `yaml:"passwd"`
Expand Down Expand Up @@ -84,6 +86,12 @@ func InitConfig() {
if err := viper.Unmarshal(DefaultRunOptIns); err != nil {
log.Panic(err)
}
if DefaultRunOptIns.ArgoWorkflowNamespace == "" {
DefaultRunOptIns.ArgoWorkflowNamespace = "default"
}
if DefaultRunOptIns.WorkflowNamespace == "" {
DefaultRunOptIns.WorkflowNamespace = "chaosmeta-inject"
}
}

func getCurrentPath() string {
Expand Down
2 changes: 1 addition & 1 deletion chaosmeta-platform/config/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Setup() {
new(namespace.ClusterNamespace), new(namespace.Label), new(namespace.Namespace), new(namespace.UserNamespace), new(user.User),
new(cluster.Cluster),
new(agent.Agent),
new(basic.Scope), new(basic.Target), new(basic.Fault), new(basic.Args),
new(basic.Scope), new(basic.Target), new(basic.Fault), new(basic.FlowInject), new(basic.MeasureInject), new(basic.Args),
new(experiment.WorkflowNode), new(experiment.LabelExperiment), new(experiment.FaultRange), new(experiment.Experiment), new(experiment.ArgsValue),
new(experiment_instance.WorkflowNodeInstance), new(experiment_instance.LabelExperimentInstance), new(experiment_instance.FaultRangeInstance), new(experiment_instance.ExperimentInstance), new(experiment_instance.ArgsValueInstance),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package experiment

import (
"chaosmeta-platform/pkg/gateway/apiserver/v1alpha1"
experimentModel "chaosmeta-platform/pkg/models/experiment"
"chaosmeta-platform/pkg/service/experiment"
"chaosmeta-platform/pkg/service/user"
"chaosmeta-platform/util/log"
"encoding/json"
"errors"
"fmt"
beego "github.com/beego/beego/v2/server/web"
"time"
)
Expand All @@ -36,8 +39,9 @@ func (c *ExperimentController) GetExperimentList() {
scheduleType := c.GetString("schedule_type")
namespaceID, _ := c.GetInt("namespace_id")
name := c.GetString("name")
creator, _ := c.GetInt("creator", 0)
creator := c.GetString("creator")
timeType := c.GetString("time_type")
timeSearchField := c.GetString("time_search_field")
recentDays, _ := c.GetInt("recent_days", 0)
startTime, _ := time.Parse(experiment.TimeLayout, c.GetString("start_time"))
endTime, _ := time.Parse(experiment.TimeLayout, c.GetString("end_time"))
Expand All @@ -46,7 +50,7 @@ func (c *ExperimentController) GetExperimentList() {
pageSize, _ := c.GetInt("page_size", 10)
experimentService := experiment.ExperimentService{}

total, experimentList, err := experimentService.SearchExperiments(lastInstanceStatus, namespaceID, creator, name, scheduleType, timeType, recentDays, startTime, endTime, orderBy, page, pageSize)
total, experimentList, err := experimentService.SearchExperiments(lastInstanceStatus, namespaceID, creator, name, scheduleType, timeType, timeSearchField, recentDays, startTime, endTime, orderBy, page, pageSize)
if err != nil {
c.Error(&c.Controller, err)
return
Expand Down Expand Up @@ -85,7 +89,7 @@ func (c *ExperimentController) CreateExperiment() {
return
}

var createExperimentRequest experiment.Experiment
var createExperimentRequest experiment.ExperimentCreate
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &createExperimentRequest); err != nil {
c.Error(&c.Controller, err)
return
Expand All @@ -106,7 +110,7 @@ func (c *ExperimentController) UpdateExperiment() {
uuid := c.Ctx.Input.Param(":uuid")
experimentService := experiment.ExperimentService{}

var updateExperimentRequest experiment.Experiment
var updateExperimentRequest experiment.ExperimentCreate
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &updateExperimentRequest); err != nil {
c.Error(&c.Controller, err)
return
Expand All @@ -121,7 +125,25 @@ func (c *ExperimentController) UpdateExperiment() {

func (c *ExperimentController) StartExperiment() {
uuid := c.Ctx.Input.Param(":uuid")
if err := experiment.StartExperiment(uuid); err != nil {
username := c.Ctx.Input.GetData("userName").(string)
experimentService := experiment.ExperimentService{}
experimentGet, err := experimentService.GetExperimentByUUID(uuid)
if err != nil {
c.Error(&c.Controller, err)
return
}
if experimentGet.ScheduleType != string(experimentModel.ManualMode) {
c.Error(&c.Controller, fmt.Errorf("manual mode is required to perform the walkthrough"))
return
}

if err := experimentService.UpdateExperimentStatusAndLastInstance(uuid, int(experimentModel.ToBeExecuted), time.Now().Format(experimentModel.TimeLayout)); err != nil {
log.Error(err)
}
if err := experiment.StartExperiment(uuid, username); err != nil {
if err := experimentService.UpdateExperimentStatusAndLastInstance(uuid, int(experimentModel.Executed), time.Now().Format(experimentModel.TimeLayout)); err != nil {
log.Error(err)
}
c.Error(&c.Controller, err)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type CreateExperimentResponse struct {
}

type GetExperimentResponse struct {
Experiment experiment.Experiment `json:"experiments"`
Experiment experiment.ExperimentGet `json:"experiments"`
}

type ExperimentListResponse struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
Total int64 `json:"total"`
Experiments []experiment.Experiment `json:"experiments"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
Total int64 `json:"total"`
Experiments []experiment.ExperimentGet `json:"experiments"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ type ExperimentInstanceController struct {

func (c *ExperimentInstanceController) GetExperimentInstances() {
lastInstance := c.GetString("last_instance")
scheduleType := c.GetString("schedule_type")
//scheduleType := c.GetString("schedule_type")
namespaceId, _ := c.GetInt("namespace_id")
experimentUUID := c.GetString("experiment_uuid")
name := c.GetString("name")
creator, _ := c.GetInt("creator", 0)
creatorName := c.GetString("creator_name")
timeType := c.GetString("time_type")
timeSearchField := c.GetString("time_search_field")
status := c.GetString("status")
recentDays, _ := c.GetInt("recent_days", 0)
startTime, _ := time.Parse(experiment.TimeLayout, c.GetString("start_time"))
endTime, _ := time.Parse(experiment.TimeLayout, c.GetString("end_time"))
orderBy := c.GetString("sort")
page, _ := c.GetInt("page", 1)
pageSize, _ := c.GetInt("page_size", 10)
es := experiment_instance.ExperimentInstanceService{}
total, experiments, err := es.SearchExperimentInstances(lastInstance, namespaceId, creator, name, scheduleType, timeType, recentDays, startTime, endTime, orderBy, page, pageSize)
total, experiments, err := es.SearchExperimentInstances(lastInstance, experimentUUID, namespaceId, creatorName, name, timeType, timeSearchField, status, recentDays, startTime, endTime, orderBy, page, pageSize)
if err != nil {
c.Error(&c.Controller, err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func (c *KubeController) ListDeployments() {
id, _ := c.GetInt(":id")
id, _ := c.GetInt(":id", 0)
nsName := c.GetString(":ns_name")
clusterService := cluster.ClusterService{}
kubeClient, restConfig, err := clusterService.GetRestConfig(context.Background(), id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func (c *KubeController) ListNamespaces() {
id, _ := c.GetInt(":id")
id, _ := c.GetInt(":id", 0)
clusterService := cluster.ClusterService{}
kubeClient, _, err := clusterService.GetRestConfig(context.Background(), id)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func (c *KubeController) ListNodes() {
id, _ := c.GetInt(":id")
id, _ := c.GetInt(":id", 0)
clusterService := cluster.ClusterService{}
kubeClient, restConfig, err := clusterService.GetRestConfig(context.Background(), id)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func (c *KubeController) ListPods() {
id, _ := c.GetInt(":id")
id, _ := c.GetInt(":id", 0)
nsName := c.GetString(":ns_name")
clusterService := cluster.ClusterService{}
kubeClient, restConfig, err := clusterService.GetRestConfig(context.Background(), id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package namespace
import (
"chaosmeta-platform/pkg/gateway/apiserver/v1alpha1"
namespace2 "chaosmeta-platform/pkg/models/namespace"
"chaosmeta-platform/pkg/models/user"
"chaosmeta-platform/pkg/service/namespace"
userService "chaosmeta-platform/pkg/service/user"
"context"
"encoding/json"
"fmt"
beego "github.com/beego/beego/v2/server/web"
)

Expand Down Expand Up @@ -67,6 +68,24 @@ func (c *NamespaceController) Get() {
c.Success(&c.Controller, getNamespaceResponse)
}

func (c *NamespaceController) GetPermission() {
namespaceId, err := c.GetInt(":id")
if err != nil {
c.Error(&c.Controller, err)
return
}
username := c.Ctx.Input.GetData("userName").(string)
userService := &userService.UserService{}
userGet, err := userService.Get(context.Background(), username)
if err != nil {
c.Error(&c.Controller, fmt.Errorf("unable to identify user"))
return
}
namespace := &namespace.NamespaceService{}
permission := namespace.GetUserPermission(context.Background(), namespaceId, userGet.ID)
c.Success(&c.Controller, permission)
}

func (c *NamespaceController) GetList() {
sort := c.GetString("sort")
name := c.GetString("name")
Expand All @@ -93,43 +112,52 @@ func (c *NamespaceController) QueryList() {

page, _ := c.GetInt("page", 1)
pageSize, _ := c.GetInt("page_size", 10)
sort := c.GetString("sort")

userGet := user.User{Email: username}

if err := user.GetUser(context.Background(), &userGet); err != nil {
c.Error(&c.Controller, err)
userService := &userService.UserService{}
userGet, errGet := userService.Get(context.Background(), username)
if errGet != nil {
c.Error(&c.Controller, errGet)
return
}

namespaceService := &namespace.NamespaceService{}
queryUserId := 0
if userNameQuery != "" {
userGet, errGet := userService.Get(context.Background(), userNameQuery)
if errGet != nil {
c.Error(&c.Controller, fmt.Errorf("no user"))
return
}
queryUserId = userGet.ID
}

var (
total int64
namespaceList []namespace.NamespaceData
err error
)

namespaceService := &namespace.NamespaceService{}
switch namespaceClass {
case "read":
total, namespaceList, err = namespaceService.GroupNamespacesByUsername(context.Background(), userGet.ID, nameSpaceName, userNameQuery, 0, sort, page, pageSize)
total, namespaceList, err = namespaceService.QueryNamespace(context.Background(), userGet.ID, queryUserId, nameSpaceName, 0, page, pageSize)
if err != nil {
c.Error(&c.Controller, err)
return
}
case "write":
total, namespaceList, err = namespaceService.GroupNamespacesByUsername(context.Background(), userGet.ID, nameSpaceName, userNameQuery, 1, sort, page, pageSize)
total, namespaceList, err = namespaceService.QueryNamespace(context.Background(), userGet.ID, queryUserId, nameSpaceName, 1, page, pageSize)
if err != nil {
c.Error(&c.Controller, err)
return
}
case "all":
total, namespaceList, err = namespaceService.GroupAllNamespaces(context.Background(), nameSpaceName, userNameQuery, sort, page, pageSize)
case "relevant":
total, namespaceList, err = namespaceService.QueryNamespace(context.Background(), userGet.ID, queryUserId, nameSpaceName, -1, page, pageSize)
if err != nil {
c.Error(&c.Controller, err)
return
}
case "not":
total, namespaceList, err = namespaceService.GroupNamespacesUserNotIn(context.Background(), userGet.ID, nameSpaceName, userNameQuery, sort, page, pageSize)
case "all":
total, namespaceList, err = namespaceService.GroupAllNamespaces(context.Background(), userGet.ID, queryUserId, nameSpaceName, page, pageSize)
if err != nil {
c.Error(&c.Controller, err)
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022-2023 Chaos Meta Authors.
*
* 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 namespace

import (
"chaosmeta-platform/pkg/service/namespace"
"context"
)

func (c *NamespaceController) GetOverview() {
recentDays, err := c.GetInt("recent_day", 7)
if err != nil {
c.Error(&c.Controller, err)
return
}
namespaceId, _ := c.GetInt(":id", 0)

namespace := &namespace.NamespaceService{}
totalExperimentCount, totalExperimentInstancesCount, failedExperimentInstancesCount, err := namespace.GetOverview(context.Background(), namespaceId, recentDays)
if err != nil {
c.Error(&c.Controller, err)
return
}

response := GetOverviewResponse{TotalExperiments: totalExperimentCount, TotalExperimentInstances: totalExperimentInstancesCount, FailedExperimentInstances: failedExperimentInstancesCount}
c.Success(&c.Controller, response)
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,9 @@ type GetNamespaceListResponse struct {
Total int64 `json:"total"`
Namespaces []namespace.UserNamespaceData `json:"namespaces,omitempty"`
}

type GetOverviewResponse struct {
TotalExperiments int64 `json:"total_experiments"`
TotalExperimentInstances int64 `json:"total_experiment_instances"`
FailedExperimentInstances int64 `json:"failed_experiment_instances"`
}
Loading

0 comments on commit 24fba01

Please sign in to comment.