2024-05-06 00:48:54 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2024-07-17 08:34:57 +00:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2024-05-06 00:48:54 +00:00
|
|
|
_ "github.com/docker/docker/api/types/strslice"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
_ "log/slog"
|
|
|
|
"net/url"
|
|
|
|
_ "os"
|
|
|
|
_ "os/exec"
|
|
|
|
_ "strings"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"gitea.rosskeen.house/rosskeen.house/machine"
|
2024-05-14 18:26:05 +00:00
|
|
|
"decl/internal/codec"
|
2024-09-19 08:11:57 +00:00
|
|
|
"decl/internal/data"
|
|
|
|
"decl/internal/folio"
|
2024-07-17 08:34:57 +00:00
|
|
|
"log/slog"
|
|
|
|
"time"
|
2024-05-06 00:48:54 +00:00
|
|
|
)
|
|
|
|
|
2024-09-19 08:11:57 +00:00
|
|
|
const (
|
|
|
|
ContainerNetworkTypeName TypeName = "container-network"
|
|
|
|
)
|
|
|
|
|
2024-05-06 00:48:54 +00:00
|
|
|
type ContainerNetworkClient interface {
|
|
|
|
ContainerClient
|
2024-07-17 08:34:57 +00:00
|
|
|
NetworkCreate(ctx context.Context, name string, options network.CreateOptions) (network.CreateResponse, error)
|
|
|
|
NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
|
|
|
|
NetworkInspect(ctx context.Context, networkID string, options network.InspectOptions) (network.Inspect, error)
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerNetwork struct {
|
2024-10-16 17:26:42 +00:00
|
|
|
*Common `json:",inline" yaml:",inline"`
|
|
|
|
stater machine.Stater `json:"-" yaml:"-"`
|
|
|
|
Id string `json:"ID,omitempty" yaml:"ID,omitempty"`
|
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
Driver string `json:"driver,omitempty" yaml:"driver,omitempty"`
|
|
|
|
EnableIPv6 bool `json:"enableipv6,omitempty" yaml:"enableipv6,omitempty"`
|
|
|
|
Internal bool `json:"internal,omitempty" yaml:"internal,omitempty"`
|
|
|
|
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
|
|
|
|
Created time.Time `json:"created" yaml:"created"`
|
|
|
|
|
|
|
|
apiClient ContainerNetworkClient
|
|
|
|
Resources data.ResourceMapper `json:"-" yaml:"-"`
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2024-10-16 17:26:42 +00:00
|
|
|
folio.DocumentRegistry.ResourceTypes.Register([]string{"container-network"}, func(u *url.URL) (n data.Resource) {
|
|
|
|
n = NewContainerNetwork(nil)
|
|
|
|
if u != nil {
|
|
|
|
if err := folio.CastParsedURI(u).ConstructResource(n); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2024-05-06 00:48:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-09 22:26:39 +00:00
|
|
|
func NewContainerNetwork(containerClientApi ContainerNetworkClient) (cn *ContainerNetwork) {
|
2024-05-06 00:48:54 +00:00
|
|
|
var apiClient ContainerNetworkClient = containerClientApi
|
|
|
|
if apiClient == nil {
|
|
|
|
var err error
|
|
|
|
apiClient, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 22:26:39 +00:00
|
|
|
cn = &ContainerNetwork{
|
2024-05-06 00:48:54 +00:00
|
|
|
apiClient: apiClient,
|
|
|
|
}
|
2024-10-09 22:26:39 +00:00
|
|
|
cn.Common = NewCommon(ContainerNetworkTypeName, true)
|
|
|
|
cn.Common.NormalizePath = cn.NormalizePath
|
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
2024-10-16 17:26:42 +00:00
|
|
|
func (n *ContainerNetwork) Init(u data.URIParser) error {
|
|
|
|
if u == nil {
|
|
|
|
u = folio.URI(n.URI()).Parse()
|
|
|
|
}
|
|
|
|
return n.SetParsedURI(u)
|
|
|
|
}
|
|
|
|
|
2024-10-09 22:26:39 +00:00
|
|
|
func (n *ContainerNetwork) NormalizePath() error {
|
|
|
|
return nil
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 08:11:57 +00:00
|
|
|
func (n *ContainerNetwork) SetResourceMapper(resources data.ResourceMapper) {
|
2024-07-17 08:34:57 +00:00
|
|
|
n.Resources = resources
|
|
|
|
}
|
|
|
|
|
2024-09-19 08:11:57 +00:00
|
|
|
func (n *ContainerNetwork) Clone() data.Resource {
|
2024-05-06 00:48:54 +00:00
|
|
|
return &ContainerNetwork {
|
2024-11-10 18:24:06 +00:00
|
|
|
Common: n.Common.Clone(),
|
2024-05-06 00:48:54 +00:00
|
|
|
Id: n.Id,
|
|
|
|
Name: n.Name,
|
|
|
|
apiClient: n.apiClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) StateMachine() machine.Stater {
|
2024-05-09 07:39:45 +00:00
|
|
|
if n.stater == nil {
|
|
|
|
n.stater = StorageMachine(n)
|
|
|
|
}
|
|
|
|
return n.stater
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) Notify(m *machine.EventMessage) {
|
|
|
|
ctx := context.Background()
|
2024-07-17 08:34:57 +00:00
|
|
|
slog.Info("Notify()", "ContainerNetwork", n, "m", m)
|
2024-05-09 07:39:45 +00:00
|
|
|
switch m.On {
|
|
|
|
case machine.ENTERSTATEEVENT:
|
|
|
|
switch m.Dest {
|
2024-07-17 08:34:57 +00:00
|
|
|
case "start_read":
|
|
|
|
if _,readErr := n.Read(ctx); readErr == nil {
|
|
|
|
if triggerErr := n.StateMachine().Trigger("state_read"); triggerErr == nil {
|
|
|
|
return
|
|
|
|
} else {
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "absent"
|
2024-07-17 08:34:57 +00:00
|
|
|
panic(triggerErr)
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "absent"
|
2024-07-17 08:34:57 +00:00
|
|
|
panic(readErr)
|
|
|
|
}
|
|
|
|
case "start_delete":
|
|
|
|
if deleteErr := n.Delete(ctx); deleteErr == nil {
|
|
|
|
if triggerErr := n.StateMachine().Trigger("deleted"); triggerErr == nil {
|
|
|
|
return
|
|
|
|
} else {
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "present"
|
2024-07-17 08:34:57 +00:00
|
|
|
panic(triggerErr)
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "present"
|
2024-07-17 08:34:57 +00:00
|
|
|
panic(deleteErr)
|
|
|
|
}
|
2024-05-09 07:39:45 +00:00
|
|
|
case "start_create":
|
2024-05-13 17:13:20 +00:00
|
|
|
if e := n.Create(ctx); e == nil {
|
2024-07-17 08:34:57 +00:00
|
|
|
if triggerErr := n.StateMachine().Trigger("created"); triggerErr == nil {
|
2024-05-13 17:13:20 +00:00
|
|
|
return
|
2024-05-13 05:41:12 +00:00
|
|
|
}
|
2024-05-13 17:13:20 +00:00
|
|
|
}
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "absent"
|
2024-07-17 08:34:57 +00:00
|
|
|
case "absent":
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "absent"
|
2024-07-17 08:34:57 +00:00
|
|
|
case "present", "created", "read":
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "present"
|
2024-05-09 07:39:45 +00:00
|
|
|
}
|
|
|
|
case machine.EXITSTATEEVENT:
|
|
|
|
}
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) URI() string {
|
2024-05-24 05:11:51 +00:00
|
|
|
return fmt.Sprintf("container-network://%s", n.Name)
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) JSON() ([]byte, error) {
|
|
|
|
return json.Marshal(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) Validate() error {
|
|
|
|
return fmt.Errorf("failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) Apply() error {
|
|
|
|
ctx := context.Background()
|
2024-09-19 08:11:57 +00:00
|
|
|
switch n.Common.State {
|
2024-05-06 00:48:54 +00:00
|
|
|
case "absent":
|
|
|
|
return n.Delete(ctx)
|
|
|
|
case "present":
|
|
|
|
return n.Create(ctx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 08:11:57 +00:00
|
|
|
func (n *ContainerNetwork) Load(docData []byte, f codec.Format) (err error) {
|
|
|
|
err = f.StringDecoder(string(docData)).Decode(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) LoadReader(r io.ReadCloser, f codec.Format) (err error) {
|
|
|
|
err = f.Decoder(r).Decode(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) LoadString(docData string, f codec.Format) (err error) {
|
|
|
|
err = f.StringDecoder(docData).Decode(n)
|
|
|
|
return
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) LoadDecl(yamlResourceDeclaration string) error {
|
2024-09-19 08:11:57 +00:00
|
|
|
return n.LoadString(yamlResourceDeclaration, codec.FormatYaml)
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ContainerNetwork) Create(ctx context.Context) error {
|
2024-07-17 08:34:57 +00:00
|
|
|
networkResp, err := n.apiClient.NetworkCreate(ctx, n.Name, network.CreateOptions{
|
2024-05-06 00:48:54 +00:00
|
|
|
Driver: "bridge",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
n.Id = networkResp.ID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 08:34:57 +00:00
|
|
|
func (n *ContainerNetwork) Inspect(ctx context.Context, networkID string) error {
|
|
|
|
networkInspect, err := n.apiClient.NetworkInspect(ctx, networkID, network.InspectOptions{})
|
|
|
|
if client.IsErrNotFound(err) {
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "absent"
|
2024-07-17 08:34:57 +00:00
|
|
|
} else {
|
2024-09-19 08:11:57 +00:00
|
|
|
n.Common.State = "present"
|
2024-07-17 08:34:57 +00:00
|
|
|
n.Id = networkInspect.ID
|
|
|
|
if n.Name == "" {
|
|
|
|
if networkInspect.Name[0] == '/' {
|
|
|
|
n.Name = networkInspect.Name[1:]
|
|
|
|
} else {
|
|
|
|
n.Name = networkInspect.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.Created = networkInspect.Created
|
|
|
|
n.Internal = networkInspect.Internal
|
|
|
|
n.Driver = networkInspect.Driver
|
|
|
|
n.Labels = networkInspect.Labels
|
|
|
|
n.EnableIPv6 = networkInspect.EnableIPv6
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-06 00:48:54 +00:00
|
|
|
|
|
|
|
func (n *ContainerNetwork) Read(ctx context.Context) ([]byte, error) {
|
2024-07-17 08:34:57 +00:00
|
|
|
var networkID string
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("name", n.Name)
|
|
|
|
networks, err := n.apiClient.NetworkList(ctx, network.ListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %s %s", err, n.Type(), n.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, net := range networks {
|
|
|
|
if net.Name == n.Name {
|
|
|
|
networkID = net.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inspectErr := n.Inspect(ctx, networkID); inspectErr != nil {
|
|
|
|
return nil, fmt.Errorf("%w: network %s", inspectErr, networkID)
|
|
|
|
}
|
|
|
|
slog.Info("Read() ", "type", n.Type(), "name", n.Name, "Id", n.Id)
|
|
|
|
|
2024-05-06 00:48:54 +00:00
|
|
|
return yaml.Marshal(n)
|
|
|
|
}
|
|
|
|
|
2024-09-19 08:11:57 +00:00
|
|
|
func (n *ContainerNetwork) Update(ctx context.Context) error {
|
|
|
|
return n.Create(ctx)
|
|
|
|
}
|
|
|
|
|
2024-05-06 00:48:54 +00:00
|
|
|
func (n *ContainerNetwork) Delete(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-24 05:11:51 +00:00
|
|
|
func (n *ContainerNetwork) Type() string { return "container-network" }
|
2024-05-06 00:48:54 +00:00
|
|
|
|
|
|
|
func (n *ContainerNetwork) ResolveId(ctx context.Context) string {
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("name", "/"+n.Name)
|
|
|
|
containers, err := n.apiClient.ContainerList(ctx, container.ListOptions{
|
|
|
|
All: true,
|
|
|
|
Filters: filterArgs,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("%w: %s %s", err, n.Type(), n.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range containers {
|
|
|
|
for _, containerName := range container.Names {
|
|
|
|
if containerName == n.Name {
|
|
|
|
if n.Id == "" {
|
|
|
|
n.Id = container.ID
|
|
|
|
}
|
|
|
|
return container.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|