Skip to content

Commit 7c07895

Browse files
authored
Fix cluster UUID race condition, add regression UT (#1977)
1 parent 252a00f commit 7c07895

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pkg/controller/elasticsearch/driver/bootstrap.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
const (
1616
// ClusterUUIDAnnotationName used to store the cluster UUID as an annotation when cluster has been bootstrapped.
1717
ClusterUUIDAnnotationName = "elasticsearch.k8s.elastic.co/cluster-uuid"
18+
formingClusterUUID = "_na_"
1819
)
1920

2021
// AnnotatedForBootstrap returns true if the cluster has been annotated with the UUID already.
@@ -75,7 +76,9 @@ func clusterNeedsReBootstrap(client k8s.Client, es *v1beta1.Elasticsearch) (bool
7576

7677
// clusterIsBootstrapped returns true if the cluster has formed and has a UUID.
7778
func clusterIsBootstrapped(observedState observer.State) bool {
78-
return observedState.ClusterInfo != nil && len(observedState.ClusterInfo.ClusterUUID) > 0
79+
return observedState.ClusterInfo != nil &&
80+
len(observedState.ClusterInfo.ClusterUUID) > 0 &&
81+
observedState.ClusterInfo.ClusterUUID != formingClusterUUID
7982
}
8083

8184
// annotateWithUUID annotates the cluster with its UUID, to mark it as "bootstrapped".

pkg/controller/elasticsearch/driver/bootstrap_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1beta1"
1616
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/client"
17+
esclient "github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/client"
1718
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/observer"
1819
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -81,6 +82,41 @@ func Test_annotateWithUUID(t *testing.T) {
8182
require.True(t, AnnotatedForBootstrap(retrieved))
8283
}
8384

85+
func Test_clusterIsBootstrapped(t *testing.T) {
86+
tests := []struct {
87+
name string
88+
state observer.State
89+
want bool
90+
}{
91+
{
92+
name: "empty state",
93+
state: observer.State{},
94+
want: false,
95+
},
96+
{
97+
name: "cluster uuid empty",
98+
state: observer.State{ClusterInfo: &esclient.Info{}},
99+
want: false,
100+
},
101+
{
102+
name: "cluster uuid _na_ (not available) yet, cluster is still forming",
103+
state: observer.State{ClusterInfo: &esclient.Info{ClusterUUID: "_na_"}},
104+
want: false,
105+
},
106+
{
107+
name: "cluster uuid set, cluster bootstrapped",
108+
state: observer.State{ClusterInfo: &esclient.Info{ClusterUUID: "6902c192-ec1d-11e9-81b4-2a2ae2dbcce4"}},
109+
want: true,
110+
},
111+
}
112+
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
require.Equal(t, tt.want, clusterIsBootstrapped(tt.state))
116+
})
117+
}
118+
}
119+
84120
func TestReconcileClusterUUID(t *testing.T) {
85121
require.NoError(t, v1beta1.AddToScheme(scheme.Scheme))
86122
tests := []struct {

0 commit comments

Comments
 (0)