65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package source
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
_ "encoding/json"
|
||
|
_ "fmt"
|
||
|
_ "gopkg.in/yaml.v3"
|
||
|
"net/url"
|
||
|
_ "path/filepath"
|
||
|
"decl/internal/resource"
|
||
|
_ "os"
|
||
|
_ "io"
|
||
|
"github.com/docker/docker/api/types"
|
||
|
"github.com/docker/docker/client"
|
||
|
"log/slog"
|
||
|
)
|
||
|
|
||
|
type Container struct {
|
||
|
apiClient resource.ContainerClient
|
||
|
}
|
||
|
|
||
|
func NewContainer(containerClientApi resource.ContainerClient) *Container {
|
||
|
var apiClient resource.ContainerClient = containerClientApi
|
||
|
if apiClient == nil {
|
||
|
var err error
|
||
|
apiClient, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
return &Container{
|
||
|
apiClient: apiClient,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
SourceTypes.Register([]string{"container"}, func(u *url.URL) DocSource {
|
||
|
c := NewContainer(nil)
|
||
|
return c
|
||
|
})
|
||
|
|
||
|
}
|
||
|
|
||
|
func (c *Container) Type() string { return "container" }
|
||
|
|
||
|
func (c *Container) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
|
||
|
ctx := context.Background()
|
||
|
slog.Info("container source ExtractResources()", "container", c)
|
||
|
containers, err := c.apiClient.ContainerList(ctx, types.ContainerListOptions{All: true})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
document := resource.NewDocument()
|
||
|
for _, container := range containers {
|
||
|
runningContainer := resource.NewContainer(nil)
|
||
|
runningContainer.Inspect(ctx, container.ID)
|
||
|
document.AddResourceDeclaration("container", runningContainer)
|
||
|
}
|
||
|
|
||
|
return []*resource.Document{document}, nil
|
||
|
}
|