Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
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
88 changes: 87 additions & 1 deletion internal/client/docker/capsule_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/rigdev/rig-go-api/api/v1/capsule"
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig/internal/gateway/cluster"
"github.com/rigdev/rig/pkg/api/v1alpha1"
"github.com/rigdev/rig/pkg/errors"
"github.com/rigdev/rig/pkg/iterator"
"go.uber.org/zap"
"golang.org/x/exp/slices"
v1 "k8s.io/api/core/v1"
)

func (c *Client) CreateCapsuleConfig(ctx context.Context, cfg *v1alpha1.Capsule) error {
Expand Down Expand Up @@ -125,6 +127,21 @@ func (c *Client) applyCapsuleConfig(ctx context.Context, capsuleID string) error
return err
}

var cf []*capsule.ConfigFile
for _, f := range cfg.Spec.Files {
if f.ConfigMap != nil {
cm, err := c.GetFile(ctx, capsuleID, f.ConfigMap.Name, cfg.Namespace)
if err != nil {
return err
}

cf = append(cf, &capsule.ConfigFile{
Path: f.Path,
Content: cm.BinaryData[f.ConfigMap.Key],
})
}
}

for i := 0; i < int(cfg.Spec.Replicas); i++ {
containerID := fmt.Sprint(cfg.GetName(), "-instance-", i)

Expand All @@ -135,7 +152,7 @@ func (c *Client) applyCapsuleConfig(ctx context.Context, capsuleID string) error
return err
}

if err := c.createAndStartContainer(ctx, containerID, dcc, dhc, dnc, cfg.Spec.Files); err != nil {
if err := c.createAndStartContainer(ctx, containerID, dcc, dhc, dnc, cf); err != nil {
return err
}

Expand Down Expand Up @@ -228,5 +245,74 @@ func (c *Client) DeleteEnvironmentVariable(ctx context.Context, capsuleID, name
return err
}

return nil
}

func (c *Client) GetFile(ctx context.Context, capsuleID, name, namespace string) (*v1.ConfigMap, error) {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return nil, err
}

for _, f := range fs {
if f.Name == name && f.Namespace == namespace {
return f, nil
}
}

return nil, errors.NotFoundErrorf("file not found")
}

func (c *Client) SetFile(ctx context.Context, capsuleID string, file *v1.ConfigMap) error {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return err
}

found := false
for i, f := range fs {
if f.Name == file.Name && f.Namespace == file.Namespace {
fs[i] = file
found = true
break
}
}
if !found {
fs = append(fs, file)
}

if err := c.rcc.SetFiles(ctx, capsuleID, fs); err != nil {
return err
}

return c.applyCapsuleConfig(ctx, capsuleID)
}

func (c *Client) ListFiles(ctx context.Context, capsuleID string, pagination *model.Pagination) (iterator.Iterator[*v1.ConfigMap], int64, error) {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return nil, 0, err
}

return iterator.FromList(fs), int64(len(fs)), nil
}

func (c *Client) DeleteFile(ctx context.Context, capsuleID, name, namespace string) error {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return err
}

for i, f := range fs {
if f.Name == name && f.Namespace == namespace {
fs = append(fs[:i], fs[i+1:]...)
break
}
}

if err := c.rcc.SetFiles(ctx, capsuleID, fs); err != nil {
return err
}

return nil
}
17 changes: 8 additions & 9 deletions internal/client/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/rigdev/rig/internal/config"
"github.com/rigdev/rig/internal/gateway/cluster"
"github.com/rigdev/rig/internal/repository"
"github.com/rigdev/rig/pkg/api/v1alpha1"
"github.com/rigdev/rig/pkg/auth"
"github.com/rigdev/rig/pkg/errors"
"github.com/rigdev/rig/pkg/iterator"
Expand Down Expand Up @@ -58,8 +57,8 @@ func New(cfg config.Config, logger *zap.Logger, rcc repository.ClusterConfig) (*
}, nil
}

