generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mohd Uzair
authored
Nov 10, 2023
1 parent
35f1088
commit 3ab4323
Showing
10 changed files
with
692 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
.PHONY: check | ||
check: lint | ||
check: tidy | ||
check: verify | ||
.PHONY: lint tidy verify | ||
|
||
|
||
lint: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package adapter | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
backoff "github.com/cenkalti/backoff/v4" | ||
"github.com/layer5io/meshkit/models/meshmodel/core/types" | ||
"github.com/layer5io/meshkit/models/meshmodel/core/v1alpha1" | ||
"github.com/layer5io/meshkit/models/meshmodel/registry" | ||
) | ||
|
||
// MeshModelRegistrantDefinitionPath - Structure for configuring registrant paths | ||
type MeshModelRegistrantDefinitionPath struct { | ||
// EntityDefinitionPath holds the path for Entity Definition file | ||
EntityDefintionPath string | ||
|
||
Type types.CapabilityType | ||
// Host is the address of the gRPC host capable of processing the request | ||
Host string | ||
Port int | ||
} | ||
|
||
// MeshModel provides utility functions for registering | ||
// MeshModel components to a registry in a reliable way | ||
type MeshModelRegistrant struct { | ||
Paths []MeshModelRegistrantDefinitionPath | ||
HTTPRegistry string | ||
} | ||
|
||
// NewOAMRegistrant returns an instance of OAMRegistrant | ||
func NewMeshModelRegistrant(paths []MeshModelRegistrantDefinitionPath, HTTPRegistry string) *MeshModelRegistrant { | ||
return &MeshModelRegistrant{ | ||
Paths: paths, | ||
HTTPRegistry: HTTPRegistry, | ||
} | ||
} | ||
|
||
// Register will register each capability individually to the OAM Capability registry | ||
// | ||
// It sends a POST request to the endpoint in the "OAMHTTPRegistry", if the request | ||
// fails then the request is retried. It uses exponential backoff algorithm to determine | ||
// the interval between in the retries. It will retry only for 10 mins and will stop retrying | ||
// after that. | ||
// | ||
// Register function is a blocking function | ||
func (or *MeshModelRegistrant) Register(ctxID string) error { | ||
for _, dpath := range or.Paths { | ||
var mrd registry.MeshModelRegistrantData | ||
definition, err := os.Open(dpath.EntityDefintionPath) | ||
if err != nil { | ||
return ErrOpenOAMDefintionFile(err) | ||
} | ||
mrd.Host = registry.Host{ | ||
Hostname: dpath.Host, | ||
Port: dpath.Port, | ||
Metadata: ctxID, | ||
} | ||
mrd.EntityType = dpath.Type | ||
switch dpath.Type { | ||
case types.ComponentDefinition: | ||
var cd v1alpha1.ComponentDefinition | ||
if err := json.NewDecoder(definition).Decode(&cd); err != nil { | ||
_ = definition.Close() | ||
return ErrJSONMarshal(err) | ||
} | ||
_ = definition.Close() | ||
enbyt, _ := json.Marshal(cd) | ||
mrd.Entity = enbyt | ||
// send request to the register | ||
backoffOpt := backoff.NewExponentialBackOff() | ||
backoffOpt.MaxElapsedTime = 10 * time.Minute | ||
if err := backoff.Retry(func() error { | ||
contentByt, err := json.Marshal(mrd) | ||
if err != nil { | ||
return backoff.Permanent(err) | ||
} | ||
content := bytes.NewReader(contentByt) | ||
|
||
// host here is given by the application itself and is trustworthy hence, | ||
// #nosec | ||
resp, err := http.Post(or.HTTPRegistry, "application/json", content) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusCreated && | ||
resp.StatusCode != http.StatusOK && | ||
resp.StatusCode != http.StatusAccepted { | ||
return fmt.Errorf( | ||
"register process failed, host returned status: %s with status code %d", | ||
resp.Status, | ||
resp.StatusCode, | ||
) | ||
} | ||
return nil | ||
}, backoffOpt); err != nil { | ||
return ErrOAMRetry(err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.