Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
persistence: store configuration in newstore
Browse files Browse the repository at this point in the history
Fixes #803

Store the configuration data in persist.json.

Signed-off-by: Wei Zhang <weizhang555.zw@gmail.com>
  • Loading branch information
WeiZhang555 committed Nov 11, 2019
1 parent 3e3e923 commit aeae231
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 31 deletions.
24 changes: 24 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/kata-containers/runtime/pkg/rootless"
"github.com/kata-containers/runtime/pkg/signals"
vc "github.com/kata-containers/runtime/virtcontainers"
exp "github.com/kata-containers/runtime/virtcontainers/experimental"
vf "github.com/kata-containers/runtime/virtcontainers/factory"
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -346,6 +347,11 @@ func beforeSubcommands(c *cli.Context) error {
"arguments": `"` + args + `"`,
}

err = addExpFeatures(c, runtimeConfig)
if err != nil {
return err
}

kataLog.WithFields(fields).Info()

// make the data accessible to the sub-commands.
Expand Down Expand Up @@ -401,6 +407,24 @@ func setupTracing(context *cli.Context, rootSpanName string) error {
return nil
}

// add supported experimental features in context
func addExpFeatures(clictx *cli.Context, runtimeConfig oci.RuntimeConfig) error {
ctx, err := cliContextToContext(clictx)
if err != nil {
return err
}

var exps []string
for _, e := range runtimeConfig.Experimental {
exps = append(exps, e.Name)
}

ctx = exp.ContextWithExp(ctx, exps)
// Add tracer to metadata and update the context
clictx.App.Metadata["context"] = ctx
return nil
}

func afterSubcommands(c *cli.Context) error {
ctx, err := cliContextToContext(c)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func (c *Container) storeContainer() error {
if err := c.sandbox.Save(); err != nil {
return err
}
return nil
}
return c.store.Store(store.Configuration, *(c.config))
}
Expand Down
17 changes: 17 additions & 0 deletions virtcontainers/experimental/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package experimental

import (
"context"
"fmt"
"regexp"
)
Expand All @@ -22,8 +23,11 @@ type Feature struct {
ExpRelease string
}

type contextKey struct{}

var (
supportedFeatures = make(map[string]Feature)
expContextKey = contextKey{}
)

// Register register a new experimental feature
Expand Down Expand Up @@ -61,3 +65,16 @@ func validateFeature(feature Feature) error {

return nil
}

func ContextWithExp(ctx context.Context, names []string) context.Context {
return context.WithValue(ctx, expContextKey, names)
}

func ExpFromContext(ctx context.Context) []string {
value := ctx.Value(expContextKey)
if value == nil {
return nil
}
names := value.([]string)
return names
}
164 changes: 164 additions & 0 deletions virtcontainers/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kata-containers/runtime/virtcontainers/persist"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/mitchellh/mapstructure"
)

var (
Expand Down Expand Up @@ -171,6 +172,86 @@ func (s *Sandbox) dumpNetwork(ss *persistapi.SandboxState) {
}
}

func (s *Sandbox) dumpConfig(ss *persistapi.SandboxState) {
sconfig := s.config
ss.Config = persistapi.SandboxConfig{
HypervisorType: string(sconfig.HypervisorType),
AgentType: string(sconfig.AgentType),
ProxyType: string(sconfig.ProxyType),
ProxyConfig: persistapi.ProxyConfig{
Path: sconfig.ProxyConfig.Path,
Debug: sconfig.ProxyConfig.Debug,
},
ShimType: string(sconfig.ShimType),
NetworkConfig: persistapi.NetworkConfig{
NetNSPath: sconfig.NetworkConfig.NetNSPath,
NetNsCreated: sconfig.NetworkConfig.NetNsCreated,
DisableNewNetNs: sconfig.NetworkConfig.DisableNewNetNs,
InterworkingModel: int(sconfig.NetworkConfig.InterworkingModel),
},

ShmSize: sconfig.ShmSize,
SharePidNs: sconfig.SharePidNs,
Stateful: sconfig.Stateful,
SystemdCgroup: sconfig.SystemdCgroup,
SandboxCgroupOnly: sconfig.SandboxCgroupOnly,
DisableGuestSeccomp: sconfig.DisableGuestSeccomp,
}

for _, e := range sconfig.Experimental {
ss.Config.Experimental = append(ss.Config.Experimental, e.Name)
}

ss.Config.HypervisorConfig = persistapi.HypervisorConfig{
NumVCPUs: sconfig.HypervisorConfig.NumVCPUs,
DefaultMaxVCPUs: sconfig.HypervisorConfig.DefaultMaxVCPUs,
MemorySize: sconfig.HypervisorConfig.MemorySize,
MemSlots: sconfig.HypervisorConfig.MemSlots,
KernelPath: sconfig.HypervisorConfig.KernelPath,
ImagePath: sconfig.HypervisorConfig.ImagePath,
InitrdPath: sconfig.HypervisorConfig.InitrdPath,
SharedFS: sconfig.HypervisorConfig.SharedFS,
VirtioFSDaemon: sconfig.HypervisorConfig.VirtioFSDaemon,
DisableBlockDeviceUse: sconfig.HypervisorConfig.DisableBlockDeviceUse,
UseVSock: sconfig.HypervisorConfig.UseVSock,
DisableVhostNet: sconfig.HypervisorConfig.DisableVhostNet,
}

if sconfig.AgentType == "kata" {
var sagent KataAgentConfig
err := mapstructure.Decode(sconfig.AgentConfig, &sagent)
if err != nil {
s.Logger().WithError(err).Error("internal error: KataAgentConfig failed to decode")
} else {
ss.Config.KataAgentConfig = &persistapi.KataAgentConfig{
LongLiveConn: sagent.LongLiveConn,
UseVSock: sagent.UseVSock,
}
}
}

if sconfig.ShimType == "kataShim" {
var shim ShimConfig
err := mapstructure.Decode(sconfig.ShimConfig, &shim)
if err != nil {
s.Logger().WithError(err).Error("internal error: ShimConfig failed to decode")
} else {
ss.Config.KataShimConfig = &persistapi.ShimConfig{
Path: shim.Path,
Debug: shim.Debug,
}
}
}

for _, contConf := range sconfig.Containers {
ss.Config.ContainerConfigs = append(ss.Config.ContainerConfigs, persistapi.ContainerConfig{
ID: contConf.ID,
Annotations: contConf.Annotations,
Resources: contConf.Resources,
})
}
}

