-
Notifications
You must be signed in to change notification settings - Fork 121
/
containerruntime.go
146 lines (121 loc) · 4 KB
/
containerruntime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2020 The Machine Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package containerruntime
import (
"github.com/Masterminds/semver/v3"
"github.com/kubermatic/machine-controller/pkg/providerconfig/types"
)
const (
dockerName = "docker"
containerdName = "containerd"
)
type Engine interface {
KubeletFlags() []string
ScriptFor(os types.OperatingSystem) (string, error)
ConfigFileName() string
Config() (string, error)
AuthConfigFileName() string
AuthConfig() (string, error)
String() string
}
type Opt func(*Config)
func withInsecureRegistries(registries []string) Opt {
return func(cfg *Config) {
cfg.InsecureRegistries = registries
}
}
func withRegistryMirrors(mirrors map[string][]string) Opt {
return func(cfg *Config) {
cfg.RegistryMirrors = mirrors
}
}
func withSandboxImage(image string) Opt {
return func(cfg *Config) {
cfg.SandboxImage = image
}
}
func get(containerRuntimeName string, opts ...Opt) Config {
cfg := Config{}
switch containerRuntimeName {
case dockerName:
cfg.Docker = &Docker{}
cfg.Containerd = nil
case containerdName:
cfg.Containerd = &Containerd{}
cfg.Docker = nil
default:
cfg.Docker = &Docker{}
cfg.Containerd = nil
}
for _, o := range opts {
o(&cfg)
}
return cfg
}
type Config struct {
Docker *Docker `json:",omitempty"`
Containerd *Containerd `json:",omitempty"`
InsecureRegistries []string `json:",omitempty"`
RegistryMirrors map[string][]string `json:",omitempty"`
RegistryCredentials map[string]AuthConfig `json:",omitempty"`
SandboxImage string `json:",omitempty"`
ContainerLogMaxFiles string `json:",omitempty"`
ContainerLogMaxSize string `json:",omitempty"`
}
// AuthConfig is a COPY of github.com/containerd/containerd/pkg/cri/config.AuthConfig.
// AuthConfig contains the config related to authentication to a specific registry.
type AuthConfig struct {
// Username is the username to login the registry.
Username string `toml:"username,omitempty" json:"username,omitempty"`
// Password is the password to login the registry.
Password string `toml:"password,omitempty" json:"password,omitempty"`
// Auth is a base64 encoded string from the concatenation of the username,
// a colon, and the password.
Auth string `toml:"auth,omitempty" json:"auth,omitempty"`
// IdentityToken is used to authenticate the user and get
// an access token for the registry.
IdentityToken string `toml:"identitytoken,omitempty" json:"identitytoken,omitempty"`
}
func (cfg Config) String() string {
switch {
case cfg.Containerd != nil:
return containerdName
case cfg.Docker != nil:
return dockerName
}
return dockerName
}
func (cfg Config) Engine(kubeletVersion *semver.Version) Engine {
docker := &Docker{
insecureRegistries: cfg.InsecureRegistries,
registryMirrors: cfg.RegistryMirrors["docker.io"],
containerLogMaxFiles: cfg.ContainerLogMaxFiles,
containerLogMaxSize: cfg.ContainerLogMaxSize,
registryCredentials: cfg.RegistryCredentials,
}
containerd := &Containerd{
insecureRegistries: cfg.InsecureRegistries,
registryMirrors: cfg.RegistryMirrors,
sandboxImage: cfg.SandboxImage,
registryCredentials: cfg.RegistryCredentials,
}
moreThan124, _ := semver.NewConstraint(">= 1.24")
switch {
case moreThan124.Check(kubeletVersion) || cfg.Containerd != nil:
// docker support has been removed in Kubernetes 1.24
return containerd
case cfg.Docker != nil:
return docker
}
return docker
}