Skip to content

Commit

Permalink
improved resources structures and coherence, fixed some bugs, added s…
Browse files Browse the repository at this point in the history
…upport for discovery without selector
  • Loading branch information
cannarelladev committed Oct 17, 2023
1 parent 087cfda commit f31ade2
Show file tree
Hide file tree
Showing 29 changed files with 444 additions and 247 deletions.
2 changes: 1 addition & 1 deletion apis/advertisement/v1alpha1/discovery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type DiscoverySpec struct {

// This is the FlavourSelector that describes the characteristics of the intent that the solver is looking to satisfy
// This pattern corresponds to what has been defined in the REAR Protocol to do a discovery with a selector
Selector nodecorev1alpha1.FlavourSelector `json:"selector"`
Selector *nodecorev1alpha1.FlavourSelector `json:"selector"`

// This flag indicates that needs to be enstablished a subscription to the provider in case a match is found.
// In order to have periodic updates of the status of the matching Flavour
Expand Down
7 changes: 6 additions & 1 deletion apis/advertisement/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions apis/nodecore/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ type NodeIdentity struct {
}

// toString() returns a string representation of the GenericRef.
func (r GenericRef) toString() string {
/* func (r GenericRef) toString() string {
if r.Namespace != "" {
return r.Namespace + "/" + r.Name
}
return r.Name
}
} */
2 changes: 1 addition & 1 deletion apis/nodecore/v1alpha1/solver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type RangeSelector struct {
type SolverSpec struct {

// Selector contains the flavour requirements for the solver.
Selector FlavourSelector `json:"selector"`
Selector *FlavourSelector `json:"selector,omitempty"`

// IntentID is the ID of the intent that the Node Orchestrator is trying to solve.
// It is used to link the solver with the intent.
Expand Down
6 changes: 5 additions & 1 deletion apis/nodecore/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/reservation/v1alpha1/contract_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ContractSpec struct {

// The partition represents the dimension of the resources sold/bought.
// So it will reflect the dimension of the resources allocated on the remote cluster and reflected on the local virtual node.
Partition Partition `json:"partition,omitempty"`
Partition *Partition `json:"partition,omitempty"`

// This is the Node identity of the buyer FLUIDOS Node.
Buyer nodecorev1alpha1.NodeIdentity `json:"buyer"`
Expand Down
2 changes: 1 addition & 1 deletion apis/reservation/v1alpha1/reservation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type ReservationSpec struct {
Seller nodecorev1alpha1.NodeIdentity `json:"seller"`

// Parition is the partition of the flavour that is being reserved
Partition Partition `json:"partition,omitempty"`
Partition *Partition `json:"partition,omitempty"`

// Reserve indicates if the reservation is a reserve or not
Reserve bool `json:"reserve,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion apis/reservation/v1alpha1/transaction_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TransactionSpec struct {
ClusterID string `json:"clusterID"`

// Partition is the partition of the flavour that is being reserved
Partition Partition `json:"partition,omitempty"`
Partition *Partition `json:"partition,omitempty"`

// StartTime is the time at which the reservation should start
StartTime string `json:"startTime,omitempty"`
Expand Down
18 changes: 15 additions & 3 deletions apis/reservation/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions cmd/rear-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,39 @@ func main() {
os.Exit(1)
}

// Periodically clear the transaction cache
if err := mgr.Add(manager.RunnableFunc(gw.CacheRefresher(flags.REFRESH_CACHE_INTERVAL))); err != nil {
klog.Errorf("Unable to set up transaction cache refresher: %s", err)
os.Exit(1)
}

// Periodically check if Liqo is ready
if err := mgr.Add(manager.RunnableFunc(gw.LiqoChecker(flags.LIQO_CHECK_INTERVAL))); err != nil {
klog.Errorf("Unable to set up Liqo checker: %s", err)
os.Exit(1)
}

// Start the REAR Gateway HTTP server
if err := mgr.Add(manager.RunnableFunc(gw.Start)); err != nil {
klog.Errorf("Unable to set up Gateway HTTP server: %s", err)
os.Exit(1)
}

// Start the REAR GRPC server
if err := mgr.Add(manager.RunnableFunc(grpcServer.Start)); err != nil {
klog.Errorf("Unable to set up Gateway GRPC server: %s", err)
os.Exit(1)
}

// Start the REAR Gateway HTTP server
go func() {
gw.StartHttpServer()
}()
/* go func() {
gw.Start()
}() */

// Start the REAR GRPC server
go func() {
/* go func() {
grpcServer.Start()
}()
}() */

// TODO: Uncomment this when the webhook is ready. For now it does not work (Ale)
// pcv := discoverymanager.NewPCValidator(mgr.GetClient())
Expand Down
1 change: 0 additions & 1 deletion deployments/node/crds/nodecore.fluidos.eu_solvers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ spec:
type: object
required:
- intentID
- selector
type: object
status:
description: SolverStatus defines the observed state of Solver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- nodecore.fluidos.eu
resources:
Expand Down
4 changes: 4 additions & 0 deletions examples/kind/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ helm install node ../../deployments/node -n fluidos \
--set networkManager.configMaps.nodeIdentity.ip=$consumer_controlplane_ip:$consumer_node_port \
--set networkManager.configMaps.providers.local=$provider_controlplane_ip:$provider_node_port

liqoctl install kind --cluster-name fluidos-consumer

export KUBECONFIG=$PWD/provider/config

kubectl apply -f ../../deployments/node/crds
Expand All @@ -49,4 +51,6 @@ helm install node ../../deployments/node -n fluidos \
--set networkManager.configMaps.nodeIdentity.ip=$provider_controlplane_ip:$provider_node_port \
--set networkManager.configMaps.providers.local=$consumer_controlplane_ip:$consumer_node_port

liqoctl install kind --cluster-name fluidos-provider


62 changes: 48 additions & 14 deletions pkg/rear-controller/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,28 @@ import (
// TODO: move this function into the REAR Gateway package
// reserveFlavour reserves a flavour with the given flavourID
func (g *Gateway) ReserveFlavour(ctx context.Context, reservation *reservationv1alpha1.Reservation, flavourID string) (*models.Transaction, error) {
err := checkLiqoReadiness(g.LiqoReady)
if err != nil {
return nil, err
}

var transaction models.Transaction

body := models.ReserveRequest{
FlavourID: flavourID,
Buyer: models.NodeIdentity{
NodeID: reservation.Spec.Buyer.NodeID,
IP: reservation.Spec.Buyer.IP,
Domain: reservation.Spec.Buyer.Domain,
NodeID: g.ID.NodeID,
IP: g.ID.IP,
Domain: g.ID.Domain,
},
Partition: parseutil.ParsePartition(reservation.Spec.Partition),
}

klog.Infof("Reservation %s for flavour %s", reservation.Name, flavourID)

if reservation.Spec.Partition != nil {
body.Partition = parseutil.ParsePartition(reservation.Spec.Partition)
}

selectorBytes, err := json.Marshal(body)
if err != nil {
return nil, err
Expand All @@ -56,13 +64,13 @@ func (g *Gateway) ReserveFlavour(ctx context.Context, reservation *reservationv1
bodyBytes := bytes.NewBuffer(selectorBytes)
url := fmt.Sprintf("http://%s%s%s", reservation.Spec.Seller.IP, RESERVE_FLAVOUR_PATH, flavourID)

klog.Infof("Sending POST request to %s", url)
klog.Infof("Sending request to %s", url)

resp, err := makeRequest("POST", url, bodyBytes)
defer resp.Body.Close()
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Check if the response status code is 200 (OK)
if resp.StatusCode != http.StatusOK {
Expand All @@ -82,7 +90,7 @@ func (g *Gateway) ReserveFlavour(ctx context.Context, reservation *reservationv1
return &transaction, fmt.Errorf("transactionID is empty")
}

klog.Infof("Flavour %s reserved: transaction %v", flavourID, transaction)
klog.Infof("Flavour %s reserved: transaction ID %s", flavourID, transaction.TransactionID)

g.addNewTransacion(transaction)

Expand All @@ -91,6 +99,11 @@ func (g *Gateway) ReserveFlavour(ctx context.Context, reservation *reservationv1

// PurchaseFlavour purchases a flavour with the given flavourID
func (g *Gateway) PurchaseFlavour(ctx context.Context, transactionID string, seller nodecorev1alpha1.NodeIdentity) (*models.ResponsePurchase, error) {
err := checkLiqoReadiness(g.LiqoReady)
if err != nil {
return nil, err
}

var purchase models.ResponsePurchase

// Check if the transaction exists
Expand All @@ -113,10 +126,10 @@ func (g *Gateway) PurchaseFlavour(ctx context.Context, transactionID string, sel
url := fmt.Sprintf("http://%s%s%s", seller.IP, PURCHASE_FLAVOUR_PATH, transactionID)

resp, err := makeRequest("POST", url, bodyBytes)
defer resp.Body.Close()
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Check if the response status code is 200 (OK)
if resp.StatusCode != http.StatusOK {
Expand All @@ -131,18 +144,24 @@ func (g *Gateway) PurchaseFlavour(ctx context.Context, transactionID string, sel
}

// SearchFlavour is a function that returns an array of Flavour that fit the Selector by performing a get request to an http server
func (g *Gateway) DiscoverFlavours(selector nodecorev1alpha1.FlavourSelector) ([]*nodecorev1alpha1.Flavour, error) {
// Marshal the selector into JSON bytes
s := parseutil.ParseFlavourSelector(selector)
func (g *Gateway) DiscoverFlavours(selector *nodecorev1alpha1.FlavourSelector) ([]*nodecorev1alpha1.Flavour, error) {
err := checkLiqoReadiness(g.LiqoReady)
if err != nil {
return nil, err
}

// Create the Flavour CR from the first flavour in the array of Flavour
var s *models.Selector
var flavoursCR []*nodecorev1alpha1.Flavour

if selector == nil {
s = parseutil.ParseFlavourSelector(selector)
}

providers := getters.GetLocalProviders(context.Background(), g.client)

// Send the POST request to all the servers in the list
for _, ADDRESS := range providers {
flavour, err := searchFlavour(s, ADDRESS)
for _, provider := range providers {
flavour, err := discover(s, provider)
if err != nil {
klog.Errorf("Error when searching Flavour: %s", err)
return nil, err
Expand All @@ -153,3 +172,18 @@ func (g *Gateway) DiscoverFlavours(selector nodecorev1alpha1.FlavourSelector) ([
klog.Infof("Found %d flavours", len(flavoursCR))
return flavoursCR, nil
}

func discover(s *models.Selector, provider string) (*nodecorev1alpha1.Flavour, error) {
if s != nil {
return searchFlavourWithSelector(s, provider)
}
return searchFlavour(provider)
}

func checkLiqoReadiness(b bool) error {
if !b {
klog.Errorf("Liqo is not ready, please check or wait for the Liqo installation")
return fmt.Errorf("Liqo is not ready, please check or wait for the Liqo installation")
}
return nil
}
Loading

0 comments on commit f31ade2

Please sign in to comment.