mirror of https://github.com/k3s-io/k3s
Only clean up containerd hosts dirs managed by k3s
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit 270f85e468
)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/10873/head
parent
d506595074
commit
1dacf77599
|
@ -1,6 +1,7 @@
|
||||||
package containerd
|
package containerd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -46,7 +47,9 @@ func writeContainerdHosts(cfg *config.Node, containerdConfig templates.Container
|
||||||
hosts := getHostConfigs(containerdConfig.PrivateRegistryConfig, containerdConfig.NoDefaultEndpoint, mirrorAddr)
|
hosts := getHostConfigs(containerdConfig.PrivateRegistryConfig, containerdConfig.NoDefaultEndpoint, mirrorAddr)
|
||||||
|
|
||||||
// Clean up previous configuration templates
|
// Clean up previous configuration templates
|
||||||
os.RemoveAll(cfg.Containerd.Registry)
|
if err := cleanContainerdHosts(cfg.Containerd.Registry, hosts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Write out new templates
|
// Write out new templates
|
||||||
for host, config := range hosts {
|
for host, config := range hosts {
|
||||||
|
@ -67,6 +70,48 @@ func writeContainerdHosts(cfg *config.Node, containerdConfig templates.Container
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cleanContainerdHosts removes any registry host config dirs containing a hosts.toml file
|
||||||
|
// with a header that indicates it was created by k3s, or directories where a hosts.toml
|
||||||
|
// is about to be written. Unmanaged directories not containing this file, or containing
|
||||||
|
// a file without the header, are left alone.
|
||||||
|
func cleanContainerdHosts(dir string, hosts HostConfigs) error {
|
||||||
|
// clean directories for any registries that we are about to generate a hosts.toml for
|
||||||
|
for host := range hosts {
|
||||||
|
hostsDir := filepath.Join(dir, host)
|
||||||
|
os.RemoveAll(hostsDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean directories that contain a hosts.toml with a header indicating it was created by k3s
|
||||||
|
ents, err := os.ReadDir(dir)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ent := range ents {
|
||||||
|
if !ent.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
hostsFile := filepath.Join(dir, ent.Name(), "hosts.toml")
|
||||||
|
file, err := os.Open(hostsFile)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
line, err := bufio.NewReader(file).ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if line == templates.HostsTomlHeader {
|
||||||
|
hostsDir := filepath.Join(dir, ent.Name())
|
||||||
|
os.RemoveAll(hostsDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// getHostConfigs merges the registry mirrors/configs into HostConfig template structs
|
// getHostConfigs merges the registry mirrors/configs into HostConfig template structs
|
||||||
func getHostConfigs(registry *registries.Registry, noDefaultEndpoint bool, mirrorAddr string) HostConfigs {
|
func getHostConfigs(registry *registries.Registry, noDefaultEndpoint bool, mirrorAddr string) HostConfigs {
|
||||||
hosts := map[string]templates.HostConfig{}
|
hosts := map[string]templates.HostConfig{}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/rancher/wharfie/pkg/registries"
|
"github.com/rancher/wharfie/pkg/registries"
|
||||||
|
|
||||||
"github.com/k3s-io/k3s/pkg/daemons/config"
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
||||||
|
"github.com/k3s-io/k3s/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContainerdRuntimeConfig struct {
|
type ContainerdRuntimeConfig struct {
|
||||||
|
@ -40,6 +41,8 @@ type HostConfig struct {
|
||||||
Endpoints []RegistryEndpoint
|
Endpoints []RegistryEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var HostsTomlHeader = "# File generated by " + version.Program + ". DO NOT EDIT."
|
||||||
|
|
||||||
const HostsTomlTemplate = `
|
const HostsTomlTemplate = `
|
||||||
{{- /* */ -}}
|
{{- /* */ -}}
|
||||||
# File generated by {{ .Program }}. DO NOT EDIT.
|
# File generated by {{ .Program }}. DO NOT EDIT.
|
||||||
|
|
Loading…
Reference in New Issue