Skip to content

Commit 1e9e95c

Browse files
authored
Merge pull request #4120 from twz123/move-containerd-component-into-subpackage
Move Containerd component into the containerd subpackage
2 parents d0d37b5 + 6aafabc commit 1e9e95c

File tree

7 files changed

+62
-67
lines changed

7 files changed

+62
-67
lines changed

cmd/worker/worker.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/k0sproject/k0s/pkg/component/status"
3535
"github.com/k0sproject/k0s/pkg/component/worker"
3636
workerconfig "github.com/k0sproject/k0s/pkg/component/worker/config"
37+
"github.com/k0sproject/k0s/pkg/component/worker/containerd"
3738
"github.com/k0sproject/k0s/pkg/component/worker/nllb"
3839
"github.com/k0sproject/k0s/pkg/config"
3940
"github.com/k0sproject/k0s/pkg/kubernetes"
@@ -148,7 +149,7 @@ func (c *Command) Start(ctx context.Context) error {
148149
}
149150

150151
if c.CriSocket == "" {
151-
componentManager.Add(ctx, worker.NewContainerd(c.Logging["containerd"], c.K0sVars, workerConfig))
152+
componentManager.Add(ctx, containerd.NewComponent(c.Logging["containerd"], c.K0sVars, workerConfig))
152153
}
153154

154155
componentManager.Add(ctx, worker.NewOCIBundleReconciler(c.K0sVars))

pkg/component/worker/containerd.go pkg/component/worker/containerd/component.go

+26-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package worker
17+
package containerd
1818

