Skip to content

Commit

Permalink
feat: inspect container
Browse files Browse the repository at this point in the history
  • Loading branch information
AmorfEvo committed Oct 5, 2023
1 parent c7d2a60 commit 436c3db
Show file tree
Hide file tree
Showing 20 changed files with 1,314 additions and 770 deletions.
31 changes: 31 additions & 0 deletions golang/internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type (
ContainerCommandFunc func(context.Context, *common.ContainerCommandRequest) error
DeleteContainersFunc func(context.Context, *common.DeleteContainersRequest) error
ContainerLogFunc func(context.Context, *agent.ContainerLogRequest) (*ContainerLogContext, error)
ContainerInspectFunc func(context.Context, *agent.ContainerInspectRequest) (string, error)
ReplaceTokenFunc func(context.Context, *agent.ReplaceTokenRequest) error
)

Expand All @@ -105,6 +106,7 @@ type WorkerFunctions struct {
ContainerCommand ContainerCommandFunc
DeleteContainers DeleteContainersFunc
ContainerLog ContainerLogFunc
ContainerInspect ContainerInspectFunc
}

type contextKey int
Expand Down Expand Up @@ -271,6 +273,8 @@ func (cl *ClientLoop) grpcProcessCommand(command *agent.AgentCommand) {
go executeDeleteMultipleContainers(cl.Ctx, command.GetDeleteContainers(), cl.WorkerFuncs.DeleteContainers)
case command.GetContainerLog() != nil:
go executeContainerLog(cl.Ctx, command.GetContainerLog(), cl.WorkerFuncs.ContainerLog)
case command.GetContainerInspect() != nil:
go executeContainerInspect(cl.Ctx, command.GetContainerInspect(), cl.WorkerFuncs.ContainerInspect)

Check warning on line 277 in golang/internal/grpc/grpc.go

View check run for this annotation

Codecov / codecov/patch

golang/internal/grpc/grpc.go#L276-L277

Added lines #L276 - L277 were not covered by tests
case command.GetReplaceToken() != nil:
// NOTE(@m8vago): should be sync?
err := cl.executeReplaceToken(command.GetReplaceToken())
Expand Down Expand Up @@ -837,6 +841,33 @@ func executeContainerLog(ctx context.Context, command *agent.ContainerLogRequest
log.Trace().Str("prefix", prefix).Str("name", name).Msg("Container log exited")
}

func executeContainerInspect(ctx context.Context, command *agent.ContainerInspectRequest, inspectFunc ContainerInspectFunc) {
if inspectFunc == nil {
log.Error().Msg("Container inspect function not implemented")
return
}

Check warning on line 848 in golang/internal/grpc/grpc.go

View check run for this annotation

Codecov / codecov/patch

golang/internal/grpc/grpc.go#L844-L848

Added lines #L844 - L848 were not covered by tests

prefix := command.Container.Prefix
name := command.Container.Name

log.Info().Str("prefix", prefix).Str("name", name).Msg("Getting container inspection")

inspection, err := inspectFunc(ctx, command)
if err != nil {
log.Error().Stack().Err(err).Msg("Failed to inspect container")
}

Check warning on line 858 in golang/internal/grpc/grpc.go

View check run for this annotation

Codecov / codecov/patch

golang/internal/grpc/grpc.go#L850-L858

Added lines #L850 - L858 were not covered by tests

resp := &common.ContainerInspectMessage{
Inspection: inspection,
}

_, err = grpcConn.Client.ContainerInspect(ctx, resp)
if err != nil {
log.Error().Stack().Err(err).Msg("Container inspection response error")
return
}

Check warning on line 868 in golang/internal/grpc/grpc.go

View check run for this annotation

Codecov / codecov/patch

golang/internal/grpc/grpc.go#L860-L868

Added lines #L860 - L868 were not covered by tests
}

func (cl *ClientLoop) executeReplaceToken(command *agent.ReplaceTokenRequest) error {
log.Debug().Msg("Replace token requested")

Expand Down
1 change: 1 addition & 0 deletions golang/pkg/dagent/dagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Serve(cfg *config.Configuration) {
ContainerCommand: utils.ContainerCommand,
DeleteContainers: utils.DeleteContainers,
ContainerLog: utils.ContainerLog,
ContainerInspect: utils.ContainerInspect,
}, cfg)
}

Expand Down
31 changes: 31 additions & 0 deletions golang/pkg/dagent/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -785,3 +786,33 @@ func ContainerLog(ctx context.Context, request *agent.ContainerLogRequest) (*grp

return logContext, nil
}

func ContainerInspect(ctx context.Context, request *agent.ContainerInspectRequest) (string, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return "", err
}

Check warning on line 794 in golang/pkg/dagent/utils/docker.go

View check run for this annotation

Codecov / codecov/patch

golang/pkg/dagent/utils/docker.go#L790-L794

Added lines #L790 - L794 were not covered by tests

prefix := request.Container.Prefix
name := request.Container.Name

container, err := GetContainerByPrefixAndName(ctx, cli, prefix, name)
if err != nil {
return "", err
}

Check warning on line 802 in golang/pkg/dagent/utils/docker.go

View check run for this annotation

Codecov / codecov/patch

golang/pkg/dagent/utils/docker.go#L796-L802

Added lines #L796 - L802 were not covered by tests

containerInfo, err := cli.ContainerInspect(ctx, container.ID)
if err != nil {
return "", err
}

Check warning on line 807 in golang/pkg/dagent/utils/docker.go

View check run for this annotation

Codecov / codecov/patch

golang/pkg/dagent/utils/docker.go#L804-L807

Added lines #L804 - L807 were not covered by tests

inspectionJSON, err := json.Marshal(containerInfo)
if err != nil {
return "", err
}
inspection := string(inspectionJSON)
// TODO(@amorfevo): maybe this works too
// inspection := fmt.Sprintf("%+v", containerInfo)

return inspection, nil

Check warning on line 817 in golang/pkg/dagent/utils/docker.go

View check run for this annotation

Codecov / codecov/patch

golang/pkg/dagent/utils/docker.go#L809-L817

Added lines #L809 - L817 were not covered by tests
}
1,099 changes: 597 additions & 502 deletions protobuf/go/agent/agent.pb.go

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions protobuf/go/agent/agent_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 436c3db

Please sign in to comment.