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 11, 2023
1 parent d31bdc6 commit 05ab5fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ func (w *webhookMutate) Handle(_ context.Context, req admission.Request) admissi
return admission.Errored(http.StatusBadRequest, err)
}

table := firewallConfiguration.Spec.Table
chains := table.Chains

generateRuleNames(chains)
generateRuleNames(firewallConfiguration.Spec.Table.Chains)

return w.CreatePatchResponse(&req, firewallConfiguration)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package firewallconfiguration
import (
"context"
"fmt"
"maps"

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

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,15 @@ func checkUniqueTableName(ctx context.Context, cl client.Client, currentFwcfg *n
if err := cl.List(ctx, &fwcfglist); err != nil {
return err
}

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 &&
maps.Equal(currentFwcfg.GetLabels(), fwcfglist.Items[i].GetLabels()) {
return fmt.Errorf("table name %s with labels %s already used",
*currentTableName, currentFwcfg.GetLabels())
}
}
return nil
Expand Down

0 comments on commit 05ab5fb

Please sign in to comment.