2019-04-19 21:08:05 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2023-11-30 02:14:01 +00:00
|
|
|
"bytes"
|
|
|
|
"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"
|
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
|
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
|
|
|
|
URI string
|
|
|
|
Rewrites map[string]string
|
|
|
|
Config registries.RegistryConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type HostConfig struct {
|
2024-01-31 18:09:43 +00:00
|
|
|
DefaultEndpoint string
|
|
|
|
Program string
|
|
|
|
Endpoints []RegistryEndpoint
|
2023-11-30 02:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const HostsTomlTemplate = `
|
|
|
|
{{- /* */ -}}
|
|
|
|
# File generated by {{ .Program }}. DO NOT EDIT.
|
2024-01-31 18:09:43 +00:00
|
|
|
{{ if .DefaultEndpoint }}server = "{{ .DefaultEndpoint }}"{{ end }}
|
2023-11-30 02:14:01 +00:00
|
|
|
|
|
|
|
{{ range $e := .Endpoints -}}
|
|
|
|
[host."{{ $e.URI }}"]
|
|
|
|
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 }}
|
|
|
|
[host."{{ $e.URI }}".rewrite]
|
|
|
|
{{- 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
|
|
|
|
}
|