GeezerVison is showing github.com — fix: preserve user-speciffide registry on OpenShift redeploy · knative/func@d707c8b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/deploy.go +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -291,9 +291,7 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) { | 291 | 291 | // also update the registry because there is a registry per namespace, | 292 | 292 | // and their name includes the namespace. | 293 | 293 | // This saves needing a manual flag ``--registry={destination namespace registry}`` | 294 | | - if changingNamespace(f) && k8s.IsOpenShift() { | 295 | | - // TODO(lkingland): this appears to force use of the openshift | 296 | | - // internal registry. | | 294 | + if changingNamespace(f) && k8s.IsOpenShift() && k8s.IsOpenShiftInternalRegistry(f.Registry) { | 297 | 295 | f.Registry = "image-registry.openshift-image-registry.svc:5000/" + f.Namespace | 298 | 296 | if cfg.Verbose { | 299 | 297 | fmt.Fprintf(cmd.OutOrStdout(), "Info: Overriding openshift registry to %s\n", f.Registry) | |
Collapse file Copy file name to clipboardExpand all lines: pkg/config/config.go +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -154,7 +154,15 @@ func (c Global) Configure(f fn.Function) fn.Function { | 154 | 154 | f.Deploy.Namespace = c.Namespace | 155 | 155 | } | 156 | 156 | if c.Registry != "" { | 157 | | - f.Registry = c.Registry | | 157 | + // Apply the config registry unless it's an auto-detected OpenShift | | 158 | + // internal registry that would overwrite a user-configured external | | 159 | + // registry. This prevents OpenShift auto-detection (via RegistryDefault) | | 160 | + // from silently overriding a previously stored registry on redeploy. | | 161 | + // Explicit sources (--registry flag, FUNC_REGISTRY env, global config) | | 162 | + // set non-OpenShift values and are always applied. | | 163 | + if f.Registry == "" || !k8s.IsOpenShiftInternalRegistry(c.Registry) { | | 164 | + f.Registry = c.Registry | | 165 | + } | 158 | 166 | } | 159 | 167 | return f | 160 | 168 | } | |
Collapse file Copy file name to clipboardExpand all lines: pkg/config/config_test.go +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -269,6 +269,31 @@ func TestConfigure(t *testing.T) { | 269 | 269 | t.Error("empty cfg.Registry should not mutate f") | 270 | 270 | } | 271 | 271 |
| | 272 | + // An auto-detected OpenShift internal registry should not overwrite | | 273 | + // a function's stored external registry. This is the key guard for | | 274 | + // https://github.com/knative/func/issues/2172 | | 275 | + f = fn.Function{Registry: "docker.io/user"} | | 276 | + cfg = config.Global{Registry: "image-registry.openshift-image-registry.svc:5000/ns"} | | 277 | + f = cfg.Configure(f) | | 278 | + if f.Registry != "docker.io/user" { | | 279 | + t.Errorf("auto-detected OpenShift registry should not overwrite stored external registry, got %q", f.Registry) | | 280 | + } | | 281 | + | | 282 | + // But when the function has no registry yet, the OpenShift default should apply. | | 283 | + f = fn.Function{} | | 284 | + f = cfg.Configure(f) | | 285 | + if f.Registry != "image-registry.openshift-image-registry.svc:5000/ns" { | | 286 | + t.Errorf("OpenShift registry should be applied when function has no registry, got %q", f.Registry) | | 287 | + } | | 288 | + | | 289 | + // An explicit (non-OpenShift) registry in config should always apply, | | 290 | + // even when the function already has a registry. | | 291 | + f = fn.Function{Registry: "docker.io/user"} | | 292 | + cfg = config.Global{Registry: "quay.io/org"} | | 293 | + f = cfg.Configure(f) | | 294 | + if f.Registry != "quay.io/org" { | | 295 | + t.Errorf("explicit config registry should override function registry, got %q", f.Registry) | | 296 | + } | 272 | 297 | } | 273 | 298 |
| 274 | 299 | // TestGet_Invalid ensures that attempting to get the value of a nonexistent | |
Collapse file Copy file name to clipboardExpand all lines: pkg/k8s/openshift.go +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | "crypto/x509" | 6 | 6 | "encoding/pem" | 7 | 7 | "errors" | | 8 | + "strings" | 8 | 9 | "sync" | 9 | 10 | "time" | 10 | 11 |
| @@ -93,6 +94,12 @@ func GetDefaultOpenShiftRegistry() string { | 93 | 94 | return openShiftRegistryHostPort + "/" + ns | 94 | 95 | } | 95 | 96 |
| | 97 | +// IsOpenShiftInternalRegistry returns true if the given registry string | | 98 | +// refers to the OpenShift internal image registry. | | 99 | +func IsOpenShiftInternalRegistry(registry string) bool { | | 100 | + return strings.HasPrefix(registry, openShiftRegistryHost) | | 101 | +} | | 102 | + | 96 | 103 | func GetOpenShiftDockerCredentialLoaders() []creds.CredentialsCallback { | 97 | 104 | conf := GetClientConfig() | 98 | 105 |
| |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package k8s | | 2 | + | | 3 | +import "testing" | | 4 | + | | 5 | +func TestIsOpenShiftInternalRegistry(t *testing.T) { | | 6 | + tests := []struct { | | 7 | + name string | | 8 | + registry string | | 9 | + want bool | | 10 | + }{ | | 11 | + {"internal with port and namespace", "image-registry.openshift-image-registry.svc:5000/mynamespace", true}, | | 12 | + {"internal without port", "image-registry.openshift-image-registry.svc/mynamespace", true}, | | 13 | + {"internal host only", "image-registry.openshift-image-registry.svc", true}, | | 14 | + {"docker.io", "docker.io/user", false}, | | 15 | + {"ghcr.io", "ghcr.io/user", false}, | | 16 | + {"quay.io", "quay.io/user", false}, | | 17 | + {"empty string", "", false}, | | 18 | + } | | 19 | + for _, tt := range tests { | | 20 | + t.Run(tt.name, func(t *testing.T) { | | 21 | + if got := IsOpenShiftInternalRegistry(tt.registry); got != tt.want { | | 22 | + t.Errorf("IsOpenShiftInternalRegistry(%q) = %v, want %v", tt.registry, got, tt.want) | | 23 | + } | | 24 | + }) | | 25 | + } | | 26 | +} |
|