-
Notifications
You must be signed in to change notification settings - Fork 719
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
Add only one master node at a time #1654
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd174df
Extract and unit test cluster bootstrap annotation for reuse
sebgl fc7f5b2
Improve test fixtures
sebgl 2d5da78
New expectations: check pod reconciliation vs. actual StatefulSets
sebgl a35007b
Remove expectations check from downscale since done at a higher level
sebgl 5e944f9
Add upscaleState to add only one master node at a time
sebgl 4306cbf
Adapt upscale code to add only one master at a time and patch config …
sebgl 7945110
Remove tests global var that we happen to mutate
sebgl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package driver | ||
|
||
import ( | ||
"github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1alpha1" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/observer" | ||
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s" | ||
) | ||
|
||
const ( | ||
// ClusterUUIDAnnotationName used to store the cluster UUID as an annotation when cluster has been bootstrapped. | ||
ClusterUUIDAnnotationName = "elasticsearch.k8s.elastic.co/cluster-uuid" | ||
) | ||
|
||
// AnnotatedForBootstrap returns true if the cluster has been annotated with the UUID already. | ||
func AnnotatedForBootstrap(cluster v1alpha1.Elasticsearch) bool { | ||
_, bootstrapped := cluster.Annotations[ClusterUUIDAnnotationName] | ||
return bootstrapped | ||
} | ||
|
||
func ReconcileClusterUUID(c k8s.Client, cluster *v1alpha1.Elasticsearch, observedState observer.State) error { | ||
if AnnotatedForBootstrap(*cluster) { | ||
// already annotated, nothing to do. | ||
return nil | ||
} | ||
if clusterIsBootstrapped(observedState) { | ||
// cluster bootstrapped but not annotated yet | ||
return annotateWithUUID(cluster, observedState, c) | ||
} | ||
// cluster not bootstrapped yet | ||
return nil | ||
} | ||
|
||
// clusterIsBootstrapped returns true if the cluster has formed and has a UUID. | ||
func clusterIsBootstrapped(observedState observer.State) bool { | ||
return observedState.ClusterState != nil && len(observedState.ClusterState.ClusterUUID) > 0 | ||
} | ||
|
||
// annotateWithUUID annotates the cluster with its UUID, to mark it as "bootstrapped". | ||
func annotateWithUUID(cluster *v1alpha1.Elasticsearch, observedState observer.State, c k8s.Client) error { | ||
log.Info("Annotating bootstrapped cluster with its UUID", "namespace", cluster.Namespace, "es_name", cluster.Name) | ||
if cluster.Annotations == nil { | ||
cluster.Annotations = make(map[string]string) | ||
} | ||
cluster.Annotations[ClusterUUIDAnnotationName] = observedState.ClusterState.ClusterUUID | ||
return c.Update(cluster) | ||
} |
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,92 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package driver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
"github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1alpha1" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/client" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/observer" | ||
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
) | ||
|
||
func bootstrappedES() *v1alpha1.Elasticsearch { | ||
return &v1alpha1.Elasticsearch{ObjectMeta: metav1.ObjectMeta{Name: "cluster", Annotations: map[string]string{ClusterUUIDAnnotationName: "uuid"}}} | ||
} | ||
|
||
func notBootstrappedES() *v1alpha1.Elasticsearch { | ||
return &v1alpha1.Elasticsearch{ObjectMeta: metav1.ObjectMeta{Name: "cluster"}} | ||
} | ||
|
||
func TestAnnotatedForBootstrap(t *testing.T) { | ||
require.True(t, AnnotatedForBootstrap(*bootstrappedES())) | ||
require.False(t, AnnotatedForBootstrap(*notBootstrappedES())) | ||
} | ||
|
||
func Test_annotateWithUUID(t *testing.T) { | ||
require.NoError(t, v1alpha1.AddToScheme(scheme.Scheme)) | ||
|
||
cluster := notBootstrappedES() | ||
observedState := observer.State{ClusterState: &client.ClusterState{ClusterUUID: "cluster-uuid"}} | ||
k8sClient := k8s.WrapClient(fake.NewFakeClient(cluster)) | ||
|
||
err := annotateWithUUID(cluster, observedState, k8sClient) | ||
require.NoError(t, err) | ||
require.True(t, AnnotatedForBootstrap(*cluster)) | ||
|
||
var retrieved v1alpha1.Elasticsearch | ||
err = k8sClient.Get(k8s.ExtractNamespacedName(cluster), &retrieved) | ||
require.NoError(t, err) | ||
require.True(t, AnnotatedForBootstrap(retrieved)) | ||
} | ||
|
||
func TestReconcileClusterUUID(t *testing.T) { | ||
require.NoError(t, v1alpha1.AddToScheme(scheme.Scheme)) | ||
tests := []struct { | ||
name string | ||
c k8s.Client | ||
cluster *v1alpha1.Elasticsearch | ||
observedState observer.State | ||
wantCluster *v1alpha1.Elasticsearch | ||
}{ | ||
{ | ||
name: "already annotated", | ||
cluster: bootstrappedES(), | ||
wantCluster: bootstrappedES(), | ||
}, | ||
{ | ||
name: "not annotated, but not bootstrapped yet (cluster state empty)", | ||
cluster: notBootstrappedES(), | ||
observedState: observer.State{ClusterState: nil}, | ||
wantCluster: notBootstrappedES(), | ||
}, | ||
{ | ||
name: "not annotated, but not bootstrapped yet (cluster UUID empty)", | ||
cluster: notBootstrappedES(), | ||
observedState: observer.State{ClusterState: &client.ClusterState{ClusterUUID: ""}}, | ||
wantCluster: notBootstrappedES(), | ||
}, | ||
{ | ||
name: "not annotated, but bootstrapped", | ||
c: k8s.WrapClient(fake.NewFakeClient(notBootstrappedES())), | ||
cluster: notBootstrappedES(), | ||
observedState: observer.State{ClusterState: &client.ClusterState{ClusterUUID: "uuid"}}, | ||
wantCluster: bootstrappedES(), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ReconcileClusterUUID(tt.c, tt.cluster, tt.observedState) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.wantCluster, tt.cluster) | ||
}) | ||
} | ||
} |
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,36 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package driver | ||
|
||
import ( | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset" | ||
) | ||
|
||
func (d *defaultDriver) expectationsMet(actualStatefulSets sset.StatefulSetList) (bool, error) { | ||
if !d.Expectations.GenerationExpected(actualStatefulSets.ObjectMetas()...) { | ||
// Our cache of StatefulSets is out of date compared to previous reconciliation operations. | ||
// Continuing with the reconciliation at this point may lead to: | ||
// - errors on rejected sset updates (conflict since cached resource out of date): that's ok | ||
// - calling ES orchestration settings (zen1/zen2/allocation excludes) with wrong assumptions: that's not ok | ||
// Hence we choose to abort the reconciliation early: will run again later with an updated cache. | ||
log.V(1).Info("StatefulSet cache out-of-date, re-queueing", "namespace", d.ES.Namespace, "es_name", d.ES.Name) | ||
return false, nil | ||
} | ||
|
||
podsReconciled, err := actualStatefulSets.PodReconciliationDone(d.Client) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !podsReconciled { | ||
// Pods we have in the cache do not match StatefulSets we have in the cache. | ||
// This can happen if some pods have been scheduled for creation/deletion/upgrade | ||
// but the operation has not happened or been observed yet. | ||
// Continuing with nodes reconciliation at this point would be dangerous, since | ||
// we may update ES orchestration settings (zen1/zen2/allocation excludes) with | ||
// wrong assumptions (especially on master-eligible and ES version mismatches). | ||
return false, nil | ||
} | ||
return true, 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,61 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package driver | ||
|
||
import ( | ||
"testing" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
"github.com/elastic/cloud-on-k8s/pkg/controller/common" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/common/reconciler" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset" | ||
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_defaultDriver_expectationsMet(t *testing.T) { | ||
d := &defaultDriver{DefaultDriverParameters{ | ||
Expectations: reconciler.NewExpectations(), | ||
Client: k8s.WrapClient(fake.NewFakeClient()), | ||
}} | ||
|
||
// no expectations set | ||
met, err := d.expectationsMet(sset.StatefulSetList{}) | ||
require.NoError(t, err) | ||
require.True(t, met) | ||
|
||
// a sset generation is expected | ||
statefulSet := sset.TestSset{Name: "sset"}.Build() | ||
statefulSet.Generation = 123 | ||
d.Expectations.ExpectGeneration(statefulSet.ObjectMeta) | ||
// but not met yet | ||
statefulSet.Generation = 122 | ||
met, err = d.expectationsMet(sset.StatefulSetList{statefulSet}) | ||
require.NoError(t, err) | ||
require.False(t, met) | ||
// met now | ||
statefulSet.Generation = 123 | ||
met, err = d.expectationsMet(sset.StatefulSetList{statefulSet}) | ||
require.NoError(t, err) | ||
require.True(t, met) | ||
|
||
// we expect some sset replicas to exist | ||
// but corresponding pod does not exist | ||
statefulSet.Spec.Replicas = common.Int32(1) | ||
// expectations should not be met: we miss a pod | ||
met, err = d.expectationsMet(sset.StatefulSetList{statefulSet}) | ||
require.NoError(t, err) | ||
require.False(t, met) | ||
|
||
// add the missing pod | ||
pod := sset.TestPod{Name: "sset-0", StatefulSetName: statefulSet.Name}.Build() | ||
d.Client = k8s.WrapClient(fake.NewFakeClient(&pod)) | ||
// expectations should be met | ||
met, err = d.expectationsMet(sset.StatefulSetList{statefulSet}) | ||
require.NoError(t, err) | ||
require.True(t, met) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So after this state we may have updated some ssets, so we'll end up with sset update conflicts in next steps (downscale, rolling upgrade). Which is fine, but I'm wondering whether we should maybe either:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may also recheck expectations before
downscale
andhandleRollingUpgrades
It would make it explicit and maybe less error prone when this code will be updated.