Skip to content

Commit

Permalink
[Bugfix] Fix Replaced Member Zone during Replace operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajanikow committed Nov 15, 2023
1 parent 82cb7f1 commit 385cec2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- (Feature) License ArangoDeployment Fetcher
- (Feature) K8S Resources Compare Generic
- (Feature) Add support for CRD validation schemas
- (Bugfix) Fix Replaced Member Zone during Replace operation

## [1.2.35](https://github.com/arangodb/kube-arangodb/tree/1.2.35) (2023-11-06)
- (Maintenance) Update go-driver to v1.6.0, update IsNotFound() checks
Expand Down
51 changes: 41 additions & 10 deletions pkg/apis/deployment/v1/topology_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
)

type TopologyZoneFilter func(g ServerGroup, id string) bool

func TopologyZoneFilterMerge(functions ...TopologyZoneFilter) TopologyZoneFilter {
return func(g ServerGroup, id string) bool {
for _, f := range functions {
if !f(g, id) {
return false
}
}

return true
}
}

type TopologyStatus struct {
ID types.UID `json:"id"`

Expand All @@ -53,25 +67,42 @@ func (t *TopologyStatus) Equal(b *TopologyStatus) bool {
}

func (t *TopologyStatus) GetLeastUsedZone(group ServerGroup) int {
return t.GetLeastUsedZoneWithFilter(group)
}

func (t *TopologyStatus) GetLeastUsedZoneWithFilter(group ServerGroup, filters ...TopologyZoneFilter) int {
if t == nil {
return -1
}

r, m := -1, math.MaxInt64
// If no zones are found
if len(t.Zones) == 0 {
return -1
}

for i, z := range t.Zones {
if n, ok := z.Members[group.AsRoleAbbreviated()]; ok {
if v := len(n); v < m {
r, m = i, v
}
} else {
if v := 0; v < m {
r, m = i, v
counts := make([]int, len(t.Zones))

for id, zone := range t.Zones {
c := 0

for _, member := range zone.Members[group.AsRoleAbbreviated()] {
if TopologyZoneFilterMerge(filters...)(group, member) {
c++
}
}

counts[id] = c
}

max := 0

for id := 1; id < len(counts); id++ {
if counts[id] < counts[max] {
max = id
}
}

return r
return max
}

func (t *TopologyStatus) RegisterTopologyLabel(zone int, label string) bool {
Expand Down
49 changes: 42 additions & 7 deletions pkg/apis/deployment/v2alpha1/arango_member_pod_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ type ArangoMemberPodTemplate struct {
Endpoint *string `json:"endpoint,omitempty"`
}

func (a *ArangoMemberPodTemplate) GetChecksum() string {
if a == nil {
return ""
}
return a.Checksum
}

func (a *ArangoMemberPodTemplate) Equals(b *ArangoMemberPodTemplate) bool {
if a == nil && b == nil {
return true
Expand Down Expand Up @@ -100,3 +93,45 @@ func (a *ArangoMemberPodTemplate) EqualPodSpecChecksum(checksum string) bool {
}
return checksum == a.PodSpecChecksum
}

func (a *ArangoMemberPodTemplate) GetTemplate() *core.PodTemplateSpec {
if a == nil {
return nil
}
return a.PodSpec.DeepCopy()
}

func (a *ArangoMemberPodTemplate) SetTemplate(t *core.PodTemplateSpec) {
if a == nil {
return
}
a.PodSpec = t.DeepCopy()
}

func (a *ArangoMemberPodTemplate) GetTemplateChecksum() string {
if a == nil {
return ""
}
return a.PodSpecChecksum
}

func (a *ArangoMemberPodTemplate) SetTemplateChecksum(s string) {
if a == nil {
return
}
a.PodSpecChecksum = s
}

func (a *ArangoMemberPodTemplate) SetChecksum(s string) {
if a == nil {
return
}
a.Checksum = s
}

func (a *ArangoMemberPodTemplate) GetChecksum() string {
if a == nil {
return ""
}
return a.Checksum
}
51 changes: 41 additions & 10 deletions pkg/apis/deployment/v2alpha1/topology_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
)

type TopologyZoneFilter func(g ServerGroup, id string) bool

func TopologyZoneFilterMerge(functions ...TopologyZoneFilter) TopologyZoneFilter {
return func(g ServerGroup, id string) bool {
for _, f := range functions {
if !f(g, id) {
return false
}
}

return true
}
}

type TopologyStatus struct {
ID types.UID `json:"id"`

Expand All @@ -53,25 +67,42 @@ func (t *TopologyStatus) Equal(b *TopologyStatus) bool {
}

func (t *TopologyStatus) GetLeastUsedZone(group ServerGroup) int {
return t.GetLeastUsedZoneWithFilter(group)
}

func (t *TopologyStatus) GetLeastUsedZoneWithFilter(group ServerGroup, filters ...TopologyZoneFilter) int {
if t == nil {
return -1
}

r, m := -1, math.MaxInt64
// If no zones are found
if len(t.Zones) == 0 {
return -1
}

for i, z := range t.Zones {
if n, ok := z.Members[group.AsRoleAbbreviated()]; ok {
if v := len(n); v < m {
r, m = i, v
}
} else {
if v := 0; v < m {
r, m = i, v
counts := make([]int, len(t.Zones))

for id, zone := range t.Zones {
c := 0

for _, member := range zone.Members[group.AsRoleAbbreviated()] {
if TopologyZoneFilterMerge(filters...)(group, member) {
c++
}
}

counts[id] = c
}

max := 0

for id := 1; id < len(counts); id++ {
if counts[id] < counts[max] {
max = id
}
}

return r
return max
}

func (t *TopologyStatus) RegisterTopologyLabel(zone int, label string) bool {
Expand Down

0 comments on commit 385cec2

Please sign in to comment.