Skip to content

Commit

Permalink
refactor labels
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Aug 31, 2023
1 parent e3b3bdd commit e3dfd99
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 102 deletions.
14 changes: 7 additions & 7 deletions internal/command/box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
commonCmd "github.com/hckops/hckctl/internal/command/common"
commonFlag "github.com/hckops/hckctl/internal/command/common/flag"
"github.com/hckops/hckctl/internal/command/config"
"github.com/hckops/hckctl/pkg/box/model"
boxModel "github.com/hckops/hckctl/pkg/box/model"
commonModel "github.com/hckops/hckctl/pkg/common/model"
"github.com/hckops/hckctl/pkg/template"
)
Expand All @@ -19,7 +19,7 @@ type boxCmdOptions struct {
configRef *config.ConfigRef
sourceFlag *commonFlag.SourceFlag
providerFlag *commonFlag.ProviderFlag
provider model.BoxProvider
provider boxModel.BoxProvider
tunnelFlag *boxFlag.TunnelFlag
}

Expand Down Expand Up @@ -118,21 +118,21 @@ func (opts *boxCmdOptions) run(cmd *cobra.Command, args []string) error {
path := args[0]
log.Debug().Msgf("temporary box from local template: path=%s", path)

sourceLoader := template.NewLocalCachedLoader[model.BoxV1](path, opts.configRef.Config.Template.CacheDir)
return opts.temporaryBox(sourceLoader, opts.provider, commonModel.NewBoxLabels().AddDefaultLocal())
sourceLoader := template.NewLocalCachedLoader[boxModel.BoxV1](path, opts.configRef.Config.Template.CacheDir)
return opts.temporaryBox(sourceLoader, opts.provider, boxModel.NewBoxLabels().AddDefaultLocal())

} else {
name := args[0]
log.Debug().Msgf("temporary box from git template: name=%s revision=%s", name, opts.sourceFlag.Revision)

sourceOpts := commonCmd.NewGitSourceOptions(opts.configRef.Config.Template.CacheDir, opts.sourceFlag.Revision)
sourceLoader := template.NewGitLoader[model.BoxV1](sourceOpts, name)
labels := commonModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName())
sourceLoader := template.NewGitLoader[boxModel.BoxV1](sourceOpts, name)
labels := boxModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName())
return opts.temporaryBox(sourceLoader, opts.provider, labels)
}
}