func (s *Sandbox) Save() error {
var (
ss = persistapi.SandboxState{}
Expand All @@ -185,6 +266,7 @@ func (s *Sandbox) Save() error {
s.dumpMounts(cs)
s.dumpAgent(&ss)
s.dumpNetwork(&ss)
s.dumpConfig(&ss)

if err := s.newStore.ToDisk(ss, cs); err != nil {
return err
Expand Down Expand Up @@ -335,3 +417,85 @@ func (s *Sandbox) supportNewStore() bool {
}
return false
}

func loadSandboxConfig(id string) (*SandboxConfig, error) {
store, err := persist.GetDriver("fs")
if err != nil || store == nil {
return nil, errors.New("failed to get fs persist driver")
}

ss, _, err := store.FromDisk(id)
if err != nil {
return nil, err
}

savedConf := ss.Config
sconfig := &SandboxConfig{
ID: id,
HypervisorType: HypervisorType(savedConf.HypervisorType),
AgentType: AgentType(savedConf.AgentType),
ProxyType: ProxyType(savedConf.ProxyType),
ProxyConfig: ProxyConfig{
Path: savedConf.ProxyConfig.Path,
Debug: savedConf.ProxyConfig.Debug,
},
ShimType: ShimType(savedConf.ShimType),
NetworkConfig: NetworkConfig{
NetNSPath: savedConf.NetworkConfig.NetNSPath,
NetNsCreated: savedConf.NetworkConfig.NetNsCreated,
DisableNewNetNs: savedConf.NetworkConfig.DisableNewNetNs,
InterworkingModel: NetInterworkingModel(savedConf.NetworkConfig.InterworkingModel),
},

ShmSize: savedConf.ShmSize,
SharePidNs: savedConf.SharePidNs,
Stateful: savedConf.Stateful,
SystemdCgroup: savedConf.SystemdCgroup,
SandboxCgroupOnly: savedConf.SandboxCgroupOnly,
DisableGuestSeccomp: savedConf.DisableGuestSeccomp,
}

for _, name := range savedConf.Experimental {
sconfig.Experimental = append(sconfig.Experimental, *exp.Get(name))
}

hconf := savedConf.HypervisorConfig
sconfig.HypervisorConfig = HypervisorConfig{
NumVCPUs: hconf.NumVCPUs,
DefaultMaxVCPUs: hconf.DefaultMaxVCPUs,
MemorySize: hconf.MemorySize,
MemSlots: hconf.MemSlots,
KernelPath: hconf.KernelPath,
ImagePath: hconf.ImagePath,
InitrdPath: hconf.InitrdPath,
SharedFS: hconf.SharedFS,
VirtioFSDaemon: hconf.VirtioFSDaemon,
DisableBlockDeviceUse: hconf.DisableBlockDeviceUse,
UseVSock: hconf.UseVSock,
DisableVhostNet: hconf.DisableVhostNet,
}

if savedConf.AgentType == "kata" {

sconfig.AgentConfig = KataAgentConfig{
LongLiveConn: savedConf.KataAgentConfig.LongLiveConn,
UseVSock: savedConf.KataAgentConfig.UseVSock,
}
}

if savedConf.ShimType == "kataShim" {
sconfig.ShimConfig = ShimConfig{
Path: savedConf.KataShimConfig.Path,
Debug: savedConf.KataShimConfig.Debug,
}
}

for _, contConf := range savedConf.ContainerConfigs {
sconfig.Containers = append(sconfig.Containers, ContainerConfig{
ID: contConf.ID,
Annotations: contConf.Annotations,
Resources: contConf.Resources,
})
}
return sconfig, nil
}
Loading

0 comments on commit aeae231

Please sign in to comment.