Skip to content

Commit ed424d9

Browse files
Improve generation for container runtime configuration (#1174)
* Improve handling for container runtime configuration Signed-off-by: Waleed Malik <[email protected]> * Update fixtures Signed-off-by: Waleed Malik <[email protected]> * Fix linting errors * Handle PR feedback Signed-off-by: Waleed Malik <[email protected]>
1 parent cb15dbc commit ed424d9

File tree

13 files changed

+237
-138
lines changed

13 files changed

+237
-138
lines changed

cmd/machine-controller/main.go

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"net"
2424
"net/http"
2525
"net/http/pprof"
26-
"net/url"
2726
"strings"
2827
"time"
2928

@@ -84,7 +83,7 @@ var (
8483
podCidr string
8584
nodePortRange string
8685
nodeRegistryCredentialsSecret string
87-
nodeContainerdRegistryMirrors = registryMirrorsFlags{}
86+
nodeContainerdRegistryMirrors = containerruntime.RegistryMirrorsFlags{}
8887
)
8988

9089
const (
@@ -237,37 +236,17 @@ func main() {
237236
ctrlMetrics := machinecontroller.NewMachineControllerMetrics()
238237
ctrlMetrics.MustRegister(metrics.Registry)
239238

240-
var insecureRegistries []string
241-
for _, registry := range strings.Split(nodeInsecureRegistries, ",") {
242-
if trimmedRegistry := strings.TrimSpace(registry); trimmedRegistry != "" {
243-
insecureRegistries = append(insecureRegistries, trimmedRegistry)
244-
}
245-
}
246-
247-
var registryMirrors []string
248-
for _, mirror := range strings.Split(nodeRegistryMirrors, ",") {
249-
if trimmedMirror := strings.TrimSpace(mirror); trimmedMirror != "" {
250-
if !strings.HasPrefix(mirror, "http") {
251-
trimmedMirror = "https://" + mirror
252-
}
253-
254-
_, err := url.Parse(trimmedMirror)
255-
if err != nil {
256-
klog.Fatalf("incorrect mirror provided: %v", err)
257-
}
258-
259-
registryMirrors = append(registryMirrors, trimmedMirror)
260-
}
239+
containerRuntimeOpts := containerruntime.Opts{
240+
ContainerRuntime: nodeContainerRuntime,
241+
ContainerdRegistryMirrors: nodeContainerdRegistryMirrors,
242+
InsecureRegistries: nodeInsecureRegistries,
243+
PauseImage: nodePauseImage,
244+
RegistryMirrors: nodeRegistryMirrors,
245+
RegistryCredentialsSecret: nodeRegistryCredentialsSecret,
261246
}
262-
263-
if len(registryMirrors) > 0 {
264-
nodeContainerdRegistryMirrors["docker.io"] = registryMirrors
265-
}
266-
267-
if nodeRegistryCredentialsSecret != "" {
268-
if secRef := strings.Split(nodeRegistryCredentialsSecret, "/"); len(secRef) != 2 {
269-
klog.Fatalf("-node-registry-credentials-secret is in incorrect format %q, should be in 'namespace/secretname'", nodeRegistryCredentialsSecret)
270-
}
247+
containerRuntimeConfig, err := containerruntime.BuildConfig(containerRuntimeOpts)
248+
if err != nil {
249+
klog.Fatalf("failed to generate container runtime config: %v", err)
271250
}
272251

273252
runOptions := controllerRunOptions{
@@ -285,12 +264,7 @@ func main() {
285264
NoProxy: nodeNoProxy,
286265
PauseImage: nodePauseImage,
287266
RegistryCredentialsSecretRef: nodeRegistryCredentialsSecret,
288-
ContainerRuntime: containerruntime.Get(
289-
nodeContainerRuntime,
290-
containerruntime.WithInsecureRegistries(insecureRegistries),
291-
containerruntime.WithRegistryMirrors(nodeContainerdRegistryMirrors),
292-
containerruntime.WithSandboxImage(nodePauseImage),
293-
),
267+
ContainerRuntime: containerRuntimeConfig,
294268
},
295269
useOSM: useOSM,
296270
podCidr: podCidr,

pkg/apis/cluster/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/containerruntime/config.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2022 The Machine Controller Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package containerruntime
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
"fmt"
23+
"net/url"
24+
"strings"
25+
26+
corev1 "k8s.io/api/core/v1"
27+
"k8s.io/apimachinery/pkg/types"
28+
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
29+
)
30+
31+
type Opts struct {
32+
ContainerRuntime string
33+
InsecureRegistries string
34+
RegistryMirrors string
35+
RegistryCredentialsSecret string
36+
PauseImage string
37+
ContainerdRegistryMirrors RegistryMirrorsFlags
38+
}
39+
40+
func BuildConfig(opts Opts) (Config, error) {
41+
var insecureRegistries []string
42+
for _, registry := range strings.Split(opts.InsecureRegistries, ",") {
43+
if trimmedRegistry := strings.TrimSpace(registry); trimmedRegistry != "" {
44+
insecureRegistries = append(insecureRegistries, trimmedRegistry)
45+
}
46+
}
47+
48+
var registryMirrors []string
49+
for _, mirror := range strings.Split(opts.RegistryMirrors, ",") {
50+
if trimmedMirror := strings.TrimSpace(mirror); trimmedMirror != "" {
51+
if !strings.HasPrefix(mirror, "http") {
52+
trimmedMirror = "https://" + mirror
53+
}
54+
55+
_, err := url.Parse(trimmedMirror)
56+
if err != nil {
57+
return Config{}, fmt.Errorf("incorrect mirror provided: %v", err)
58+
}
59+
60+
registryMirrors = append(registryMirrors, trimmedMirror)
61+
}
62+
}
63+
64+
if len(registryMirrors) > 0 {
65+
if opts.ContainerdRegistryMirrors == nil {
66+
opts.ContainerdRegistryMirrors = make(RegistryMirrorsFlags)
67+
}
68+
opts.ContainerdRegistryMirrors["docker.io"] = registryMirrors
69+
}
70+
71+
// Only validate registry credential here
72+
if opts.RegistryCredentialsSecret != "" {
73+
if secRef := strings.Split(opts.RegistryCredentialsSecret, "/"); len(secRef) != 2 {
74+
return Config{}, fmt.Errorf("-node-registry-credentials-secret is in incorrect format %q, should be in 'namespace/secretname'", opts.RegistryCredentialsSecret)
75+
}
76+
}
77+
78+
return get(
79+
opts.ContainerRuntime,
80+
withInsecureRegistries(insecureRegistries),
81+
withRegistryMirrors(opts.ContainerdRegistryMirrors),
82+
withSandboxImage(opts.PauseImage),
83+
), nil
84+
}
85+
86+
func GetContainerdAuthConfig(ctx context.Context, client ctrlruntimeclient.Client, registryCredentialsSecret string) (map[string]AuthConfig, error) {
87+
registryCredentials := map[string]AuthConfig{}
88+
89+
if secRef := strings.SplitN(registryCredentialsSecret, "/", 2); len(secRef) == 2 {
90+
var credsSecret corev1.Secret
91+
err := client.Get(ctx, types.NamespacedName{Namespace: secRef[0], Name: secRef[1]}, &credsSecret)
92+
if err != nil {
93+
return nil, fmt.Errorf("failed to retrieve registry credentials secret object: %w", err)
94+
}
95+
96+
for registry, data := range credsSecret.Data {
97+
var regCred AuthConfig
98+
if err := json.Unmarshal(data, &regCred); err != nil {
99+
return nil, fmt.Errorf("failed to unmarshal registry credentials: %w", err)
100+
}
101+
registryCredentials[registry] = regCred
102+
}
103+
}
104+
return registryCredentials, nil
105+
}

pkg/containerruntime/containerruntime.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,25 @@ type Engine interface {
3737

3838
type Opt func(*Config)
3939

40-
func WithInsecureRegistries(registries []string) Opt {
40+
func withInsecureRegistries(registries []string) Opt {
4141
return func(cfg *Config) {
4242
cfg.InsecureRegistries = registries
4343
}
4444
}
4545

46-
func WithRegistryMirrors(mirrors map[string][]string) Opt {
46+
func withRegistryMirrors(mirrors map[string][]string) Opt {
4747
return func(cfg *Config) {
4848
cfg.RegistryMirrors = mirrors
4949
}
5050
}
5151

52-
func WithRegistryCredentials(auth map[string]AuthConfig) Opt {
53-
return func(cfg *Config) {
54-
cfg.RegistryCredentials = auth
55-
}
56-
}
57-
58-
func WithSandboxImage(image string) Opt {
52+
func withSandboxImage(image string) Opt {
5953
return func(cfg *Config) {
6054
cfg.SandboxImage = image
6155
}
6256
}
6357

64-
func Get(containerRuntimeName string, opts ...Opt) Config {
58+
func get(containerRuntimeName string, opts ...Opt) Config {
6559
cfg := Config{}
6660

6761
switch containerRuntimeName {

cmd/machine-controller/custom_flags.go renamed to pkg/containerruntime/flags.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2019 The Machine Controller Authors.
2+
Copyright 2022 The Machine Controller Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package containerruntime
1818

1919
import (
2020
"fmt"
2121
"sort"
2222
"strings"
2323
)
2424

25-
type registryMirrorsFlags map[string][]string
25+
type RegistryMirrorsFlags map[string][]string
2626

27-
func (fl registryMirrorsFlags) Set(val string) error {
27+
func (fl RegistryMirrorsFlags) Set(val string) error {
2828
split := strings.SplitN(val, "=", 2)
2929
if len(split) != 2 {
3030
return fmt.Errorf("should have exactly 1 =")
@@ -38,7 +38,7 @@ func (fl registryMirrorsFlags) Set(val string) error {
3838
return nil
3939
}
4040

41-
func (fl registryMirrorsFlags) String() string {
41+
func (fl RegistryMirrorsFlags) String() string {
4242
var (
4343
registryNames []string
4444
result []string

pkg/controller/machine/machine_controller.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controller
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"errors"
2322
"fmt"
2423
"net"
@@ -726,22 +725,9 @@ func (r *Reconciler) ensureInstanceExistsForMachine(
726725
externalCloudProvider, _ = strconv.ParseBool(val)
727726
}
728727

729-
registryCredentials := map[string]containerruntime.AuthConfig{}
730-
731-
if secRef := strings.SplitN(r.nodeSettings.RegistryCredentialsSecretRef, "/", 2); len(secRef) == 2 {
732-
var credsSecret corev1.Secret
733-
err := r.client.Get(ctx, types.NamespacedName{Namespace: secRef[0], Name: secRef[1]}, &credsSecret)
734-
if err != nil {
735-
return nil, fmt.Errorf("failed to retrieve registry credentials secret object: %w", err)
736-
}
737-
738-
for registry, data := range credsSecret.Data {
739-
var regCred containerruntime.AuthConfig
740-
if err := json.Unmarshal(data, &regCred); err != nil {
741-
return nil, fmt.Errorf("failed to unmarshal registry credentials: %w", err)
742-
}
743-
registryCredentials[registry] = regCred
744-
}
728+
registryCredentials, err := containerruntime.GetContainerdAuthConfig(ctx, r.client, r.nodeSettings.RegistryCredentialsSecretRef)
729+
if err != nil {
730+
return nil, fmt.Errorf("failed to get containerd auth config: %v", err)
745731
}
746732

747733
crRuntime := r.nodeSettings.ContainerRuntime

pkg/machines/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/userdata/amzn2/provider_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ type userDataTestCase struct {
8787
externalCloudProvider bool
8888
httpProxy string
8989
noProxy string
90-
insecureRegistries []string
91-
registryMirrors map[string][]string
90+
insecureRegistries string
91+
registryMirrors string
9292
pauseImage string
9393
containerruntime string
9494
}
@@ -158,7 +158,7 @@ func TestUserDataGeneration(t *testing.T) {
158158
cloudProviderName: stringPtr("vsphere"),
159159
httpProxy: "http://192.168.100.100:3128",
160160
noProxy: "192.168.1.0",
161-
insecureRegistries: []string{"192.168.100.100:5000", "10.0.0.1:5000"},
161+
insecureRegistries: "192.168.100.100:5000, 10.0.0.1:5000",
162162
pauseImage: "192.168.100.100:5000/kubernetes/pause:v3.1",
163163
},
164164
{
@@ -172,7 +172,7 @@ func TestUserDataGeneration(t *testing.T) {
172172
cloudProviderName: stringPtr("vsphere"),
173173
httpProxy: "http://192.168.100.100:3128",
174174
noProxy: "192.168.1.0",
175-
registryMirrors: map[string][]string{"docker.io": {"https://registry.docker-cn.com"}},
175+
registryMirrors: "https://registry.docker-cn.com",
176176
pauseImage: "192.168.100.100:5000/kubernetes/pause:v3.1",
177177
},
178178
{
@@ -240,6 +240,16 @@ func TestUserDataGeneration(t *testing.T) {
240240
t.Fatalf("failed to get cloud config: %v", err)
241241
}
242242

243+
containerRuntimeOpts := containerruntime.Opts{
244+
ContainerRuntime: test.containerruntime,
245+
InsecureRegistries: test.insecureRegistries,
246+
RegistryMirrors: test.registryMirrors,
247+
}
248+
containerRuntimeConfig, err := containerruntime.BuildConfig(containerRuntimeOpts)
249+
if err != nil {
250+
t.Fatalf("failed to generate container runtime config: %v", err)
251+
}
252+
243253
req := plugin.UserDataRequest{
244254
MachineSpec: test.spec,
245255
Kubeconfig: kubeconfig,
@@ -252,11 +262,7 @@ func TestUserDataGeneration(t *testing.T) {
252262
NoProxy: test.noProxy,
253263
PauseImage: test.pauseImage,
254264
KubeletFeatureGates: kubeletFeatureGates,
255-
ContainerRuntime: containerruntime.Get(
256-
test.containerruntime,
257-
containerruntime.WithInsecureRegistries(test.insecureRegistries),
258-
containerruntime.WithRegistryMirrors(test.registryMirrors),
259-
),
265+
ContainerRuntime: containerRuntimeConfig,
260266
}
261267

262268
s, err := provider.UserData(req)

0 commit comments

Comments
 (0)