Skip to content

Commit

Permalink
update task kube events
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Oct 8, 2023
1 parent db10632 commit bf635f7
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 35 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ provider:

### Cloud

Access to the platform is in ***preview*** and limited. If you are interested, please leave a comment or a :thumbsup: to this [issue](https://github.com/hckops/hckctl/issues/104) and we'll reach out with more details
Access to the platform is limited and in ***preview***. If you are interested, please leave a comment or a :thumbsup: to this [issue](https://github.com/hckops/hckctl/issues/104) and we'll reach out with more details
```bash
provider:
cloud:
Expand Down Expand Up @@ -275,17 +275,20 @@ tail -F ${HOME}/.local/state/hck/log/hckctl-*.log
<!--
* rename `template` to catalog? or alias?
* cmd aliases e.g. start/up/create
* task kube log file
* task kube vpn
* task kube shareDir ? copy dir?
* task kube interrupt
* replace task/htb example with thm
* verify/support kube config relative path
* update cloud
* update platform prs/dependencies
TODO
* priority
- add task providers: kube and cloud
- add box kube --network-vpn
- add box kube shareDir
- add task cloud
- debug `htb-postman`
- lab inputs
- add flow example
Expand Down Expand Up @@ -313,6 +316,8 @@ TODO
* https://houdini.secsi.io
- flaky tests (?)
* kubernetes_test.go:TestNewResources
- rename `template` to catalog? or alias?
- cmd aliases e.g. start/up/create
* cli
- autocomplete commands and values
* e.g. `box connect <list of boxes>` with `ValidArgsFunction`
Expand Down
16 changes: 0 additions & 16 deletions pkg/box/docker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,10 @@ func (e *dockerBoxEvent) String() string {
return e.value
}

func newImagePullDockerEvent(imageName string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogInfo, value: fmt.Sprintf("image pull: imageName=%s", imageName)}
}

func newImagePullDockerLoaderEvent(imageName string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LoaderUpdate, value: fmt.Sprintf("pulling image %s", imageName)}
}

func newImagePullIgnoreDockerEvent(imageName string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogWarning, value: fmt.Sprintf("image pull ignored: imageName=%s", imageName)}
}

func newImageRemoveDockerEvent(imageId string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogInfo, value: fmt.Sprintf("image remove: imageId=%s", imageId)}
}

func newImageRemoveIgnoreDockerEvent(imageId string, err error) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogWarning, value: fmt.Sprintf("image remove ignored: imageId=%s error=%v", imageId, err)}
}

func newNetworkUpsertDockerEvent(networkName string, networkId string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogInfo, value: fmt.Sprintf("network upsert: networkName=%s networkId=%s", networkName, networkId)}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (client *KubeClient) podDescribe(namespace string, listOptions metav1.ListO

func newPodInfo(namespace string, pods *corev1.PodList) (*PodInfo, error) {
if len(pods.Items) != SingleReplica {
return nil, fmt.Errorf("found %d pods, expected only 1 pod for deployment: namespace=%s", len(pods.Items), namespace)
return nil, fmt.Errorf("found %d pods, expected only 1 pod: namespace=%s", len(pods.Items), namespace)
}

podItem := pods.Items[0]
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/kubernetes/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestNewPodInfoErrorReplica(t *testing.T) {
}
result, err := newPodInfo("myPodNamespace", pods)

assert.EqualError(t, err, "found 2 pods, expected only 1 pod for deployment: namespace=myPodNamespace")
assert.EqualError(t, err, "found 2 pods, expected only 1 pod: namespace=myPodNamespace")
assert.Nil(t, result)
}

Expand Down
16 changes: 9 additions & 7 deletions pkg/task/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func (task *DockerTaskClient) close() error {

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

// pull image
imageName := opts.Template.Image.Name()
if err := task.docker.PullImageOffline(imageName, func() {
task.eventBus.Publish(newImagePullDockerLoaderEvent(imageName))
}); err != nil {
return err
}

// taskName
containerName := opts.Template.GenerateName()

Expand All @@ -48,13 +56,6 @@ func (task *DockerTaskClient) runTask(opts *taskModel.RunOptions) error {
networkMode = docker.DefaultNetworkMode()
}

imageName := opts.Template.Image.Name()
if err := task.docker.PullImageOffline(imageName, func() {
task.eventBus.Publish(newImagePullDockerLoaderEvent(imageName))
}); err != nil {
return err
}

containerConfig, err := docker.BuildContainerConfig(&docker.ContainerConfigOpts{
ImageName: imageName,
Hostname: "", // vpn NetworkMode conflicts with Hostname containerName
Expand Down Expand Up @@ -131,5 +132,6 @@ func (task *DockerTaskClient) runTask(opts *taskModel.RunOptions) error {
task.eventBus.Publish(newContainerLogDockerConsoleEvent(logFileName))

// remove temporary container
task.eventBus.Publish(newContainerRemoveDockerEvent(containerId))
return task.docker.Client.ContainerRemove(containerId)
}
4 changes: 4 additions & 0 deletions pkg/task/docker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ func newContainerCreateDockerLoaderEvent() *dockerTaskEvent {
func newContainerWaitDockerLoaderEvent() *dockerTaskEvent {
return &dockerTaskEvent{kind: event.LoaderStop, value: "waiting"}
}

func newContainerRemoveDockerEvent(containerId string) *dockerTaskEvent {
return &dockerTaskEvent{kind: event.LogInfo, value: fmt.Sprintf("container remove: containerId=%s", containerId)}
}
28 changes: 28 additions & 0 deletions pkg/task/kubernetes/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@ func newCloseKubeClientEvent() *kubeTaskEvent {
func newNamespaceApplyKubeEvent(namespace string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LogInfo, value: fmt.Sprintf("namespace apply: namespace=%s", namespace)}
}

func newJobCreateStatusKubeEvent(status string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LogDebug, value: status}
}

func newJobCreateKubeEvent(namespace string, name string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LogInfo, value: fmt.Sprintf("job create: namespace=%s name=%s", namespace, name)}
}

func newJobDeleteKubeEvent(namespace string, name string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LogInfo, value: fmt.Sprintf("job delete: namespace=%s name=%s", namespace, name)}
}

func newPodNameKubeEvent(namespace string, podName string, containerName string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LogInfo, value: fmt.Sprintf("found unique pod: namespace=%s podName=%s containerName=%s", namespace, podName, containerName)}
}

func newPodLogKubeEvent(logFileName string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LogInfo, value: fmt.Sprintf("pod log: logFileName=%s", logFileName)}
}

func newPodLogKubeConsoleEvent(logFileName string) *kubeTaskEvent {
return &kubeTaskEvent{kind: event.PrintConsole, value: fmt.Sprintf("output file: %s", logFileName)}
}

func newContainerWaitKubeLoaderEvent() *kubeTaskEvent {
return &kubeTaskEvent{kind: event.LoaderStop, value: "waiting"}
}
20 changes: 14 additions & 6 deletions pkg/task/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func (task *KubeTaskClient) close() error {
return task.client.Close()
}

func (task *KubeTaskClient) runTask(opts *taskModel.RunOptions) error {
// TODO copy vpn https://github.com/goccy/kubejob/blob/master/copy.go

func (task *KubeTaskClient) runTask(opts *taskModel.RunOptions) error {
namespace := task.clientOpts.Namespace

// create namespace
Expand Down Expand Up @@ -63,31 +64,38 @@ func (task *KubeTaskClient) runTask(opts *taskModel.RunOptions) error {
Namespace: namespace,
Spec: jobSpec,
OnStatusEventCallback: func(event string) {
// TODO task.eventBus.Publish(newDeploymentCreateStatusKubeEvent(event))
task.eventBus.Publish(newJobCreateStatusKubeEvent(event))
},
}
err := task.client.JobCreate(jobOpts)
if err != nil {
return err
}
// TODO task.eventBus.Publish
task.eventBus.Publish(newJobCreateKubeEvent(namespace, jobName))

podInfo, err := task.client.PodDescribeFromJob(jobSpec)
if err != nil {
return err
}
// TODO task.eventBus.Publish(newPodNameKubeEvent(namespace, podInfo.PodName, podInfo.ContainerName))
task.eventBus.Publish(newPodNameKubeEvent(namespace, podInfo.PodName, podInfo.ContainerName))

// stop loader
task.eventBus.Publish(newContainerWaitKubeLoaderEvent())

// TODO tee
logFileName := "TODO_LOG_FILE_NAME"

logOpts := &kubernetes.PodLogOpts{
Namespace: namespace,
PodName: podInfo.PodName,
PodId: podInfo.ContainerName,
}
// TODO task.eventBus.Publish
task.eventBus.Publish(newPodLogKubeEvent(logFileName))
if err := task.client.PodLog(logOpts); err != nil {
return err
}

// TODO task.eventBus.Publish
task.eventBus.Publish(newPodLogKubeConsoleEvent(logFileName))
task.eventBus.Publish(newJobDeleteKubeEvent(namespace, jobName))
return task.client.JobDelete(namespace, jobName)
}

0 comments on commit bf635f7

Please sign in to comment.