Skip to content

Commit

Permalink
cleanup imports
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Oct 7, 2023
1 parent e5f5128 commit 1ef2c63
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ TODO
- filter/list box (list and delete) and template (list and validate) columns by provider + sorting
- flaky issue zerolog `could not write event: write /home/ubuntu/.local/state/hck/hckctl-ubuntu.log: file already closed`
* lab
- `command` cli description and example
- in `create` add override e.g. `--input alias=parrot --input password=changeme --input vpn=htb-eu`
- inputs should look for HCK_LAB_??? env var override if --input is not present before using default
- verify optional merge/overrides
Expand Down
1 change: 1 addition & 0 deletions internal/command/box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package box

import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down
2 changes: 1 addition & 1 deletion internal/command/config/model_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package config

import (
"github.com/hckops/hckctl/pkg/util"
"testing"

"github.com/stretchr/testify/assert"

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

func TestNewConfig(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/box/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (box *DockerBoxClient) createBox(opts *boxModel.CreateOptions) (*boxModel.B
var hostname string
var networkMode string
if opts.NetworkInfo.Vpn != nil {
// set all networks configs on the sidecar to avoid options conflicts
if sidecarContainerId, err := box.docker.StartVpnSidecar(containerName, opts.NetworkInfo.Vpn, portConfig); err != nil {
// set all network configs on the sidecar to avoid option conflicts
if sidecarContainerId, err := box.docker.StartSidecarVpn(containerName, opts.NetworkInfo.Vpn, portConfig); err != nil {
return nil, err
} else {
// fix conflicting options: hostname and the network mode
Expand Down Expand Up @@ -130,8 +130,8 @@ func (box *DockerBoxClient) createBox(opts *boxModel.CreateOptions) (*boxModel.B
HostConfig: hostConfig,
NetworkingConfig: docker.BuildNetworkingConfig(networkName, networkId), // all on the same network
WaitStatus: false,
CaptureInterrupt: false, // TODO ???
OnContainerInterruptCallback: func(string) {}, // TODO ???
CaptureInterrupt: false,
OnContainerInterruptCallback: func(string) {},
OnContainerCreateCallback: func(string) error { return nil },
OnContainerWaitCallback: func(string) error { return nil },
OnContainerStatusCallback: func(status string) {
Expand Down Expand Up @@ -199,6 +199,7 @@ func (box *DockerBoxClient) execBox(template *boxModel.BoxV1, info *boxModel.Box
}
}

// restart main container
restartsOpts := &docker.ContainerRestartOpts{
ContainerId: info.Id,
OnRestartCallback: func(status string) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package docker

import (
"fmt"
"github.com/docker/docker/api/types/mount"
"strings"

"github.com/pkg/errors"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/docker/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package docker

import (
"github.com/docker/docker/api/types/mount"
"testing"

"github.com/stretchr/testify/assert"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker
import (
"context"
"fmt"
"github.com/hckops/hckctl/pkg/util"
"io"
"os"
"os/signal"
Expand All @@ -23,6 +22,7 @@ import (
"golang.org/x/exp/maps"

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

func NewDockerClient() (*DockerClient, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/docker/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"

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

Expand Down
16 changes: 8 additions & 8 deletions pkg/common/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package docker

import (
"fmt"
"github.com/hckops/hckctl/pkg/schema"
"strings"

"github.com/pkg/errors"

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

type DockerCommonClient struct {
Expand Down Expand Up @@ -73,7 +73,7 @@ func (common *DockerCommonClient) PullImageOffline(imageName string, onImagePull
return nil
}

func buildVpnSidecarName(containerName string) string {
func buildSidecarVpnName(containerName string) string {
// expect valid name always
tokens := strings.Split(containerName, "-")
return fmt.Sprintf("%svpn-%s", commonModel.SidecarPrefixName, tokens[len(tokens)-1])
Expand Down Expand Up @@ -104,19 +104,19 @@ func (common *DockerCommonClient) GetSidecars(containerName string) ([]commonMod
return sidecars, nil
}

func (common *DockerCommonClient) StartVpnSidecar(mainContainerName string, vpnInfo *commonModel.VpnNetworkInfo, portConfig *docker.ContainerPortConfigOpts) (string, error) {
func (common *DockerCommonClient) StartSidecarVpn(mainContainerName string, vpnInfo *commonModel.VpnNetworkInfo, portConfig *docker.ContainerPortConfigOpts) (string, error) {

// sidecarName
containerName := buildVpnSidecarName(mainContainerName)
containerName := buildSidecarVpnName(mainContainerName)

// constants
imageName := commonModel.SidecarVpnImageName
// base directory "/usr/share" must exist
vpnConfigPath := "/usr/share/client.ovpn"

if err := common.PullImageOffline(imageName, func() {
common.eventBus.Publish(newVpnSidecarConnectDockerEvent(vpnInfo.Name))
common.eventBus.Publish(newVpnSidecarConnectDockerLoaderEvent(vpnInfo.Name))
common.eventBus.Publish(newSidecarVpnConnectDockerEvent(vpnInfo.Name))
common.eventBus.Publish(newSidecarVpnConnectDockerLoaderEvent(vpnInfo.Name))
}); err != nil {
return "", err
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (common *DockerCommonClient) StartVpnSidecar(mainContainerName string, vpnI
return common.Client.CopyFileToContainer(containerId, vpnInfo.LocalPath, vpnConfigPath)
},
OnContainerStatusCallback: func(status string) {
common.eventBus.Publish(newVpnSidecarCreateStatusDockerEvent(status))
common.eventBus.Publish(newSidecarVpnCreateStatusDockerEvent(status))
},
OnContainerStartCallback: func() {},
}
Expand All @@ -159,7 +159,7 @@ func (common *DockerCommonClient) StartVpnSidecar(mainContainerName string, vpnI
if err != nil {
return "", err
}
common.eventBus.Publish(newVpnSidecarCreateDockerEvent(containerName, containerId))
common.eventBus.Publish(newSidecarVpnCreateDockerEvent(containerName, containerId))

return containerId, nil
}
4 changes: 2 additions & 2 deletions pkg/common/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBuildVpnSidecarName(t *testing.T) {
func TestBuildSidecarVpnName(t *testing.T) {
expected := "sidecar-vpn-12345"
assert.Equal(t, expected, buildVpnSidecarName("aaa-bbb-ccc-ddd-12345"))
assert.Equal(t, expected, buildSidecarVpnName("aaa-bbb-ccc-ddd-12345"))
}
9 changes: 5 additions & 4 deletions pkg/common/docker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"fmt"

"github.com/hckops/hckctl/pkg/common/model"
"github.com/hckops/hckctl/pkg/event"
)
Expand Down Expand Up @@ -47,18 +48,18 @@ func newImageRemoveIgnoreDockerEvent(imageId string, err error) *dockerCommonEve
return &dockerCommonEvent{kind: event.LogWarning, value: fmt.Sprintf("image remove ignored: imageId=%s error=%v", imageId, err)}
}

func newVpnSidecarCreateDockerEvent(containerName string, containerId string) *dockerCommonEvent {
func newSidecarVpnCreateDockerEvent(containerName string, containerId string) *dockerCommonEvent {
return &dockerCommonEvent{kind: event.LogInfo, value: fmt.Sprintf("sidecar-vpn create: containerName=%s containerId=%s", containerName, containerId)}
}

func newVpnSidecarCreateStatusDockerEvent(status string) *dockerCommonEvent {
func newSidecarVpnCreateStatusDockerEvent(status string) *dockerCommonEvent {
return &dockerCommonEvent{kind: event.LogDebug, value: status}
}

func newVpnSidecarConnectDockerEvent(vpnName string) *dockerCommonEvent {
func newSidecarVpnConnectDockerEvent(vpnName string) *dockerCommonEvent {
return &dockerCommonEvent{kind: event.LogInfo, value: fmt.Sprintf("sidecar-vpn connect: vpnName=%s", vpnName)}
}

func newVpnSidecarConnectDockerLoaderEvent(vpnName string) *dockerCommonEvent {
func newSidecarVpnConnectDockerLoaderEvent(vpnName string) *dockerCommonEvent {
return &dockerCommonEvent{kind: event.LoaderUpdate, value: fmt.Sprintf("connecting to %s", vpnName)}
}
3 changes: 2 additions & 1 deletion pkg/common/model/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package model

import (
"fmt"
"github.com/hckops/hckctl/pkg/schema"
"path/filepath"
"strings"

"golang.org/x/exp/maps"

"github.com/hckops/hckctl/pkg/schema"
)

type Labels map[string]string
Expand Down
10 changes: 5 additions & 5 deletions pkg/lab/cloud/cloud.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cloud

import (
model2 "github.com/hckops/hckctl/pkg/common/model"
"github.com/pkg/errors"

v1 "github.com/hckops/hckctl/pkg/api/v1"
"github.com/hckops/hckctl/pkg/client/ssh"
"github.com/hckops/hckctl/pkg/lab/model"
commonModel "github.com/hckops/hckctl/pkg/common/model"
labModel "github.com/hckops/hckctl/pkg/lab/model"
)

func newCloudLabClient(commonOpts *model.CommonLabOptions, cloudOpts *model2.CloudOptions) (*CloudLabClient, error) {
func newCloudLabClient(commonOpts *labModel.CommonLabOptions, cloudOpts *commonModel.CloudOptions) (*CloudLabClient, error) {
commonOpts.EventBus.Publish(newInitCloudClientEvent())

clientConfig := &ssh.SshClientConfig{
Expand All @@ -29,7 +29,7 @@ func newCloudLabClient(commonOpts *model.CommonLabOptions, cloudOpts *model2.Clo
}, nil
}

func (lab *CloudLabClient) createLab(opts *model.CreateOptions) (*model.LabInfo, error) {
func (lab *CloudLabClient) createLab(opts *labModel.CreateOptions) (*labModel.LabInfo, error) {
lab.eventBus.Publish(newApiCreateCloudLoaderEvent(lab.clientOpts.Address, opts.LabTemplate.Name))

request := v1.NewLabCreateRequest(lab.clientOpts.Version, opts.LabTemplate.Name, opts.Parameters)
Expand All @@ -49,5 +49,5 @@ func (lab *CloudLabClient) createLab(opts *model.CreateOptions) (*model.LabInfo,
labName := response.Body.Name
lab.eventBus.Publish(newApiCreateCloudEvent(opts.LabTemplate.Name, labName))

return &model.LabInfo{Id: labName, Name: labName}, nil
return &labModel.LabInfo{Id: labName, Name: labName}, nil
}
1 change: 1 addition & 0 deletions pkg/lab/cloud/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloud

import (
"fmt"

"github.com/hckops/hckctl/pkg/event"
"github.com/hckops/hckctl/pkg/lab/model"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (task *DockerTaskClient) runTask(opts *taskModel.RunOptions) error {
// vpn sidecar
var networkMode string
if opts.NetworkInfo.Vpn != nil {
if sidecarContainerId, err := task.docker.StartVpnSidecar(containerName, opts.NetworkInfo.Vpn, &docker.ContainerPortConfigOpts{}); err != nil {
if sidecarContainerId, err := task.docker.StartSidecarVpn(containerName, opts.NetworkInfo.Vpn, &docker.ContainerPortConfigOpts{}); err != nil {
return err
} else {
networkMode = docker.ContainerNetworkMode(sidecarContainerId)
Expand Down

0 comments on commit 1ef2c63

Please sign in to comment.