Skip to content

Commit

Permalink
Fixed metallb#2173
Browse files Browse the repository at this point in the history
Resetting the fields that cause Transienterrors

Signed-off-by: shimritproj <shimritp74@gmail.com>
  • Loading branch information
shimritproj committed Dec 12, 2023
1 parent 9fa533c commit 89c3f47
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,69 @@ func (v *validator) Validate(resources ...client.ObjectList) error {
clusterResources.Nodes = append(clusterResources.Nodes, list.Items...)
}
}
clusterResources = resetfields(clusterResources)
_, err := For(clusterResources, v.validate)
if errors.As(err, &TransientError{}) { // we do not want to make assumption on ordering in webhooks.
return nil
}
return err
}

func resetfields(clusterResources ClusterResources) ClusterResources {
for i := range clusterResources.Peers {
clusterResources.Peers[i].Spec.BFDProfile = ""
clusterResources.Peers[i].Spec.PasswordSecret.Name = ""
}
for _, bgpAdv := range clusterResources.BGPAdvs {
for _, community := range bgpAdv.Spec.Communities {
if !ExistCommunity(community, clusterResources.Communities) {
bgpAdv.Spec.Communities = RemoveElement(bgpAdv.Spec.Communities, community)
}
}
}
for _, legacyAddrPool := range clusterResources.LegacyAddressPools {
for _, bgpAdv := range legacyAddrPool.Spec.BGPAdvertisements {
for k := range bgpAdv.Communities {
if !ExistCommunity(bgpAdv.Communities[k], clusterResources.Communities) {
bgpAdv.Communities = RemoveElement(bgpAdv.Communities, bgpAdv.Communities[k])
}
}
}
}
return clusterResources
}

func NewValidator(validate Validate) apivalidate.ClusterObjects {
return &validator{validate: validate}
}

func ExistCommunity(community string, communities []metallbv1beta1.Community) bool {
for i := range communities {
for j := range communities[i].Spec.Communities {
if community == communities[i].Spec.Communities[j].Name {
return true
}
}
}
return false
}

func RemoveElement(slice []string, elementToRemove string) []string {
// Find the index of the element
index := -1
for i, v := range slice {
if v == elementToRemove {
index = i
break
}
}

// If the element is found, remove it
if index != -1 {
// Create a new slice without the element
return append(slice[:index], slice[index+1:]...)
}

// If the element is not found, return the original slice
return slice
}

0 comments on commit 89c3f47

Please sign in to comment.