Skip to content

Commit

Permalink
update task model
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Sep 11, 2023
1 parent ac984f1 commit f5ca7f5
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 44 deletions.
2 changes: 1 addition & 1 deletion internal/command/box/flag/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type TunnelFlag struct {
func (f *TunnelFlag) ToConnectOptions(template *boxModel.BoxV1, name string, temporary bool) *boxModel.ConnectOptions {
return &boxModel.ConnectOptions{
Template: template,
StreamOpts: commonModel.NewStreamOpts(true),
StreamOpts: commonModel.NewStdStreamOpts(true),
Name: name,
DisableExec: f.NoExec,
DisableTunnel: f.NoTunnel,
Expand Down
7 changes: 4 additions & 3 deletions internal/command/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ func runTask(sourceLoader template.SourceLoader[taskModel.TaskV1], provider task
return err
}

createOpts := &taskModel.CreateOptions{
runOpts := &taskModel.RunOptions{
Template: &info.Value.Data,
Parameters: commonModel.Parameters{}, // TODO common model
Parameters: commonModel.Parameters{}, // TODO []string rename commands
Labels: commonCmd.AddTemplateLabels[taskModel.TaskV1](info, labels),
StreamOpts: commonModel.NewStdStreamOpts(false),
}

return taskClient.Run(createOpts)
return taskClient.Run(runOpts)
}

func newDefaultTaskClient(provider taskModel.TaskProvider, configRef *config.ConfigRef, loader *commonCmd.Loader) (task.TaskClient, error) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/box/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"golang.org/x/exp/slices"

boxModel "github.com/hckops/hckctl/pkg/box/model"
"github.com/hckops/hckctl/pkg/client/common"
"github.com/hckops/hckctl/pkg/client/kubernetes"
commonModel "github.com/hckops/hckctl/pkg/common/model"
"github.com/hckops/hckctl/pkg/schema"
Expand Down Expand Up @@ -100,7 +99,7 @@ func newResources(namespace string, name string, opts *boxModel.CreateOptions) *
Name: name,
Annotations: opts.Labels,
Labels: kubernetes.BuildLabels(name, opts.Template.Image.Repository, opts.Template.Image.ResolveVersion(),
map[string]string{commonModel.LabelSchemaKind: common.ToKebabCase(schema.KindBoxV1.String())}),
map[string]string{commonModel.LabelSchemaKind: util.ToLowerKebabCase(schema.KindBoxV1.String())}),
Ports: ports,
PodInfo: &kubernetes.PodInfo{
Namespace: namespace,
Expand Down Expand Up @@ -178,7 +177,7 @@ func (box *KubeBoxClient) execBox(template *boxModel.BoxV1, info *boxModel.BoxIn
// exec
opts := &kubernetes.PodExecOpts{
Namespace: box.clientOpts.Namespace,
PodName: common.ToKebabCase(template.Image.Repository), // pod.Spec.Containers[0].Name
PodName: util.ToLowerKebabCase(template.Image.Repository), // pod.Spec.Containers[0].Name
PodId: info.Id,
Shell: template.Shell,
InStream: streamOpts.In,
Expand Down
2 changes: 1 addition & 1 deletion pkg/box/model/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func SortEnv(env []BoxEnv) []BoxEnv {
}

func (box *BoxV1) GenerateName() string {
return fmt.Sprintf("%s%s-%s", BoxPrefixName, box.Name, util.RandomAlphanumeric(5))
return fmt.Sprintf("%s%s-%s", BoxPrefixName, util.ToLowerKebabCase(box.Name), util.RandomAlphanumeric(5))
}

// ToBoxTemplateName returns the strictly validated template name, or the original trimmed name
Expand Down
4 changes: 2 additions & 2 deletions pkg/box/model/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"strings"

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

const (
Expand All @@ -33,5 +33,5 @@ func ToBoxSize(labels commonModel.Labels) (ResourceSize, error) {

func BoxLabelSelector() string {
// value must be sanitized
return fmt.Sprintf("%s=%s", commonModel.LabelSchemaKind, common.ToKebabCase(schema.KindBoxV1.String()))
return fmt.Sprintf("%s=%s", commonModel.LabelSchemaKind, util.ToLowerKebabCase(schema.KindBoxV1.String()))
}
8 changes: 0 additions & 8 deletions pkg/client/common/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"regexp"
"strings"
)

Expand All @@ -12,10 +11,3 @@ func DefaultShell(command string) string {
return "/bin/bash"
}
}

// matches anything other than a letter, digit or underscore, equivalent to "[^a-zA-Z0-9_]"
var anyNonWordCharacterRegex = regexp.MustCompile(`\W+`)

func ToKebabCase(value string) string {
return anyNonWordCharacterRegex.ReplaceAllString(value, "-")
}
4 changes: 0 additions & 4 deletions pkg/client/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ func TestDefaultShell(t *testing.T) {

assert.Equal(t, "foo", DefaultShell("foo"))
}

func TestToKebabCase(t *testing.T) {
assert.Equal(t, "hckops-my-test_value-example", ToKebabCase("hckops/my-test_value$example"))
}
6 changes: 3 additions & 3 deletions pkg/client/kubernetes/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/hckops/hckctl/pkg/client/common"
"github.com/hckops/hckctl/pkg/util"
)

func BuildResources(opts *ResourcesOpts) (*appsv1.Deployment, *corev1.Service, error) {
Expand Down Expand Up @@ -42,7 +42,7 @@ func BuildLabels(name, instance, version string, extra map[string]string) map[st
// default
labels := map[string]string{
LabelKubeName: name,
LabelKubeInstance: common.ToKebabCase(instance),
LabelKubeInstance: util.ToLowerKebabCase(instance),
LabelKubeVersion: version,
LabelKubeManagedBy: "hckops", // TODO common?
}
Expand All @@ -62,7 +62,7 @@ func buildPod(objectMeta metav1.ObjectMeta, podInfo *PodInfo, ports []KubePort)
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: common.ToKebabCase(podInfo.ContainerName),
Name: util.ToLowerKebabCase(podInfo.ContainerName),
Image: podInfo.ImageName,
ImagePullPolicy: corev1.PullIfNotPresent,
TTY: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type StreamOptions struct {
IsTty bool // tty is false for ssh tunnel or logs
}

func NewStreamOpts(tty bool) *StreamOptions {
func NewStdStreamOpts(tty bool) *StreamOptions {
return &StreamOptions{
In: os.Stdin,
Out: os.Stdout,
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type TaskClient interface {
Provider() model.TaskProvider
Events() *event.EventBus
Run(opts *model.CreateOptions) error
Run(opts *model.RunOptions) error
}

func NewTaskClient(opts *model.TaskClientOptions) (TaskClient, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func (task *DockerTaskClient) Events() *event.EventBus {
return task.eventBus
}

func (task *DockerTaskClient) Run(opts *taskModel.CreateOptions) error {
func (task *DockerTaskClient) Run(opts *taskModel.RunOptions) error {
return task.runTask(opts)
}
22 changes: 8 additions & 14 deletions pkg/task/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (task *DockerTaskClient) close() error {
return task.client.Close()
}

func (task *DockerTaskClient) runTask(opts *taskModel.CreateOptions) error {
func (task *DockerTaskClient) runTask(opts *taskModel.RunOptions) error {

imageName := opts.Template.Image.Name()
imagePullOpts := &docker.ImagePullOpts{
Expand Down Expand Up @@ -64,18 +64,16 @@ func (task *DockerTaskClient) runTask(opts *taskModel.CreateOptions) error {
}

// taskName
// TODO containerName := opts.Template.GenerateName()
containerName := "task-whalesay-12345"
containerName := opts.Template.GenerateName()

// TODO tty false?
containerConfig, err := docker.BuildContainerConfig(&docker.ContainerConfigOpts{
ImageName: imageName,
ContainerName: containerName,
//Env: containerEnv,
//Ports: containerPorts,
Labels: opts.Labels,
Tty: false,
Cmd: []string{"cowsay", "hello world"},
Env: []docker.ContainerEnv{},
Ports: []docker.ContainerPort{},
Labels: opts.Labels,
Tty: opts.StreamOpts.IsTty,
Cmd: []string{"cowsay", "hello world"}, // TODO parameters vs command
})
if err != nil {
return err
Expand All @@ -87,8 +85,7 @@ func (task *DockerTaskClient) runTask(opts *taskModel.CreateOptions) error {
return err
}

// TODO networkName := box.clientOpts.NetworkName
networkName := "hckops" // TODO unique network?
networkName := task.clientOpts.NetworkName
networkId, err := task.client.NetworkUpsert(networkName)
if err != nil {
return err
Expand Down Expand Up @@ -118,9 +115,6 @@ func (task *DockerTaskClient) runTask(opts *taskModel.CreateOptions) error {
}
// TODO box.eventBus.Publish(newContainerCreateDockerEvent(opts.Template.Name, containerName, containerId))

// TODO background task
//return &boxModel.BoxInfo{Id: containerId, Name: containerName, Healthy: true}, nil

if err := task.client.ContainerLogsStd(containerId); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/task/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func NewCommonTaskOpts() *CommonTaskOptions {
}
}

type CreateOptions struct {
type RunOptions struct {
Template *TaskV1
Parameters map[string]string
Labels commonModel.Labels
StreamOpts *commonModel.StreamOptions
}
11 changes: 11 additions & 0 deletions pkg/task/model/task.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package model

import (
"fmt"

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

const (
TagPrefixName = "task-"
)

type TaskV1 struct {
Expand All @@ -10,3 +17,7 @@ type TaskV1 struct {
Tags []string
Image commonModel.Image
}

func (task *TaskV1) GenerateName() string {
return fmt.Sprintf("%s%s-%s", TagPrefixName, util.ToLowerKebabCase(task.Name), util.RandomAlphanumeric(5))
}
17 changes: 17 additions & 0 deletions pkg/task/model/task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package model

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerateName(t *testing.T) {
task := &TaskV1{
Name: "my-name",
}
taskId := task.GenerateName()
assert.True(t, strings.HasPrefix(taskId, "task-my-name-"))
assert.Equal(t, 18, len(taskId))
}
10 changes: 9 additions & 1 deletion pkg/util/helper.go → pkg/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package util

import (
"fmt"
"github.com/pkg/errors"
"regexp"
"strings"

"github.com/dchest/uniuri"
"github.com/pkg/errors"
)

func IotaToValues[T comparable](kv map[T]string) []string {
Expand Down Expand Up @@ -41,3 +42,10 @@ func SplitKeyValue(kv string) (string, string, error) {
}
return "", "", errors.New("invalid key-value pair")
}

// matches anything other than a letter, digit or underscore, equivalent to "[^a-zA-Z0-9_]"
var anyNonWordCharacterRegex = regexp.MustCompile(`\W+`)

func ToLowerKebabCase(value string) string {
return anyNonWordCharacterRegex.ReplaceAllString(strings.ToLower(strings.TrimSpace(value)), "-")
}
11 changes: 11 additions & 0 deletions pkg/util/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestToLowerKebabCase(t *testing.T) {
assert.Equal(t, "hckops-my-test_value-example", ToLowerKebabCase(" hCKops/my-tEst_value$ExamplE\t"))
}

0 comments on commit f5ca7f5

Please sign in to comment.