Skip to content

Commit

Permalink
Merge branch 'development' of github.com:threefoldtech/tfgrid-sdk-go …
Browse files Browse the repository at this point in the history
…into development_integrate_multiple_stacks_in_client
  • Loading branch information
rawdaGastan committed Jul 30, 2024
2 parents ec4ee9c + bb5f6ba commit 76938ce
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 158 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tfrobot_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ jobs:
env:
MNEMONIC: ${{ secrets.MNEMONICS }}
NETWORK: main
# run: make integration
run: |
go run main.go deploy -c ./example/test.yaml
- name: Cleanup
if: always()
env:
MNEMONIC: ${{ secrets.MNEMONICS }}
NETWORK: main
run: |
sleep 120 # sleep to make sure graphql is up to date
go run main.go cancel -c ./example/test.yaml -d
13 changes: 8 additions & 5 deletions grid-cli/cmd/deploy_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,18 @@ var deployKubernetesCmd = &cobra.Command{
disks,
nil,
rootfss,
uint64(len(workers)),
)
if err != nil {
log.Fatal().Err(err).Send()
}
workersNode = uint32(workersNodes[0].NodeID)
}
for i := 0; i < workerNumber; i++ {
workers[i].Node = workersNode
for i, node := range workersNodes {
workers[i].Node = uint32(node.NodeID)
}
} else {
for i := 0; i < workerNumber; i++ {
workers[i].Node = workersNode
}
}
cluster, err := command.DeployKubernetesCluster(cmd.Context(), t, master, workers, string(sshKey))
if err != nil {
Expand Down Expand Up @@ -291,5 +295,4 @@ func init() {
deployKubernetesCmd.Flags().Bool("ipv6", false, "assign public ipv6 for master")
deployKubernetesCmd.Flags().Bool("ygg", true, "assign yggdrasil ip for master")
deployKubernetesCmd.Flags().Bool("mycelium", true, "assign mycelium ip for master")

}
8 changes: 6 additions & 2 deletions grid-cli/internal/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"net"
"slices"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -57,9 +58,12 @@ func DeployKubernetesCluster(ctx context.Context, t deployer.TFPluginClient, mas
networkName := fmt.Sprintf("%snetwork", master.Name)
projectName := fmt.Sprintf("kubernetes/%s", master.Name)
networkNodes := []uint32{master.Node}
if len(workers) > 0 && workers[0].Node != master.Node {
networkNodes = append(networkNodes, workers[0].Node)
for _, worker := range workers {
if !slices.Contains(networkNodes, worker.Node) {
networkNodes = append(networkNodes, worker.Node)
}
}

network, err := buildNetwork(networkName, projectName, networkNodes, len(master.MyceliumIPSeed) != 0)
if err != nil {
return workloads.K8sCluster{}, err
Expand Down
8 changes: 4 additions & 4 deletions grid-client/deployer/tf_plugin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func parsePluginOpts(opts ...PluginOpt) (pluginCfg, error) {
}

if cfg.network != DevNetwork && cfg.network != QaNetwork && cfg.network != TestNetwork && cfg.network != MainNetwork {
return cfg, errors.Errorf("network must be one of dev, qa, test, and main not %s", cfg.network)
return cfg, errors.Errorf("network must be one of %s, %s, %s, and %s not %s", DevNetwork, QaNetwork, TestNetwork, MainNetwork, cfg.network)
}

if len(cfg.proxyURLs) == 0 {
Expand Down Expand Up @@ -204,12 +204,12 @@ func NewTFPluginClient(

var identity substrate.Identity
switch cfg.keyType {
case "ed25519":
case peer.KeyTypeEd25519:
identity, err = substrate.NewIdentityFromEd25519Phrase(tfPluginClient.mnemonicOrSeed)
case "sr25519":
case peer.KeyTypeSr25519:
identity, err = substrate.NewIdentityFromSr25519Phrase(tfPluginClient.mnemonicOrSeed)
default:
err = errors.Errorf("key type must be one of ed25519 and sr25519 not %s", cfg.keyType)
err = errors.Errorf("key type must be one of %s and %s not %s", peer.KeyTypeEd25519, peer.KeyTypeSr25519, cfg.keyType)
}

if err != nil {
Expand Down
64 changes: 53 additions & 11 deletions grid-client/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ import (
"net/http"
"time"

"github.com/cenkalti/backoff"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

// GraphQl for tf graphql
type GraphQl struct {
url string
urls []string
activeStackIdx int
}

// NewGraphQl new tf graphql
func NewGraphQl(url string) (GraphQl, error) {
return GraphQl{url}, nil
func NewGraphQl(urls ...string) (GraphQl, error) {
if len(urls) == 0 {
return GraphQl{}, errors.New("graphql url is required")
}

return GraphQl{urls: urls, activeStackIdx: 0}, nil
}

// GetItemTotalCount return count of items
Expand All @@ -34,10 +41,7 @@ func (g *GraphQl) GetItemTotalCount(itemName string, options string) (float64, e

bodyReader := bytes.NewReader(jsonBody)

cl := &http.Client{
Timeout: 10 * time.Second,
}
countResponse, err := cl.Post(g.url, "application/json", bodyReader)
countResponse, err := g.httpPost(bodyReader)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -66,10 +70,7 @@ func (g *GraphQl) Query(body string, variables map[string]interface{}) (map[stri

bodyReader := bytes.NewReader(jsonBody)

cl := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := cl.Post(g.url, "application/json", bodyReader)
resp, err := g.httpPost(bodyReader)
if err != nil {
return result, err
}
Expand Down Expand Up @@ -103,3 +104,44 @@ func parseHTTPResponse(resp *http.Response) (map[string]interface{}, error) {

return data, nil
}

func (g *GraphQl) httpPost(body io.Reader) (*http.Response, error) {
cl := &http.Client{
Timeout: 10 * time.Second,
}

var (
endpoint string
reqErr error
resp *http.Response
)

backoffCfg := backoff.WithMaxRetries(
backoff.NewConstantBackOff(1*time.Millisecond),
2,
)

err := backoff.RetryNotify(func() error {
endpoint = g.urls[g.activeStackIdx]
log.Debug().Str("url", endpoint).Msg("checking")

resp, reqErr = cl.Post(endpoint, "application/json", body)
if reqErr != nil &&
(errors.Is(reqErr, http.ErrAbortHandler) ||
errors.Is(reqErr, http.ErrHandlerTimeout) ||
errors.Is(reqErr, http.ErrServerClosed)) {
g.activeStackIdx = (g.activeStackIdx + 1) % len(g.urls)
return reqErr
}

return nil
}, backoffCfg, func(err error, _ time.Duration) {
log.Error().Err(err).Msg("failed to connect to endpoint, retrying")
})

if err != nil {
log.Error().Err(err).Msg("failed to connect to endpoint")
}

return resp, reqErr
}
2 changes: 1 addition & 1 deletion grid-proxy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/threefoldtech/tfgrid-sdk-go/grid-proxy
go 1.21

require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cenkalti/backoff/v3 v3.2.2
github.com/go-acme/lego/v4 v4.16.1
github.com/google/go-cmp v0.6.0
Expand All @@ -28,7 +29,6 @@ require (
github.com/ChainSafe/go-schnorrkel v1.1.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.12 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
Expand Down
Loading

0 comments on commit 76938ce

Please sign in to comment.