-
Notifications
You must be signed in to change notification settings - Fork 176
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
feat: replace image registry dynamically #8018
base: main
Are you sure you want to change the base?
Changes from 6 commits
ab1a0d9
91df060
008e87d
4ac55aa
e9e6a00
16ac308
2e4c7a5
cc1b236
afed11b
08b87ae
1a2c615
0d2f2dd
3e98dad
521ada0
981ec63
4c75882
815afa6
db3cdb3
9cc0260
8cd721a
0f2558b
916749b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
|
||
This file is part of KubeBlocks project | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
|
||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package controllerutil | ||
|
||
import ( | ||
cjc7373 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Import the crypto sha256 algorithm for the docker image parser to work | ||
_ "crypto/sha256" | ||
"fmt" | ||
"strings" | ||
|
||
// Import the crypto/sha512 algorithm for the docker image parser to work with 384 and 512 sha hashes | ||
_ "crypto/sha512" | ||
|
||
"github.com/distribution/reference" | ||
|
||
"github.com/apecloud/kubeblocks/pkg/constant" | ||
viper "github.com/apecloud/kubeblocks/pkg/viperx" | ||
) | ||
|
||
// RegistryNamespaceConfig maps registry namespaces. | ||
// | ||
// Quote from https://docs.docker.com/reference/cli/docker/image/tag/ | ||
// > While the OCI Distribution Specification supports more than two slash-separated components, | ||
// > most registries only support two slash-separated components. | ||
// > For Docker's public registry, the path format is as follows: | ||
// > [NAMESPACE/]REPOSITORY: The first, optional component is typically a user's or an organization's | ||
// namespace. The second, mandatory component is the repository name. When the namespace is | ||
// not present, Docker uses `library` as the default namespace. | ||
// | ||
// So if there are more than two components, specify them both, or they won't be matched. | ||
type RegistryNamespaceConfig struct { | ||
From string | ||
To string | ||
} | ||
|
||
type RegistryConfig struct { | ||
From string | ||
To string | ||
Namespaces []RegistryNamespaceConfig | ||
} | ||
|
||
type RegistriesConfig struct { | ||
DefaultRegistry string | ||
cjc7373 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DefaultNamespace string | ||
Registries []RegistryConfig | ||
} | ||
|
||
var registriesConfig = RegistriesConfig{} | ||
|
||
func init() { | ||
ReloadRegistryConfig() | ||
} | ||
|
||
func ReloadRegistryConfig() { | ||
// TODO: this is needed in componnet controller test, is there a better way? | ||
registriesConfig = RegistriesConfig{} | ||
if err := viper.UnmarshalKey(constant.CfgRegistries, ®istriesConfig); err != nil { | ||
zjx20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
panic(err) | ||
} | ||
|
||
for _, registry := range registriesConfig.Registries { | ||
if len(registry.From) == 0 { | ||
panic("from can't be empty") | ||
} | ||
|
||
for _, namespace := range registry.Namespaces { | ||
if len(namespace.From) == 0 || len(namespace.To) == 0 { | ||
panic("namespace can't be empty") | ||
} | ||
} | ||
} | ||
} | ||
|
||
// For a detailed explanation of an image's format, see: | ||
// https://pkg.go.dev/github.com/distribution/reference | ||
|
||
// if registry is omitted, the default (docker hub) will be added. | ||
// if namespace is omiited when using docker hub, the default namespace (library) will be added. | ||
func parseImageName(image string) ( | ||
host string, namespace string, repository string, remainder string, err error, | ||
) { | ||
named, err := reference.ParseNormalizedNamed(image) | ||
if err != nil { | ||
return | ||
} | ||
|
||
tagged, ok := named.(reference.Tagged) | ||
if ok { | ||
remainder += ":" + tagged.Tag() | ||
} | ||
|
||
digested, ok := named.(reference.Digested) | ||
if ok { | ||
remainder += "@" + digested.Digest().String() | ||
} | ||
|
||
host = reference.Domain(named) | ||
|
||
pathSplit := strings.Split(reference.Path(named), "/") | ||
if len(pathSplit) > 1 { | ||
namespace = strings.Join(pathSplit[:len(pathSplit)-1], "/") | ||
} | ||
repository = pathSplit[len(pathSplit)-1] | ||
|
||
return | ||
} | ||
|
||
func ReplaceImageRegistry(image string) (string, error) { | ||
registry, namespace, repository, remainder, err := parseImageName(image) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
chooseRegistry := func() string { | ||
if registriesConfig.DefaultRegistry != "" { | ||
return registriesConfig.DefaultRegistry | ||
} else { | ||
return registry | ||
} | ||
} | ||
|
||
chooseNamespace := func() string { | ||
if registriesConfig.DefaultNamespace != "" { | ||
return registriesConfig.DefaultNamespace | ||
} else { | ||
return namespace | ||
} | ||
} | ||
|
||
var dstRegistry, dstNamespace string | ||
for _, registryMapping := range registriesConfig.Registries { | ||
if registryMapping.From == registry { | ||
if len(registryMapping.To) != 0 { | ||
dstRegistry = registryMapping.To | ||
} else { | ||
dstRegistry = chooseRegistry() | ||
} | ||
|
||
for _, namespaceConf := range registryMapping.Namespaces { | ||
if namespace == namespaceConf.From { | ||
dstNamespace = namespaceConf.To | ||
break | ||
} | ||
} | ||
|
||
if dstNamespace == "" { | ||
dstNamespace = namespace | ||
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. so if namespace is not found, the original namespace rather than the defaultNamespace is used. 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. Maybe add a type RegistryConfig struct {
From string
To string
DefaultNamespace string
Namespaces []RegistryNamespaceConfig
} When the namespace is not found in 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.
It does seem better. |
||
} | ||
|
||
break | ||
} | ||
} | ||
|
||
// no match in registriesConf.Registries | ||
if dstRegistry == "" { | ||
dstRegistry = chooseRegistry() | ||
dstNamespace = chooseNamespace() | ||
} | ||
|
||
if dstNamespace == "" { | ||
return fmt.Sprintf("%v/%v%v", dstRegistry, repository, remainder), nil | ||
} | ||
return fmt.Sprintf("%v/%v/%v%v", dstRegistry, dstNamespace, repository, remainder), nil | ||
} |
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.
A global flag is hard to config and maintain for the user, why not using the kb manager config CM?
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.
It does uses the manager config CM. This flag is the first level key in the config.