func (c *Client) Logs(ctx context.Context, capsuleID string, instanceID string, follow bool) (iterator.Iterator[*capsule.Log], error) {
c.logger.Debug("reading docker logs", zap.String("deployment_id", capsuleID), zap.String("instance_id", instanceID))
func (c *Client) Logs(ctx context.Context, capsuleName string, instanceID string, follow bool) (iterator.Iterator[*capsule.Log], error) {
c.logger.Debug("reading docker logs", zap.String("deployment_id", capsuleName), zap.String("instance_id", instanceID))

ls, err := c.dc.ContainerLogs(ctx, instanceID, types.ContainerLogsOptions{
ShowStdout: true,
Expand Down Expand Up @@ -181,7 +180,7 @@ func (c *Client) copyFileToContainer(ctx context.Context, containerID string, fi
return c.dc.CopyToContainer(ctx, containerID, dir, bufio.NewReader(&buffer), types.CopyToContainerOptions{})
}

func (c *Client) createAndStartContainer(ctx context.Context, containerID string, cc *container.Config, hc *container.HostConfig, nc *network.NetworkingConfig, files []v1alpha1.File) error {
func (c *Client) createAndStartContainer(ctx context.Context, containerID string, cc *container.Config, hc *container.HostConfig, nc *network.NetworkingConfig, configFiles []*capsule.ConfigFile) error {
id, err := c.lookupContainer(ctx, containerID)
if errors.IsNotFound(err) {
// Already ready to create.
Expand All @@ -199,11 +198,11 @@ func (c *Client) createAndStartContainer(ctx context.Context, containerID string
return err
}

// for _, f := range files {
// if err := c.copyFileToContainer(ctx, containerID, f); err != nil {
// return err
// }
// }
for _, f := range configFiles {
if err := c.copyFileToContainer(ctx, containerID, f); err != nil {
return err
}
}

if err := c.dc.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {
c.logger.Info("error starting container", zap.Error(err), zap.String("instance_id", containerID))
Expand Down
87 changes: 87 additions & 0 deletions internal/client/k8s/capsule_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig/internal/gateway/cluster"
"github.com/rigdev/rig/pkg/api/v1alpha1"
"github.com/rigdev/rig/pkg/errors"
"github.com/rigdev/rig/pkg/iterator"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
)

func (c *Client) CreateCapsuleConfig(ctx context.Context, cfg *v1alpha1.Capsule) error {
Expand Down Expand Up @@ -92,6 +94,21 @@ func (c *Client) applyCapsuleConfig(ctx context.Context, capsuleID string) error
network.Interfaces = append(network.Interfaces, netIf)
}

var cf []*capsule.ConfigFile
for _, f := range cfg.Spec.Files {
if f.ConfigMap != nil {
cm, err := c.GetFile(ctx, capsuleID, f.ConfigMap.Name, cfg.Namespace)
if err != nil {
return err
}

cf = append(cf, &capsule.ConfigFile{
Path: f.Path,
Content: cm.BinaryData[f.ConfigMap.Key],
})
}
}

return c.upsertCapsule(ctx, cfg.GetName(), &cluster.Capsule{
CapsuleID: cfg.GetName(),
Image: cfg.Spec.Image,
Expand All @@ -102,6 +119,7 @@ func (c *Client) applyCapsuleConfig(ctx context.Context, capsuleID string) error
Replicas: uint32(cfg.Spec.Replicas),
Namespace: cfg.GetNamespace(),
RegistryAuth: auth,
ConfigFiles: cf,
})
}

Expand Down Expand Up @@ -171,3 +189,72 @@ func (c *Client) DeleteEnvironmentVariable(ctx context.Context, capsuleID, name

return c.applyCapsuleConfig(ctx, capsuleID)
}

func (c *Client) GetFile(ctx context.Context, capsuleID, name, namespace string) (*v1.ConfigMap, error) {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return nil, err
}

for _, f := range fs {
if f.Name == name && f.Namespace == namespace {
return f, nil
}
}

return nil, errors.NotFoundErrorf("file not found")
}

func (c *Client) SetFile(ctx context.Context, capsuleID string, file *v1.ConfigMap) error {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return err
}

found := false
for i, f := range fs {
if f.Name == file.Name && f.Namespace == file.Namespace {
fs[i] = file
found = true
break
}
}
if !found {
fs = append(fs, file)
}

if err := c.rcc.SetFiles(ctx, capsuleID, fs); err != nil {
return err
}

return nil
}

func (c *Client) ListFiles(ctx context.Context, capsuleID string, pagination *model.Pagination) (iterator.Iterator[*v1.ConfigMap], int64, error) {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return nil, 0, err
}

return iterator.FromList(fs), int64(len(fs)), nil
}

func (c *Client) DeleteFile(ctx context.Context, capsuleID, name, namespace string) error {
fs, err := c.rcc.GetFiles(ctx, capsuleID)
if err != nil {
return err
}

for i, f := range fs {
if f.Name == name && f.Namespace == namespace {
fs = append(fs[:i], fs[i+1:]...)
break
}
}

if err := c.rcc.SetFiles(ctx, capsuleID, fs); err != nil {
return err
}

return nil
}
6 changes: 6 additions & 0 deletions internal/gateway/cluster/config_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig/pkg/api/v1alpha1"
"github.com/rigdev/rig/pkg/iterator"
v1 "k8s.io/api/core/v1"
)

type ConfigGateway interface {
Expand All @@ -24,4 +25,9 @@ type ConfigGateway interface {
SetEnvironmentVariable(ctx context.Context, capsuleID, name, value string) error
GetEnvironmentVariable(ctx context.Context, capsuleID, name string) (value string, ok bool, err error)
DeleteEnvironmentVariable(ctx context.Context, capsuleID, name string) error

GetFile(ctx context.Context, capsuleID, name, namespace string) (*v1.ConfigMap, error)
SetFile(ctx context.Context, capsuleID string, file *v1.ConfigMap) error
ListFiles(ctx context.Context, capsuleID string, pagination *model.Pagination) (iterator.Iterator[*v1.ConfigMap], int64, error)
DeleteFile(ctx context.Context, capsuleID, name, namespace string) error
}
4 changes: 4 additions & 0 deletions internal/repository/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rigdev/rig-go-api/model"
"github.com/rigdev/rig/pkg/api/v1alpha1"
"github.com/rigdev/rig/pkg/iterator"
v1 "k8s.io/api/core/v1"
)

type ClusterConfig interface {
Expand All @@ -18,5 +19,8 @@ type ClusterConfig interface {
SetEnvironmentVariables(ctx context.Context, capsuleID string, envs map[string]string) error
GetEnvironmentVariables(ctx context.Context, capsuleID string) (map[string]string, error)

SetFiles(ctx context.Context, capsuleID string, files []*v1.ConfigMap) error
GetFiles(ctx context.Context, capsuleID string) ([]*v1.ConfigMap, error)

BuildIndexes(ctx context.Context) error
}
2 changes: 1 addition & 1 deletion internal/repository/cluster_config/mongo/build_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (r *MongoRepository) BuildIndexes(ctx context.Context) error {
capsuleConfigIndexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "project_id", Value: 1},
{Key: "name", Value: 1},
{Key: "capsule_id", Value: 1},
},
Options: options.Index().SetUnique(true),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *MongoRepository) DeleteCapsuleConfig(ctx context.Context, capsuleID str
return err
}

filter := bson.M{"project_id": projectID, "name": capsuleID}
filter := bson.M{"project_id": projectID, "capsule_id": capsuleID}
result, err := c.CapsuleConfigCol.DeleteOne(ctx, filter)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *MongoRepository) GetCapsuleConfig(ctx context.Context, capsuleID string
}

