Skip to content

Commit

Permalink
Network: firewall controller webhook uniqe table name
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Dec 6, 2023
1 parent 1923408 commit 25944b1
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"

networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
Expand All @@ -41,7 +42,8 @@ func checkUniqueChainName(chains []firewallapi.Chain) error {

// checkImmutableTableName checks if the table name is immutable.
func checkImmutableTableName(fwcfg, oldFwcfg *networkingv1alpha1.FirewallConfiguration) error {
if oldFwcfg.Spec.Table.Name != fwcfg.Spec.Table.Name {
if fwcfg.Spec.Table.Name != nil && oldFwcfg.Spec.Table.Name != nil &&
oldFwcfg.Spec.Table.Name != fwcfg.Spec.Table.Name {
return fmt.Errorf("table name is immutable")
}
return nil
Expand All @@ -60,17 +62,13 @@ func checkUniqueTableName(ctx context.Context, cl client.Client, currentFwcfg *n
if err := cl.List(ctx, &fwcfglist); err != nil {
return err
}
labelselector := labels.SelectorFromSet(currentFwcfg.GetLabels())
for i := range fwcfglist.Items {
if fwcfglist.Items[i].UID == currentFwcfg.UID {
continue
}
fwcfg := fwcfglist.Items[i]
tableName := fwcfg.Spec.Table.Name
if tableName == nil {
return fmt.Errorf("table name is nil")
}
if *tableName == *currentTableName {
return fmt.Errorf("table name %v is duplicated", *tableName)
if fwcfglist.Items[i].Spec.Table.Name == currentFwcfg.Spec.Table.Name &&
labelselector.Matches(labels.Set(fwcfglist.Items[i].GetLabels())) &&
fwcfglist.Items[i].UID != currentFwcfg.UID {
return fmt.Errorf("table name %s with labels %s already used",
*currentTableName, currentFwcfg.GetLabels())
}
}
return nil
Expand Down

0 comments on commit 25944b1

Please sign in to comment.