func (opts *boxCmdOptions) temporaryBox(sourceLoader template.SourceLoader[model.BoxV1], provider model.BoxProvider, labels commonModel.Labels) error {
func (opts *boxCmdOptions) temporaryBox(sourceLoader template.SourceLoader[boxModel.BoxV1], provider boxModel.BoxProvider, labels commonModel.Labels) error {

temporaryClient := func(invokeOpts *invokeOptions) error {

Expand Down
30 changes: 15 additions & 15 deletions internal/command/box/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (
"github.com/hckops/hckctl/internal/command/config"
"github.com/hckops/hckctl/internal/command/version"
"github.com/hckops/hckctl/pkg/box"
"github.com/hckops/hckctl/pkg/box/model"
boxModel "github.com/hckops/hckctl/pkg/box/model"
commonModel "github.com/hckops/hckctl/pkg/common/model"
"github.com/hckops/hckctl/pkg/schema"
"github.com/hckops/hckctl/pkg/template"
)

type invokeOptions struct {
client box.BoxClient
template *template.TemplateInfo[model.BoxV1]
template *template.TemplateInfo[boxModel.BoxV1]
loader *commonCmd.Loader
}

// start and temporary
func runBoxClient(sourceLoader template.SourceLoader[model.BoxV1], provider model.BoxProvider, configRef *config.ConfigRef, invokeClient func(*invokeOptions) error) error {
func runBoxClient(sourceLoader template.SourceLoader[boxModel.BoxV1], provider boxModel.BoxProvider, configRef *config.ConfigRef, invokeClient func(*invokeOptions) error) error {

boxTemplate, err := sourceLoader.Read()
if err != nil || boxTemplate.Value.Kind != schema.KindBoxV1 {
Expand Down Expand Up @@ -56,7 +56,7 @@ func runBoxClient(sourceLoader template.SourceLoader[model.BoxV1], provider mode
}

// open, info and stop-one
func attemptRunBoxClients(configRef *config.ConfigRef, boxName string, invokeClient func(*invokeOptions, *model.BoxDetails) error) error {
func attemptRunBoxClients(configRef *config.ConfigRef, boxName string, invokeClient func(*invokeOptions, *boxModel.BoxDetails) error) error {

loader := commonCmd.NewLoader()
loader.Start("loading %s", boxName)
Expand Down Expand Up @@ -107,25 +107,25 @@ func attemptRunBoxClients(configRef *config.ConfigRef, boxName string, invokeCli
return errors.New("not found")
}

func newSourceLoader(boxDetails *model.BoxDetails, cacheDir string) template.SourceLoader[model.BoxV1] {
func newSourceLoader(boxDetails *boxModel.BoxDetails, cacheDir string) template.SourceLoader[boxModel.BoxV1] {
if boxDetails.TemplateInfo.IsCached() {
return template.NewLocalLoader[model.BoxV1](boxDetails.TemplateInfo.CachedTemplate.Path)
return template.NewLocalLoader[boxModel.BoxV1](boxDetails.TemplateInfo.CachedTemplate.Path)
} else {
sourceOpts := commonCmd.NewGitSourceOptions(cacheDir, boxDetails.TemplateInfo.GitTemplate.Commit)
return template.NewGitLoader[model.BoxV1](sourceOpts, boxDetails.TemplateInfo.GitTemplate.Name)
return template.NewGitLoader[boxModel.BoxV1](sourceOpts, boxDetails.TemplateInfo.GitTemplate.Name)
}
}

func newBoxClientOpts(provider model.BoxProvider, configRef *config.ConfigRef) *model.BoxClientOptions {
return &model.BoxClientOptions{
func newBoxClientOpts(provider boxModel.BoxProvider, configRef *config.ConfigRef) *boxModel.BoxClientOptions {
return &boxModel.BoxClientOptions{
Provider: provider,
DockerOpts: configRef.Config.Provider.Docker.ToDockerOptions(),
KubeOpts: configRef.Config.Provider.Kube.ToKubeOptions(),
CloudOpts: configRef.Config.Provider.Cloud.ToCloudOptions(version.ClientVersion()),
}
}

func newDefaultBoxClient(provider model.BoxProvider, configRef *config.ConfigRef, loader *commonCmd.Loader) (box.BoxClient, error) {
func newDefaultBoxClient(provider boxModel.BoxProvider, configRef *config.ConfigRef, loader *commonCmd.Loader) (box.BoxClient, error) {

boxClientOpts := newBoxClientOpts(provider, configRef)
boxClient, err := box.NewBoxClient(boxClientOpts)
Expand All @@ -138,21 +138,21 @@ func newDefaultBoxClient(provider model.BoxProvider, configRef *config.ConfigRef
return boxClient, nil
}

func newCreateOptions(info *template.TemplateInfo[model.BoxV1], labels commonModel.Labels, sizeValue string) (*model.CreateOptions, error) {
size, err := model.ExistResourceSize(sizeValue)
func newCreateOptions(info *template.TemplateInfo[boxModel.BoxV1], labels commonModel.Labels, sizeValue string) (*boxModel.CreateOptions, error) {
size, err := boxModel.ExistResourceSize(sizeValue)
if err != nil {
return nil, err
}

var allLabels commonModel.Labels
switch info.SourceType {
case template.Local:
allLabels = labels.AddBoxSize(size).AddLocal(info.Path)
allLabels = boxModel.AddBoxSize(labels, size).AddLocal(info.Path)
case template.Git:
allLabels = labels.AddBoxSize(size).AddGit(info.Path, info.Revision)
allLabels = boxModel.AddBoxSize(labels, size).AddGit(info.Path, info.Revision)
}

return &model.CreateOptions{
return &boxModel.CreateOptions{
Template: &info.Value.Data,
Size: size,
Labels: allLabels,
Expand Down
4 changes: 2 additions & 2 deletions internal/command/box/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func (opts *boxStartCmdOptions) run(cmd *cobra.Command, args []string) error {
log.Debug().Msgf("start box from local template: path=%s", path)

sourceLoader := template.NewLocalCachedLoader[boxModel.BoxV1](path, opts.configRef.Config.Template.CacheDir)
return startBox(sourceLoader, opts.provider, opts.configRef, commonModel.NewBoxLabels().AddDefaultLocal())
return startBox(sourceLoader, opts.provider, opts.configRef, boxModel.NewBoxLabels().AddDefaultLocal())

} else {
name := args[0]
log.Debug().Msgf("start box from git template: name=%s revision=%s", name, opts.sourceFlag.Revision)

sourceOpts := commonCmd.NewGitSourceOptions(opts.configRef.Config.Template.CacheDir, opts.sourceFlag.Revision)
sourceLoader := template.NewGitLoader[boxModel.BoxV1](sourceOpts, name)
labels := commonModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName())
labels := boxModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName())
return startBox(sourceLoader, opts.provider, opts.configRef, labels)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/box/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func toBoxDetails(response *v1.Message[v1.BoxDescribeResponseBody]) (*boxModel.B
},
TemplateInfo: &boxModel.BoxTemplateInfo{
// TODO valid only if response.Body.Template.Public
GitTemplate: &boxModel.GitTemplateInfo{
GitTemplate: &commonModel.GitTemplateInfo{
Url: response.Body.Template.Url,
Revision: response.Body.Template.Revision,
Commit: response.Body.Template.Commit,
Expand Down
21 changes: 11 additions & 10 deletions pkg/box/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/stretchr/testify/assert"

v1 "github.com/hckops/hckctl/pkg/api/v1"
"github.com/hckops/hckctl/pkg/box/model"
boxModel "github.com/hckops/hckctl/pkg/box/model"
commonModel "github.com/hckops/hckctl/pkg/common/model"
)

func TestToBoxDetails(t *testing.T) {
Expand All @@ -30,29 +31,29 @@ func TestToBoxDetails(t *testing.T) {
Env: []string{"KEY_1=VALUE_1", "KEY_2=VALUE_2", "INVALID", "=INVALID="},
Ports: []string{"alias-1/123", "alias-2/456", "INVALID", "/INVALID/"},
})
expected := &model.BoxDetails{
Info: model.BoxInfo{
expected := &boxModel.BoxDetails{
Info: boxModel.BoxInfo{
Id: "myId",
Name: "myName",
Healthy: true,
},
TemplateInfo: &model.BoxTemplateInfo{
GitTemplate: &model.GitTemplateInfo{
TemplateInfo: &boxModel.BoxTemplateInfo{
GitTemplate: &commonModel.GitTemplateInfo{
Url: "infoUrl",
Revision: "infoRevision",
Commit: "infoCommit",
Name: "infoName",
},
},
ProviderInfo: &model.BoxProviderInfo{
Provider: model.Cloud,
ProviderInfo: &boxModel.BoxProviderInfo{
Provider: boxModel.Cloud,
},
Size: model.Medium,
Env: []model.BoxEnv{
Size: boxModel.Medium,
Env: []boxModel.BoxEnv{
{Key: "KEY_1", Value: "VALUE_1"},
{Key: "KEY_2", Value: "VALUE_2"},
},
Ports: []model.BoxPort{
Ports: []boxModel.BoxPort{
{Alias: "alias-1", Local: "none", Remote: "123", Public: false},
{Alias: "alias-2", Local: "none", Remote: "456", Public: false},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/box/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func toBoxDetails(container docker.ContainerDetails) (*boxModel.BoxDetails, erro

labels := commonModel.Labels(container.Labels)

size, err := labels.ToBoxSize()
size, err := boxModel.ToBoxSize(labels)
if err != nil {
return nil, err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/box/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,8 @@ func (box *KubeBoxClient) connectBox(opts *boxModel.ConnectOptions) error {
}
}

func BoxLabelSelector() string {
// value must be sanitized
return fmt.Sprintf("%s=%s", commonModel.LabelSchemaKind, common.ToKebabCase(schema.KindBoxV1.String()))
}

func boxNameLabelSelector(name string) string {
return fmt.Sprintf("%s,%s=%s", BoxLabelSelector(), kubernetes.LabelKubeName, name)
return fmt.Sprintf("%s,%s=%s", boxModel.BoxLabelSelector(), kubernetes.LabelKubeName, name)
}

func (box *KubeBoxClient) searchBox(name string) (*boxModel.BoxInfo, error) {
Expand Down Expand Up @@ -291,7 +286,7 @@ func ToBoxDetails(deployment *kubernetes.DeploymentDetails, serviceInfo *kuberne

labels := commonModel.Labels(deployment.Annotations)

size, err := labels.ToBoxSize()
size, err := boxModel.ToBoxSize(labels)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -336,7 +331,7 @@ func ToBoxDetails(deployment *kubernetes.DeploymentDetails, serviceInfo *kuberne
func (box *KubeBoxClient) listBoxes() ([]boxModel.BoxInfo, error) {
namespace := box.clientOpts.Namespace

deployments, err := box.client.DeploymentList(namespace, boxModel.BoxPrefixName, BoxLabelSelector())
deployments, err := box.client.DeploymentList(namespace, boxModel.BoxPrefixName, boxModel.BoxLabelSelector())
if err != nil {
return nil, err
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/box/model/labels.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package model

import (
"fmt"
"strings"

"github.com/hckops/hckctl/pkg/client/common"
commonModel "github.com/hckops/hckctl/pkg/common/model"
"github.com/hckops/hckctl/pkg/schema"
)

const (
LabelBoxSize = "com.hckops.box.size"
)

func NewBoxLabels() commonModel.Labels {
return map[string]string{
commonModel.LabelSchemaKind: schema.KindBoxV1.String(),
}
}

func AddBoxSize(labels commonModel.Labels, size ResourceSize) commonModel.Labels {
return labels.AddLabel(LabelBoxSize, strings.ToLower(size.String()))
}

func ToBoxSize(labels commonModel.Labels) (ResourceSize, error) {
if label, err := labels.Exist(LabelBoxSize); err != nil {
return Small, err
} else {
return ExistResourceSize(label)
}
}

func BoxLabelSelector() string {
// value must be sanitized
return fmt.Sprintf("%s=%s", commonModel.LabelSchemaKind, common.ToKebabCase(schema.KindBoxV1.String()))
}
Loading

0 comments on commit e3dfd99

Please sign in to comment.