squakez commented on code in PR #6866: URL: https://github.com/apache/camel-k/pull/6866#discussion_r4205522831
########## .github/workflows/network-policy.yml: ########## @@ -0,0 +1,132 @@ +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +name: network-policy + +on: + pull_request: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + push: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + workflow_dispatch: + +permissions: + contents: read + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CAMEL_K_TEST_TIMEOUT_SHORT: 2m + CAMEL_K_TEST_TIMEOUT_MEDIUM: 5m + CAMEL_K_TEST_TIMEOUT_LONG: 10m + +jobs: + network-policy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + persist-credentials: false + submodules: recursive + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version-file: 'go.mod' + check-latest: true + + - name: Set up Kubectl + uses: azure/setup-kubectl@829323503d1be3d00ac8346e5391ca0b07a9ab0d + + - name: Start Minikube with Calico + uses: medyagh/[email protected] + with: + cpus: max + memory: max + container-runtime: docker + cni: calico + + - name: Build Camel K images + shell: bash + run: | + eval "$(minikube -p minikube docker-env)" + LAST_RELEASED_VERSION="$(make get-last-released-version)" + echo "LAST_RELEASED_VERSION=$LAST_RELEASED_VERSION" >> "$GITHUB_ENV" + NOTEST=true make images + + - name: Install operator + shell: bash + run: | + kubectl create ns camel-k + kubectl create configmap camel-k-operator-configmap-configuration \ + --from-literal=ENABLE_DEV_REGISTRY_SECRET="true" \ Review Comment: We don't need this workflow to use registry secret ########## .github/workflows/network-policy.yml: ########## @@ -0,0 +1,132 @@ +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +name: network-policy + +on: + pull_request: + branches: + - main + paths: Review Comment: I think it makes sense to run the test always as any change outside the Service trait could influence this test as well. ########## .github/workflows/network-policy.yml: ########## @@ -0,0 +1,132 @@ +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +name: network-policy + +on: + pull_request: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + push: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + workflow_dispatch: + +permissions: + contents: read + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CAMEL_K_TEST_TIMEOUT_SHORT: 2m + CAMEL_K_TEST_TIMEOUT_MEDIUM: 5m + CAMEL_K_TEST_TIMEOUT_LONG: 10m + +jobs: + network-policy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + persist-credentials: false + submodules: recursive + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version-file: 'go.mod' + check-latest: true + + - name: Set up Kubectl + uses: azure/setup-kubectl@829323503d1be3d00ac8346e5391ca0b07a9ab0d + + - name: Start Minikube with Calico + uses: medyagh/[email protected] + with: + cpus: max + memory: max + container-runtime: docker + cni: calico + + - name: Build Camel K images + shell: bash + run: | + eval "$(minikube -p minikube docker-env)" + LAST_RELEASED_VERSION="$(make get-last-released-version)" + echo "LAST_RELEASED_VERSION=$LAST_RELEASED_VERSION" >> "$GITHUB_ENV" + NOTEST=true make images + + - name: Install operator + shell: bash + run: | + kubectl create ns camel-k + kubectl create configmap camel-k-operator-configmap-configuration \ + --from-literal=ENABLE_DEV_REGISTRY_SECRET="true" \ + --from-literal=MAVEN_REPOSITORIES_ALLOWED="https://maven.repository.redhat.com/ga@id=redhat" \ + -n camel-k + echo "E2E_TEST_REGISTRY_SECRET_COPY=true" >> "$GITHUB_ENV" + + kubectl apply -k install/overlays/all-namespaces/ --server-side --force-conflicts + kubectl wait --for=condition=available deployment/camel-k-operator -n camel-k --timeout=60s + + - name: Run NetworkPolicy E2E test + shell: bash + run: | + set -o pipefail + go test -p 1 -timeout 45m -v \ + ./e2e/common/traits \ + --parallel 1 \ + -tags=integration \ + -run '^TestServiceNetworkPolicy$' 2>&1 | tee network-policy-test.log + + - name: Collect Kubernetes diagnostics Review Comment: Not needed really. If the test fails we need to probably run it locally and verify what's going on. ########## docs/modules/traits/pages/service.adoc: ########## Review Comment: We could add an example to illustrate how it works ########## .github/workflows/network-policy.yml: ########## @@ -0,0 +1,132 @@ +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +name: network-policy + +on: + pull_request: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + push: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + workflow_dispatch: + +permissions: + contents: read + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CAMEL_K_TEST_TIMEOUT_SHORT: 2m + CAMEL_K_TEST_TIMEOUT_MEDIUM: 5m + CAMEL_K_TEST_TIMEOUT_LONG: 10m + +jobs: + network-policy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + persist-credentials: false + submodules: recursive + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version-file: 'go.mod' + check-latest: true + + - name: Set up Kubectl + uses: azure/setup-kubectl@829323503d1be3d00ac8346e5391ca0b07a9ab0d + + - name: Start Minikube with Calico + uses: medyagh/[email protected] + with: + cpus: max + memory: max + container-runtime: docker + cni: calico + + - name: Build Camel K images + shell: bash + run: | + eval "$(minikube -p minikube docker-env)" + LAST_RELEASED_VERSION="$(make get-last-released-version)" + echo "LAST_RELEASED_VERSION=$LAST_RELEASED_VERSION" >> "$GITHUB_ENV" + NOTEST=true make images + + - name: Install operator + shell: bash + run: | + kubectl create ns camel-k + kubectl create configmap camel-k-operator-configmap-configuration \ + --from-literal=ENABLE_DEV_REGISTRY_SECRET="true" \ + --from-literal=MAVEN_REPOSITORIES_ALLOWED="https://maven.repository.redhat.com/ga@id=redhat" \ + -n camel-k + echo "E2E_TEST_REGISTRY_SECRET_COPY=true" >> "$GITHUB_ENV" + + kubectl apply -k install/overlays/all-namespaces/ --server-side --force-conflicts + kubectl wait --for=condition=available deployment/camel-k-operator -n camel-k --timeout=60s + + - name: Run NetworkPolicy E2E test Review Comment: Better create a dedicated `make test-network-policy` as we are doing for the rest of wf ########## .github/workflows/network-policy.yml: ########## @@ -0,0 +1,132 @@ +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +name: network-policy + +on: + pull_request: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + push: + branches: + - main + paths: + - 'pkg/apis/camel/v1/trait/service.go' + - 'pkg/apis/camel/v1/trait/zz_generated.deepcopy.go' + - 'pkg/trait/service.go' + - 'pkg/trait/service_test.go' + - 'e2e/common/traits/network_policy_test.go' + - 'e2e/common/traits/files/PlatformHttpServer.java' + - '.github/workflows/network-policy.yml' + workflow_dispatch: + +permissions: + contents: read + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CAMEL_K_TEST_TIMEOUT_SHORT: 2m + CAMEL_K_TEST_TIMEOUT_MEDIUM: 5m + CAMEL_K_TEST_TIMEOUT_LONG: 10m + +jobs: + network-policy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + persist-credentials: false + submodules: recursive + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version-file: 'go.mod' + check-latest: true + + - name: Set up Kubectl + uses: azure/setup-kubectl@829323503d1be3d00ac8346e5391ca0b07a9ab0d + + - name: Start Minikube with Calico + uses: medyagh/[email protected] + with: + cpus: max + memory: max + container-runtime: docker + cni: calico Review Comment: We may just include a param in the existing `infra-settings` action and make the rest straightforward instead ########## e2e/common/traits/network_policy_test.go: ########## @@ -0,0 +1,148 @@ +//go:build integration +// +build 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 common + +import ( + "context" + "fmt" + "testing" + + . "github.com/onsi/gomega" + + corev1 "k8s.io/api/core/v1" + networkingv1 "k8s.io/api/networking/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" + + . "github.com/apache/camel-k/v2/e2e/support" + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" +) + +func TestServiceNetworkPolicy(t *testing.T) { + WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) { + namespace, err := TestClient(t).CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{}) + g.Expect(err).ToNot(HaveOccurred()) + + labels := namespace.Labels + if labels == nil { + labels = map[string]string{} + } + labels["network-policy-test"] = "enabled" + namespace.Labels = labels + _, err = TestClient(t).CoreV1().Namespaces().Update(ctx, namespace, metav1.UpdateOptions{}) + g.Expect(err).ToNot(HaveOccurred()) + + name := RandomizedSuffixName("network-policy-server") + g.Expect(KamelRun(t, ctx, ns, "files/PlatformHttpServer.java", + "-t", "service.enabled=true", + "-t", "service.network-policy-enabled=true", + "-t", "service.network-policy-namespace-selector.'network-policy-test'=enabled", + "-t", "service.network-policy-pod-selector.app=allowed", + "--name", name, + ).Execute()).To(Succeed()) + + g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady), TestTimeoutMedium). + Should(Equal(corev1.ConditionTrue)) + g.Eventually(NetworkPolicyByName(t, ctx, ns, name), TestTimeoutShort).ShouldNot(BeNil()) + + allowedPod := networkPolicyClientPod(name+"-allowed", ns, name, "allowed") + deniedPod := networkPolicyClientPod(name+"-denied", ns, name, "denied") + _, err = TestClient(t).CoreV1().Pods(ns).Create(ctx, allowedPod, metav1.CreateOptions{}) + g.Expect(err).ToNot(HaveOccurred()) + _, err = TestClient(t).CoreV1().Pods(ns).Create(ctx, deniedPod, metav1.CreateOptions{}) + g.Expect(err).ToNot(HaveOccurred()) + + t.Cleanup(func() { + _ = TestClient(t).CoreV1().Pods(ns).Delete(ctx, allowedPod.Name, metav1.DeleteOptions{}) + _ = TestClient(t).CoreV1().Pods(ns).Delete(ctx, deniedPod.Name, metav1.DeleteOptions{}) + }) + + g.Eventually(PodPhase(t, ctx, ns, allowedPod.Name), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) + g.Eventually(PodPhase(t, ctx, ns, deniedPod.Name), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) + + g.Eventually(Logs(t, ctx, ns, allowedPod.Name, corev1.PodLogOptions{}), TestTimeoutMedium). + Should(ContainSubstring("ACCESS_GRANTED")) + g.Eventually(Logs(t, ctx, ns, deniedPod.Name, corev1.PodLogOptions{}), TestTimeoutMedium). + Should(ContainSubstring("ACCESS_DENIED")) + }) +} + +func networkPolicyClientPod(name, namespace, serviceName, app string) *corev1.Pod { Review Comment: It can work, but it would be better instead to run a new consumer Camel route (such as a timer to http) in order to make it easier to maintain in the long term, instead of using a third party container and a very low level network access. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