cp := schema.CapsuleConfig{}
filter := bson.M{"project_id": projectID, "name": capsuleID}
filter := bson.M{"project_id": projectID, "capsule_id": capsuleID}
if err := c.CapsuleConfigCol.FindOne(ctx, filter).Decode(&cp); err == mongo.ErrNoDocuments {
return nil, errors.NotFoundErrorf("capsule not found")
} else if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (r *MongoRepository) GetEnvironmentVariables(ctx context.Context, capsuleID
}

cp := schema.CapsuleConfig{}
filter := bson.M{"project_id": projectID, "name": capsuleID}
filter := bson.M{"project_id": projectID, "capsule_id": capsuleID}
if err := r.CapsuleConfigCol.FindOne(ctx, filter).Decode(&cp); err == mongo.ErrNoDocuments {
return nil, errors.NotFoundErrorf("capsule not found")
} else if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions internal/repository/cluster_config/mongo/get_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mongo

import (
"context"

"github.com/rigdev/rig/internal/repository/cluster_config/mongo/schema"
"github.com/rigdev/rig/pkg/auth"
"github.com/rigdev/rig/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
v1 "k8s.io/api/core/v1"
)

func (r *MongoRepository) GetFiles(ctx context.Context, capsuleID string) ([]*v1.ConfigMap, error) {
projectID, err := auth.GetProjectID(ctx)
if err != nil {
return nil, err
}

cp := schema.CapsuleConfig{}
filter := bson.M{"project_id": projectID, "capsule_id": capsuleID}
if err := r.CapsuleConfigCol.FindOne(ctx, filter).Decode(&cp); err == mongo.ErrNoDocuments {
return nil, errors.NotFoundErrorf("capsule not found")
} else if err != nil {
return nil, err
}

return cp.Files, nil
}
Loading