Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
If ignore label exist don`t add namespace (#4473)
Browse files Browse the repository at this point in the history
If ignore label exits don`t add namespace

Signed-off-by: mudit singh <[email protected]>
Co-authored-by: Shashank Ram <[email protected]>
  • Loading branch information
mudit-01 and shashankram authored Feb 1, 2022
1 parent fb5f9df commit e350bc0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 7 additions & 0 deletions cmd/cli/namespace_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/openservicemesh/osm/pkg/constants"
)

const trueValue = "true"

const namespaceAddDescription = `
This command adds a namespace or set of namespaces to the mesh so that the osm
control plane with the given mesh name can observe resources within that namespace
Expand Down Expand Up @@ -121,6 +123,11 @@ func (a *namespaceAddCmd) run() error {
continue
}

// if ignore label exits don`t add namespace
if val, ok := namespace.ObjectMeta.Labels[constants.IgnoreLabel]; ok && val == trueValue {
return errors.Errorf("Cannot add ignored namespace")
}

var patch string
if a.disableSidecarInjection {
// Patch the namespace with monitoring label.
Expand Down
30 changes: 28 additions & 2 deletions cmd/cli/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/openservicemesh/osm/pkg/constants"
)

const trueValue = "true"

var (
testNamespace = "namespace"
testMeshName = "mesh"
Expand Down Expand Up @@ -62,6 +60,34 @@ var _ = Describe("Running the namespace add command", func() {
})
})

Context("adding namespace to the mesh that is ignored", func() {

BeforeEach(func() {
out = new(bytes.Buffer)
fakeClientSet = fake.NewSimpleClientset()

_, err = addDeployment(fakeClientSet, constants.OSMControllerName, testMeshName, "osm-system-namespace", "testVersion0.1.2", true)
Expect(err).To(BeNil())

nsSpec := createNamespaceSpec(testNamespace, "", false, true)
_, err = fakeClientSet.CoreV1().Namespaces().Create(context.TODO(), nsSpec, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

namespaceAddCmd := &namespaceAddCmd{
out: out,
meshName: testMeshName,
namespaces: []string{testNamespace},
clientSet: fakeClientSet,
}

err = namespaceAddCmd.run()
})

It("error in adding ignored namespace", func() {
Expect(err).To(MatchError("Cannot add ignored namespace"))
})
})

Context("adding namespace to the mesh that is already a part of the mesh", func() {

BeforeEach(func() {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/e2e_ignore_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var _ = OSMDescribe("Ignore Namespaces",
// Create test NS in mesh with ignore label
Expect(Td.CreateNs(ignoreNs, map[string]string{constants.IgnoreLabel: "true"})).To(Succeed())

// Add test NS to mesh with sidecar injection enabled
Expect(Td.AddNsToMesh(true, ignoreNs)).To(Succeed())
// Add test NS(with ignore label) to mesh with sidecar injection enabled, it should not succeed
Expect(Td.AddNsToMesh(true, ignoreNs)).NotTo(Succeed())

By("Ensuring a pod is not injected with a sidecar when added to namespace the ignore, and sidecar injection labels set")

Expand Down

0 comments on commit e350bc0

Please sign in to comment.