-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed deployment of Elasticsearch via its operator (#234)
Fixed deployment of Elasticsearch via its operator Signed-off-by: Juraci Paixão Kröhling <[email protected]>
- Loading branch information
1 parent
ed9f1b2
commit 401ef74
Showing
16 changed files
with
406 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jaeger | ||
|
||
import ( | ||
"context" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
"github.com/jaegertracing/jaeger-operator/pkg/inventory" | ||
esv1alpha1 "github.com/jaegertracing/jaeger-operator/pkg/storage/elasticsearch/v1alpha1" | ||
) | ||
|
||
func (r *ReconcileJaeger) applyElasticsearches(jaeger v1alpha1.Jaeger, desired []esv1alpha1.Elasticsearch) error { | ||
opts := client.MatchingLabels(map[string]string{ | ||
"app.kubernetes.io/instance": jaeger.Name, | ||
"app.kubernetes.io/managed-by": "jaeger-operator", | ||
}) | ||
list := &esv1alpha1.ElasticsearchList{} | ||
if err := r.client.List(context.Background(), opts, list); err != nil { | ||
return err | ||
} | ||
|
||
logFields := log.WithFields(log.Fields{ | ||
"namespace": jaeger.Namespace, | ||
"instance": jaeger.Name, | ||
}) | ||
|
||
inv := inventory.ForElasticsearches(list.Items, desired) | ||
for _, d := range inv.Create { | ||
logFields.WithField("elasticsearch", d.Name).Debug("creating elasticsearch") | ||
if err := r.client.Create(context.Background(), &d); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, d := range inv.Update { | ||
logFields.WithField("elasticsearch", d.Name).Debug("updating elasticsearch") | ||
if err := r.client.Update(context.Background(), &d); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, d := range inv.Delete { | ||
logFields.WithField("elasticsearch", d.Name).Debug("deleting elasticsearch") | ||
if err := r.client.Delete(context.Background(), &d); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package jaeger | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
esv1alpha1 "github.com/jaegertracing/jaeger-operator/pkg/storage/elasticsearch/v1alpha1" | ||
"github.com/jaegertracing/jaeger-operator/pkg/strategy" | ||
) | ||
|
||
func TestElasticsearchesCreate(t *testing.T) { | ||
// prepare | ||
nsn := types.NamespacedName{ | ||
Name: "TestElasticsearchesCreate", | ||
} | ||
|
||
objs := []runtime.Object{ | ||
v1alpha1.NewJaeger(nsn.Name), | ||
} | ||
|
||
req := reconcile.Request{ | ||
NamespacedName: nsn, | ||
} | ||
|
||
r, cl := getReconciler(objs) | ||
r.strategyChooser = func(jaeger *v1alpha1.Jaeger) strategy.S { | ||
s := strategy.New().WithElasticsearches([]esv1alpha1.Elasticsearch{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: nsn.Name, | ||
}, | ||
}}) | ||
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 := &esv1alpha1.Elasticsearch{} | ||
persistedName := types.NamespacedName{ | ||
Name: nsn.Name, | ||
Namespace: nsn.Namespace, | ||
} | ||
err = cl.Get(context.Background(), persistedName, persisted) | ||
assert.Equal(t, persistedName.Name, persisted.Name) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestElasticsearchesUpdate(t *testing.T) { | ||
// prepare | ||
nsn := types.NamespacedName{ | ||
Name: "TestElasticsearchesUpdate", | ||
} | ||
|
||
orig := esv1alpha1.Elasticsearch{} | ||
orig.Name = nsn.Name | ||
orig.Annotations = map[string]string{"key": "value"} | ||
|
||
objs := []runtime.Object{ | ||
v1alpha1.NewJaeger(nsn.Name), | ||
&orig, | ||
} | ||
|
||
r, cl := getReconciler(objs) | ||
r.strategyChooser = func(jaeger *v1alpha1.Jaeger) strategy.S { | ||
updated := esv1alpha1.Elasticsearch{} | ||
updated.Name = orig.Name | ||
updated.Annotations = map[string]string{"key": "new-value"} | ||
|
||
s := strategy.New().WithElasticsearches([]esv1alpha1.Elasticsearch{updated}) | ||
return s | ||
} | ||
|
||
// test | ||
_, err := r.Reconcile(reconcile.Request{NamespacedName: nsn}) | ||
assert.NoError(t, err) | ||
|
||
// verify | ||
persisted := &esv1alpha1.Elasticsearch{} | ||
persistedName := types.NamespacedName{ | ||
Name: orig.Name, | ||
Namespace: orig.Namespace, | ||
} | ||
err = cl.Get(context.Background(), persistedName, persisted) | ||
assert.Equal(t, "new-value", persisted.Annotations["key"]) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestElasticsearchesDelete(t *testing.T) { | ||
// prepare | ||
nsn := types.NamespacedName{ | ||
Name: "TestElasticsearchesDelete", | ||
} | ||
|
||
orig := esv1alpha1.Elasticsearch{} | ||
orig.Name = nsn.Name | ||
|
||
objs := []runtime.Object{ | ||
v1alpha1.NewJaeger(nsn.Name), | ||
&orig, | ||
} | ||
|
||
r, cl := getReconciler(objs) | ||
r.strategyChooser = func(jaeger *v1alpha1.Jaeger) strategy.S { | ||
return strategy.S{} | ||
} | ||
|
||
// test | ||
_, err := r.Reconcile(reconcile.Request{NamespacedName: nsn}) | ||
assert.NoError(t, err) | ||
|
||
// verify | ||
persisted := &esv1alpha1.Elasticsearch{} | ||
persistedName := types.NamespacedName{ | ||
Name: orig.Name, | ||
Namespace: orig.Namespace, | ||
} | ||
err = cl.Get(context.Background(), persistedName, persisted) | ||
assert.Empty(t, persisted.Name) | ||
assert.Error(t, err) // not found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package inventory | ||
|
||
import ( | ||
esv1alpha1 "github.com/jaegertracing/jaeger-operator/pkg/storage/elasticsearch/v1alpha1" | ||
) | ||
|
||
// Elasticsearch represents the elastic search inventory based on the current and desired states | ||
type Elasticsearch struct { | ||
Create []esv1alpha1.Elasticsearch | ||
Update []esv1alpha1.Elasticsearch | ||
Delete []esv1alpha1.Elasticsearch | ||
} | ||
|
||
// ForElasticsearches builds a new elastic search inventory based on the existing and desired states | ||
func ForElasticsearches(existing []esv1alpha1.Elasticsearch, desired []esv1alpha1.Elasticsearch) Elasticsearch { | ||
update := []esv1alpha1.Elasticsearch{} | ||
mcreate := esMap(desired) | ||
mdelete := esMap(existing) | ||
|
||
for k, v := range mcreate { | ||
if t, ok := mdelete[k]; ok { | ||
tp := t.DeepCopy() | ||
|
||
tp.Spec = v.Spec | ||
tp.ObjectMeta.OwnerReferences = v.ObjectMeta.OwnerReferences | ||
|
||
for k, v := range v.ObjectMeta.Annotations { | ||
tp.ObjectMeta.Annotations[k] = v | ||
} | ||
|
||
for k, v := range v.ObjectMeta.Labels { | ||
tp.ObjectMeta.Labels[k] = v | ||
} | ||
|
||
update = append(update, *tp) | ||
delete(mcreate, k) | ||
delete(mdelete, k) | ||
} | ||
} | ||
|
||
return Elasticsearch{ | ||
Create: esList(mcreate), | ||
Update: update, | ||
Delete: esList(mdelete), | ||
} | ||
} | ||
|
||
func esMap(deps []esv1alpha1.Elasticsearch) map[string]esv1alpha1.Elasticsearch { | ||
m := map[string]esv1alpha1.Elasticsearch{} | ||
for _, d := range deps { | ||
m[d.Name] = d | ||
} | ||
return m | ||
} | ||
|
||
func esList(m map[string]esv1alpha1.Elasticsearch) []esv1alpha1.Elasticsearch { | ||
l := []esv1alpha1.Elasticsearch{} | ||
for _, v := range m { | ||
l = append(l, v) | ||
} | ||
return l | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package inventory | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
esv1alpha1 "github.com/jaegertracing/jaeger-operator/pkg/storage/elasticsearch/v1alpha1" | ||
) | ||
|
||
func TestElasticsearchInventory(t *testing.T) { | ||
toCreate := esv1alpha1.Elasticsearch{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "to-create", | ||
}, | ||
} | ||
toUpdate := esv1alpha1.Elasticsearch{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "to-update", | ||
}, | ||
Spec: esv1alpha1.ElasticsearchSpec{ | ||
ManagementState: esv1alpha1.ManagementStateManaged, | ||
}, | ||
} | ||
updated := esv1alpha1.Elasticsearch{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "to-update", | ||
}, | ||
Spec: esv1alpha1.ElasticsearchSpec{ | ||
ManagementState: esv1alpha1.ManagementStateUnmanaged, | ||
}, | ||
} | ||
toDelete := esv1alpha1.Elasticsearch{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "to-delete", | ||
}, | ||
} | ||
|
||
existing := []esv1alpha1.Elasticsearch{toUpdate, toDelete} | ||
desired := []esv1alpha1.Elasticsearch{updated, toCreate} | ||
|
||
inv := ForElasticsearches(existing, desired) | ||
assert.Len(t, inv.Create, 1) | ||
assert.Equal(t, "to-create", inv.Create[0].Name) | ||
|
||
assert.Len(t, inv.Update, 1) | ||
assert.Equal(t, "to-update", inv.Update[0].Name) | ||
assert.Equal(t, esv1alpha1.ManagementStateUnmanaged, inv.Update[0].Spec.ManagementState) | ||
|
||
assert.Len(t, inv.Delete, 1) | ||
assert.Equal(t, "to-delete", inv.Delete[0].Name) | ||
} |
Oops, something went wrong.