Skip to content

Commit

Permalink
Changed to always use namespace when a name is involved
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Jun 28, 2019
1 parent bd76c19 commit 2ef6f4d
Show file tree
Hide file tree
Showing 73 changed files with 1,291 additions and 318 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ e2e-tests-streaming: prepare-e2e-tests es kafka
.PHONY: run
run: crd
@rm -rf /tmp/_cert*
@bash -c 'trap "exit 0" INT; OPERATOR_NAME=${OPERATOR_NAME} KUBERNETES_CONFIG=${KUBERNETES_CONFIG} WATCH_NAMESPACE=${WATCH_NAMESPACE} go run -ldflags ${LD_FLAGS} main.go start'
@bash -c 'trap "exit 0" INT; OPERATOR_NAME=${OPERATOR_NAME} KUBERNETES_CONFIG=${KUBERNETES_CONFIG} WATCH_NAMESPACE=${WATCH_NAMESPACE} go run -ldflags ${LD_FLAGS} main.go start --log-level=debug'

.PHONY: set-max-map-count
set-max-map-count:
Expand Down
11 changes: 6 additions & 5 deletions pkg/account/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
)

func TestWithSecurityNil(t *testing.T) {
jaeger := v1.NewJaeger("TestWithOAuthProxyNil")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithOAuthProxyNil"})
assert.Equal(t, v1.IngressSecurityNone, jaeger.Spec.Ingress.Security)
sas := Get(jaeger)
assert.Len(t, sas, 1)
assert.Equal(t, getMain(jaeger), sas[0])
}

func TestWithSecurityNone(t *testing.T) {
jaeger := v1.NewJaeger("TestWithOAuthProxyFalse")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithOAuthProxyFalse"})
jaeger.Spec.Ingress.Security = v1.IngressSecurityNone
sas := Get(jaeger)
assert.Len(t, sas, 1)
assert.Equal(t, getMain(jaeger), sas[0])
}

func TestWithSecurityOAuthProxy(t *testing.T) {
jaeger := v1.NewJaeger("TestWithOAuthProxyTrue")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithOAuthProxyTrue"})
jaeger.Spec.Ingress.Security = v1.IngressSecurityOAuthProxy

assert.Len(t, Get(jaeger), 2)
}

func TestJaegerName(t *testing.T) {
jaeger := v1.NewJaeger("foo")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "foo"})
jaeger.Spec.ServiceAccount = "bar"
jaeger.Spec.Collector.ServiceAccount = "col-sa"
jaeger.Spec.Query.ServiceAccount = "query-sa"
Expand Down
7 changes: 4 additions & 3 deletions pkg/account/oauth_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
)

func TestOAuthRedirectReference(t *testing.T) {
jaeger := v1.NewJaeger("TestOAuthRedirectReference")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestOAuthRedirectReference"})
jaeger.Spec.Ingress.Security = v1.IngressSecurityOAuthProxy

assert.Contains(t, getOAuthRedirectReference(jaeger), jaeger.Name)
}

func TestOAuthProxy(t *testing.T) {
jaeger := v1.NewJaeger("TestOAuthProxy")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestOAuthProxy"})
jaeger.Spec.Ingress.Security = v1.IngressSecurityOAuthProxy

assert.Equal(t, OAuthProxy(jaeger).Name, fmt.Sprintf("%s-ui-proxy", jaeger.Name))
Expand Down
10 changes: 7 additions & 3 deletions pkg/apis/jaegertracing/v1/builder.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package v1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

// NewJaeger returns a new Jaeger instance with the given name
func NewJaeger(name string) *Jaeger {
func NewJaeger(nsn types.NamespacedName) *Jaeger {
return &Jaeger{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: nsn.Name,
Namespace: nsn.Namespace,
},
}
}
13 changes: 7 additions & 6 deletions pkg/config/sampling/sampling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
)

func TestNoSamplingConfig(t *testing.T) {
jaeger := v1.NewJaeger("TestNoSamplingConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestNoSamplingConfig"})

