Skip to content
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

connectivity: use the right test namespace in cnps #330

Merged
merged 2 commits into from
Sep 1, 2021

Conversation

bmcustodio
Copy link
Contributor

@bmcustodio bmcustodio commented Jun 17, 2021

Previously, the CNPs created during the connectivity test referenced cilium-test in matchLabels as opposed to the user-specified namespace. This commit addresses that by looking up potential matches and doing the proper replacement.

Copy link
Contributor

@ti-mo ti-mo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: #275. Also, see #276, which makes spinning down test namespaces quite time-consuming at the moment. Should be an easy fix, though. Let's hold off on generating random namespaces for now.

I think a solution to this problem might require some more thought. Populating the namespace field is trivial, but the target field of matchLabel annotations looks a bit trickier. Any thoughts?

@@ -32,6 +32,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
// testNamespacePlaceholder will get replaced with the test namespace in use when parsing CPNs from YAML.
testNamespacePlaceholder = "${TEST_NAMESPACE}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the placeholder idea, that will create complexity sprawl in the long run.

My idea was to have policies in yaml form with e.g. the namespace field empty and populate them in Go during execution. (empty fields to make sure we don't inadvertently clobber them) This would mean these policies can no longer be applied using kubectl, but that's not the case with this approach either.

Copy link
Contributor Author

@bmcustodio bmcustodio Jun 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mean these policies can no longer be applied using kubectl, but that's not the case with this approach either.

cat connectivity/manifests/client-egress-to-echo.yaml| TEST_NAMESPACE=foo envsubst | k apply -f- works 🙂 Of course it isn't as simple as just kubectling, but it does actually work.

@@ -58,7 +58,7 @@ func (k *K8sUninstaller) Uninstall(ctx context.Context) error {
return err
}

k.Log("🔥 Deleting cilium-test namespace...")
k.Log("🔥 Deleting the %q test namespace...", k.params.TestNamespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require passing the correct test namespace to cilium uninstall as well, which a user would quite easily overlook. Not a comment on this PR, but we've discussed before that this code shouldn't be here, only cilium test should be responsible for managing the lifecycle of the test namespace.

@bmcustodio
Copy link
Contributor Author

bmcustodio commented Jun 17, 2021

I think a solution to this problem might require some more thought.

@ti-mo that's why I was proposing this as a quick, interim fix until we figure it out. 🙂 At least in the meantime users wouldn't be running into this issue.

Copy link
Member

@nbusseneau nbusseneau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as Timo, I'll approve because I do think the quick fix is sound. I would suggest tracking in an issue if we end up merging this.

Copy link
Member

@tklauser tklauser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of the placeholder idea either and I think that it will probably stick around for quite a while, even if we create an issue to eventually replace it 😉

How about modifying the policy in Go code as suggested by @ti-mo? Or is there any problem with that approach?

Would something like along the lines of the following, naïve, untested change work?

diff --git a/connectivity/check/test.go b/connectivity/check/test.go
index 634ee982acc0..31606ad6917a 100644
--- a/connectivity/check/test.go
+++ b/connectivity/check/test.go
@@ -22,6 +22,7 @@ import (
        "sync"
        "time"
 
+       k8sConst "github.com/cilium/cilium/pkg/k8s/apis/cilium.io"
        ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
 )
 
@@ -209,6 +210,19 @@ func (t *Test) WithPolicy(policy string) *Test {
                t.Fatal("Error parsing policy YAML: %w", err)
        }
 
+       for i := range pl {
+               pl[i].Namespace = t.ctx.params.TestNamespace
+               if pl[i].Spec != nil {
+                       for _, e := range pl[i].Spec.Egress {
+                               for _, es := range e.ToEndpoints {
+                                       if _, ok := es.MatchLabels[k8sConst.PodNamespaceLabel]; ok {
+                                               es.MatchLabels[k8sConst.PodNamespaceLabel] = t.ctx.params.TestNamespace
+                                       }
+                               }
+                       }
+               }
+       }
+
        if err := t.addCNPs(pl...); err != nil {
                t.Fatal("adding CNPs to policy context: %w", err)
        }

@bmcustodio
Copy link
Contributor Author

@tklauser nice one — I have based the new approach on that, with slight changes (cc @ti-mo @jrajahalme).

Copy link
Member

@tklauser tklauser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@bmcustodio bmcustodio temporarily deployed to ci September 1, 2021 07:50 Inactive
@bmcustodio bmcustodio force-pushed the bruno-fix-test-namespace branch from 53f129d to a59701e Compare September 1, 2021 07:59
@bmcustodio bmcustodio temporarily deployed to ci September 1, 2021 07:59 Inactive
@tklauser tklauser requested a review from christarazi September 1, 2021 08:42
@jrajahalme jrajahalme added ready-to-merge This PR has passed all tests and received consensus from code owners to merge. and removed ready-to-merge This PR has passed all tests and received consensus from code owners to merge. labels Sep 1, 2021
@tklauser tklauser removed the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Sep 1, 2021
@tklauser
Copy link
Member

tklauser commented Sep 1, 2021

Removed the ready-to-merge label to give @christarazi a chance to review again re. #330 (comment)

@maintainer-s-little-helper maintainer-s-little-helper bot removed the request for review from a team September 1, 2021 16:15
Copy link
Member

@christarazi christarazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's squash the last commit into the first

@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Sep 1, 2021
@tklauser tklauser force-pushed the bruno-fix-test-namespace branch from a59701e to 974527d Compare September 1, 2021 16:29
@tklauser
Copy link
Member

tklauser commented Sep 1, 2021

Let's squash the last commit into the first

Done and force pushed.

@tklauser tklauser removed the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Sep 1, 2021
Previously, the CNPs created during the connectivity test referenced
'cilium-test' in 'matchLabels' as opposed to the user-specified
namespace. This commit addresses that by looking up potential matches
and doing the proper replacement.

Signed-off-by: Bruno Miguel Custódio <[email protected]>
Signed-off-by: Bruno Miguel Custódio <[email protected]>
@tklauser tklauser force-pushed the bruno-fix-test-namespace branch from 974527d to c616cca Compare September 1, 2021 16:30
@tklauser tklauser temporarily deployed to ci September 1, 2021 16:30 Inactive
@tklauser tklauser merged commit 55b3dca into master Sep 1, 2021
@tklauser tklauser deleted the bruno-fix-test-namespace branch September 1, 2021 17:21
sayboras added a commit to sayboras/cilium-cli that referenced this pull request Sep 28, 2022
As the connectivity test can be from another namespace, which is passed
by --test-namespace CLI flag, we should not have any hard coded namespace
in all related policy manifests.

The namespace label is automatically added as part of cilium#330, so we don't
need to hard code it in match labels.

Signed-off-by: Tam Mach <[email protected]>
sayboras added a commit to sayboras/cilium-cli that referenced this pull request Sep 28, 2022
As the connectivity test can be from another namespace, which is passed
by --test-namespace CLI flag, we should not have any hard coded namespace
in all related policy manifests.

The namespace label is automatically added as part of cilium#330, so we don't
need to hard code it in match labels.

Related: cilium#1112

Signed-off-by: Tam Mach <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants