Skip to content

Commit

Permalink
feat: support vclusters
Browse files Browse the repository at this point in the history
To make vclusters work properly, we need to get the vcluster pod
name/namespace in some places but keep using the host name/namespace in
others. We solve this by passing the vcluster annotations that contain
the name/namespace to the shim and then use the host/vcluster names
where appropriate.
  • Loading branch information
ctrox committed Jun 22, 2024
1 parent e93d709 commit 3ce27bc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
4 changes: 3 additions & 1 deletion cmd/installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ network-lock skip
"zeropod.ctrox.dev/scaledown-duration",
"zeropod.ctrox.dev/disable-checkpointing",
"zeropod.ctrox.dev/pre-dump",
"io.containerd.runc.v2.group"
"io.containerd.runc.v2.group",
"vcluster.loft.sh/name",
"vcluster.loft.sh/namespace"
]
`
)
Expand Down
46 changes: 40 additions & 6 deletions zeropod/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
PreDumpAnnotationKey = "zeropod.ctrox.dev/pre-dump"
CRIContainerNameAnnotation = "io.kubernetes.cri.container-name"
CRIContainerTypeAnnotation = "io.kubernetes.cri.container-type"
VClusterNameAnnotationKey = "vcluster.loft.sh/name"
VClusterNamespaceAnnotationKey = "vcluster.loft.sh/namespace"

defaultScaleDownDuration = time.Minute
containersDelim = ","
Expand All @@ -43,6 +45,8 @@ type annotationConfig struct {
PodName string `mapstructure:"io.kubernetes.cri.sandbox-name"`
PodNamespace string `mapstructure:"io.kubernetes.cri.sandbox-namespace"`
PodUID string `mapstructure:"io.kubernetes.cri.sandbox-uid"`
VClusterPodName string `mapstructure:"vcluster.loft.sh/name"`
VClusterPodNamespace string `mapstructure:"vcluster.loft.sh/namespace"`
}

type Config struct {
Expand All @@ -53,11 +57,13 @@ type Config struct {
PreDump bool
ContainerName string
ContainerType string
PodName string
PodNamespace string
PodUID string
podName string
podNamespace string
podUID string
ContainerdNamespace string
spec *specs.Spec
vclusterPodName string
vclusterPodNamespace string
}

// NewConfig uses the annotations from the container spec to create a new
Expand Down Expand Up @@ -141,11 +147,13 @@ func NewConfig(ctx context.Context, spec *specs.Spec) (*Config, error) {
ZeropodContainerNames: containerNames,
ContainerName: cfg.ContainerName,
ContainerType: cfg.ContainerType,
PodName: cfg.PodName,
PodNamespace: cfg.PodNamespace,
PodUID: cfg.PodUID,
ContainerdNamespace: ns,
podName: cfg.PodName,
podNamespace: cfg.PodNamespace,
podUID: cfg.PodUID,
spec: spec,
vclusterPodName: cfg.VClusterPodName,
vclusterPodNamespace: cfg.VClusterPodNamespace,
}, nil
}

Expand All @@ -159,3 +167,29 @@ func (cfg Config) IsZeropodContainer() bool {
// if there is none specified, every one of them is considered.
return len(cfg.ZeropodContainerNames) == 0
}

func (cfg Config) HostPodName() string {
return cfg.podName
}

func (cfg Config) HostPodNamespace() string {
return cfg.podNamespace
}

func (cfg Config) HostPodUID() string {
return cfg.podUID
}

func (cfg Config) PodName() string {
if cfg.vclusterPodName != "" {
return cfg.vclusterPodName
}
return cfg.podName
}

func (cfg Config) PodNamespace() string {
if cfg.vclusterPodNamespace != "" {
return cfg.vclusterPodNamespace
}
return cfg.podNamespace
}
6 changes: 3 additions & 3 deletions zeropod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func New(ctx context.Context, cfg *Config, cr *sync.Mutex, container *runc.Conta
return nil, err
}

logPath, err := getLogPath(ctx, cfg)
logPath, err := getLogPath(cfg)
if err != nil {
return nil, fmt.Errorf("unable to get log path: %w", err)
}
Expand Down Expand Up @@ -174,8 +174,8 @@ func (c *Container) Status() *v1.ContainerStatus {
return &v1.ContainerStatus{
Id: c.ID(),
Name: c.cfg.ContainerName,
PodName: c.cfg.PodName,
PodNamespace: c.cfg.PodNamespace,
PodName: c.cfg.PodName(),
PodNamespace: c.cfg.PodNamespace(),
Phase: phase,
}
}
Expand Down
5 changes: 2 additions & 3 deletions zeropod/log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zeropod

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -14,8 +13,8 @@ import (
// containerd only passes that to the sandbox container (pause). One possible
// solution would be to implement log restoring in the sandbox container
// instead of the zeropod.
func getLogPath(ctx context.Context, cfg *Config) (string, error) {
logDir := fmt.Sprintf("/var/log/pods/%s_%s_%s/%s", cfg.PodNamespace, cfg.PodName, cfg.PodUID, cfg.ContainerName)
func getLogPath(cfg *Config) (string, error) {
logDir := fmt.Sprintf("/var/log/pods/%s_%s_%s/%s", cfg.HostPodNamespace(), cfg.HostPodName(), cfg.HostPodUID(), cfg.ContainerName)

dir, err := os.Open(logDir)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions zeropod/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func NewRegistry() *prometheus.Registry {
func (c *Container) labels() map[string]string {
return map[string]string{
labelContainerName: c.cfg.ContainerName,
LabelPodName: c.cfg.PodName,
LabelPodNamespace: c.cfg.PodNamespace,
LabelPodName: c.cfg.PodName(),
LabelPodNamespace: c.cfg.PodNamespace(),
}
}

Expand Down

0 comments on commit 3ce27bc

Please sign in to comment.