2019-04-19 21:08:05 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2023-11-30 02:14:01 +00:00
|
|
|
"bytes"
|
2024-02-02 02:51:13 +00:00
|
|
|
"net/url"
|
2023-11-30 02:14:01 +00:00
|
|
|
"text/template"
|
|
|
|
|
2021-05-10 22:58:41 +00:00
|
|
|
"github.com/rancher/wharfie/pkg/registries"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2024-09-04 22:13:00 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2019-04-19 21:08:05 +00:00
|
|
|
)
|
|
|
|
|
2021-09-15 21:31:11 +00:00
|
|
|
type ContainerdRuntimeConfig struct {
|
|
|
|
RuntimeType string
|
|
|
|
BinaryName string
|
|
|
|
}
|
|
|
|
|
2019-04-19 21:08:05 +00:00
|
|
|
type ContainerdConfig struct {
|
2019-10-07 23:04:58 +00:00
|
|
|
NodeConfig *config.Node
|
2021-03-02 08:57:40 +00:00
|
|
|
DisableCgroup bool
|
2022-04-18 23:06:50 +00:00
|
|
|
SystemdCgroup bool
|
2019-10-07 23:04:58 +00:00
|
|
|
IsRunningInUserNS bool
|
2022-07-28 08:53:56 +00:00
|
|
|
EnableUnprivileged bool
|
2023-11-30 02:14:01 +00:00
|
|
|
NoDefaultEndpoint bool
|
2024-10-30 20:55:40 +00:00
|
|
|
NonrootDevices bool
|
2021-05-10 22:58:41 +00:00
|
|
|
PrivateRegistryConfig *registries.Registry
|
2021-09-15 21:31:11 +00:00
|
|
|
ExtraRuntimes map[string]ContainerdRuntimeConfig
|
2023-03-13 20:42:17 +00:00
|
|
|
Program string
|
2019-04-19 21:08:05 +00:00
|
|
|
}
|
2023-11-30 02:14:01 +00:00
|
|
|
|
|
|
|
type RegistryEndpoint struct {
|
|
|
|
OverridePath bool
|
2024-02-02 02:51:13 +00:00
|
|
|
URL *url.URL
|
2023-11-30 02:14:01 +00:00
|
|
|
Rewrites map[string]string
|
|
|
|
Config registries.RegistryConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type HostConfig struct {
|
2024-02-02 02:51:13 +00:00
|
|
|
Default *RegistryEndpoint
|
|
|
|
Program string
|
|
|
|
Endpoints []RegistryEndpoint
|
2023-11-30 02:14:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 18:51:42 +00:00
|
|
|
var HostsTomlHeader = "# File generated by " + version.Program + ". DO NOT EDIT.\n"
|
2024-09-04 22:13:00 +00:00
|
|
|
|
2023-11-30 02:14:01 +00:00
|
|
|
const HostsTomlTemplate = `
|
|
|
|
{{- /* */ -}}
|
|
|
|
# File generated by {{ .Program }}. DO NOT EDIT.
|
2024-02-02 02:51:13 +00:00
|
|
|
{{ with $e := .Default }}
|
|
|
|
{{- if $e.URL }}
|
|
|
|
server = "{{ $e.URL }}"
|
|
|
|
capabilities = ["pull", "resolve", "push"]
|
|
|
|
{{ end }}
|
|
|
|
{{- if $e.Config.TLS }}
|
|
|
|
{{- if $e.Config.TLS.CAFile }}
|
|
|
|
ca = [{{ printf "%q" $e.Config.TLS.CAFile }}]
|
|
|
|
{{- end }}
|
|
|
|
{{- if or $e.Config.TLS.CertFile $e.Config.TLS.KeyFile }}
|
|
|
|
client = [[{{ printf "%q" $e.Config.TLS.CertFile }}, {{ printf "%q" $e.Config.TLS.KeyFile }}]]
|
|
|
|
{{- end }}
|
|
|
|
{{- if $e.Config.TLS.InsecureSkipVerify }}
|
|
|
|
skip_verify = true
|
|
|
|
{{- end }}
|
|
|
|
{{ end }}
|
|
|
|
{{ end }}
|
2024-04-01 23:09:26 +00:00
|
|
|
[host]
|
2023-11-30 02:14:01 +00:00
|
|
|
{{ range $e := .Endpoints -}}
|
2024-02-02 02:51:13 +00:00
|
|
|
[host."{{ $e.URL }}"]
|
2023-11-30 02:14:01 +00:00
|
|
|
capabilities = ["pull", "resolve"]
|
|
|
|
{{- if $e.OverridePath }}
|
|
|
|
override_path = true
|
|
|
|
{{- end }}
|
|
|
|
{{- if $e.Config.TLS }}
|
|
|
|
{{- if $e.Config.TLS.CAFile }}
|
|
|
|
ca = [{{ printf "%q" $e.Config.TLS.CAFile }}]
|
|
|
|
{{- end }}
|
|
|
|
{{- if or $e.Config.TLS.CertFile $e.Config.TLS.KeyFile }}
|
|
|
|
client = [[{{ printf "%q" $e.Config.TLS.CertFile }}, {{ printf "%q" $e.Config.TLS.KeyFile }}]]
|
|
|
|
{{- end }}
|
|
|
|
{{- if $e.Config.TLS.InsecureSkipVerify }}
|
|
|
|
skip_verify = true
|
|
|
|
{{- end }}
|
|
|
|
{{ end }}
|
|
|
|
{{- if $e.Rewrites }}
|
2024-02-02 02:51:13 +00:00
|
|
|
[host."{{ $e.URL }}".rewrite]
|
2023-11-30 02:14:01 +00:00
|
|
|
{{- range $pattern, $replace := $e.Rewrites }}
|
|
|
|
"{{ $pattern }}" = "{{ $replace }}"
|
|
|
|
{{- end }}
|
|
|
|
{{ end }}
|
|
|
|
{{ end -}}
|
|
|
|
`
|
|
|
|
|
|
|
|
func ParseTemplateFromConfig(templateBuffer string, config interface{}) (string, error) {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
t := template.Must(template.New("compiled_template").Funcs(templateFuncs).Parse(templateBuffer))
|
|
|
|
template.Must(t.New("base").Parse(ContainerdConfigTemplate))
|
|
|
|
if err := t.Execute(out, config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return out.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseHostsTemplateFromConfig(templateBuffer string, config interface{}) (string, error) {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
t := template.Must(template.New("compiled_template").Funcs(templateFuncs).Parse(templateBuffer))
|
|
|
|
if err := t.Execute(out, config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return out.String(), nil
|
|
|
|
}
|