Skip to content

Commit

Permalink
feat: feature discovery enablement
Browse files Browse the repository at this point in the history
  • Loading branch information
chainzero authored and troian committed Jan 22, 2024
1 parent 737e027 commit af85c28
Show file tree
Hide file tree
Showing 4 changed files with 450 additions and 1 deletion.
55 changes: 55 additions & 0 deletions gateway/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -43,6 +44,8 @@ import (
"github.com/akash-network/provider/gateway/utils"
pmanifest "github.com/akash-network/provider/manifest"
ipoptypes "github.com/akash-network/provider/operator/ipoperator/types"

v1 "github.com/akash-network/akash-api/go/inventory/v1"
)

type CtxAuthKey string
Expand Down Expand Up @@ -107,6 +110,12 @@ func newRouter(log log.Logger, addr sdk.Address, pclient provider.Client, ipopcl
createStatusHandler(log, pclient, addr)).
Methods("GET")

// GET /features
// provider features endpoint does not require authentication
router.HandleFunc("/features",
createFeaturesHandler(log, pclient, addr)).
Methods("GET")

vrouter := router.NewRoute().Subrouter()
vrouter.Use(requireOwner())

Expand Down Expand Up @@ -524,6 +533,52 @@ func createStatusHandler(log log.Logger, sclient provider.StatusClient, provider
}
}

func createFeaturesHandler(log log.Logger, sclient provider.StatusClient, providerAddr sdk.Address) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {

// URLs slice and use in range allows execution in both dev and prod
urls := []string{
"http://inventory-operator.akash-services.svc.cluster.local:8081/getClusterState",
"http://localhost:8081/getClusterState",
}

var resp *http.Response
var err error
for _, url := range urls {
resp, err = http.Get(url)
if err != nil {
fmt.Printf("Failed to get '%s': %v\n", url, err)
continue
}
defer resp.Body.Close()
break
}

if err != nil {
fmt.Printf("All attempts failed: %v\n", err)
return
}

defer resp.Body.Close()

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}

var clusterState v1.Cluster
err = json.Unmarshal(bodyBytes, &clusterState)
if err != nil {
fmt.Println(err)
}

fmt.Println("clusterState: ", clusterState)

writeJSON(log, w, clusterState)

}
}

func validateHandler(log log.Logger, cl provider.ValidateClient) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
data, err := io.ReadAll(req.Body)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
go.uber.org/zap v1.24.0
golang.org/x/net v0.14.0
golang.org/x/sync v0.3.0
google.golang.org/grpc v1.57.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
Expand Down Expand Up @@ -252,7 +253,6 @@ require (
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
25 changes: 25 additions & 0 deletions operator/inventory/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -94,6 +95,30 @@ func Cmd() *cobra.Command {
}
storage = append(storage, st)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Channel to receive error messages from the goroutine
errChan := make(chan error, 1)

// Start FeatureDiscovery in a separate goroutine
go func() {
errChan <- FeatureDiscovery(ctx)
}()

// ... other code ...

select {
case err := <-errChan:
// Handle error from FeatureDiscovery
// You might log the error, or take corrective action
log.Printf("FeatureDiscovery encountered an error: %v", err)
case <-cmd.Context().Done():
// Handle the case where the main command is stopped
// Cancel the context used by FeatureDiscovery
cancel()
}

if st, err = NewRancher(cmd.Context()); err != nil {
return err
}
Expand Down
Loading

0 comments on commit af85c28

Please sign in to comment.