Skip to content

Commit

Permalink
Added separate gRPC client
Browse files Browse the repository at this point in the history
Signed-off-by: Patryk Strusiewicz-Surmacki <[email protected]>
  • Loading branch information
p-strusiewiczsurmacki-mobica committed Jun 5, 2024
1 parent 971cf33 commit abdc55e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
13 changes: 6 additions & 7 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@ func main() {

log.Info("agent's port", "port", port)

var err error

anycastTracker := anycast.NewTracker(&nl.Toolkit{})

var err error
var adapter agent.Adapter
switch agentType {
case "netconf":
case "vrf-igbp":
adapter, err = vrfigbpadapter.New(anycastTracker, log)
default:
log.Error(fmt.Errorf("agent is currently not supported"), "type", agentType)
os.Exit(1)
default:
adapter, err = vrfigbpadapter.New(anycastTracker, log)
}

log.Info("created adapter", "type", agentType)

if err != nil {
log.Error(err, "error creating adapter")
os.Exit(1)
}

log.Info("created adapter", "type", agentType)

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Error(err, "error on listening start")
Expand Down
24 changes: 13 additions & 11 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"os"
"sort"
"strconv"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -32,10 +31,10 @@ import (

"github.com/telekom/das-schiff-network-operator/api/v1alpha1"
"github.com/telekom/das-schiff-network-operator/controllers"
vrfigbpadapter "github.com/telekom/das-schiff-network-operator/pkg/adapters/vrf_igbp"
"github.com/telekom/das-schiff-network-operator/pkg/agent"
"github.com/telekom/das-schiff-network-operator/pkg/anycast"
"github.com/telekom/das-schiff-network-operator/pkg/bpf"
grpcclient "github.com/telekom/das-schiff-network-operator/pkg/clients/grpc"
"github.com/telekom/das-schiff-network-operator/pkg/config"
"github.com/telekom/das-schiff-network-operator/pkg/healthcheck"
"github.com/telekom/das-schiff-network-operator/pkg/macvlan"
Expand Down Expand Up @@ -98,6 +97,7 @@ func main() {
var nodeConfigPath string
var agentType string
var agentPort int
var agentAddr string
flag.StringVar(&configFile, "config", "",
"The controller will load its initial configuration from this file. "+
"Omit this flag to use the default configuration values. "+
Expand All @@ -109,8 +109,8 @@ func main() {
flag.StringVar(&nodeConfigPath, "nodeconfig-path", reconciler.DefaultNodeConfigPath,
"Path to store working node configuration.")
flag.StringVar(&agentType, "agent", "vrf-igbp", "Use selected agent type (default: vrf-igbp).")
flag.IntVar(&agentPort, "agentPort", agent.DefaultPort,
"gRPC agent port. (default: "+strconv.Itoa(agent.DefaultPort)+")")
flag.StringVar(&agentAddr, "agentAddr", "", "Agent's address (default: '').")
flag.IntVar(&agentPort, "agentPort", agent.DefaultPort, fmt.Sprintf("Agent's port (default: %d).", agent.DefaultPort))
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -139,14 +139,16 @@ func main() {

var agentClient agent.Client
switch agentType {
case "netconf":
setupLog.Error(fmt.Errorf("netconf agent is currently not supported"), "unsupported error")
os.Exit(1)
case "vrf-igbp":
agentClient, err = grpcclient.NewClient(fmt.Sprintf("%s:%d", agentAddr, agentPort))
default:
agentClient, err = vrfigbpadapter.NewClient(fmt.Sprintf(":%d", agentPort))
if err != nil {
setupLog.Error(err, "error creating agent's client")
}
setupLog.Error(fmt.Errorf("agent %s is currently not supported", agentType), "unsupported error")
os.Exit(1)
}

if err != nil {
setupLog.Error(err, "error creating agent's client")
os.Exit(1)
}

if err := start(&options, onlyBPFMode, nodeConfigPath, interfacePrefix, agentClient); err != nil {
Expand Down
54 changes: 54 additions & 0 deletions pkg/clients/grpc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package clients

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/telekom/das-schiff-network-operator/api/v1alpha1"
"github.com/telekom/das-schiff-network-operator/pkg/agent"
agentpb "github.com/telekom/das-schiff-network-operator/pkg/agent/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const defaultTimeout = 30 * time.Second

type Client struct {
agentpb.AgentClient
}

func NewClient(address string) (agent.Client, error) {
var grpcOpts []grpc.DialOption
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(address, grpcOpts...)
if err != nil {
return nil, fmt.Errorf("unable to create gRPC connection: %w", err)
}

vrfigbpClient := Client{agentpb.NewAgentClient(conn)}

return &vrfigbpClient, nil
}

func (c *Client) SendConfig(ctx context.Context, nodeConfig *v1alpha1.NodeConfig) error {
timeoutCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()

nc := agentpb.NetworkConfiguration{
Data: []byte{},
}
data, err := json.Marshal(*nodeConfig)
if err != nil {
return fmt.Errorf("error marshaling NodeConfig: %w", err)
}

nc.Data = data

if _, err = c.SetConfiguration(timeoutCtx, &nc); err != nil {
return fmt.Errorf("error setting configuration: %w", err)
}

return nil
}

0 comments on commit abdc55e

Please sign in to comment.