-
-
Notifications
You must be signed in to change notification settings - Fork 751
feat: generate mirrors patch #12265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
talos-bot
merged 1 commit into
siderolabs:main
from
shanduur:feat-generate-mirrors-patch
Nov 24, 2025
Merged
feat: generate mirrors patch #12265
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "io" | ||
| "log" | ||
| "net" | ||
| "net/url" | ||
| "os" | ||
| "os/signal" | ||
| "slices" | ||
|
|
@@ -21,6 +22,7 @@ import ( | |
|
|
||
| "github.com/blang/semver/v4" | ||
| "github.com/dustin/go-humanize" | ||
| "github.com/siderolabs/gen/ensure" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/pflag" | ||
| "go.uber.org/zap" | ||
|
|
@@ -34,8 +36,11 @@ import ( | |
| "github.com/siderolabs/talos/pkg/machinery/api/common" | ||
| "github.com/siderolabs/talos/pkg/machinery/api/machine" | ||
| "github.com/siderolabs/talos/pkg/machinery/client" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/config" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/container" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/encoder" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/types/cri" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/types/meta" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/types/security" | ||
| "github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1" | ||
| "github.com/siderolabs/talos/pkg/machinery/constants" | ||
|
|
@@ -443,6 +448,14 @@ var imageCacheServeCmd = &cobra.Command{ | |
| return fmt.Errorf("failed to create development logger: %w", err) | ||
| } | ||
|
|
||
| if err = generateMirrorsConfigPatch( | ||
| imageCacheServeCmdFlags.address, | ||
| imageCacheServeCmdFlags.mirrors, | ||
| imageCacheServeCmdFlags.tlsCertFile != "" && imageCacheServeCmdFlags.tlsKeyFile != "", | ||
| ); err != nil { | ||
| development.Error("failed to generate Talos config patch for registry mirrors", zap.Error(err)) | ||
| } | ||
|
|
||
| it := func(yield func(string) bool) { | ||
| for _, root := range []string{imageCacheServeCmdFlags.imageCachePath} { | ||
| if !yield(root) { | ||
|
|
@@ -462,9 +475,88 @@ var imageCacheServeCmd = &cobra.Command{ | |
| }, | ||
| } | ||
|
|
||
| //nolint:gocyclo | ||
| func generateMirrorsConfigPatch(addr string, mirrors []string, secure bool) error { | ||
| addresses := []string{} | ||
|
|
||
| host, port, err := net.SplitHostPort(addr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if host == "" || host == "0.0.0.0" || host == "[::]" { | ||
| // list all IPs for the host | ||
| ips, err := net.InterfaceAddrs() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, addr := range ips { | ||
| if ipnet, ok := addr.(*net.IPNet); ok { | ||
| if ipnet.IP.IsLoopback() { | ||
| continue | ||
| } | ||
|
|
||
| addresses = append(addresses, net.JoinHostPort(ipnet.IP.String(), port)) | ||
| } | ||
|
|
||
| if ipa, ok := addr.(*net.IPAddr); ok { | ||
| if ipa.IP.IsLoopback() { | ||
| continue | ||
| } | ||
|
|
||
| addresses = append(addresses, net.JoinHostPort(ipa.IP.String(), port)) | ||
| } | ||
| } | ||
| } else { | ||
| addresses = []string{net.JoinHostPort(host, port)} | ||
| } | ||
|
|
||
| if port == "0" { | ||
| return nil // we do not generate patch for dynamic ports | ||
| } | ||
|
|
||
| patches := make([]config.Document, 0, len(mirrors)) | ||
|
|
||
| prefix := "http://" | ||
| if secure { | ||
| prefix = "https://" | ||
| } | ||
|
|
||
| for _, mirror := range mirrors { | ||
| patch := cri.NewRegistryMirrorConfigV1Alpha1(mirror) | ||
| patch.RegistryEndpoints = []cri.RegistryEndpoint{} | ||
|
|
||
| for _, endpoint := range addresses { | ||
| patch.RegistryEndpoints = append(patch.RegistryEndpoints, cri.RegistryEndpoint{ | ||
| EndpointURL: meta.URL{URL: ensure.Value(url.Parse(prefix + endpoint))}, | ||
| }) | ||
| } | ||
|
|
||
| patches = append(patches, patch) | ||
| } | ||
|
|
||
| ctr, err := container.New(patches...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| patchBytes, err := ctr.EncodeBytes(encoder.WithComments(encoder.CommentsDisabled)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| const patchFile = "image-cache-mirrors-patch.yaml" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we make this configurable?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of our patches are not configurable now. I want to change it at some point, but make it all at once. |
||
|
|
||
| log.Printf("writing config patch to %s", patchFile) | ||
|
|
||
| return os.WriteFile(patchFile, patchBytes, 0o644) | ||
| } | ||
|
|
||
| var imageCacheServeCmdFlags struct { | ||
| imageCachePath string | ||
| address string | ||
| mirrors []string | ||
| tlsCertFile string | ||
| tlsKeyFile string | ||
| } | ||
|
|
@@ -485,7 +577,7 @@ var imageCacheCertGenCmd = &cobra.Command{ | |
| return nil | ||
| } | ||
|
|
||
| if err = generateConfigPatch(caPEM); err != nil { | ||
| if err = generateCAConfigPatch(caPEM); err != nil { | ||
| return err | ||
| } | ||
|
|
||
|
|
@@ -505,7 +597,7 @@ var imageCacheCertGenCmd = &cobra.Command{ | |
| }, | ||
| } | ||
|
|
||
| func generateConfigPatch(caPEM []byte) error { | ||
| func generateCAConfigPatch(caPEM []byte) error { | ||
| patch := security.NewTrustedRootsConfigV1Alpha1() | ||
| patch.MetaName = "image-cache-ca" | ||
| patch.Certificates = string(caPEM) | ||
|
|
@@ -562,6 +654,8 @@ func init() { | |
| imageCacheServeCmd.PersistentFlags().StringVar(&imageCacheServeCmdFlags.imageCachePath, "image-cache-path", "", "directory to save the image cache in flat format") | ||
| imageCacheServeCmd.MarkPersistentFlagRequired("image-cache-path") //nolint:errcheck | ||
| imageCacheServeCmd.PersistentFlags().StringVar(&imageCacheServeCmdFlags.address, "address", constants.RegistrydListenAddress, "address to serve the registry on") | ||
| imageCacheServeCmd.PersistentFlags().StringSliceVar(&imageCacheServeCmdFlags.mirrors, "mirror", []string{"docker.io", "ghcr.io", "registry.k8s.io"}, | ||
| "list of registry mirrors to add to the Talos config patch") | ||
| imageCacheServeCmd.PersistentFlags().StringVar(&imageCacheServeCmdFlags.tlsCertFile, "tls-cert-file", "", "TLS certificate file to use for serving") | ||
| imageCacheServeCmd.PersistentFlags().StringVar(&imageCacheServeCmdFlags.tlsKeyFile, "tls-key-file", "", "TLS key file to use for serving") | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a fatal error, we don't want to crash image-cache server here, so just log it.