Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea committed Apr 20, 2024
1 parent f12299a commit a7eedc2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
14 changes: 11 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sys/unix"
"sigs.k8s.io/kube-network-policies/pkg/networkpolicy"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"

"golang.org/x/sys/unix"
)

var (
Expand Down Expand Up @@ -76,7 +78,10 @@ func main() {
informersFactory := informers.NewSharedInformerFactory(clientset, 0)

http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(metricsBindAddress, nil)
go func() {
err := http.ListenAndServe(metricsBindAddress, nil)
utilruntime.HandleError(err)
}()

networkPolicyController := networkpolicy.NewController(
clientset,
Expand All @@ -85,7 +90,10 @@ func main() {
informersFactory.Core().V1().Pods(),
cfg,
)
go networkPolicyController.Run(ctx)
go func() {
err := networkPolicyController.Run(ctx)
utilruntime.HandleError(err)
}()

informersFactory.Start(ctx.Done())

Expand Down
16 changes: 11 additions & 5 deletions pkg/networkpolicy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func NewController(client clientset.Interface,

}

podInformer.Informer().AddIndexers(cache.Indexers{
err := podInformer.Informer().AddIndexers(cache.Indexers{
podIPIndex: func(obj interface{}) ([]string, error) {
pod, ok := obj.(*v1.Pod)
if !ok {
Expand All @@ -140,6 +140,9 @@ func NewController(client clientset.Interface,
return result, nil
},
})
if err != nil {
panic(err)
}

podIndexer := podInformer.Informer().GetIndexer()
// Theoretically only one IP can be active at a time
Expand Down Expand Up @@ -173,7 +176,10 @@ func NewController(client clientset.Interface,
}
return obj, nil
}
podInformer.Informer().SetTransform(trim)
err = podInformer.Informer().SetTransform(trim)
if err != nil {
utilruntime.HandleError(err)
}

c.podLister = podInformer.Lister()
c.podsSynced = podInformer.Informer().HasSynced
Expand Down Expand Up @@ -304,14 +310,14 @@ func (c *Controller) Run(ctx context.Context) error {
packet, err := parsePacket(*a.Payload)
if err != nil {
klog.Infof("Can not process packet %d accepting it: %v", *a.PacketID, err)
c.nfq.SetVerdict(*a.PacketID, nfqueue.NfAccept)
c.nfq.SetVerdict(*a.PacketID, nfqueue.NfAccept) //nolint:errcheck
}

verdict := c.acceptPacket(packet)
if verdict {
c.nfq.SetVerdict(*a.PacketID, nfqueue.NfAccept)
c.nfq.SetVerdict(*a.PacketID, nfqueue.NfAccept) //nolint:errcheck
} else {
c.nfq.SetVerdict(*a.PacketID, nfqueue.NfDrop)
c.nfq.SetVerdict(*a.PacketID, nfqueue.NfDrop) //nolint:errcheck
}

processingTime := float64(time.Since(startTime).Microseconds())
Expand Down
35 changes: 28 additions & 7 deletions pkg/networkpolicy/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ func newController() *networkpolicyController {
}

func TestSyncPacket(t *testing.T) {
logs.GlogSetter("4")
_, err := logs.GlogSetter("4")
if err != nil {
t.Fatal(err)
}
state := klog.CaptureState()
t.Cleanup(state.Restore)

Expand Down Expand Up @@ -498,13 +501,22 @@ func TestSyncPacket(t *testing.T) {
controller := newController()
// Add objects to the Store
for _, n := range tt.networkpolicy {
controller.networkpolicyStore.Add(n)
err := controller.networkpolicyStore.Add(n)
if err != nil {
t.Fatal(err)
}
}
for _, n := range tt.namespace {
controller.namespaceStore.Add(n)
err := controller.namespaceStore.Add(n)
if err != nil {
t.Fatal(err)
}
}
for _, p := range tt.pod {
controller.podStore.Add(p)
err := controller.podStore.Add(p)
if err != nil {
t.Fatal(err)
}
}

ok := controller.acceptPacket(tt.p)
Expand Down Expand Up @@ -535,13 +547,22 @@ func TestController_evaluateSelectors(t *testing.T) {
c := newController()
// Add objects to the Store
for _, n := range tt.networkpolicies {
c.networkpolicyStore.Add(n)
err := c.networkpolicyStore.Add(n)
if err != nil {
t.Fatal(err)
}
}
for _, n := range tt.namespaces {
c.namespaceStore.Add(n)
err := c.namespaceStore.Add(n)
if err != nil {
t.Fatal(err)
}
}
for _, p := range tt.pods {
c.podStore.Add(p)
err := c.podStore.Add(p)
if err != nil {
t.Fatal(err)
}
}
if got := c.evaluateSelectors(tt.peerPodSelector, tt.peerNSSelector, tt.pod, tt.policyNs); got != tt.want {
t.Errorf("Controller.evaluateSelectors() = %v, want %v", got, tt.want)
Expand Down
16 changes: 8 additions & 8 deletions pkg/networkpolicy/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ var registerMetricsOnce sync.Once
func registerMetrics(ctx context.Context) {
registerMetricsOnce.Do(func() {
klog.Infof("Registering metrics")
prometheus.Register(packetProcessingHist)
prometheus.Register(packetProcessingSum)
prometheus.Register(packetCounterVec)
prometheus.Register(nfqueueQueueTotal)
prometheus.Register(nfqueueQueueDropped)
prometheus.Register(nfqueueUserDropped)
prometheus.Register(nfqueuePacketID)
prometheus.MustRegister(packetProcessingHist)
prometheus.MustRegister(packetProcessingSum)
prometheus.MustRegister(packetCounterVec)
prometheus.MustRegister(nfqueueQueueTotal)
prometheus.MustRegister(nfqueueQueueDropped)
prometheus.MustRegister(nfqueueUserDropped)
prometheus.MustRegister(nfqueuePacketID)
})
}

Expand All @@ -79,7 +79,7 @@ type nfnetlinkQueue struct {
queue_dropped int // Number of packets that had to be dropped by the kernel because too many packets are already waiting for user space to send back the mandatory accept/drop verdicts.
user_dropped int // Number of packets that were dropped within the netlink subsystem. Such drops usually happen when the corresponding socket buffer is full; that is, user space is not able to read messages fast enough.
id_sequence int // sequence number. Every queued packet is associated with a (32-bit) monotonically increasing sequence number. This shows the ID of the most recent packet queued.
dummy int // Field is always ‘1’ and is ignored, only kept for compatibility reasons.
// dummy int // Field is always ‘1’ and is ignored, only kept for compatibility reasons.
}

func readNfnetlinkQueueStats() ([]nfnetlinkQueue, error) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/networkpolicy/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func parsePacket(b []byte) (packet, error) {
}
version := int(b[0] >> 4)
// initialize variables
hdrlen := -1
protocol := -1
var hdrlen, protocol int
switch version {
case 4:
t.family = v1.IPv4Protocol
Expand Down

0 comments on commit a7eedc2

Please sign in to comment.