1919
import (
2020
"bufio"
@@ -41,7 +41,6 @@ import (
4141
"github.com/k0sproject/k0s/pkg/assets"
4242
"github.com/k0sproject/k0s/pkg/component/manager"
4343
workerconfig "github.com/k0sproject/k0s/pkg/component/worker/config"
44-
"github.com/k0sproject/k0s/pkg/component/worker/containerd"
4544
"github.com/k0sproject/k0s/pkg/config"
4645
"github.com/k0sproject/k0s/pkg/constant"
4746
"github.com/k0sproject/k0s/pkg/debounce"
@@ -68,8 +67,8 @@ const confPathWindows = "C:\\Program Files\\containerd\\config.toml"
6867
const importsPathPosix = "/etc/k0s/containerd.d/"
6968
const importsPathWindows = "C:\\etc\\k0s\\containerd.d\\"
7069

71-
// Containerd implements the component interface to manage containerd as a k0s component.
72-
type Containerd struct {
70+
// Component implements the component interface to manage containerd as a k0s component.
71+
type Component struct {
7372
supervisor supervisor.Supervisor
7473
LogLevel string
7574
K0sVars *config.CfgVars
@@ -80,8 +79,8 @@ type Containerd struct {
8079
importsPath string
8180
}
8281

83-
func NewContainerd(logLevel string, vars *config.CfgVars, profile *workerconfig.Profile) *Containerd {
84-
c := &Containerd{
82+
func NewComponent(logLevel string, vars *config.CfgVars, profile *workerconfig.Profile) *Component {
83+
c := &Component{
8584
LogLevel: logLevel,
8685
K0sVars: vars,
8786
Profile: profile,
@@ -99,10 +98,10 @@ func NewContainerd(logLevel string, vars *config.CfgVars, profile *workerconfig.
9998
return c
10099
}
101100

102-
var _ manager.Component = (*Containerd)(nil)
101+
var _ manager.Component = (*Component)(nil)
103102

104103
// Init extracts the needed binaries
105-
func (c *Containerd) Init(ctx context.Context) error {
104+
func (c *Component) Init(ctx context.Context) error {
106105
g, _ := errgroup.WithContext(ctx)
107106
for _, bin := range c.binaries {
108107
b := bin
@@ -116,7 +115,7 @@ func (c *Containerd) Init(ctx context.Context) error {
116115
return g.Wait()
117116
}
118117

119-
func (c *Containerd) windowsInit() error {
118+
func (c *Component) windowsInit() error {
120119
if runtime.GOOS != "windows" {
121120
return nil
122121
}
@@ -127,7 +126,7 @@ func (c *Containerd) windowsInit() error {
127126
}
128127

129128
// Run runs containerd.
130-
func (c *Containerd) Start(ctx context.Context) error {
129+
func (c *Component) Start(ctx context.Context) error {
131130
logrus.Info("Starting containerd")
132131

133132
if err := c.setupConfig(); err != nil {
@@ -162,21 +161,21 @@ func (c *Containerd) Start(ctx context.Context) error {
162161
return nil
163162
}
164163

165-
func (c *Containerd) windowsStart(_ context.Context) error {
164+
func (c *Component) windowsStart(_ context.Context) error {
166165
if err := winExecute("Start-Service containerd"); err != nil {
167166
return fmt.Errorf("failed to start Windows Service %q: %w", "containerd", err)
168167
}
169168
return nil
170169
}
171170

172-
func (c *Containerd) windowsStop() error {
171+
func (c *Component) windowsStop() error {
173172
if err := winExecute("Stop-Service containerd"); err != nil {
174173
return fmt.Errorf("failed to stop Windows Service %q: %w", "containerd", err)
175174
}
176175
return nil
177176
}
178177

179-
func (c *Containerd) setupConfig() error {
178+
func (c *Component) setupConfig() error {
180179
// Check if the config file is user managed
181180
// If it is, we should not touch it
182181

@@ -195,9 +194,18 @@ func (c *Containerd) setupConfig() error {
195194
if err := dir.Init(filepath.Dir(c.importsPath), 0755); err != nil {
196195
return fmt.Errorf("can't create containerd config imports dir: %w", err)
197196
}
198-
containerdConfigurer := containerd.NewConfigurer(c.Profile.PauseImage, filepath.Join(c.importsPath, "*.toml"))
199197

200-
imports, err := containerdConfigurer.HandleImports()
198+
configurer := &configurer{
199+
loadPath: filepath.Join(c.importsPath, "*.toml"),
200+
pauseImage: c.Profile.PauseImage.URI(),
201+
log: logrus.WithField("component", "containerd"),
202+
criRuntimePath: "/run/k0s/containerd-cri.toml",
203+
}
204+
if runtime.GOOS == "windows" {
205+
configurer.criRuntimePath = `C:\var\lib\k0s\run\containerd-cri.toml`
206+
}
207+
208+
imports, err := configurer.handleImports()
201209
if err != nil {
202210
return fmt.Errorf("can't handle imports: %w", err)
203211
}
@@ -217,7 +225,7 @@ func (c *Containerd) setupConfig() error {
217225
return file.WriteContentAtomically(c.confPath, output.Bytes(), 0644)
218226
}
219227

220-
func (c *Containerd) watchDropinConfigs(ctx context.Context) {
228+
func (c *Component) watchDropinConfigs(ctx context.Context) {
221229
log := logrus.WithField("component", "containerd")
222230
watcher, err := fsnotify.NewWatcher()
223231
if err != nil {
@@ -265,7 +273,7 @@ func (c *Containerd) watchDropinConfigs(ctx context.Context) {
265273
}
266274
}
267275

268-
func (c *Containerd) restart() {
276+
func (c *Component) restart() {
269277
log := logrus.WithFields(logrus.Fields{"component": "containerd", "phase": "restart"})
270278

271279
log.Info("restart requested")
@@ -293,7 +301,7 @@ func (c *Containerd) restart() {
293301
}
294302

295303
// Stop stops containerd.
296-
func (c *Containerd) Stop() error {
304+
func (c *Component) Stop() error {
297305
if runtime.GOOS == "windows" {
298306
return c.windowsStop()
299307
}

pkg/component/worker/containerd_test.go pkg/component/worker/containerd/component_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package worker
17+
package containerd
1818

1919
import (
2020
"os"

pkg/component/worker/containerd/criconfig.go pkg/component/worker/containerd/configurer.go

+10-32
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,26 @@ import (
2525
"runtime"
2626
"strings"
2727

28-
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
2928
"github.com/mesosphere/toml-merge/pkg/patch"
3029
"github.com/pelletier/go-toml"
3130
"github.com/sirupsen/logrus"
3231

3332
criconfig "github.com/containerd/containerd/pkg/cri/config"
3433
)
3534

36-
// TODO: move to K0sVars
37-
const containerdCRIConfigPathPosix = "/run/k0s/containerd-cri.toml"
38-
const containerdCRIConfigPathWindows = "C:\\var\\lib\\k0s\\run\\containerd-cri.toml"
39-
40-
type CRIConfigurer struct {
35+
type configurer struct {
4136
loadPath string
4237
pauseImage string
4338
criRuntimePath string
4439

4540
log *logrus.Entry
4641
}
4742

48-
func NewConfigurer(pauseImage *v1beta1.ImageSpec, importsPath string) *CRIConfigurer {
49-
c := &CRIConfigurer{
50-
loadPath: importsPath,
51-
pauseImage: pauseImage.URI(),
52-
log: logrus.WithField("component", "containerd"),
53-
}
54-
if runtime.GOOS == "windows" {
55-
c.criRuntimePath = containerdCRIConfigPathWindows
56-
57-
} else {
58-
c.criRuntimePath = containerdCRIConfigPathPosix
59-
}
60-
return c
61-
}
62-
63-
// HandleImports Resolves containerd imports from the import glob path.
43+
// Resolves containerd imports from the import glob path.
6444
// If the partial config has CRI plugin enabled, it will add to the runc CRI config (single file).
6545
// if no CRI plugin is found, it will add the file as-is to imports list returned.
6646
// Once all files are processed the concatenated CRI config file is written and added to the imports list.
67-
func (c *CRIConfigurer) HandleImports() ([]string, error) {
47+
func (c *configurer) handleImports() ([]string, error) {
6848
var imports []string
6949
var criConfigBuffer bytes.Buffer
7050

@@ -124,23 +104,21 @@ func escapedPath(s string) string {
124104
return s
125105
}
126106

127-
// We need to use custom struct so we can unmarshal the CRI plugin config only
128-
type config struct {
129-
Version int
130-
Plugins map[string]interface{} `toml:"plugins"`
131-
}
132-
133107
// generateDefaultCRIConfig generates the default CRI config and writes it to the given writer
134108
// It uses the containerd containerd package to generate the config so we can keep it in sync with containerd
135-
func (c *CRIConfigurer) generateDefaultCRIConfig(w io.Writer) error {
109+
func (c *configurer) generateDefaultCRIConfig(w io.Writer) error {
136110
criPluginConfig := criconfig.DefaultConfig()
137111
// Set pause image
138112
criPluginConfig.SandboxImage = c.pauseImage
139113
if runtime.GOOS == "windows" {
140114
criPluginConfig.CniConfig.NetworkPluginBinDir = "c:\\opt\\cni\\bin"
141115
criPluginConfig.CniConfig.NetworkPluginConfDir = "c:\\opt\\cni\\conf"
142116
}
143-
containerdConfig := config{
117+
// We need to use custom struct so we can unmarshal the CRI plugin config only
118+
containerdConfig := struct {
119+
Version int
120+
Plugins map[string]interface{} `toml:"plugins"`
121+
}{
144122
Version: 2,
145123
Plugins: map[string]interface{}{
146124
"io.containerd.grpc.v1.cri": criPluginConfig,
@@ -154,7 +132,7 @@ func (c *CRIConfigurer) generateDefaultCRIConfig(w io.Writer) error {
154132
return nil
155133
}
156134

157-
func (c *CRIConfigurer) hasCRIPluginConfig(data []byte) (bool, error) {
135+
func (c *configurer) hasCRIPluginConfig(data []byte) (bool, error) {
158136
var tomlConfig map[string]interface{}
159137
if err := toml.Unmarshal(data, &tomlConfig); err != nil {
160138
return false, err

pkg/component/worker/containerd/criconfig_test.go pkg/component/worker/containerd/configurer_test.go

+20-13
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@ import (
2222
"testing"
2323

2424
srvconfig "github.com/containerd/containerd/services/server/config"
25-
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
2625
"github.com/sirupsen/logrus"
2726
"github.com/stretchr/testify/require"
2827
)
2928

3029
const testImportsPath = "/etc/k0s/containerd.d/"
3130

32-
func TestCRIConfigurer_hasCRIPluginConfig(t *testing.T) {
31+
func TestConfigurer_hasCRIPluginConfig(t *testing.T) {
3332
t.Run("should return true if config has cri plugin configs", func(t *testing.T) {
3433
cfg := `
3534
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
3635
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
3736
endpoint = ["https://registry-1.docker.io"]
3837
`
39-
c := NewConfigurer(&v1beta1.DefaultClusterImages().Pause, testImportsPath)
38+
39+
c := configurer{
40+
loadPath: testImportsPath,
41+
log: logrus.New().WithField("test", t.Name()),
42+
}
4043
hasCRIPluginConfig, err := c.hasCRIPluginConfig([]byte(cfg))
4144
require.NoError(t, err)
4245
require.True(t, hasCRIPluginConfig)
@@ -47,15 +50,19 @@ func TestCRIConfigurer_hasCRIPluginConfig(t *testing.T) {
4750
timeout = 3
4851
version = 2
4952
`
50-
c := NewConfigurer(&v1beta1.DefaultClusterImages().Pause, testImportsPath)
53+
54+
c := configurer{
55+
loadPath: testImportsPath,
56+
log: logrus.New().WithField("test", t.Name()),
57+
}
5158
hasCRIPluginConfig, err := c.hasCRIPluginConfig([]byte(cfg))
5259
require.NoError(t, err)
5360
require.False(t, hasCRIPluginConfig)
5461
})
5562

5663
}
5764

58-
func TestCRIConfigurer_HandleImports(t *testing.T) {
65+
func TestConfigurer_HandleImports(t *testing.T) {
5966
t.Run("should merge CRI configs", func(t *testing.T) {
6067
tmp := t.TempDir()
6168
testLoadPath := filepath.Join(tmp, "*.toml")
@@ -67,12 +74,12 @@ func TestCRIConfigurer_HandleImports(t *testing.T) {
6774
`
6875
err := os.WriteFile(filepath.Join(tmp, "foo.toml"), []byte(criRuntimeConfig), 0644)
6976
require.NoError(t, err)
70-
c := CRIConfigurer{
77+
c := configurer{
7178
loadPath: testLoadPath,
7279
criRuntimePath: criRuntimePath,
7380
log: logrus.New().WithField("test", t.Name()),
7481
}
75-
_, err = c.HandleImports()
82+
_, err = c.handleImports()
7683
require.NoError(t, err)
7784

7885
// Dump the config for inspection
@@ -92,12 +99,12 @@ func TestCRIConfigurer_HandleImports(t *testing.T) {
9299
t.Run("should have single import for CRI if there's nothing in imports dir", func(t *testing.T) {
93100
testLoadPath := filepath.Join(t.TempDir(), "*.toml")
94101
criRuntimePath := filepath.Join(t.TempDir(), "cri.toml")
95-
c := CRIConfigurer{
102+
c := configurer{
96103
loadPath: testLoadPath,
97104
criRuntimePath: criRuntimePath,
98105
log: logrus.New().WithField("test", t.Name()),
99106
}
100-
imports, err := c.HandleImports()
107+
imports, err := c.handleImports()
101108
require.NoError(t, err)
102109
require.Len(t, imports, 1)
103110
require.Equal(t, escapedPath(criRuntimePath), imports[0])
@@ -114,12 +121,12 @@ func TestCRIConfigurer_HandleImports(t *testing.T) {
114121
`
115122
err := os.WriteFile(filepath.Join(tmp, "foo.toml"), []byte(criRuntimeConfig), 0644)
116123
require.NoError(t, err)
117-
c := CRIConfigurer{
124+
c := configurer{
118125
loadPath: testLoadPath,
119126
criRuntimePath: criRuntimePath,
120127
log: logrus.New().WithField("test", t.Name()),
121128
}
122-
imports, err := c.HandleImports()
129+
imports, err := c.handleImports()
123130
require.NoError(t, err)
124131
require.Len(t, imports, 1)
125132
require.Contains(t, imports, escapedPath(criRuntimePath))
@@ -145,12 +152,12 @@ version = 2
145152
nonCriConfigPath := filepath.Join(tmp, "foo.toml")
146153
err := os.WriteFile(nonCriConfigPath, []byte(criRuntimeConfig), 0644)
147154
require.NoError(t, err)
148-
c := CRIConfigurer{
155+
c := configurer{
149156
loadPath: testLoadPath,
150157
criRuntimePath: criRuntimePath,
151158
log: logrus.New().WithField("test", t.Name()),
152159
}
153-
imports, err := c.HandleImports()
160+
imports, err := c.handleImports()
154161
require.NoError(t, err)
155162
require.Len(t, imports, 2)
156163
require.Contains(t, imports, escapedPath(criRuntimePath))

pkg/component/worker/utils_linux.go pkg/component/worker/containerd/utils_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package worker
17+
package containerd
1818

1919
func winExecute(args ...string) error {
2020
panic("Invariant broken: this function should never be called on non-winodws platforms")

pkg/component/worker/utils_windows.go pkg/component/worker/containerd/utils_windows.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
package worker
16+
17+
package containerd
1718

1819
import (
1920
"os"

0 commit comments

Comments
 (0)