Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

[WIP] Make KPNG more like a library and decouple from the GRPC API #389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
)

require (
github.com/prometheus/client_golang v1.12.1
github.com/spf13/cobra v1.4.0
google.golang.org/grpc v1.50.0
google.golang.org/protobuf v1.28.1
Expand All @@ -17,6 +18,8 @@ require (
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand All @@ -34,9 +37,13 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20221004154528-8021a29435af // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
Expand Down
7 changes: 7 additions & 0 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -78,6 +80,7 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -89,7 +92,11 @@ github.com/onsi/ginkgo/v2 v2.1.6 h1:Fx2POJZfKRQcM1pH49qSZiYeu319wji004qX+GDovrU=
github.com/onsi/gomega v1.20.1 h1:PA/3qinGoukvymdIDV8pii6tiZgC8kbmJO6Z5+b002Q=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
22 changes: 22 additions & 0 deletions cmd/kpng/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,25 @@ func local2sinkCmd() *cobra.Command {

return cmd
}

func Watch() *cobra.Command {
// local to * command
cmd := &cobra.Command{
Use: "local",
Short: "watch kpng API's local state",
}

flags := cmd.PersistentFlags()

job := api2local.New(nil)
job.BindFlags(flags)

cmd.AddCommand(storecmds.LocalCmds(func(sink localsink.Sink) (err error) {
ctx := setupGlobal()
job.Sink = sink
job.Run(ctx)
return
})...)

return cmd
}
23 changes: 23 additions & 0 deletions cmd/kpng/storecmds/storecmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ func (c SetupFunc) ToLocalCmd() (cmd *cobra.Command) {
return
}

// // ToLocalCmd sends the incoming events to a local backend, such as IPVS or IPTABLES or NFT.
// // This gives users an out of the box KPNG implementation.
// func (c SetupFunc) ToInterfaceCmd() (cmd *cobra.Command) {
// cmd = &cobra.Command{
// Use: "to-interface",
// }

// var ctx context.Context
// job := &store2localinterface.SendNodeLocalState{}

// cmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) (err error) {
// ctx, job.Store, err = c()
// return
// }

// cmd.AddCommand(LocalCmds(func(sink store2localinterface.NodeLocalStateConsumer) error {
// job.Sink = sink
// return job.Run(ctx)
// })...)

// return
// }

func LocalCmds(run func(sink localsink.Sink) error) (cmds []*cobra.Command) {
// sink backends
for _, useCmd := range backendcmd.Registered() {
Expand Down
28 changes: 28 additions & 0 deletions examples/interface-print-state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Interface Print State Example

This Example showcases the use of KPNG as a library, which simply prints the
ENLS(expected node local state) for services and endpoints from a running
K8s cluster.

To run this manually simply point it to a kubeconfig and specify the node name
you're interested in i.e

```bash
go run main.go --kubeconfig=/home/USER/.kube/config --nodename=kpng-e2e-ipv4-nft-control-plane
```

And you can watch as the state is delivered.

For example when a simple hello world service is created with

`kubectl expose deployment nginx-deployment --type=ClusterIP`

The example shows the expected changes for a given node:

```bash
go run main.go --kubeconfig=/home/astoycos/.kube/config --nodename=kpng-e2e-ipv4-nft-worker
...
I1021 15:06:50.966214 1033987 main.go:37] Got Service Update: Name -> nginx-deployment, Namespace -> default
I1021 15:06:50.970474 1033987 main.go:53] Got Endpoint Update: [EPSName: nginx-deployment-qzxrf, Ips: V4:"10.1.1.7", isLocal: true] for Service: nginx-deployment
I1021 15:06:50.970576 1033987 main.go:53] Got Endpoint Update: [EPSName: nginx-deployment-qzxrf, Ips: V4:"10.1.2.8", isLocal: false] for Service: nginx-deployment
```
7 changes: 7 additions & 0 deletions examples/interface-print-state/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module sigs.k8s.io/kpng/examples/interface-print-state

go 1.19

require k8s.io/klog/v2 v2.80.1

require github.com/go-logr/logr v1.2.3 // indirect
2 changes: 2 additions & 0 deletions examples/interface-print-state/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
90 changes: 90 additions & 0 deletions examples/interface-print-state/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"flag"
"fmt"
"os"

"k8s.io/klog/v2"
"sigs.k8s.io/kpng/api/localnetv1"
"sigs.k8s.io/kpng/server/jobs/kube2store"
"sigs.k8s.io/kpng/server/jobs/store2localinterface"
)

type myBackend struct{}

var _ store2localinterface.NodeLocalStateConsumer = myBackend{}

func (m myBackend) UpdateServices(services <-chan *localnetv1.Service) {
klog.V(2).Info("In Backend Update Service")
for service := range services {
klog.Infof("Got Service Update: Name -> %s, Namespace -> %s\n", service.Name, service.Namespace)
}
}

func (m myBackend) DeleteServices(services <-chan *localnetv1.Service) {
klog.V(2).Info("In Backend Delete Service")

for service := range services {
klog.Infof("Got Service Delete: %v\n", service)
}
}

func (m myBackend) UpdateEndpoints(endpoints <-chan *localnetv1.EndpointInfo) {
klog.V(2).Info("In Backend Update Endpoint")

for endpoint := range endpoints {
klog.Infof("Got Endpoint Update: [EPSName: %s, Ips: %+v, isLocal: %v] for Service: %s \n",
endpoint.SourceName, endpoint.Endpoint.IPs, endpoint.Endpoint.Local, endpoint.ServiceName)
}
}

func (m myBackend) DeleteEndpoints(endpoints <-chan *localnetv1.EndpointInfo) {
klog.V(2).Info("In Backend Delete Endpoint")

for endpoint := range endpoints {
klog.Infof("Got Endpoint Delete: %v\n", endpoint)
}
}

var (
nodeName = flag.String("nodename", "", "Node Name to get services data")
kubeconfig = flag.String("kubeconfig", "", "Path to Kube Config")
)

func main() {
klog.InitFlags(nil)
defer klog.Flush()

flag.Parse()
printBackend := myBackend{}
k2sCfg := &kube2store.Config{}
// kubeconfig := "~/.kube/config"
// kubeserver := ""

ctx, store, err := store2localinterface.StartKube2store(k2sCfg, *kubeconfig, "")
if err != nil {
fmt.Printf("Unable to start kube2store exiting %v", err)
os.Exit(1)
}
// If this returns something broke :-)
store2localinterface.WatchStore(ctx, store, *nodeName, printBackend)

ctx.Done()
}
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use (
./backends/windows/userspace
./client
./cmd
./examples/interface-print-state
./examples/iptables-extip
./examples/pipe-exec
./examples/print-state
Expand Down
1 change: 0 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXym
github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/exp v0.0.0-20220317015231-48e79f11773a // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/kube-openapi v0.0.0-20220928191237-829ce0c27909 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
Expand Down
1 change: 1 addition & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
golang.org/x/exp v0.0.0-20220317015231-48e79f11773a h1:DAzrdbxsb5tXNOhMCSwF7ZdfMbW46hE9fSVO6BsmUZM=
golang.org/x/net v0.0.0-20221004154528-8021a29435af h1:wv66FM3rLZGPdxpYL+ApnDe2HzHcTFta3z5nsc13wI4=
golang.org/x/oauth2 v0.0.0-20221006150949-b44042a4b9c1 h1:3VPzK7eqH25j7GYw5w6g/GzNRc0/fYtrxz27z1gD4W0=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
Expand Down
4 changes: 2 additions & 2 deletions server/jobs/kube2store/event-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
proxystore "sigs.k8s.io/kpng/server/proxystore"
)

type eventHandler struct {
type kpngEventHandler struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why prefix an internal type with the project's name, or any part of the package path btw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed :)

config *Config
s *proxystore.Store
informer cache.SharedIndexInformer
syncSet bool
}

func (h *eventHandler) updateSync(set proxystore.Set, tx *proxystore.Tx) {
func (h *kpngEventHandler) updateSync(set proxystore.Set, tx *proxystore.Tx) {
if h.syncSet {
return
}
Expand Down
10 changes: 5 additions & 5 deletions server/jobs/kube2store/kube2store.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,23 @@ func (j Job) Run(ctx context.Context) {
coreFactory := factory.Core().V1()

servicesInformer := svcFactory.Core().V1().Services().Informer()
servicesInformer.AddEventHandler(&serviceEventHandler{j.eventHandler(servicesInformer)})
servicesInformer.AddEventHandler(&serviceEventHandler{j.kpngEventHandler(servicesInformer)})
go servicesInformer.Run(stopCh)

nodesInformer := coreFactory.Nodes().Informer()
nodesInformer.AddEventHandler(&nodeEventHandler{j.eventHandler(nodesInformer)})
nodesInformer.AddEventHandler(&nodeEventHandler{j.kpngEventHandler(nodesInformer)})
go nodesInformer.Run(stopCh)

slicesInformer := factory.Discovery().V1().EndpointSlices().Informer()
slicesInformer.AddEventHandler(&sliceEventHandler{j.eventHandler(slicesInformer)})
slicesInformer.AddEventHandler(&sliceEventHandler{j.kpngEventHandler(slicesInformer)})
go slicesInformer.Run(stopCh)

<-stopCh
j.Store.Close()
}

func (j Job) eventHandler(informer cache.SharedIndexInformer) eventHandler {
return eventHandler{
func (j Job) kpngEventHandler(informer cache.SharedIndexInformer) kpngEventHandler {
return kpngEventHandler{
config: j.Config,
s: j.Store,
informer: informer,
Expand Down
2 changes: 1 addition & 1 deletion server/jobs/kube2store/node-event-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
nodeZoneLabel = "topology.kubernetes.io/zone"
)

type nodeEventHandler struct{ eventHandler }
type nodeEventHandler struct{ kpngEventHandler }

func (h *nodeEventHandler) OnAdd(obj interface{}) {
node := obj.(*v1.Node)
Expand Down
2 changes: 1 addition & 1 deletion server/jobs/kube2store/service-event-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sigs.k8s.io/kpng/server/proxystore"
)

type serviceEventHandler struct{ eventHandler }
type serviceEventHandler struct{ kpngEventHandler }

func (h *serviceEventHandler) onChange(obj interface{}) {
svc := obj.(*v1.Service)
Expand Down
2 changes: 1 addition & 1 deletion server/jobs/kube2store/service-event-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestServiceEventHandlerTrafficPolicy(t *testing.T) {
store := proxystore.New()

handler := serviceEventHandler{
eventHandler: eventHandler{
kpngEventHandler: kpngEventHandler{
s: store,
syncSet: true,
config: &Config{
Expand Down
2 changes: 1 addition & 1 deletion server/jobs/kube2store/slice-event-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

const hostNameLabel = "kubernetes.io/hostname"

type sliceEventHandler struct{ eventHandler }
type sliceEventHandler struct{ kpngEventHandler }

func serviceNameFrom(eps *discovery.EndpointSlice) string {
if eps.Labels == nil {
Expand Down
Loading