Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ replace (

require (
github.com/beevik/etree v1.5.0
github.com/containerd/errdefs v1.0.0
github.com/distribution/reference v0.6.0
github.com/docker/docker v28.5.2+incompatible
github.com/docker/go-connections v0.5.0
github.com/getkin/kin-openapi v0.131.0
github.com/jstemmer/go-junit-report/v2 v2.1.0
github.com/kubesmarts/logic-operator/api v0.0.0
github.com/kubesmarts/logic-operator/workflowproj v0.0.0
github.com/moby/moby/api v1.55.0
github.com/moby/moby/client v0.5.0
github.com/ory/viper v1.7.5
github.com/spf13/afero v1.12.0
github.com/spf13/cobra v1.9.1
Expand All @@ -30,15 +31,16 @@ require (
k8s.io/client-go v0.31.6
)

replace github.com/moby/moby => github.com/moby/moby/api v1.55.0

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/docker/go-connections v0.7.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down Expand Up @@ -76,18 +78,15 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/spdystream v0.4.0 // indirect
github.com/moby/sys/atomicwriter v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pb33f/libopenapi v0.10.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
Expand All @@ -110,28 +109,23 @@ require (
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
golang.org/x/crypto v0.51.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.35.0 // indirect
golang.org/x/net v0.55.0 // indirect
golang.org/x/oauth2 v0.35.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/term v0.43.0 // indirect
golang.org/x/text v0.37.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.44.0 // indirect
golang.org/x/time v0.11.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
Expand Down
82 changes: 30 additions & 52 deletions cli/go.sum

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions cli/pkg/common/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package common
import (
"context"
"fmt"
"github.com/docker/docker/api/types/container"
"os"
"path/filepath"
"regexp"
Expand All @@ -31,7 +30,7 @@ import (

"github.com/kubesmarts/logic-operator/cli/pkg/metadata"
apiMetadata "github.com/kubesmarts/logic-operator/api/metadata"
"github.com/docker/docker/client"
"github.com/moby/moby/client"
)

func CheckJavaDependencies() error {
Expand Down Expand Up @@ -94,12 +93,12 @@ func checkMaven() error {

func CheckDocker() error {
fmt.Println("✅ Checking if Docker is available...")
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
cli, err := client.New(client.FromEnv)
if err != nil {
fmt.Println("Error creating docker client")
return err
}
_, err = cli.ContainerList(context.Background(), container.ListOptions{})
_, err = cli.ContainerList(context.Background(), client.ContainerListOptions{})
if err != nil {
fmt.Println("ERROR: Docker not found.")
fmt.Println("Download from https://docs.docker.com/get-docker/")
Expand Down
88 changes: 49 additions & 39 deletions cli/pkg/common/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/distribution/reference"
"github.com/docker/docker/api/types/image"
"io"
"net/netip"
"os"
"os/exec"
"os/signal"
Expand All @@ -38,10 +37,12 @@ import (
"time"

"github.com/kubesmarts/logic-operator/cli/pkg/metadata"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
"github.com/distribution/reference"
"github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/api/pkg/stdcopy"
"github.com/moby/moby/client"
)

const (
Expand All @@ -55,11 +56,11 @@ type DockerLogMessage struct {
}

type DockerClient interface {
ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
ImageList(ctx context.Context, options client.ImageListOptions) (client.ImageListResult, error)
}

func getDockerClient() (*client.Client, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
cli, err := client.New(client.FromEnv)
if err != nil {
return nil, fmt.Errorf("failed to create Docker client: %s", err)
}
Expand Down Expand Up @@ -102,12 +103,12 @@ func getDockerContainerID() (string, error) {
return "", err
}

containers, err := cli.ContainerList(context.Background(), container.ListOptions{})
result, err := cli.ContainerList(context.Background(), client.ContainerListOptions{})
if err != nil {
return "", err
}

for _, c := range containers {
for _, c := range result.Items {
// Check if the container has the expected image name or other identifying information
if strings.Contains(c.Image, metadata.DevModeImage) {
return c.ID, nil
Expand All @@ -130,7 +131,8 @@ func StopContainer(containerTool string, containerID string) error {
fmt.Printf("unable to create client for docker")
return err
}
if err := cli.ContainerStop(context.Background(), containerID, container.StopOptions{}); err != nil {
_, err = cli.ContainerStop(context.Background(), containerID, client.ContainerStopOptions{})
if err != nil {
fmt.Printf("Unable to stop container %s: %s", containerID, err)
return err
}
Expand Down Expand Up @@ -257,12 +259,12 @@ func CheckImageExists(cli DockerClient, ctx context.Context, imageName string) (
} else {
imageName = fmt.Sprintf("%s:%s", reference.Path(named), "latest")
}
images, err := cli.ImageList(ctx, image.ListOptions{All: true})
result, err := cli.ImageList(ctx, client.ImageListOptions{All: true})
if err != nil {
return false, fmt.Errorf("error listing images: %s", err)
}

for _, i := range images {
for _, i := range result.Items {
for _, tag := range i.RepoTags {
if strings.HasSuffix(tag, imageName) {
return true, nil
Expand Down Expand Up @@ -297,37 +299,43 @@ func waitToImageBeReady(reader io.ReadCloser) error {
return nil
}

func createDockerContainer(cli *client.Client, ctx context.Context, portMapping string, path string) (container.CreateResponse, error) {
containerConfig := &container.Config{
Image: metadata.DevModeImage,
}
hostConfig := &container.HostConfig{
AutoRemove: true,
PortBindings: nat.PortMap{
metadata.DockerInternalPort: []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: portMapping,
func createDockerContainer(cli *client.Client, ctx context.Context, portMapping string, path string) (client.ContainerCreateResult, error) {
port := network.MustParsePort(string(metadata.DockerInternalPort))
hostIP := netip.MustParseAddr("0.0.0.0")
createOpts := client.ContainerCreateOptions{
Config: &container.Config{
Image: metadata.DevModeImage,
ExposedPorts: network.PortSet{port: struct{}{}},
},
HostConfig: &container.HostConfig{
AutoRemove: true,
PortBindings: network.PortMap{
port: []network.PortBinding{
{
HostIP: hostIP,
HostPort: portMapping,
},
},
},
},
Binds: []string{
fmt.Sprintf("%s:%s", path, metadata.VolumeBindPath),
Binds: []string{
fmt.Sprintf("%s:%s", path, metadata.VolumeBindPath),
},
},
}

resp, err := cli.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, "")
resp, err := cli.ContainerCreate(ctx, createOpts)
if err != nil {
return resp, fmt.Errorf("\nUnable to create container %s: %s", metadata.DevModeImage, err)
}
return resp, nil
}

func startDockerContainer(cli *client.Client, ctx context.Context, resp container.CreateResponse) error {
func startDockerContainer(cli *client.Client, ctx context.Context, resp client.ContainerCreateResult) error {
fmt.Printf("\nCreated container with ID %s", resp.ID)
fmt.Println("\n⏳ Starting your container and SonataFlow project...")

if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
_, err := cli.ContainerStart(ctx, resp.ID, client.ContainerStartOptions{})
if err != nil {
return fmt.Errorf("\nUnable to start container %s", resp.ID)
}

Expand Down Expand Up @@ -362,28 +370,30 @@ func runDockerContainer(portMapping string, path string) error {

return nil
}
func processOutputDuringContainerExecution(cli *client.Client, ctx context.Context, resp container.CreateResponse) error {
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
func processOutputDuringContainerExecution(cli *client.Client, ctx context.Context, resp client.ContainerCreateResult) error {
waitResult := cli.ContainerWait(ctx, resp.ID, client.ContainerWaitOptions{
Condition: container.WaitConditionNotRunning,
})

//Print all container logs
out, err := cli.ContainerLogs(ctx, resp.ID, container.LogsOptions{ShowStdout: false, ShowStderr: true, Follow: true})
logsResult, err := cli.ContainerLogs(ctx, resp.ID, client.ContainerLogsOptions{ShowStdout: false, ShowStderr: true, Follow: true})
if err != nil {
return fmt.Errorf("\nError getting container logs: %s", err)
}

go func() {
_, err := stdcopy.StdCopy(os.Stdout, os.Stderr, out)
_, err := stdcopy.StdCopy(os.Stdout, os.Stderr, logsResult)
if err != nil {
fmt.Errorf("\nError copying container logs to stdout: %s", err)
}
}()

select {
case err := <-errCh:
case err := <-waitResult.Error:
if err != nil {
return fmt.Errorf("\nError starting the container %s: %s", resp.ID, err)
}
case <-statusCh:
case <-waitResult.Result:
//state of the container matches the condition, in our case WaitConditionNotRunning
}

Expand Down Expand Up @@ -412,14 +422,14 @@ func IsContainerRunning(containerID string) (bool, error) {
if err != nil {
return false, fmt.Errorf("unable to create docker client: %w", err)
}
containerJSON, err := cli.ContainerInspect(context.Background(), containerID)
result, err := cli.ContainerInspect(context.Background(), containerID, client.ContainerInspectOptions{})
if err != nil {
if client.IsErrNotFound(err) {
if errdefs.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("unable to inspect container %s with docker: %w", containerID, err)
}
return containerJSON.State.Running, nil
return result.Container.State.Running, nil

} else if errPodman := CheckPodman(); errPodman == nil {
cmd := exec.Command("podman", "inspect", containerID, "--format", "{{.State.Running}}")
Expand Down
19 changes: 11 additions & 8 deletions cli/pkg/common/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ package common

import (
"context"
"github.com/docker/docker/api/types/image"
"testing"

"github.com/moby/moby/api/types/image"
"github.com/moby/moby/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"testing"
)

type MockDockerClient struct {
mock.Mock
}

func (m *MockDockerClient) ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error) {
func (m *MockDockerClient) ImageList(ctx context.Context, options client.ImageListOptions) (client.ImageListResult, error) {
args := m.Called(ctx, options)
return args.Get(0).([]image.Summary), args.Error(1)
return args.Get(0).(client.ImageListResult), args.Error(1)
}

func TestCheckImageExists(t *testing.T) {
Expand Down Expand Up @@ -62,9 +63,11 @@ func TestCheckImageExists(t *testing.T) {
ctx := context.Background()
mockClient := new(MockDockerClient)

mockClient.On("ImageList", ctx, mock.Anything).Return([]image.Summary{
{
RepoTags: test.images,
mockClient.On("ImageList", ctx, mock.Anything).Return(client.ImageListResult{
Items: []image.Summary{
{
RepoTags: test.images,
},
},
}, nil)

Expand Down
Loading
Loading