Skip to content

Commit

Permalink
Revert "Remove OAM related code"
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohd Uzair authored Nov 10, 2023
1 parent 35f1088 commit 3ab4323
Show file tree
Hide file tree
Showing 10 changed files with 692 additions and 27 deletions.
5 changes: 1 addition & 4 deletions Makefile
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:
Expand Down
7 changes: 5 additions & 2 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ type Handler interface {
// CreateInstance(*chan interface{}) error // Instantiates clients used in deploying and managing mesh instances, e.g. Kubernetes clients.
ApplyOperation(context.Context, OperationRequest) error // Applies an adapter operation. This is adapter specific and needs to be implemented by each adapter.
ListOperations() (Operations, error) // List all operations an adapter supports.
StreamErr(*meshes.EventsResponse, error) // Streams an error event, e.g. to a channel
StreamInfo(*meshes.EventsResponse) // Streams an informational event, e.g. to a channel
ProcessOAM(ctx context.Context, srv OAMRequest) (string, error)

// Need not implement this method and can be reused
StreamErr(*meshes.EventsResponse, error) // Streams an error event, e.g. to a channel
StreamInfo(*meshes.EventsResponse) // Streams an informational event, e.g. to a channel
}

// Adapter contains all handlers, channels, clients, and other parameters for an adapter.
Expand Down
64 changes: 43 additions & 21 deletions adapter/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,30 @@ import (
)

const (
ErrGetNameCode = "1000"
ErrCreateInstanceCode = "1001"
ErrMeshConfigCode = "1002"
ErrValidateKubeconfigCode = "1003"
ErrClientConfigCode = "1004"
ErrClientSetCode = "1005"
ErrStreamEventCode = "1006"
ErrOpInvalidCode = "1007"
ErrApplyOperationCode = "1008"
ErrListOperationsCode = "1009"
ErrNewSmiCode = "1010"
ErrRunSmiCode = "1011"
ErrNoResponseCode = "1011"
ErrJSONMarshalCode = "1015"
ErrSmiInitCode = "1007"
ErrInstallSmiCode = "1008"
ErrConnectSmiCode = "1009"
ErrDeleteSmiCode = "1010"
ErrGenerateComponentsCode = "1011"
ErrAuthInfosInvalidMsgCode = "1012"
ErrCreatingComponentsCode = "1013"
ErrGetNameCode = "1000"
ErrCreateInstanceCode = "1001"
ErrMeshConfigCode = "1002"
ErrValidateKubeconfigCode = "1003"
ErrClientConfigCode = "1004"
ErrClientSetCode = "1005"
ErrStreamEventCode = "1006"
ErrOpInvalidCode = "1007"
ErrApplyOperationCode = "1008"
ErrListOperationsCode = "1009"
ErrNewSmiCode = "1010"
ErrRunSmiCode = "1011"
ErrNoResponseCode = "1011"
ErrOpenOAMDefintionFileCode = "1013"
ErrOpenOAMRefFileCode = "1014"
ErrJSONMarshalCode = "1015"
ErrOAMRetryCode = "1016"
ErrSmiInitCode = "1007"
ErrInstallSmiCode = "1008"
ErrConnectSmiCode = "1009"
ErrDeleteSmiCode = "1010"
ErrGenerateComponentsCode = "1011"
ErrAuthInfosInvalidMsgCode = "1012"
ErrCreatingComponentsCode = "1013"
)

var (
Expand Down Expand Up @@ -113,6 +116,25 @@ func ErrDeleteSmi(err error) error {
return errors.New(ErrDeleteSmiCode, errors.Alert, []string{"Error deleting smi tool: %s"}, []string{err.Error()}, []string{}, []string{})
}

// ErrOpenOAMDefintionFile is the error for opening OAM Definition file
func ErrOpenOAMDefintionFile(err error) error {
return errors.New(ErrOpenOAMDefintionFileCode, errors.Alert, []string{"error opening OAM Definition File: %s"}, []string{err.Error()}, []string{}, []string{})
}

// ErrOpenOAMRefFile is the error for opening OAM Schema Ref file
func ErrOpenOAMRefFile(err error) error {
return errors.New(ErrOpenOAMRefFileCode, errors.Alert, []string{"error opening OAM Schema Ref File: %s"}, []string{err.Error()}, []string{}, []string{})
}

// ErrJSONMarshal is the error for json marhal failure
func ErrJSONMarshal(err error) error {
return errors.New(ErrOAMRetryCode, errors.Alert, []string{"error marshal JSON: %s"}, []string{err.Error()}, []string{}, []string{})
}

func ErrOAMRetry(err error) error {
return errors.New(ErrOAMRetryCode, errors.Alert, []string{"error marshal JSON: %s"}, []string{err.Error()}, []string{}, []string{})
}

// will be depracated
func ErrGenerateComponents(err error) error {
return errors.New(ErrGenerateComponentsCode, errors.Alert, []string{"error generating components"}, []string{err.Error()}, []string{"Invalid component generation method passed, Some invalid field passed in DynamicComponentsConfig"}, []string{"Pass the correct GenerationMethod in DynamicComponentsConfig", "Pass the correct fields in DynamicComponentsConfig"})
Expand Down
20 changes: 20 additions & 0 deletions adapter/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (s *adapterLogger) GetComponentInfo(svc interface{}) error {
return s.next.GetComponentInfo(svc)
}

// func (s *adapterLogger) CreateInstance(c *chan interface{}) error {
// s.log.Info("Creating instance")
// err := s.next.CreateInstance(c)
// if err != nil {
// s.log.Error(err)
// }
// return err
// }

func (s *adapterLogger) ApplyOperation(ctx context.Context, op OperationRequest) error {
s.log.Info("Applying operation ", op.OperationName)
err := s.next.ApplyOperation(ctx, op)
Expand All @@ -57,6 +66,17 @@ func (s *adapterLogger) ApplyOperation(ctx context.Context, op OperationRequest)
return err
}

// ProcessOAM wraps the Handler's ProcessOAM method along with relevant logging
func (s *adapterLogger) ProcessOAM(ctx context.Context, oamRequest OAMRequest) (string, error) {
s.log.Info("Process OAM components")
msg, err := s.next.ProcessOAM(ctx, oamRequest)
if err != nil {
s.log.Error(err)
}

return msg, err
}

func (s *adapterLogger) ListOperations() (Operations, error) {
s.log.Info("Listing Operations")
ops, err := s.next.ListOperations()
Expand Down
107 changes: 107 additions & 0 deletions adapter/meshmodel.go
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
}
Loading

0 comments on commit 3ab4323

Please sign in to comment.