mirror of https://github.com/kubernetes/kops.git
				
				
				
			Add containerd config file to Flatcar based instances
This commit is contained in:
		
							parent
							
								
									0ca0e38518
								
							
						
					
					
						commit
						422cfad1da
					
				|  | @ -51,16 +51,14 @@ func (b *ContainerdBuilder) Build(c *fi.ModelBuilderContext) error { | ||||||
| 	switch b.Distribution { | 	switch b.Distribution { | ||||||
| 	case distributions.DistributionFlatcar: | 	case distributions.DistributionFlatcar: | ||||||
| 		klog.Infof("Detected Flatcar; won't install containerd") | 		klog.Infof("Detected Flatcar; won't install containerd") | ||||||
| 		if err := b.buildContainerOSConfigurationDropIn(c); err != nil { | 		if b.Cluster.Spec.ContainerRuntime == "containerd" { | ||||||
| 			return err | 			b.buildSystemdServiceOverrideFlatcar(c) | ||||||
|  | 			b.buildConfigFile(c) | ||||||
| 		} | 		} | ||||||
| 		return nil | 		return nil | ||||||
| 
 |  | ||||||
| 	case distributions.DistributionContainerOS: | 	case distributions.DistributionContainerOS: | ||||||
| 		klog.Infof("Detected ContainerOS; won't install containerd") | 		klog.Infof("Detected ContainerOS; won't install containerd") | ||||||
| 		if err := b.buildContainerOSConfigurationDropIn(c); err != nil { | 		b.buildSystemdServiceOverrideContainerOS(c) | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -75,19 +73,7 @@ func (b *ContainerdBuilder) Build(c *fi.ModelBuilderContext) error { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Add config file
 | 	// Add config file
 | ||||||
| 	{ | 	b.buildConfigFile(c) | ||||||
| 		containerdConfigOverride := "" |  | ||||||
| 		if b.Cluster.Spec.Containerd != nil { |  | ||||||
| 			containerdConfigOverride = fi.StringValue(b.Cluster.Spec.Containerd.ConfigOverride) |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		t := &nodetasks.File{ |  | ||||||
| 			Path:     "/etc/containerd/config-kops.toml", |  | ||||||
| 			Contents: fi.NewStringResource(containerdConfigOverride), |  | ||||||
| 			Type:     nodetasks.FileType_File, |  | ||||||
| 		} |  | ||||||
| 		c.AddTask(t) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	// Add binaries from assets
 | 	// Add binaries from assets
 | ||||||
| 	if b.Cluster.Spec.ContainerRuntime == "containerd" { | 	if b.Cluster.Spec.ContainerRuntime == "containerd" { | ||||||
|  | @ -129,7 +115,7 @@ func (b *ContainerdBuilder) Build(c *fi.ModelBuilderContext) error { | ||||||
| 	} | 	} | ||||||
| 	c.AddTask(b.buildSystemdService(sv)) | 	c.AddTask(b.buildSystemdService(sv)) | ||||||
| 
 | 
 | ||||||
| 	if err := b.buildSysconfig(c); err != nil { | 	if err := b.buildSysconfigFile(c); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -191,18 +177,16 @@ func (b *ContainerdBuilder) buildSystemdService(sv semver.Version) *nodetasks.Se | ||||||
| 	return service | 	return service | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // buildContainerOSConfigurationDropIn is responsible for configuring the containerd daemon options
 | // buildSystemdServiceOverrideContainerOS is responsible for overriding the containerd service for ContainerOS
 | ||||||
| func (b *ContainerdBuilder) buildContainerOSConfigurationDropIn(c *fi.ModelBuilderContext) error { | func (b *ContainerdBuilder) buildSystemdServiceOverrideContainerOS(c *fi.ModelBuilderContext) { | ||||||
| 	lines := []string{ | 	lines := []string{ | ||||||
| 		"[Service]", | 		"[Service]", | ||||||
| 		"EnvironmentFile=/etc/sysconfig/containerd", |  | ||||||
| 		"EnvironmentFile=/etc/environment", | 		"EnvironmentFile=/etc/environment", | ||||||
| 		"TasksMax=infinity", | 		"TasksMax=infinity", | ||||||
| 	} | 	} | ||||||
| 	contents := strings.Join(lines, "\n") | 	contents := strings.Join(lines, "\n") | ||||||
| 
 | 
 | ||||||
| 	c.AddTask(&nodetasks.File{ | 	c.AddTask(&nodetasks.File{ | ||||||
| 		AfterFiles: []string{"/etc/sysconfig/containerd"}, |  | ||||||
| 		Path:     "/etc/systemd/system/containerd.service.d/10-kops.conf", | 		Path:     "/etc/systemd/system/containerd.service.d/10-kops.conf", | ||||||
| 		Contents: fi.NewStringResource(contents), | 		Contents: fi.NewStringResource(contents), | ||||||
| 		Type:     nodetasks.FileType_File, | 		Type:     nodetasks.FileType_File, | ||||||
|  | @ -210,22 +194,41 @@ func (b *ContainerdBuilder) buildContainerOSConfigurationDropIn(c *fi.ModelBuild | ||||||
| 			{"systemctl", "daemon-reload"}, | 			{"systemctl", "daemon-reload"}, | ||||||
| 			{"systemctl", "restart", "containerd.service"}, | 			{"systemctl", "restart", "containerd.service"}, | ||||||
| 			// We need to restart kops-configuration service since nodeup needs to load images
 | 			// We need to restart kops-configuration service since nodeup needs to load images
 | ||||||
| 			// into containerd with the new config. Restart is on the background because
 | 			// into containerd with the new config. We restart in the background because
 | ||||||
| 			// kops-configuration is of type 'one-shot' so the restart command will wait for
 | 			// kops-configuration is of type "one-shot", so the restart command will wait for
 | ||||||
| 			// nodeup to finish executing
 | 			// nodeup to finish executing.
 | ||||||
| 			{"systemctl", "restart", "kops-configuration.service", "&"}, | 			{"systemctl", "restart", "kops-configuration.service", "&"}, | ||||||
| 		}, | 		}, | ||||||
| 	}) | 	}) | ||||||
| 
 |  | ||||||
| 	if err := b.buildSysconfig(c); err != nil { |  | ||||||
| 		return err |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 	return nil | // buildSystemdServiceOverrideFlatcar is responsible for overriding the containerd service for Flatcar
 | ||||||
|  | func (b *ContainerdBuilder) buildSystemdServiceOverrideFlatcar(c *fi.ModelBuilderContext) { | ||||||
|  | 	lines := []string{ | ||||||
|  | 		"[Service]", | ||||||
|  | 		"Environment=CONTAINERD_CONFIG=/etc/containerd/config-kops.toml", | ||||||
|  | 		"EnvironmentFile=/etc/environment", | ||||||
|  | 	} | ||||||
|  | 	contents := strings.Join(lines, "\n") | ||||||
|  | 
 | ||||||
|  | 	c.AddTask(&nodetasks.File{ | ||||||
|  | 		Path:     "/etc/systemd/system/containerd.service.d/10-kops.conf", | ||||||
|  | 		Contents: fi.NewStringResource(contents), | ||||||
|  | 		Type:     nodetasks.FileType_File, | ||||||
|  | 		OnChangeExecute: [][]string{ | ||||||
|  | 			{"systemctl", "daemon-reload"}, | ||||||
|  | 			{"systemctl", "restart", "containerd.service"}, | ||||||
|  | 			// We need to restart kops-configuration service since nodeup needs to load images
 | ||||||
|  | 			// into containerd with the new config. We restart in the background because
 | ||||||
|  | 			// kops-configuration is of type "one-shot", so the restart command will wait for
 | ||||||
|  | 			// nodeup to finish executing.
 | ||||||
|  | 			{"systemctl", "restart", "kops-configuration.service", "&"}, | ||||||
|  | 		}, | ||||||
|  | 	}) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // buildSysconfig is responsible for extracting the containerd configuration and writing the sysconfig file
 | // buildSysconfigFile is responsible for creating the containerd sysconfig file
 | ||||||
| func (b *ContainerdBuilder) buildSysconfig(c *fi.ModelBuilderContext) error { | func (b *ContainerdBuilder) buildSysconfigFile(c *fi.ModelBuilderContext) error { | ||||||
| 	var containerd kops.ContainerdConfig | 	var containerd kops.ContainerdConfig | ||||||
| 	if b.Cluster.Spec.Containerd != nil { | 	if b.Cluster.Spec.Containerd != nil { | ||||||
| 		containerd = *b.Cluster.Spec.Containerd | 		containerd = *b.Cluster.Spec.Containerd | ||||||
|  | @ -250,6 +253,20 @@ func (b *ContainerdBuilder) buildSysconfig(c *fi.ModelBuilderContext) error { | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // buildConfigFile is responsible for creating the containerd configuration file
 | ||||||
|  | func (b *ContainerdBuilder) buildConfigFile(c *fi.ModelBuilderContext) { | ||||||
|  | 	containerdConfigOverride := "" | ||||||
|  | 	if b.Cluster.Spec.Containerd != nil { | ||||||
|  | 		containerdConfigOverride = fi.StringValue(b.Cluster.Spec.Containerd.ConfigOverride) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	c.AddTask(&nodetasks.File{ | ||||||
|  | 		Path:     "/etc/containerd/config-kops.toml", | ||||||
|  | 		Contents: fi.NewStringResource(containerdConfigOverride), | ||||||
|  | 		Type:     nodetasks.FileType_File, | ||||||
|  | 	}) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // skipInstall determines if kops should skip the installation and configuration of containerd
 | // skipInstall determines if kops should skip the installation and configuration of containerd
 | ||||||
| func (b *ContainerdBuilder) skipInstall() bool { | func (b *ContainerdBuilder) skipInstall() bool { | ||||||
| 	d := b.Cluster.Spec.Containerd | 	d := b.Cluster.Spec.Containerd | ||||||
|  |  | ||||||
|  | @ -29,15 +29,19 @@ import ( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestContainerdBuilder_Docker_19_03_13(t *testing.T) { | func TestContainerdBuilder_Docker_19_03_13(t *testing.T) { | ||||||
| 	runContainerdBuilderTest(t, "from_docker_19.03.11") | 	runContainerdBuilderTest(t, "from_docker_19.03.11", distributions.DistributionUbuntu2004) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestContainerdBuilder_Docker_19_03_14(t *testing.T) { | func TestContainerdBuilder_Docker_19_03_14(t *testing.T) { | ||||||
| 	runContainerdBuilderTest(t, "from_docker_19.03.14") | 	runContainerdBuilderTest(t, "from_docker_19.03.14", distributions.DistributionUbuntu2004) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestContainerdBuilder_Simple(t *testing.T) { | func TestContainerdBuilder_Simple(t *testing.T) { | ||||||
| 	runContainerdBuilderTest(t, "simple") | 	runContainerdBuilderTest(t, "simple", distributions.DistributionUbuntu2004) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestContainerdBuilder_Flatcar(t *testing.T) { | ||||||
|  | 	runContainerdBuilderTest(t, "flatcar", distributions.DistributionFlatcar) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestContainerdBuilder_SkipInstall(t *testing.T) { | func TestContainerdBuilder_SkipInstall(t *testing.T) { | ||||||
|  | @ -123,7 +127,7 @@ func TestContainerdBuilder_BuildFlags(t *testing.T) { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func runContainerdBuilderTest(t *testing.T, key string) { | func runContainerdBuilderTest(t *testing.T, key string, distro distributions.Distribution) { | ||||||
| 	basedir := path.Join("tests/containerdbuilder/", key) | 	basedir := path.Join("tests/containerdbuilder/", key) | ||||||
| 
 | 
 | ||||||
| 	nodeUpModelContext, err := BuildNodeupModelContext(basedir) | 	nodeUpModelContext, err := BuildNodeupModelContext(basedir) | ||||||
|  | @ -132,7 +136,7 @@ func runContainerdBuilderTest(t *testing.T, key string) { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	nodeUpModelContext.Distribution = distributions.DistributionUbuntu1604 | 	nodeUpModelContext.Distribution = distro | ||||||
| 
 | 
 | ||||||
| 	nodeUpModelContext.Assets = fi.NewAssetStore("") | 	nodeUpModelContext.Assets = fi.NewAssetStore("") | ||||||
| 	nodeUpModelContext.Assets.AddForTest("containerd", "usr/local/bin/containerd", "testing containerd content") | 	nodeUpModelContext.Assets.AddForTest("containerd", "usr/local/bin/containerd", "testing containerd content") | ||||||
|  |  | ||||||
|  | @ -0,0 +1,39 @@ | ||||||
|  | apiVersion: kops.k8s.io/v1alpha2 | ||||||
|  | kind: Cluster | ||||||
|  | metadata: | ||||||
|  |   name: minimal.example.com | ||||||
|  | spec: | ||||||
|  |   kubernetesApiAccess: | ||||||
|  |     - 0.0.0.0/0 | ||||||
|  |   channel: stable | ||||||
|  |   cloudProvider: aws | ||||||
|  |   configBase: memfs://clusters.example.com/minimal.example.com | ||||||
|  |   containerRuntime: containerd | ||||||
|  |   containerd: | ||||||
|  |     version: 1.4.3 | ||||||
|  |   etcdClusters: | ||||||
|  |     - etcdMembers: | ||||||
|  |         - instanceGroup: master-us-test-1a | ||||||
|  |           name: master-us-test-1a | ||||||
|  |       name: main | ||||||
|  |     - etcdMembers: | ||||||
|  |         - instanceGroup: master-us-test-1a | ||||||
|  |           name: master-us-test-1a | ||||||
|  |       name: events | ||||||
|  |   kubernetesVersion: v1.19.0 | ||||||
|  |   masterInternalName: api.internal.minimal.example.com | ||||||
|  |   masterPublicName: api.minimal.example.com | ||||||
|  |   networkCIDR: 172.20.0.0/16 | ||||||
|  |   networking: | ||||||
|  |     kubenet: {} | ||||||
|  |   nonMasqueradeCIDR: 100.64.0.0/10 | ||||||
|  |   sshAccess: | ||||||
|  |     - 0.0.0.0/0 | ||||||
|  |   topology: | ||||||
|  |     masters: public | ||||||
|  |     nodes: public | ||||||
|  |   subnets: | ||||||
|  |     - cidr: 172.20.32.0/19 | ||||||
|  |       name: us-test-1a | ||||||
|  |       type: Public | ||||||
|  |       zone: us-test-1a | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | contents: "" | ||||||
|  | path: /etc/containerd/config-kops.toml | ||||||
|  | type: file | ||||||
|  | --- | ||||||
|  | contents: |- | ||||||
|  |   [Service] | ||||||
|  |   Environment=CONTAINERD_CONFIG=/etc/containerd/config-kops.toml | ||||||
|  |   EnvironmentFile=/etc/environment | ||||||
|  | onChangeExecute: | ||||||
|  | - - systemctl | ||||||
|  |   - daemon-reload | ||||||
|  | - - systemctl | ||||||
|  |   - restart | ||||||
|  |   - containerd.service | ||||||
|  | - - systemctl | ||||||
|  |   - restart | ||||||
|  |   - kops-configuration.service | ||||||
|  |   - '&' | ||||||
|  | path: /etc/systemd/system/containerd.service.d/10-kops.conf | ||||||
|  | type: file | ||||||
		Loading…
	
		Reference in New Issue