Skip to content

Commit

Permalink
nACL optimization - fix cmd (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
YairSlobodin1 authored Jan 1, 2025
1 parent f84bd78 commit a0e7fb1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Use the `vpcgen` CLI tool with one of the following commands:
* `vpcgen synth acl` - generate an nACL for each subnet separately.
* `vpcgen synth acl --single` - generate a single nACL for all subnets in the same VPC.
* `vpcgen optimize sg` - optimize SGs.
* `vpcgen optimize acl` - optimize nACLs (In progress).
* `vpcgen optimize acl` - optimize nACLs.

## Synthesis
#### nACLs Generation
Expand Down Expand Up @@ -38,7 +38,7 @@ Flags:
-n, --sg-name string which security group to optimize
```

#### nACL optimization (in progress)
#### nACL optimization
nACL optimizatin attempts to reduce the number of nACL rules in an nACL without changing the semantic.
Specifying the `-n` flag results in optimizing only one given nACL. Otherwise, all nACLs will be optimized.
```
Expand Down
5 changes: 3 additions & 2 deletions cmd/subcmds/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
func newOptimizeCommand(args *inArgs) *cobra.Command {
cmd := &cobra.Command{
Use: "optimize",
Short: "optimization of existing SG (nACLS are not supported yet)",
Long: `optimization of existing SG (nACLS are not supported yet)`,
Short: "optimization of existing SGs and nACLs",
Long: `optimization of existing SGs and nACLs`,
}

// sub cmds
cmd.AddCommand(newOptimizeSGCommand(args))
cmd.AddCommand(newOptimizeACLCommand(args))

return cmd
}
Expand Down
21 changes: 15 additions & 6 deletions cmd/subcmds/optimizeACL.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ SPDX-License-Identifier: Apache-2.0

package subcmds

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"

// temporarily exported and currently unused
func NewOptimizeACLCommand(_ *inArgs) *cobra.Command {
acloptimizer "github.com/np-guard/vpc-network-config-synthesis/pkg/optimize/acl"
)

const aclNameFlag = "acl-name"

func newOptimizeACLCommand(args *inArgs) *cobra.Command {
cmd := &cobra.Command{
Use: "acl",
Short: "OptimizeACL is not supported yet",
Long: `OptimizeACL is not supported yet`,
Short: "OptimizeACL attempts to reduce the number of nACL rules in an nACL without changing the semantic.",
Long: `OptimizeACL attempts to reduce the number of nACL rules in an nACL without changing the semantic.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return nil
return optimization(cmd, args, acloptimizer.NewACLOptimizer, false)
},
}

// flags
cmd.PersistentFlags().StringVarP(&args.firewallName, aclNameFlag, "n", "", "which nACL to optimize")

return cmd
}
31 changes: 31 additions & 0 deletions pkg/optimize/acl/acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2023- IBM Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package acloptimizer

import (
"github.com/np-guard/vpc-network-config-synthesis/pkg/ir"
"github.com/np-guard/vpc-network-config-synthesis/pkg/optimize"
)

type (
aclOptimizer struct {
aclCollection *ir.ACLCollection
aclName string
aclVPC string
}
)

func NewACLOptimizer(collection ir.Collection, aclName string) optimize.Optimizer {
components := ir.ScopingComponents(aclName)
if len(components) == 1 {
return &aclOptimizer{aclCollection: collection.(*ir.ACLCollection), aclName: aclName, aclVPC: ""}
}
return &aclOptimizer{aclCollection: collection.(*ir.ACLCollection), aclName: components[1], aclVPC: components[0]}
}

func (a *aclOptimizer) Optimize() (ir.Collection, error) {
return a.aclCollection, nil
}

0 comments on commit a0e7fb1

Please sign in to comment.