config := NewConfig(jaeger)
cm := config.Get()
Expand All @@ -19,7 +20,7 @@ func TestNoSamplingConfig(t *testing.T) {

func TestWithEmptySamplingConfig(t *testing.T) {
uiconfig := v1.NewFreeForm(map[string]interface{}{})
jaeger := v1.NewJaeger("TestWithEmptySamplingConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithEmptySamplingConfig"})
jaeger.Spec.UI.Options = uiconfig

config := NewConfig(jaeger)
Expand All @@ -36,7 +37,7 @@ func TestWithSamplingConfig(t *testing.T) {
},
})
json := `{"default_strategy":{"param":"20","type":"probabilistic"}}`
jaeger := v1.NewJaeger("TestWithSamplingConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithSamplingConfig"})
jaeger.Spec.Sampling.Options = samplingconfig

config := NewConfig(jaeger)
Expand All @@ -45,7 +46,7 @@ func TestWithSamplingConfig(t *testing.T) {
}

func TestUpdateNoSamplingConfig(t *testing.T) {
jaeger := v1.NewJaeger("TestUpdateNoSamplingConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestUpdateNoSamplingConfig"})

commonSpec := v1.JaegerCommonSpec{}
options := []string{}
Expand All @@ -65,7 +66,7 @@ func TestUpdateWithSamplingConfig(t *testing.T) {
"gaID": "UA-000000-2",
},
})
jaeger := v1.NewJaeger("TestUpdateWithSamplingConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestUpdateWithSamplingConfig"})
jaeger.Spec.UI.Options = uiconfig

commonSpec := v1.JaegerCommonSpec{}
Expand Down
13 changes: 7 additions & 6 deletions pkg/config/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
)

func TestNoUIConfig(t *testing.T) {
jaeger := v1.NewJaeger("TestNoUIConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestNoUIConfig"})

config := NewUIConfig(jaeger)
dep := config.Get()
Expand All @@ -18,7 +19,7 @@ func TestNoUIConfig(t *testing.T) {

func TestWithEmptyUIConfig(t *testing.T) {
uiconfig := v1.NewFreeForm(map[string]interface{}{})
jaeger := v1.NewJaeger("TestWithEmptyUIConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithEmptyUIConfig"})
jaeger.Spec.UI.Options = uiconfig

config := NewUIConfig(jaeger)
Expand All @@ -33,7 +34,7 @@ func TestWithUIConfig(t *testing.T) {
},
})
json := `{"tracking":{"gaID":"UA-000000-2"}}`
jaeger := v1.NewJaeger("TestWithUIConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestWithUIConfig"})
jaeger.Spec.UI.Options = uiconfig

config := NewUIConfig(jaeger)
Expand All @@ -42,7 +43,7 @@ func TestWithUIConfig(t *testing.T) {
}

func TestUpdateNoUIConfig(t *testing.T) {
jaeger := v1.NewJaeger("TestUpdateNoUIConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestUpdateNoUIConfig"})

commonSpec := v1.JaegerCommonSpec{}
options := []string{}
Expand All @@ -59,7 +60,7 @@ func TestUpdateWithUIConfig(t *testing.T) {
"gaID": "UA-000000-2",
},
})
jaeger := v1.NewJaeger("TestUpdateWithUIConfig")
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestUpdateWithUIConfig"})
jaeger.Spec.UI.Options = uiconfig

commonSpec := v1.JaegerCommonSpec{}
Expand Down
20 changes: 15 additions & 5 deletions pkg/controller/jaeger/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package jaeger
import (
"context"

log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/inventory"
)

