mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
feat: use cm data to store ref-count and move control-plane into a subcommand of kubevpn
This commit is contained in:
48
pkg/controlplane/main.go
Normal file
48
pkg/controlplane/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
|
||||
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
|
||||
serverv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Main(filename string, port uint, logger *log.Logger) {
|
||||
snapshotCache := cache.NewSnapshotCache(false, cache.IDHash{}, logger)
|
||||
proc := NewProcessor(snapshotCache, logger)
|
||||
|
||||
go func() {
|
||||
ctx := context.Background()
|
||||
server := serverv3.NewServer(ctx, snapshotCache, nil)
|
||||
RunServer(ctx, server, port)
|
||||
}()
|
||||
|
||||
notifyCh := make(chan NotifyMessage, 100)
|
||||
|
||||
notifyCh <- NotifyMessage{
|
||||
Operation: Create,
|
||||
FilePath: filename,
|
||||
}
|
||||
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Errorf("failed to create file watcher, err: %v", err))
|
||||
}
|
||||
defer watcher.Close()
|
||||
err = watcher.Add(filename)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Errorf("failed to add file: %s to wather, err: %v", filename, err))
|
||||
}
|
||||
go Watch(watcher, filename, notifyCh)
|
||||
|
||||
for {
|
||||
select {
|
||||
case msg := <-notifyCh:
|
||||
log.Infof("path: %s, event: %v", msg.FilePath, msg.Operation)
|
||||
proc.ProcessFile(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@ package controlplane
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
|
||||
@@ -78,7 +78,7 @@ func (p *Processor) ProcessFile(file NotifyMessage) {
|
||||
func ParseYaml(file string) ([]*Virtual, error) {
|
||||
var virtualList = make([]*Virtual, 0)
|
||||
|
||||
yamlFile, err := ioutil.ReadFile(file)
|
||||
yamlFile, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error reading YAML file: %s\n", err)
|
||||
}
|
||||
|
||||
@@ -20,17 +20,7 @@ type NotifyMessage struct {
|
||||
FilePath string
|
||||
}
|
||||
|
||||
func Watch(directory string, notifyCh chan<- NotifyMessage) {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
err = watcher.Add(directory)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
func Watch(watcher *fsnotify.Watcher, filename string, notifyCh chan<- NotifyMessage) {
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
@@ -63,7 +53,7 @@ func Watch(directory string, notifyCh chan<- NotifyMessage) {
|
||||
case <-time.Tick(time.Second * 3):
|
||||
notifyCh <- NotifyMessage{
|
||||
Operation: Modify,
|
||||
FilePath: directory,
|
||||
FilePath: filename,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user