podman.go 2.31 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
package podman

import (
4
	"dcu-container-toolkit/pkg/config/engine"
songlinfeng's avatar
songlinfeng committed
5
6
7
8
9
10
11
12
13
	"fmt"

	"gopkg.in/ini.v1"
)

type Config struct {
	File *ini.File
}
const (
14
    defaultDTKRuntimeExpecutablePath = "/usr/bin/dcu-container-runtime"
songlinfeng's avatar
songlinfeng committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
)
var _ engine.Interface = (*Config)(nil)

func New(opts ...Option) (engine.Interface, error) {
	b := &builder{}
	for _, opt := range opts {
		opt(b)
	}

	return b.build()
}

func (c *Config) AddRuntime(name string, path string, setAsDefault bool, _ ...map[string]interface{}) error {
	if c == nil {
		return fmt.Errorf("config is nil")
	}

	cfg := c.File
	runc := cfg.Section("engine").Key("runtime").String()
	if runc == "" {
		containers := cfg.Section("containers")
		containers.Key("default_capabilities").SetValue(`["NET_RAW", "CHOWN", "DAC_OVERRIDE", "FOWNER", "FSETID", "KILL", "NET_BIND_SERVICE", "SETFCAP", "SETGID", "SETPCAP", "SETUID", "SYS_CHROOT"]`)
		containers.Key("default_sysctls").SetValue(`["net.ipv4.ping_group_range=0 0"]`)
		containers.Key("log_driver").SetValue("k8s-file")

		// engine section
		engine := cfg.Section("engine")
		engine.Key("events_logger").SetValue("file")
		engine.Key("runtime").SetValue("runc")

		
		cfg.NewSection("engine.runtimes")
		cfg.NewSection("engine.volume_plugins")
		cfg.NewSection("farms")
		cfg.NewSection("machine")

		// network section
		network := cfg.Section("network")
		network.Key("network_backend").SetValue("cni")

		// secrets sections
		cfg.NewSection("secrets")
		cfg.NewSection("secrets.opts")

	}
	if setAsDefault {
		cfg.Section("engine").Key("runtime").SetValue(defaultDTKRuntimeExpecutablePath)
	}
	c.File = cfg
	return nil
}

func (c Config) DefaultRuntime() string {
	runc := c.File.Section("engine").Key("runtime").String()
	if runc == "" {
		return ""
	}
	return runc
}

func (c *Config) RemoveRuntime(name string) error {
	if c == nil {
		return nil
	}
	cfg := c.File
	if runc := cfg.Section("engine").Key("runtime").String(); runc != "" {
		cfg.DeleteSection("engine.runtimes")
	}
	c.File = cfg
	return nil
}

func (c *Config) Set(key string, value interface{}) {
	cfg := c.File
	if str, ok := value.(string); ok {
	    cfg.Section("engine").Key(key).SetValue(str)
        } else {
      	    fmt.Println("key %s value is not string", key)
        }
	c.File = cfg
}

func (c *Config) Save(path string) (int64, error) {
	cfg := c.File
	return 0, cfg.SaveTo(path)
}