func (r *ReconcileJaeger) applyAccounts(jaeger v1.Jaeger, desired []corev1.ServiceAccount) error {
opts := client.MatchingLabels(map[string]string{
opts := client.InNamespace(jaeger.Namespace).MatchingLabels(map[string]string{
"app.kubernetes.io/instance": jaeger.Name,
"app.kubernetes.io/managed-by": "jaeger-operator",
})
Expand All @@ -22,21 +23,30 @@ func (r *ReconcileJaeger) applyAccounts(jaeger v1.Jaeger, desired []corev1.Servi

inv := inventory.ForAccounts(list.Items, desired)
for _, d := range inv.Create {
jaeger.Logger().WithField("account", d.Name).Debug("creating service account")
jaeger.Logger().WithFields(log.Fields{
"account": d.Name,
"namespace": d.Namespace,
}).Debug("creating service account")
if err := r.client.Create(context.Background(), &d); err != nil {
return err
}
}

for _, d := range inv.Update {
jaeger.Logger().WithField("account", d.Name).Debug("updating service account")
jaeger.Logger().WithFields(log.Fields{
"account": d.Name,
"namespace": d.Namespace,
}).Debug("updating service account")
if err := r.client.Update(context.Background(), &d); err != nil {
return err
}
}

for _, d := range inv.Delete {
jaeger.Logger().WithField("account", d.Name).Debug("deleting service account")
jaeger.Logger().WithFields(log.Fields{
"account": d.Name,
"namespace": d.Namespace,
}).Debug("deleting service account")
if err := r.client.Delete(context.Background(), &d); err != nil {
return err
}
Expand Down
65 changes: 61 additions & 4 deletions pkg/controller/jaeger/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/strategy"
)

Expand All @@ -22,7 +22,7 @@ func TestServiceAccountCreate(t *testing.T) {
}

objs := []runtime.Object{
v1.NewJaeger(nsn.Name),
v1.NewJaeger(nsn),
}

r, cl := getReconciler(objs)
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestServiceAccountUpdate(t *testing.T) {
orig.Annotations = map[string]string{"key": "value"}

objs := []runtime.Object{
v1.NewJaeger(nsn.Name),
v1.NewJaeger(nsn),
&orig,
}

Expand Down Expand Up @@ -106,7 +106,7 @@ func TestServiceAccountDelete(t *testing.T) {
orig.Name = nsn.Name

objs := []runtime.Object{
v1.NewJaeger(nsn.Name),
v1.NewJaeger(nsn),
&orig,
}

Expand All @@ -129,3 +129,60 @@ func TestServiceAccountDelete(t *testing.T) {
assert.Empty(t, persisted.Name)
assert.Error(t, err) // not found
}

func TestAccountCreateExistingNameInAnotherNamespace(t *testing.T) {
// prepare
nsn := types.NamespacedName{
Name: "TestAccountCreateExistingNameInAnotherNamespace",
Namespace: "tenant1",
}
nsnExisting := types.NamespacedName{
Name: "TestAccountCreateExistingNameInAnotherNamespace",
Namespace: "tenant2",
}

objs := []runtime.Object{
v1.NewJaeger(nsn),
v1.NewJaeger(nsnExisting),
&corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: nsnExisting.Name,
Namespace: nsnExisting.Namespace,
},
},
}

req := reconcile.Request{
NamespacedName: nsn,
}

r, cl := getReconciler(objs)
r.strategyChooser = func(jaeger *v1.Jaeger) strategy.S {
s := strategy.New().WithAccounts([]corev1.ServiceAccount{{
ObjectMeta: metav1.ObjectMeta{
Name: nsn.Name,
Namespace: nsn.Namespace,
},
}})
return s
}

// test
res, err := r.Reconcile(req)

// verify
assert.NoError(t, err)
assert.False(t, res.Requeue, "We don't requeue for now")

persisted := &corev1.ServiceAccount{}
err = cl.Get(context.Background(), nsn, persisted)
assert.NoError(t, err)
assert.Equal(t, nsn.Name, persisted.Name)
assert.Equal(t, nsn.Namespace, persisted.Namespace)

persistedExisting := &corev1.ServiceAccount{}
err = cl.Get(context.Background(), nsnExisting, persistedExisting)
assert.NoError(t, err)
assert.Equal(t, nsnExisting.Name, persistedExisting.Name)
assert.Equal(t, nsnExisting.Namespace, persistedExisting.Namespace)
}
Loading

0 comments on commit 2ef6f4d

Please sign in to comment.