This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit f77b55362b894143b7326c2ca955fba0b1298bec Author: Antonin Stefanutti <[email protected]> AuthorDate: Tue Jan 11 09:27:13 2022 +0100 fix(e2e): Add container registry to NO_PROXY --- e2e/common/traits/environment_test.go | 26 ++++++-- e2e/support/regexp.go | 119 ++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 7 deletions(-) diff --git a/e2e/common/traits/environment_test.go b/e2e/common/traits/environment_test.go index 57c0201..d17bc5a 100644 --- a/e2e/common/traits/environment_test.go +++ b/e2e/common/traits/environment_test.go @@ -24,6 +24,7 @@ package traits import ( "fmt" + "os" "strings" "testing" @@ -38,21 +39,32 @@ import ( func TestEnvironmentTrait(t *testing.T) { WithNewTestNamespace(t, func(ns string) { - // Retrieve the Kubernetes Service ClusterIPs to populate the NO_PROXY environment variable - svc := Service("default", "kubernetes")() - Expect(svc).NotTo(BeNil()) - + // HTTP proxy configuration + httpProxy := "http://proxy" noProxy := []string{ ".cluster.local", ".svc", "localhost", ".apache.org", } + + // Retrieve the Kubernetes Service ClusterIPs to populate the NO_PROXY environment variable + svc := Service("default", "kubernetes")() + Expect(svc).NotTo(BeNil()) + noProxy = append(noProxy, svc.Spec.ClusterIPs...) + // Retrieve the internal container registry to populate the NO_PROXY environment variable + if registry, ok := os.LookupEnv("KAMEL_INSTALL_REGISTRY"); ok { + domain := RegistryRegexp.FindString(registry) + Expect(domain).NotTo(BeNil()) + domain = strings.Split(domain, ":")[0] + noProxy = append(noProxy, domain) + } + // Install Camel K with the HTTP proxy environment variable Expect(Kamel("install", "-n", ns, - "--operator-env-vars", fmt.Sprintf("HTTP_PROXY=http://proxy"), + "--operator-env-vars", fmt.Sprintf("HTTP_PROXY=%s", httpProxy), "--operator-env-vars", "NO_PROXY="+strings.Join(noProxy, ","), ).Execute()).To(Succeed()) @@ -65,7 +77,7 @@ func TestEnvironmentTrait(t *testing.T) { Expect(IntegrationPod(ns, "java")()).To(WithTransform(podEnvVars, And( ContainElement(corev1.EnvVar{Name: "CAMEL_K_VERSION", Value: defaults.Version}), ContainElement(corev1.EnvVar{Name: "NAMESPACE", Value: ns}), - ContainElement(corev1.EnvVar{Name: "HTTP_PROXY", Value: "http://proxy"}), + ContainElement(corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy}), ContainElement(corev1.EnvVar{Name: "NO_PROXY", Value: strings.Join(noProxy, ",")}), ))) }) @@ -97,7 +109,7 @@ func TestEnvironmentTrait(t *testing.T) { Expect(IntegrationPod(ns, "java")()).To(WithTransform(podEnvVars, And( ContainElement(corev1.EnvVar{Name: "CAMEL_K_VERSION", Value: defaults.Version}), ContainElement(corev1.EnvVar{Name: "NAMESPACE", Value: ns}), - Not(ContainElement(corev1.EnvVar{Name: "HTTP_PROXY", Value: "http://proxy"})), + Not(ContainElement(corev1.EnvVar{Name: "HTTP_PROXY", Value: httpProxy})), Not(ContainElement(corev1.EnvVar{Name: "NO_PROXY", Value: strings.Join(noProxy, ",")})), ))) }) diff --git a/e2e/support/regexp.go b/e2e/support/regexp.go new file mode 100644 index 0000000..ba44168 --- /dev/null +++ b/e2e/support/regexp.go @@ -0,0 +1,119 @@ +//go:build integration +// +build integration + +// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration" + +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package support + +import "regexp" + +var ( + // alphaNumericRegexp defines the alpha numeric atom, typically a + // component of names. This only allows lower case characters and digits. + alphaNumericRegexp = match(`[a-z0-9]+`) + + // separatorRegexp defines the separators allowed to be embedded in name + // components. This allow one period, one or two underscore and multiple + // dashes. + separatorRegexp = match(`(?:[._]|__|[-]*)`) + + // nameComponentRegexp restricts registry path component names to start + // with at least one letter or number, with following parts able to be + // separated by one period, one or two underscore and multiple dashes. + nameComponentRegexp = expression( + alphaNumericRegexp, + optional(repeated(separatorRegexp, alphaNumericRegexp))) + + // domainComponentRegexp restricts the registry domain component of a + // repository name to start with a component as defined by DomainRegexp + // and followed by an optional port. + domainComponentRegexp = match(`(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])`) + + // DomainRegexp defines the structure of potential domain components + // that may be part of image names. This is purposely a subset of what is + // allowed by DNS to ensure backwards compatibility with Docker image + // names. + DomainRegexp = expression( + domainComponentRegexp, + optional(repeated(literal(`.`), domainComponentRegexp)), + optional(literal(`:`), match(`[0-9]+`))) + + RegistryRegexp = anchored( + capture(DomainRegexp), + optional( + literal(`/`), + nameComponentRegexp, + optional(repeated(literal(`/`), nameComponentRegexp)), + ), + ) +) + +// match compiles the string to a regular expression. +var match = regexp.MustCompile + +// literal compiles s into a literal regular expression, escaping any regexp +// reserved characters. +func literal(s string) *regexp.Regexp { + re := match(regexp.QuoteMeta(s)) + + if _, complete := re.LiteralPrefix(); !complete { + panic("must be a literal") + } + + return re +} + +// expression defines a full expression, where each regular expression must +// follow the previous. +func expression(res ...*regexp.Regexp) *regexp.Regexp { + var s string + for _, re := range res { + s += re.String() + } + + return match(s) +} + +// optional wraps the expression in a non-capturing group and makes the +// production optional. +func optional(res ...*regexp.Regexp) *regexp.Regexp { + return match(group(expression(res...)).String() + `?`) +} + +// repeated wraps the regexp in a non-capturing group to get one or more +// matches. +func repeated(res ...*regexp.Regexp) *regexp.Regexp { + return match(group(expression(res...)).String() + `+`) +} + +// group wraps the regexp in a non-capturing group. +func group(res ...*regexp.Regexp) *regexp.Regexp { + return match(`(?:` + expression(res...).String() + `)`) +} + +// capture wraps the expression in a capturing group. +func capture(res ...*regexp.Regexp) *regexp.Regexp { + return match(`(` + expression(res...).String() + `)`) +} + +// anchored anchors the regular expression by adding start and end delimiters. +func anchored(res ...*regexp.Regexp) *regexp.Regexp { + return match(`^` + expression(res...).String() + `$`) +}
