Skip to content

Commit

Permalink
Webhook does not process transient errors properly
Browse files Browse the repository at this point in the history
Fixed metallb#2173

Signed-off-by: shimritproj <shimritp74@gmail.com>
  • Loading branch information
shimritproj committed Dec 10, 2023
1 parent 9fa533c commit fa764aa
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,35 @@ func (v *validator) Validate(resources ...client.ObjectList) error {
clusterResources.Pools = append(clusterResources.Pools, list.Items...)
case *metallbv1beta2.BGPPeerList:
clusterResources.Peers = append(clusterResources.Peers, list.Items...)
for i := range (clusterResources.Peers) {
clusterResources.Peers[i].Spec.BFDProfile = ""
clusterResources.Peers[i].Spec.PasswordSecret.Name = ""
}
case *metallbv1beta1.BFDProfileList:
clusterResources.BFDProfiles = append(clusterResources.BFDProfiles, list.Items...)
case *metallbv1beta1.BGPAdvertisementList:
clusterResources.BGPAdvs = append(clusterResources.BGPAdvs, list.Items...)
for i := range(clusterResources.BGPAdvs){
for j:= range(clusterResources.BGPAdvs[i].Spec.Communities){
if !existString(clusterResources.BGPAdvs[i].Spec.Communities[j],clusterResources.Communities){
clusterResources.BGPAdvs[i].Spec.Communities = removeElement(clusterResources.BGPAdvs[i].Spec.Communities, clusterResources.BGPAdvs[i].Spec.Communities[j])
}
}
}
case *metallbv1beta1.L2AdvertisementList:
clusterResources.L2Advs = append(clusterResources.L2Advs, list.Items...)
case *metallbv1beta1.AddressPoolList:
clusterResources.LegacyAddressPools = append(clusterResources.LegacyAddressPools, list.Items...)
for i := range (clusterResources.LegacyAddressPools) {
for j := range(clusterResources.LegacyAddressPools[i].Spec.BGPAdvertisements) {
for k := range(clusterResources.LegacyAddressPools[i].Spec.BGPAdvertisements[j].Communities){
if !existString(clusterResources.LegacyAddressPools[i].Spec.BGPAdvertisements[j].Communities[k], clusterResources.Communities) {
clusterResources.LegacyAddressPools[i].Spec.BGPAdvertisements[j].Communities = removeElement(clusterResources.LegacyAddressPools[i].Spec.BGPAdvertisements[j].Communities,
clusterResources.LegacyAddressPools[i].Spec.BGPAdvertisements[j].Communities[k])
}
}
}
}
case *metallbv1beta1.CommunityList:
clusterResources.Communities = append(clusterResources.Communities, list.Items...)
case *v1.NodeList:
Expand All @@ -68,3 +89,34 @@ func (v *validator) Validate(resources ...client.ObjectList) error {
func NewValidator(validate Validate) apivalidate.ClusterObjects {
return &validator{validate: validate}
}

func existString(c string, community []metallbv1beta1.Community ) bool {
for i := range community {
for j := range community[i].Spec.Communities{
if c == community[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 fa764aa

Please sign in to comment.