Skip to content

Commit

Permalink
reformat json
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Lin committed Oct 4, 2018
1 parent f7d61a5 commit 8d7431d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 106 deletions.
46 changes: 23 additions & 23 deletions src/entity/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,23 @@ type DeploymentConfig struct {

// Deployment is the structure for deployment info
type Deployment struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
OwnerID bson.ObjectId `bson:"ownerID,omitempty" json:"ownerID" validate:"-"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
Labels map[string]string `bson:"labels,omitempty" json:"labels" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
EnvVars map[string]string `bson:"envVars,omitempty" json:"envVars" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
Containers []Container `bson:"containers" json:"containers" validate:"required,dive,required"`
Volumes []DeploymentVolume `bson:"volumes,omitempty" json:"volumes" validate:"required,dive,required"`
ConfigMaps []DeploymentConfig `bson:"configMaps,omitempty" json:"configMaps" validate:"required,dive,required"`
Networks []DeploymentNetwork `bson:"networks,omitempty" json:"networks" validate:"required,dive,required"`
Capability bool `bson:"capability" json:"capability" validate:"-"`
NetworkType string `bson:"networkType" json:"networkType" validate:"required,eq=host|eq=cluster|eq=custom"`
NodeAffinity []string `bson:"nodeAffinity" json:"nodeAffinity" validate:"required"`
IsCapableAutoscaleResources []string `bson:"isCapableAutoscaleResources" json:"isCapableAutoscaleResources" validate:"-"`
IsEnableAutoscale bool `bson:"isEnableAutoscale" json:"isEnableAutoscale" validate:"-"`
AutoscalerInfo AutoscalerInfo `bson:"autoscalerInfo" json:"autoscalerInfo" validate:"-"`
CreatedBy User `json:"createdBy" validate:"-"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
OwnerID bson.ObjectId `bson:"ownerID,omitempty" json:"ownerID" validate:"-"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
Labels map[string]string `bson:"labels,omitempty" json:"labels" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
EnvVars map[string]string `bson:"envVars,omitempty" json:"envVars" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
Containers []Container `bson:"containers" json:"containers" validate:"required,dive,required"`
Volumes []DeploymentVolume `bson:"volumes,omitempty" json:"volumes" validate:"required,dive,required"`
ConfigMaps []DeploymentConfig `bson:"configMaps,omitempty" json:"configMaps" validate:"required,dive,required"`
Networks []DeploymentNetwork `bson:"networks,omitempty" json:"networks" validate:"required,dive,required"`
Capability bool `bson:"capability" json:"capability" validate:"-"`
NetworkType string `bson:"networkType" json:"networkType" validate:"required,eq=host|eq=cluster|eq=custom"`
NodeAffinity []string `bson:"nodeAffinity" json:"nodeAffinity" validate:"required"`
IsEnableAutoscale bool `bson:"isEnableAutoscale" json:"isEnableAutoscale" validate:"-"`
AutoscalerInfo AutoscalerInfo `bson:"autoscalerInfo" json:"autoscalerInfo" validate:"-"`
CreatedBy User `json:"createdBy" validate:"-"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`

Replicas int32 `bson:"replicas" json:"replicas" validate:"required"`
}
Expand All @@ -86,11 +85,12 @@ type AutoscalerInfo struct {
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
// ScaleTargetRef is deployment name
ScaleTargetRefName string `bson:"scaleTargetRefName" json:"scaleTargetRefName" validate:"required,k8sname"`
ResourceName corev1.ResourceName `bson:"resourceName" json:"resourceName" validate:"required,eq=cpu|eq=memory"`
MinReplicas int32 `bson:"minReplicas" json:"minReplicas" validate:"required,numeric"`
MaxReplicas int32 `bson:"maxReplicas" json:"maxReplicas" validate:"required,numeric"`
TargetAverageUtilization int32 `bson:"targetAverageUtilization" json:"targetAverageUtilization" validate:"required,numeric"`
ScaleTargetRefName string `bson:"scaleTargetRefName" json:"scaleTargetRefName" validate:"required,k8sname"`
IsCapableAutoscaleResources []string `bson:"isCapableAutoscaleResources" json:"isCapableAutoscaleResources" validate:"-"`
ResourceName corev1.ResourceName `bson:"resourceName" json:"resourceName" validate:"required,eq=cpu|eq=memory"`
MinReplicas int32 `bson:"minReplicas" json:"minReplicas" validate:"required,numeric"`
MaxReplicas int32 `bson:"maxReplicas" json:"maxReplicas" validate:"required,numeric"`
TargetAverageUtilization int32 `bson:"targetAverageUtilization" json:"targetAverageUtilization" validate:"required,numeric"`
}

// GetCollection - get model mongo collection name.
Expand Down
25 changes: 12 additions & 13 deletions src/server/handler_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,18 @@ func (suite *AppTestSuite) TestCreateApp() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
IsCapableAutoscaleResources: []string{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
}

ports := []entity.ServicePort{
Expand Down
10 changes: 5 additions & 5 deletions src/server/handler_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ func createDeploymentHandler(ctx *web.Context) {
// autoscale resource request setting prerequisite check
for _, container := range p.Containers {
if container.ResourceRequestCPU != 0 {
p.IsCapableAutoscaleResources = append(p.IsCapableAutoscaleResources, "cpu")
p.AutoscalerInfo.IsCapableAutoscaleResources = append(p.AutoscalerInfo.IsCapableAutoscaleResources, "cpu")
}
if container.ResourceRequestMemory != 0 {
p.IsCapableAutoscaleResources = append(p.IsCapableAutoscaleResources, "memory")
p.AutoscalerInfo.IsCapableAutoscaleResources = append(p.AutoscalerInfo.IsCapableAutoscaleResources, "memory")
}
}

if p.NetworkType == entity.DeploymentCustomNetwork {
// clear the slice
p.IsCapableAutoscaleResources = nil
p.AutoscalerInfo.IsCapableAutoscaleResources = nil
}

if err := sp.Validator.Struct(p); err != nil {
Expand Down Expand Up @@ -294,11 +294,11 @@ func uploadDeploymentYAMLHandler(ctx *web.Context) {
for _, container := range deploymentObj.Spec.Template.Spec.Containers {
// check if map contains cpu key
if _, ok := container.Resources.Requests[corev1.ResourceCPU]; ok {
d.IsCapableAutoscaleResources = append(d.IsCapableAutoscaleResources, "cpu")
d.AutoscalerInfo.IsCapableAutoscaleResources = append(d.AutoscalerInfo.IsCapableAutoscaleResources, "cpu")
}
// check if map contains memory key
if _, ok := container.Resources.Requests[corev1.ResourceMemory]; ok {
d.IsCapableAutoscaleResources = append(d.IsCapableAutoscaleResources, "memory")
d.AutoscalerInfo.IsCapableAutoscaleResources = append(d.AutoscalerInfo.IsCapableAutoscaleResources, "memory")
}
}

Expand Down
117 changes: 55 additions & 62 deletions src/server/handler_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,18 @@ func (suite *DeploymentTestSuite) TestCreateDeployment() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
IsCapableAutoscaleResources: []string{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
}
bodyBytes, err := json.MarshalIndent(deploy, "", " ")
suite.NoError(err)
Expand Down Expand Up @@ -145,10 +144,9 @@ func (suite *DeploymentTestSuite) TestCreateDeploymentFail() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
Name: tName,
Namespace: namespace,
Containers: containers,
IsCapableAutoscaleResources: []string{},
Name: tName,
Namespace: namespace,
Containers: containers,
Volumes: []entity.DeploymentVolume{
{Name: namesgenerator.GetRandomName(0)},
},
Expand Down Expand Up @@ -181,15 +179,14 @@ func (suite *DeploymentTestSuite) TestDeleteDeployment() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
IsCapableAutoscaleResources: []string{},
Replicas: 1,
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
}

err := p.CreateDeployment(suite.sp, &deploy)
Expand Down Expand Up @@ -241,11 +238,10 @@ func (suite *DeploymentTestSuite) TestGetDeployment() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
IsCapableAutoscaleResources: []string{},
Containers: containers,
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
}

//Create data into mongo manually
Expand Down Expand Up @@ -293,11 +289,10 @@ func (suite *DeploymentTestSuite) TestListDeployment() {
},
}
deployments = append(deployments, entity.Deployment{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Namespace: namespace,
Containers: containers,
IsCapableAutoscaleResources: []string{},
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Namespace: namespace,
Containers: containers,
})
}

Expand Down Expand Up @@ -476,19 +471,18 @@ func (suite *DeploymentTestSuite) TestEnableDeploymentWithAutoscaler() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
IsCapableAutoscaleResources: []string{},
NodeAffinity: []string{},
Replicas: 1,
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
}
bodyBytes, err := json.MarshalIndent(deploy, "", " ")
suite.NoError(err)
Expand Down Expand Up @@ -551,19 +545,18 @@ func (suite *DeploymentTestSuite) TestDisableDeploymentWithAutoscaler() {
}
tName := namesgenerator.GetRandomName(0)
deploy := entity.Deployment{
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
IsCapableAutoscaleResources: []string{},
NodeAffinity: []string{},
Replicas: 1,
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.DeploymentVolume{},
ConfigMaps: []entity.DeploymentConfig{},
Networks: []entity.DeploymentNetwork{},
Capability: true,
NetworkType: entity.DeploymentHostNetwork,
NodeAffinity: []string{},
Replicas: 1,
}
bodyBytes, err := json.MarshalIndent(deploy, "", " ")
suite.NoError(err)
Expand Down
1 change: 0 additions & 1 deletion tests/05-deployment/deployment.info
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"myip":"1.2.3.4"
},
"namespace":"default",
"isCapableAutoscaleResources":[],
"containers":[
{
"name":"first-container",
Expand Down
1 change: 0 additions & 1 deletion tests/06-app/app.info
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"myip":"1.2.3.4"
},
"namespace":"default",
"isCapableAutoscaleResources": [],
"containers":[
{
"name":"first-container",
Expand Down
1 change: 0 additions & 1 deletion tests/07-ovs/deployment.info
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"labels":{},
"envVars":{},
"namespace":"default",
"isCapableAutoscaleResources":[],
"containers":[
{
"name":"first-container",
Expand Down

0 comments on commit 8d7431d

Please sign in to comment.