Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Jun 16, 2020
1 parent ec2ff4b commit dd1004d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 68 deletions.
2 changes: 1 addition & 1 deletion internal/app/controller/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Applications(c *gin.Context) {
}).Warn(`Error while fetching beetle configMap`)
}

applications := []kubernetes.Application{}
applications := []model.Application{}

for _, app := range config.Applications {
application, err := cluster.GetApplication(
Expand Down
34 changes: 7 additions & 27 deletions internal/app/kubernetes/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,14 @@ import (
"fmt"
"strings"

"github.com/clivern/beetle/internal/app/model"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Container struct
type Container struct {
Name string `json:"name"`
Image string `json:"image"`
Version string `json:"version"`
Deployment Deployment `json:"deployment"`
}

// Deployment struct
type Deployment struct {
Name string `json:"name"`
UID string `json:"uid"`
}

// Application struct
type Application struct {
ID string `json:"id"`
Name string `json:"name"`
Format string `json:"format"`
Containers []Container `json:"containers"`
}

// GetApplication gets current application version
func (c *Cluster) GetApplication(ctx context.Context, namespace, id, name, format string) (Application, error) {
result := Application{}
func (c *Cluster) GetApplication(ctx context.Context, namespace, id, name, format string) (model.Application, error) {
result := model.Application{}

err := c.Config()

Expand All @@ -61,11 +41,11 @@ func (c *Cluster) GetApplication(ctx context.Context, namespace, id, name, forma
result.ID = id
result.Name = name
result.Format = format
result.Containers = []Container{}
result.Containers = []model.Container{}

for _, deployment := range data.Items {
for _, container := range deployment.Spec.Template.Spec.Containers {
result.Containers = append(result.Containers, Container{
result.Containers = append(result.Containers, model.Container{
Name: container.Name,
Image: container.Image,
Version: strings.Replace(
Expand All @@ -74,7 +54,7 @@ func (c *Cluster) GetApplication(ctx context.Context, namespace, id, name, forma
"",
-1,
),
Deployment: Deployment{
Deployment: model.Deployment{
Name: deployment.ObjectMeta.Name,
UID: string(deployment.ObjectMeta.UID),
},
Expand Down
4 changes: 2 additions & 2 deletions internal/app/kubernetes/deployment_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Cluster) Deploy(deploymentRequest model.DeploymentRequest) (bool, error
// {"op":"replace","path":"/spec/template/spec/containers/0/image","value":"clivern/toad:release-0.2.4"}
// ]'
func (c *Cluster) RecreateStrategy(deploymentRequest model.DeploymentRequest) (bool, error) {
result := Application{}
result := model.Application{}
patch := make(map[string][]model.PatchStringValue)

config, err := c.GetConfig(context.TODO(), deploymentRequest.Namespace)
Expand Down Expand Up @@ -133,7 +133,7 @@ func (c *Cluster) RecreateStrategy(deploymentRequest model.DeploymentRequest) (b
// {"op":"replace","path":"/spec/template/spec/containers/0/image","value":"clivern/toad:release-0.2.4"}
// ]'
func (c *Cluster) RampedStrategy(deploymentRequest model.DeploymentRequest) (bool, error) {
result := Application{}
result := model.Application{}
patch := make(map[string][]model.PatchStringValue)

config, err := c.GetConfig(context.TODO(), deploymentRequest.Namespace)
Expand Down
67 changes: 67 additions & 0 deletions internal/app/model/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package model

import (
"encoding/json"
)

// Container struct
type Container struct {
Name string `json:"name"`
Image string `json:"image"`
Version string `json:"version"`
Deployment Deployment `json:"deployment"`
}

// Application struct
type Application struct {
ID string `json:"id"`
Name string `json:"name"`
Format string `json:"format"`
Containers []Container `json:"containers"`
}

// Deployment struct
type Deployment struct {
Name string `json:"name"`
UID string `json:"uid"`
}

// LoadFromJSON update object from json
func (c *Application) LoadFromJSON(data []byte) (bool, error) {
err := json.Unmarshal(data, &c)
if err != nil {
return false, err
}
return true, nil
}

// ConvertToJSON convert object to json
func (c *Application) ConvertToJSON() (string, error) {
data, err := json.Marshal(&c)
if err != nil {
return "", err
}
return string(data), nil
}

// LoadFromJSON update object from json
func (d *Deployment) LoadFromJSON(data []byte) (bool, error) {
err := json.Unmarshal(data, &d)
if err != nil {
return false, err
}
return true, nil
}

// ConvertToJSON convert object to json
func (d *Deployment) ConvertToJSON() (string, error) {
data, err := json.Marshal(&d)
if err != nil {
return "", err
}
return string(data), nil
}
10 changes: 5 additions & 5 deletions internal/app/model/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"gopkg.in/yaml.v3"
)

// Application struct
type Application struct {
// App struct
type App struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
ImageFormat string `yaml:"imageFormat"`
}

// Configs struct
type Configs struct {
Exists bool `yaml:"exists"`
Version string `yaml:"version"`
Applications []Application `yaml:"applications"`
Exists bool `yaml:"exists"`
Version string `yaml:"version"`
Applications []App `yaml:"applications"`
}

// LoadFromYAML update object from yaml
Expand Down
33 changes: 0 additions & 33 deletions internal/app/model/deployment.go

This file was deleted.

0 comments on commit dd1004d

Please sign in to comment.