Skip to content

Commit 08a3638

Browse files
author
Josef Karasek
committed
Bug 1707323: Operator may attempt to write into nil map
panic: assignment to entry in nil map [recovered] panic: assignment to entry in nil map
1 parent 1ea563a commit 08a3638

File tree

5 files changed

+150
-1
lines changed

5 files changed

+150
-1
lines changed

pkg/k8shandler/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func calculateReplicaCount(dpl *api.Elasticsearch) int {
5757
case api.ZeroRedundancy:
5858
return 0
5959
default:
60+
if dataNodeCount == 1 {
61+
return 0
62+
}
6063
return 1
6164
}
6265
}

pkg/k8shandler/deployment.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,14 @@ func (node *deploymentNode) isChanged() bool {
538538
nodeContainer := node.self.Spec.Template.Spec.Containers[index]
539539
desiredContainer := desired.Spec.Template.Spec.Containers[index]
540540

541+
if nodeContainer.Resources.Requests == nil {
542+
nodeContainer.Resources.Requests = v1.ResourceList{}
543+
}
544+
545+
if nodeContainer.Resources.Limits == nil {
546+
nodeContainer.Resources.Limits = v1.ResourceList{}
547+
}
548+
541549
// check that both exist
542550

543551
if nodeContainer.Image != desiredContainer.Image {

pkg/k8shandler/statefulset.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ func (node *statefulSetNode) isChanged() bool {
475475
nodeContainer := node.self.Spec.Template.Spec.Containers[index]
476476
desiredContainer := desired.Spec.Template.Spec.Containers[index]
477477

478+
if nodeContainer.Resources.Requests == nil {
479+
nodeContainer.Resources.Requests = v1.ResourceList{}
480+
}
481+
482+
if nodeContainer.Resources.Limits == nil {
483+
nodeContainer.Resources.Limits = v1.ResourceList{}
484+
}
485+
478486
// check that both exist
479487

480488
if nodeContainer.Image != desiredContainer.Image {

pkg/k8shandler/util.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ func isValidDataCount(dpl *api.Elasticsearch) bool {
121121
func isValidRedundancyPolicy(dpl *api.Elasticsearch) bool {
122122
dataCount := int(getDataCount(dpl))
123123

124+
switch dpl.Spec.RedundancyPolicy {
125+
case "":
126+
case api.ZeroRedundancy:
127+
case api.SingleRedundancy:
128+
case api.MultipleRedundancy:
129+
case api.FullRedundancy:
130+
default:
131+
return false
132+
}
133+
124134
return !(dataCount == 1 && dpl.Spec.RedundancyPolicy == api.SingleRedundancy)
125135
}
126136

@@ -149,7 +159,7 @@ func isValidConf(dpl *api.Elasticsearch) error {
149159
if err := updateConditionWithRetry(dpl, v1.ConditionTrue, updateInvalidReplicationCondition); err != nil {
150160
return err
151161
}
152-
return fmt.Errorf("Wrong RedundancyPolicy selected. Choose different RedundancyPolicy or add more nodes with data roles")
162+
return fmt.Errorf("Wrong RedundancyPolicy selected '%s'. Choose different RedundancyPolicy or add more nodes with data roles", dpl.Spec.RedundancyPolicy)
153163
} else {
154164
if err := updateConditionWithRetry(dpl, v1.ConditionFalse, updateInvalidReplicationCondition); err != nil {
155165
return err

pkg/k8shandler/util_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package k8shandler
22

33
import (
44
"testing"
5+
6+
api "github.com/openshift/elasticsearch-operator/pkg/apis/elasticsearch/v1"
57
)
68

79
func TestSelectorsBothUndefined(t *testing.T) {
@@ -104,3 +106,121 @@ func TestSelectorsCommonOverwritten(t *testing.T) {
104106
t.Errorf("Expected %v but got %v", expected, actual)
105107
}
106108
}
109+
110+
func TestNoRedundancyPolicySpecified(t *testing.T) {
111+
112+
esCR := &api.Elasticsearch{
113+
Spec: api.ElasticsearchSpec{},
114+
}
115+
116+
isValid := isValidRedundancyPolicy(esCR)
117+
118+
if !isValid {
119+
t.Errorf("Expected default policy of SingleRedundancy to be used, incorrectly flagged as invalid")
120+
}
121+
122+
}
123+
124+
func TestValidRedundancyPolicySpecified(t *testing.T) {
125+
126+
esCR := &api.Elasticsearch{
127+
Spec: api.ElasticsearchSpec{
128+
RedundancyPolicy: api.FullRedundancy,
129+
},
130+
}
131+
132+
isValid := isValidRedundancyPolicy(esCR)
133+
134+
if !isValid {
135+
t.Error("Expected FullRedundancy to be valid, flagged as invalid")
136+
}
137+
138+
}
139+
140+
func TestInvalidRedundancyPolicySpecified(t *testing.T) {
141+
142+
esCR := &api.Elasticsearch{
143+
Spec: api.ElasticsearchSpec{
144+
RedundancyPolicy: "NoRedundancy",
145+
},
146+
}
147+
148+
isValid := isValidRedundancyPolicy(esCR)
149+
150+
if isValid {
151+
t.Error("Expected NoRedundancy to be flagged as invalid, was found to be valid")
152+
}
153+
154+
}
155+
156+
func TestValidReplicaCount(t *testing.T) {
157+
158+
dataNodeCount := 5
159+
160+
esNode := api.ElasticsearchNode{
161+
Roles: []api.ElasticsearchNodeRole{"data"},
162+
NodeCount: int32(dataNodeCount),
163+
}
164+
165+
esCR := &api.Elasticsearch{
166+
Spec: api.ElasticsearchSpec{
167+
RedundancyPolicy: api.FullRedundancy,
168+
Nodes: []api.ElasticsearchNode{esNode},
169+
},
170+
}
171+
172+
rc := calculateReplicaCount(esCR)
173+
174+
// FullRedundancy = dataNodeCount - 1
175+
if rc != dataNodeCount-1 {
176+
t.Errorf("Expected 4 replica shards for 5 data nodes and FullRedundancy policy, got %d", rc)
177+
}
178+
179+
}
180+
181+
func TestNoReplicaCount(t *testing.T) {
182+
183+
dataNodeCount := 5
184+
185+
esNode := api.ElasticsearchNode{
186+
Roles: []api.ElasticsearchNodeRole{"data"},
187+
NodeCount: int32(dataNodeCount),
188+
}
189+
190+
esCR := &api.Elasticsearch{
191+
Spec: api.ElasticsearchSpec{
192+
Nodes: []api.ElasticsearchNode{esNode},
193+
},
194+
}
195+
196+
rc := calculateReplicaCount(esCR)
197+
198+
// we default to 1
199+
if rc != 1 {
200+
t.Errorf("Expected SingleRedundancy, when no policy is specified and cluster has 2 or more data nodes, got %d replica shards", rc)
201+
}
202+
203+
}
204+
205+
func TestSingleNodeNoReplicaCount(t *testing.T) {
206+
207+
dataNodeCount := 1
208+
209+
esNode := api.ElasticsearchNode{
210+
Roles: []api.ElasticsearchNodeRole{"data"},
211+
NodeCount: int32(dataNodeCount),
212+
}
213+
214+
esCR := &api.Elasticsearch{
215+
Spec: api.ElasticsearchSpec{
216+
Nodes: []api.ElasticsearchNode{esNode},
217+
},
218+
}
219+
220+
rc := calculateReplicaCount(esCR)
221+
222+
if rc != 0 {
223+
t.Errorf("Expected ZeroRedundancy, when no policy is specified and cluster has only 1 data node, got %d replica shards", rc)
224+
}
225+
226+
}

0 commit comments

Comments
 (0)