Skip to content

Commit

Permalink
shave out things that are uncessary for interception
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgmiller committed Nov 25, 2024
1 parent 784bf6b commit c89d625
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
19 changes: 4 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"sigs.k8s.io/network-policy-api/pkg/client/informers/externalversions/apis/v1alpha1"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
v1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -56,12 +55,6 @@ func init() {
}
}

type interceptor interface {
// Run should block until context is done and then clean up its resources.
Run(context.Context, func(networkpolicy.Packet) networkpolicy.Verdict) error
Sync(ctx context.Context, podV4IPs, podV6IPs sets.Set[string]) error
}

// This is a pattern to ensure that deferred functions executes before os.Exit
func main() {
os.Exit(run())
Expand Down Expand Up @@ -97,8 +90,7 @@ func run() int {

nodeName, err := nodeutil.GetHostname(hostnameOverride)
if err != nil {
logger.Error(err, "can not obtain the node name, use the hostname-override flag if you want to set it to a specific value")
return 1
klog.Fatalf("can not obtain the node name, use the hostname-override flag if you want to set it to a specific value: %v", err)
}

cfg := networkpolicy.Config{
Expand All @@ -112,8 +104,7 @@ func run() int {
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
logger.Error(err, "could not get cluster config")
return 1
panic(err.Error())
}

// use protobuf for better performance at scale
Expand All @@ -125,8 +116,7 @@ func run() int {
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logger.Error(err, "could not create clientset")
return 1
panic(err.Error())
}

informersFactory := informers.NewSharedInformerFactory(clientset, 0)
Expand All @@ -138,8 +128,7 @@ func run() int {
nodeInformer = informersFactory.Core().V1().Nodes()
npaClient, err = npaclient.NewForConfig(npaConfig)
if err != nil {
logger.Error(err, "Failed to create Network client")
return 1
klog.Fatalf("Failed to create Network client: %v", err)
}
npaInformerFactory = npainformers.NewSharedInformerFactory(npaClient, 0)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/networkpolicy/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ type networkpolicyController struct {

type mockInterceptor struct{}

func (mockInterceptor) Run(_ context.Context, _ func(Packet) int) (_ error) {
return nil
}

func (mockInterceptor) Sync(ctx context.Context, podV4IPs sets.Set[string], podV6IPs sets.Set[string]) (_ error) {
return nil
}
Expand All @@ -105,14 +101,14 @@ func newTestController() *networkpolicyController {
npaClient := npaclientfake.NewSimpleClientset()
npaInformerFactory := npainformers.NewSharedInformerFactory(npaClient, 0)

controller, err := newController(
controller, err := NewController(
client,
mockInterceptor{},
informersFactory.Networking().V1().NetworkPolicies(),
informersFactory.Core().V1().Namespaces(),
informersFactory.Core().V1().Pods(),
informersFactory.Core().V1().Nodes(),
npaClient,
mockInterceptor{},
npaInformerFactory.Policy().V1alpha1().AdminNetworkPolicies(),
npaInformerFactory.Policy().V1alpha1().BaselineAdminNetworkPolicies(),
Config{
Expand Down

0 comments on commit c89d625

Please sign in to comment.