Skip to content

Commit 6c98ba2

Browse files
authored
[Feature] Allow to customize id pod selector (#582)
1 parent de6b43a commit 6c98ba2

File tree

6 files changed

+147
-37
lines changed

6 files changed

+147
-37
lines changed

pkg/apis/deployment/v1/deployment_spec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type DeploymentSpec struct {
8181
Metrics MetricsSpec `json:"metrics"`
8282
Lifecycle LifecycleSpec `json:"lifecycle,omitempty"`
8383

84+
ID ServerIDGroupSpec `json:"id"`
85+
8486
Single ServerGroupSpec `json:"single"`
8587
Agents ServerGroupSpec `json:"agents"`
8688
DBServers ServerGroupSpec `json:"dbservers"`

pkg/apis/deployment/v1/server_group_spec.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import (
2929
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
3030

3131
"github.com/pkg/errors"
32-
v1 "k8s.io/api/core/v1"
32+
core "k8s.io/api/core/v1"
3333
"k8s.io/apimachinery/pkg/api/resource"
3434

3535
"github.com/arangodb/kube-arangodb/pkg/util"
36-
arangod_options "github.com/arangodb/kube-arangodb/pkg/util/arangod/options"
37-
arangosync_options "github.com/arangodb/kube-arangodb/pkg/util/arangosync/options"
36+
arangodOptions "github.com/arangodb/kube-arangodb/pkg/util/arangod/options"
37+
arangosyncOptions "github.com/arangodb/kube-arangodb/pkg/util/arangosync/options"
3838
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
3939
)
4040

@@ -51,11 +51,11 @@ type ServerGroupSpec struct {
5151
// StorageClassName specifies the classname for storage of the servers.
5252
StorageClassName *string `json:"storageClassName,omitempty"`
5353
// Resources holds resource requests & limits
54-
Resources v1.ResourceRequirements `json:"resources,omitempty"`
54+
Resources core.ResourceRequirements `json:"resources,omitempty"`
5555
// OverrideDetectedTotalMemory determines if memory should be overrided based on values in resources.
5656
OverrideDetectedTotalMemory *bool `json:"overrideDetectedTotalMemory,omitempty"`
5757
// Tolerations specifies the tolerations added to Pods in this group.
58-
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
58+
Tolerations []core.Toleration `json:"tolerations,omitempty"`
5959
// Annotations specified the annotations added to Pods in this group.
6060
Annotations map[string]string `json:"annotations,omitempty"`
6161
// ServiceAccountName specifies the name of the service account used for Pods in this group.
@@ -67,17 +67,17 @@ type ServerGroupSpec struct {
6767
// PriorityClassName specifies a priority class name
6868
PriorityClassName string `json:"priorityClassName,omitempty"`
6969
// VolumeClaimTemplate specifies a template for volume claims
70-
VolumeClaimTemplate *v1.PersistentVolumeClaim `json:"volumeClaimTemplate,omitempty"`
70+
VolumeClaimTemplate *core.PersistentVolumeClaim `json:"volumeClaimTemplate,omitempty"`
7171
// VolumeResizeMode specified resize mode for pvc
7272
VolumeResizeMode *PVCResizeMode `json:"pvcResizeMode,omitempty"`
7373
// AntiAffinity specified additional antiAffinity settings in ArangoDB Pod definitions
74-
AntiAffinity *v1.PodAntiAffinity `json:"antiAffinity,omitempty"`
74+
AntiAffinity *core.PodAntiAffinity `json:"antiAffinity,omitempty"`
7575
// Affinity specified additional affinity settings in ArangoDB Pod definitions
76-
Affinity *v1.PodAffinity `json:"affinity,omitempty"`
76+
Affinity *core.PodAffinity `json:"affinity,omitempty"`
7777
// NodeAffinity specified additional nodeAffinity settings in ArangoDB Pod definitions
78-
NodeAffinity *v1.NodeAffinity `json:"nodeAffinity,omitempty"`
78+
NodeAffinity *core.NodeAffinity `json:"nodeAffinity,omitempty"`
7979
// Sidecars specifies a list of additional containers to be started
80-
Sidecars []v1.Container `json:"sidecars,omitempty"`
80+
Sidecars []core.Container `json:"sidecars,omitempty"`
8181
// SecurityContext specifies security context for group
8282
SecurityContext *ServerGroupSpecSecurityContext `json:"securityContext,omitempty"`
8383
// Volumes define list of volumes mounted to pod
@@ -95,7 +95,7 @@ type ServerGroupSpecSecurityContext struct {
9595
// Deprecated: This field is added for backward compatibility. Will be removed in 1.1.0.
9696
DropAllCapabilities *bool `json:"dropAllCapabilities,omitempty"`
9797
// AddCapabilities add new capabilities to containers
98-
AddCapabilities []v1.Capability `json:"addCapabilities,omitempty"`
98+
AddCapabilities []core.Capability `json:"addCapabilities,omitempty"`
9999
}
100100

101101
// GetDropAllCapabilities returns flag if capabilities should be dropped
@@ -114,7 +114,7 @@ func (s *ServerGroupSpecSecurityContext) GetDropAllCapabilities() bool {
114114
}
115115

116116
// GetAddCapabilities add capabilities to pod context
117-
func (s *ServerGroupSpecSecurityContext) GetAddCapabilities() []v1.Capability {
117+
func (s *ServerGroupSpecSecurityContext) GetAddCapabilities() []core.Capability {
118118
if s == nil {
119119
return nil
120120
}
@@ -127,19 +127,19 @@ func (s *ServerGroupSpecSecurityContext) GetAddCapabilities() []v1.Capability {
127127
}
128128

129129
// NewSecurityContext creates new security context
130-
func (s *ServerGroupSpecSecurityContext) NewSecurityContext() *v1.SecurityContext {
131-
r := &v1.SecurityContext{}
130+
func (s *ServerGroupSpecSecurityContext) NewSecurityContext() *core.SecurityContext {
131+
r := &core.SecurityContext{}
132132

133-
capabilities := &v1.Capabilities{}
133+
capabilities := &core.Capabilities{}
134134

135135
if s.GetDropAllCapabilities() {
136-
capabilities.Drop = []v1.Capability{
136+
capabilities.Drop = []core.Capability{
137137
"ALL",
138138
}
139139
}
140140

141141
if caps := s.GetAddCapabilities(); caps != nil {
142-
capabilities.Add = []v1.Capability{}
142+
capabilities.Add = []core.Capability{}
143143

144144
capabilities.Add = append(capabilities.Add, caps...)
145145
}
@@ -246,7 +246,7 @@ func (s *ServerGroupProbeSpec) GetFailureThreshold(d int32) int32 {
246246
}
247247

248248
// GetSidecars returns a list of sidecars the use wish to add
249-
func (s ServerGroupSpec) GetSidecars() []v1.Container {
249+
func (s ServerGroupSpec) GetSidecars() []core.Container {
250250
return s.Sidecars
251251
}
252252

@@ -256,7 +256,7 @@ func (s ServerGroupSpec) HasVolumeClaimTemplate() bool {
256256
}
257257

258258
// GetVolumeClaimTemplate returns a pointer to a volume claim template or nil if none is specified
259-
func (s ServerGroupSpec) GetVolumeClaimTemplate() *v1.PersistentVolumeClaim {
259+
func (s ServerGroupSpec) GetVolumeClaimTemplate() *core.PersistentVolumeClaim {
260260
return s.VolumeClaimTemplate
261261
}
262262

@@ -299,7 +299,7 @@ func (s ServerGroupSpec) GetStorageClassName() string {
299299
}
300300

301301
// GetTolerations returns the value of tolerations.
302-
func (s ServerGroupSpec) GetTolerations() []v1.Toleration {
302+
func (s ServerGroupSpec) GetTolerations() []core.Toleration {
303303
return s.Tolerations
304304
}
305305

@@ -377,11 +377,11 @@ func (s ServerGroupSpec) Validate(group ServerGroup, used bool, mode DeploymentM
377377
parts := strings.Split(arg, "=")
378378
optionKey := strings.TrimSpace(parts[0])
379379
if group.IsArangod() {
380-
if arangod_options.IsCriticalOption(optionKey) {
380+
if arangodOptions.IsCriticalOption(optionKey) {
381381
return maskAny(errors.Wrapf(ValidationError, "Critical option '%s' cannot be overriden", optionKey))
382382
}
383383
} else if group.IsArangosync() {
384-
if arangosync_options.IsCriticalOption(optionKey) {
384+
if arangosyncOptions.IsCriticalOption(optionKey) {
385385
return maskAny(errors.Wrapf(ValidationError, "Critical option '%s' cannot be overriden", optionKey))
386386
}
387387
}
@@ -443,19 +443,19 @@ func (s *ServerGroupSpec) SetDefaults(group ServerGroup, used bool, mode Deploym
443443
s.MaxCount = nil
444444
}
445445
if !s.HasVolumeClaimTemplate() {
446-
if _, found := s.Resources.Requests[v1.ResourceStorage]; !found {
446+
if _, found := s.Resources.Requests[core.ResourceStorage]; !found {
447447
switch group {
448448
case ServerGroupSingle, ServerGroupAgents, ServerGroupDBServers:
449-
volumeMode := v1.PersistentVolumeFilesystem
450-
s.VolumeClaimTemplate = &v1.PersistentVolumeClaim{
451-
Spec: v1.PersistentVolumeClaimSpec{
452-
AccessModes: []v1.PersistentVolumeAccessMode{
453-
v1.ReadWriteOnce,
449+
volumeMode := core.PersistentVolumeFilesystem
450+
s.VolumeClaimTemplate = &core.PersistentVolumeClaim{
451+
Spec: core.PersistentVolumeClaimSpec{
452+
AccessModes: []core.PersistentVolumeAccessMode{
453+
core.ReadWriteOnce,
454454
},
455455
VolumeMode: &volumeMode,
456-
Resources: v1.ResourceRequirements{
457-
Requests: v1.ResourceList{
458-
v1.ResourceStorage: resource.MustParse("8Gi"),
456+
Resources: core.ResourceRequirements{
457+
Requests: core.ResourceList{
458+
core.ResourceStorage: resource.MustParse("8Gi"),
459459
},
460460
},
461461
},
@@ -466,10 +466,10 @@ func (s *ServerGroupSpec) SetDefaults(group ServerGroup, used bool, mode Deploym
466466
}
467467

468468
// setDefaultsFromResourceList fills unspecified fields with a value from given source spec.
469-
func setDefaultsFromResourceList(s *v1.ResourceList, source v1.ResourceList) {
469+
func setDefaultsFromResourceList(s *core.ResourceList, source core.ResourceList) {
470470
for k, v := range source {
471471
if *s == nil {
472-
*s = make(v1.ResourceList)
472+
*s = make(core.ResourceList)
473473
}
474474
if _, found := (*s)[k]; !found {
475475
(*s)[k] = v
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package v1
24+
25+
import core "k8s.io/api/core/v1"
26+
27+
// ServerIDGroupSpec contains the specification for Image Discovery image.
28+
type ServerIDGroupSpec struct {
29+
// Tolerations specifies the tolerations added to Pods in this group.
30+
Tolerations []core.Toleration `json:"tolerations,omitempty"`
31+
// NodeSelector speficies a set of selectors for nodes
32+
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
33+
// PriorityClassName specifies a priority class name
34+
PriorityClassName string `json:"priorityClassName,omitempty"`
35+
// AntiAffinity specified additional antiAffinity settings in ArangoDB Pod definitions
36+
AntiAffinity *core.PodAntiAffinity `json:"antiAffinity,omitempty"`
37+
// Affinity specified additional affinity settings in ArangoDB Pod definitions
38+
Affinity *core.PodAffinity `json:"affinity,omitempty"`
39+
// NodeAffinity specified additional nodeAffinity settings in ArangoDB Pod definitions
40+
NodeAffinity *core.NodeAffinity `json:"nodeAffinity,omitempty"`
41+
}

pkg/apis/deployment/v1/zz_generated.deepcopy.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/deployment/images.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ func (i *ImageUpdatePod) GetRole() string {
272272
func (i *ImageUpdatePod) Init(pod *core.Pod) {
273273
terminationGracePeriodSeconds := int64((time.Second * 30).Seconds())
274274
pod.Spec.TerminationGracePeriodSeconds = &terminationGracePeriodSeconds
275+
pod.Spec.PriorityClassName = i.spec.ID.PriorityClassName
275276
}
276277

277278
func (i *ImageUpdatePod) GetImagePullSecrets() []string {
@@ -311,13 +312,19 @@ func (i *ImageUpdatePod) GetFinalizers() []string {
311312
}
312313

313314
func (i *ImageUpdatePod) GetTolerations() []core.Toleration {
314-
315315
shortDur := k8sutil.TolerationDuration{
316316
Forever: false,
317317
TimeSpan: time.Second * 5,
318318
}
319319

320-
tolerations := make([]core.Toleration, 0, 2)
320+
tolerations := make([]core.Toleration, 0, 3+len(i.spec.ID.Tolerations))
321+
322+
if idTolerations := i.spec.ID.Tolerations; len(idTolerations) > 0 {
323+
for _, toleration := range idTolerations {
324+
tolerations = k8sutil.AddTolerationIfNotFound(tolerations, toleration)
325+
}
326+
}
327+
321328
tolerations = k8sutil.AddTolerationIfNotFound(tolerations,
322329
k8sutil.NewNoExecuteToleration(k8sutil.TolerationKeyNodeNotReady, shortDur))
323330
tolerations = k8sutil.AddTolerationIfNotFound(tolerations,
@@ -333,7 +340,7 @@ func (i *ImageUpdatePod) IsDeploymentMode() bool {
333340
}
334341

335342
func (i *ImageUpdatePod) GetNodeSelector() map[string]string {
336-
return nil
343+
return i.spec.ID.NodeSelector
337344
}
338345

339346
func (i *ImageUpdatePod) GetServiceAccountName() string {
@@ -361,18 +368,26 @@ func (i *ImageUpdatePod) GetPodAntiAffinity() *core.PodAntiAffinity {
361368

362369
pod.AppendPodAntiAffinityDefault(i, &a)
363370

371+
pod.MergePodAntiAffinity(&a, i.spec.ID.AntiAffinity)
372+
364373
return pod.ReturnPodAntiAffinityOrNil(a)
365374
}
366375

367376
func (i *ImageUpdatePod) GetPodAffinity() *core.PodAffinity {
368-
return nil
377+
a := core.PodAffinity{}
378+
379+
pod.MergePodAffinity(&a, i.spec.ID.Affinity)
380+
381+
return pod.ReturnPodAffinityOrNil(a)
369382
}
370383

371384
func (i *ImageUpdatePod) GetNodeAffinity() *core.NodeAffinity {
372385
a := core.NodeAffinity{}
373386

374387
pod.AppendNodeSelector(&a)
375388

389+
pod.MergeNodeAffinity(&a, i.spec.ID.NodeAffinity)
390+
376391
return pod.ReturnNodeAffinityOrNil(a)
377392
}
378393

pkg/util/k8sutil/tolerations.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func NewNoExecuteToleration(key string, duration TolerationDuration) v1.Tolerati
5757

5858
// AddTolerationIfNotFound adds the given tolerations, if no such toleration has been set in the given source.
5959
func AddTolerationIfNotFound(source []v1.Toleration, toAdd v1.Toleration) []v1.Toleration {
60+
if len(source) == 0 {
61+
return []v1.Toleration{
62+
toAdd,
63+
}
64+
}
65+
6066
for _, t := range source {
6167
if (t.Key == toAdd.Key || len(t.Key) == 0) && (t.Effect == toAdd.Effect || len(t.Effect) == 0) {
6268
// Toleration alread exists, do not add

0 commit comments

Comments
 (0)