|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023 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 | + |
| 21 | +package v1 |
| 22 | + |
| 23 | +import ( |
| 24 | + core "k8s.io/api/core/v1" |
| 25 | + |
| 26 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 27 | +) |
| 28 | + |
| 29 | +type ArangoMemberSpecOverrides struct { |
| 30 | + // VolumeClaimTemplate specifies a template for volume claims. Overrides template provided on the group level. |
| 31 | + // +doc/type: core.PersistentVolumeClaim |
| 32 | + // +doc/link: Documentation of core.PersistentVolumeClaim|https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#persistentvolumeclaim-v1-core |
| 33 | + VolumeClaimTemplate *core.PersistentVolumeClaim `json:"volumeClaimTemplate,omitempty"` |
| 34 | + |
| 35 | + // Resources holds resource requests & limits. Overrides template provided on the group level. |
| 36 | + // +doc/type: core.ResourceRequirements |
| 37 | + // +doc/link: Documentation of core.ResourceRequirements|https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#resourcerequirements-v1-core |
| 38 | + Resources core.ResourceRequirements `json:"resources,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +func (a *ArangoMemberSpecOverrides) HasVolumeClaimTemplate(g *ServerGroupSpec) bool { |
| 42 | + if g != nil { |
| 43 | + if g.HasVolumeClaimTemplate() { |
| 44 | + return true |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if a != nil { |
| 49 | + return a.VolumeClaimTemplate != nil |
| 50 | + } |
| 51 | + |
| 52 | + return false |
| 53 | +} |
| 54 | + |
| 55 | +func (a *ArangoMemberSpecOverrides) GetVolumeClaimTemplate(g *ServerGroupSpec) *core.PersistentVolumeClaim { |
| 56 | + if a != nil { |
| 57 | + if a.VolumeClaimTemplate != nil { |
| 58 | + return a.VolumeClaimTemplate.DeepCopy() |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if g != nil { |
| 63 | + if g.VolumeClaimTemplate != nil { |
| 64 | + return g.VolumeClaimTemplate.DeepCopy() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return &core.PersistentVolumeClaim{} |
| 69 | +} |
| 70 | + |
| 71 | +func (a *ArangoMemberSpecOverrides) GetResources(g *ServerGroupSpec) core.ResourceRequirements { |
| 72 | + rl := core.ResourceList{} |
| 73 | + rr := core.ResourceList{} |
| 74 | + |
| 75 | + if g != nil { |
| 76 | + if l := g.Resources.Limits; len(l) > 0 { |
| 77 | + for k, v := range l { |
| 78 | + rl[k] = v |
| 79 | + } |
| 80 | + } |
| 81 | + if l := g.Resources.Requests; len(l) > 0 { |
| 82 | + for k, v := range l { |
| 83 | + rr[k] = v |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if a != nil { |
| 89 | + if l := a.Resources.Limits; len(l) > 0 { |
| 90 | + for k, v := range l { |
| 91 | + rl[k] = v |
| 92 | + } |
| 93 | + } |
| 94 | + if l := a.Resources.Requests; len(l) > 0 { |
| 95 | + for k, v := range l { |
| 96 | + rr[k] = v |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + ret := core.ResourceRequirements{} |
| 102 | + |
| 103 | + if len(rl) > 0 { |
| 104 | + ret.Limits = rl |
| 105 | + } |
| 106 | + if len(rr) > 0 { |
| 107 | + ret.Requests = rr |
| 108 | + } |
| 109 | + |
| 110 | + return ret |
| 111 | +} |
| 112 | + |
| 113 | +func (a *ArangoMemberSpecOverrides) GetStorageClassName(g *ServerGroupSpec) string { |
| 114 | + if a != nil { |
| 115 | + if z := a.VolumeClaimTemplate; z != nil { |
| 116 | + return util.TypeOrDefault[string](z.Spec.StorageClassName) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if g != nil { |
| 121 | + return g.GetStorageClassName() |
| 122 | + } |
| 123 | + |
| 124 | + return "" |
| 125 | +} |
0 commit comments