|
| 1 | +// Copyright 2023 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package mirrors |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + _ "embed" |
| 9 | + "fmt" |
| 10 | + "net/url" |
| 11 | + "path" |
| 12 | + "strings" |
| 13 | + "text/template" |
| 14 | + |
| 15 | + cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" |
| 16 | + |
| 17 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/common" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + containerdHostsConfigurationOnRemote = "/etc/containerd/certs.d/_default/hosts.toml" |
| 22 | + secretKeyForCACert = "ca.crt" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + //go:embed templates/hosts.toml.gotmpl |
| 27 | + containerdHostsConfiguration []byte |
| 28 | + |
| 29 | + containerdHostsConfigurationTemplate = template.Must( |
| 30 | + template.New("").Parse(string(containerdHostsConfiguration)), |
| 31 | + ) |
| 32 | + |
| 33 | + //go:embed templates/containerd-registry-config-drop-in.toml |
| 34 | + containerdRegistryConfigDropIn []byte |
| 35 | + containerdRegistryConfigDropInFileOnRemote = common.ContainerdPatchPathOnRemote( |
| 36 | + "registry-config.toml", |
| 37 | + ) |
| 38 | + |
| 39 | + mirrorCACertPathOnRemoteFmt = "/etc/certs/%s.pem" |
| 40 | +) |
| 41 | + |
| 42 | +type containerdConfig struct { |
| 43 | + URL string |
| 44 | + CASecretName string |
| 45 | + CACert string |
| 46 | + Mirror bool |
| 47 | +} |
| 48 | + |
| 49 | +// fileNameFromURL returns a file name for a registry URL. |
| 50 | +// Follows a convention of replacing all non-alphanumeric characters with "-". |
| 51 | +func (c containerdConfig) filePathFromURL() (string, error) { |
| 52 | + registryURL, err := url.ParseRequestURI(c.URL) |
| 53 | + if err != nil { |
| 54 | + return "", fmt.Errorf("failed parsing registry URL: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + registryHostWithPath := registryURL.Host |
| 58 | + if registryURL.Path != "" { |
| 59 | + registryHostWithPath = path.Join(registryURL.Host, registryURL.Path) |
| 60 | + } |
| 61 | + |
| 62 | + replaced := strings.ReplaceAll(registryHostWithPath, "/", "-") |
| 63 | + |
| 64 | + return fmt.Sprintf(mirrorCACertPathOnRemoteFmt, replaced), nil |
| 65 | +} |
| 66 | + |
| 67 | +// Return true if configuration is a mirror or has a CA certificate. |
| 68 | +func (c containerdConfig) needContainerdConfiguration() bool { |
| 69 | + return c.CACert != "" || c.Mirror |
| 70 | +} |
| 71 | + |
| 72 | +// Containerd registry configuration created at /etc/containerd/certs.d/_default/hosts.toml for: |
| 73 | +// |
| 74 | +// 1. Set the default mirror for all registries. |
| 75 | +// The upstream registry will be automatically used after all defined mirrors have been tried. |
| 76 | +// https://github.com/containerd/containerd/blob/main/docs/hosts.md#setup-default-mirror-for-all-registries |
| 77 | +// |
| 78 | +// 2. Setting CA certificate for global image registry mirror and image registries. |
| 79 | +func generateContainerdHostsFile( |
| 80 | + configs []containerdConfig, |
| 81 | +) (*cabpkv1.File, error) { |
| 82 | + if len(configs) == 0 { |
| 83 | + return nil, nil |
| 84 | + } |
| 85 | + |
| 86 | + type templateInput struct { |
| 87 | + URL string |
| 88 | + CACertPath string |
| 89 | + Mirror bool |
| 90 | + } |
| 91 | + |
| 92 | + inputs := make([]templateInput, 0, len(configs)) |
| 93 | + |
| 94 | + for _, config := range configs { |
| 95 | + if !config.needContainerdConfiguration() { |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + formattedURL, err := formatURLForContainerd(config.URL) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed formatting image registry URL for Containerd: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + input := templateInput{ |
| 105 | + URL: formattedURL, |
| 106 | + Mirror: config.Mirror, |
| 107 | + } |
| 108 | + // CA cert is optional for mirror registry. |
| 109 | + // i.e. registry is using signed certificates. Insecure registry will not be allowed. |
| 110 | + if config.CACert != "" { |
| 111 | + var registryCACertPathOnRemote string |
| 112 | + registryCACertPathOnRemote, err = config.filePathFromURL() |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf( |
| 115 | + "failed generating CA certificate file path from URL: %w", |
| 116 | + err, |
| 117 | + ) |
| 118 | + } |
| 119 | + input.CACertPath = registryCACertPathOnRemote |
| 120 | + } |
| 121 | + |
| 122 | + inputs = append(inputs, input) |
| 123 | + } |
| 124 | + |
| 125 | + var b bytes.Buffer |
| 126 | + err := containerdHostsConfigurationTemplate.Execute(&b, inputs) |
| 127 | + if err != nil { |
| 128 | + return nil, fmt.Errorf("failed executing template for Containerd hosts.toml file: %w", err) |
| 129 | + } |
| 130 | + return &cabpkv1.File{ |
| 131 | + Path: containerdHostsConfigurationOnRemote, |
| 132 | + // Trimming the leading and trailing whitespaces in the template did not work as expected with multiple configs. |
| 133 | + Content: fmt.Sprintf("%s\n", strings.TrimSpace(b.String())), |
| 134 | + Permissions: "0600", |
| 135 | + }, nil |
| 136 | +} |
| 137 | + |
| 138 | +func generateRegistryCACertFiles( |
| 139 | + configs []containerdConfig, |
| 140 | +) ([]cabpkv1.File, error) { |
| 141 | + if len(configs) == 0 { |
| 142 | + return nil, nil |
| 143 | + } |
| 144 | + |
| 145 | + var files []cabpkv1.File //nolint:prealloc // We don't know the size of the slice yet. |
| 146 | + |
| 147 | + for _, config := range configs { |
| 148 | + if config.CASecretName == "" { |
| 149 | + continue |
| 150 | + } |
| 151 | + |
| 152 | + registryCACertPathOnRemote, err := config.filePathFromURL() |
| 153 | + if err != nil { |
| 154 | + return nil, fmt.Errorf("failed generating CA certificate file path from URL: %w", err) |
| 155 | + } |
| 156 | + files = append(files, cabpkv1.File{ |
| 157 | + Path: registryCACertPathOnRemote, |
| 158 | + Permissions: "0600", |
| 159 | + ContentFrom: &cabpkv1.FileSource{ |
| 160 | + Secret: cabpkv1.SecretFileSource{ |
| 161 | + Name: config.CASecretName, |
| 162 | + Key: secretKeyForCACert, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }) |
| 166 | + } |
| 167 | + |
| 168 | + return files, nil |
| 169 | +} |
| 170 | + |
| 171 | +func generateContainerdRegistryConfigDropInFile() []cabpkv1.File { |
| 172 | + return []cabpkv1.File{ |
| 173 | + { |
| 174 | + Path: containerdRegistryConfigDropInFileOnRemote, |
| 175 | + Content: string(containerdRegistryConfigDropIn), |
| 176 | + Permissions: "0600", |
| 177 | + }, |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func formatURLForContainerd(uri string) (string, error) { |
| 182 | + mirrorURL, err := url.ParseRequestURI(uri) |
| 183 | + if err != nil { |
| 184 | + return "", fmt.Errorf("failed parsing mirror: %w", err) |
| 185 | + } |
| 186 | + |
| 187 | + mirror := fmt.Sprintf("%s://%s", mirrorURL.Scheme, mirrorURL.Host) |
| 188 | + // assume Containerd expects the following pattern: |
| 189 | + // scheme://host/v2/path |
| 190 | + mirrorPath := "v2" |
| 191 | + if mirrorURL.Path != "" { |
| 192 | + mirrorPath = path.Join(mirrorPath, mirrorURL.Path) |
| 193 | + } |
| 194 | + // using path.Join on all elements incorrectly drops a "/" from "https://" |
| 195 | + return fmt.Sprintf("%s/%s", mirror, mirrorPath), nil |
| 196 | +} |
0 commit comments