Skip to content

Commit

Permalink
Version 0.0.4 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloud-j-luna authored Aug 17, 2022
1 parent 3347683 commit 9801a0b
Show file tree
Hide file tree
Showing 25 changed files with 640 additions and 95 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.0.4] - 2022-08-17
### Added
- Introduce provider filters with `enforce` and `providers` filters
### Changed
- Make temporary deployment file location cross-platform
### Fixed
- `net` field in provider configuration had wrong default value
- Bug where cheapest bids were not being selected
- Issue where gas adjustment was not enough on deployment update
### Development
- More unit tests
- Implemented several string utilities including `CointainsAny` and `FindAll` functions
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=joaoluna.com
NAMESPACE=cloud
NAME=akash
BINARY=terraform-provider-${NAME}
VERSION=0.0.3
VERSION=0.0.4
OS_ARCH=darwin_arm64

default: install
Expand Down Expand Up @@ -31,8 +31,8 @@ install: build
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}

test:
go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 --cover

testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ cd examples && terraform init && terraform apply --auto-approve
./bin/akash provider lease-status --home ~/.akash --dseq <dseq> --provider <provider>
```

### Get logs

```shell
./bin/akash provider lease-logs --dseq <dseq> --provider <provider> --from "$AKASH_KEY_NAME"
```

## Troubleshooting

### `Error: error unmarshalling: invalid character '<' looking for beginning of value`
Expand Down
2 changes: 0 additions & 2 deletions akash/client/bid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func (ak *AkashClient) GetBids(seqs Seqs, timeout time.Duration) (types.Bids, er
bids := types.Bids{}
for timeout > 0 && len(bids) <= 0 {
startTime := time.Now()
// Check bids on deployments and choose one
currentBids, err := queryBidList(ak, seqs)
if err != nil {
tflog.Error(ak.ctx, "Failed to query bid list")
Expand All @@ -22,7 +21,6 @@ func (ak *AkashClient) GetBids(seqs Seqs, timeout time.Duration) (types.Bids, er
}
tflog.Debug(ak.ctx, fmt.Sprintf("Received %d bids", len(bids)))
bids = currentBids
time.Sleep(time.Second)
timeout -= time.Since(startTime)
}

Expand Down
4 changes: 2 additions & 2 deletions akash/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func (c AkashCommand) SetFrom(key string) AkashCommand {
func (c AkashCommand) GasAuto() AkashCommand {
return c.append("--gas=auto")
}
func (c AkashCommand) SetGasAdjustment() AkashCommand {
return c.append("--gas-adjustment=1.15")
func (c AkashCommand) SetGasAdjustment(adjustment float32) AkashCommand {
return c.append(fmt.Sprintf("--gas-adjustment=%2f", adjustment))
}

func (c AkashCommand) SetGasPrices() AkashCommand {
Expand Down
4 changes: 4 additions & 0 deletions akash/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (c AkashCommand) Raw() ([]byte, error) {
return nil, errors.New(errb.String())
}

tflog.Trace(c.ctx, fmt.Sprintf("Output: %s", out))

return out, nil
}

Expand All @@ -54,6 +56,8 @@ func (c AkashCommand) DecodeJson(v any) error {
return errors.New(errb.String())
}

tflog.Trace(c.ctx, fmt.Sprintf("Output: %s", out))

err = json.NewDecoder(strings.NewReader(string(out))).Decode(v)
if err != nil {
tflog.Debug(c.ctx, fmt.Sprintf("Error while unmarshalling command output"))
Expand Down
2 changes: 1 addition & 1 deletion akash/client/cli/groupings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cli

func (c AkashCommand) DefaultGas() AkashCommand {
return c.GasAuto().SetGasAdjustment().SetGasPrices().SetSignMode("amino-json")
return c.GasAuto().SetGasAdjustment(1.15).SetGasPrices().SetSignMode("amino-json")
}

func (c AkashCommand) SetSeqs(dseq string, gseq string, oseq string) AkashCommand {
Expand Down
6 changes: 3 additions & 3 deletions akash/client/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (ak *AkashClient) GetDeployment(dseq string, owner string) (map[string]inte

func (ak *AkashClient) CreateDeployment(manifestLocation string) (Seqs, error) {

tflog.Debug(ak.ctx, "Creating deployment")
tflog.Info(ak.ctx, "Creating deployment")
// Create deployment using the file created with the SDL
attributes, err := transactionCreateDeployment(ak, manifestLocation)
if err != nil {
Expand All @@ -102,7 +102,7 @@ func (ak *AkashClient) CreateDeployment(manifestLocation string) (Seqs, error) {
gseq, _ := attributes.Get("gseq")
oseq, _ := attributes.Get("oseq")

tflog.Info(ak.ctx, "Deployment created with DSEQ="+dseq+" GSEQ="+gseq+" OSEQ="+oseq)
tflog.Info(ak.ctx, fmt.Sprintf("Deployment created with DSEQ=%s GSEQ=%s OSEQ=%s", dseq, gseq, oseq))

return Seqs{dseq, gseq, oseq}, nil
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (ak *AkashClient) UpdateDeployment(dseq string, manifestLocation string) er
cmd := cli.AkashCli(ak).Tx().Deployment().Update().Manifest(manifestLocation).
SetDseq(dseq).SetFrom(ak.Config.KeyName).SetNode(ak.Config.Node).
SetKeyringBackend(ak.Config.KeyringBackend).SetChainId(ak.Config.ChainId).
DefaultGas().AutoAccept().OutputJson()
GasAuto().SetGasAdjustment(1.5).SetGasPrices().SetSignMode("amino-json").AutoAccept().OutputJson()

out, err := cmd.Raw()
if err != nil {
Expand Down
20 changes: 0 additions & 20 deletions akash/client/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"terraform-provider-akash/akash/client/cli"
Expand Down Expand Up @@ -39,22 +38,3 @@ func (ak *AkashClient) GetLeaseStatus(seqs Seqs, provider string) (*types.LeaseS

return &leaseStatus, nil
}

func (ak *AkashClient) FindCheapest(bids types.Bids) (string, error) {
if len(bids) == 0 {
tflog.Error(ak.ctx, "Empty bid slice")
return "", errors.New("empty bid slice")
}

var cheapestBid *types.Bid = nil

tflog.Info(ak.ctx, "Finding cheapest bid")

for _, bid := range bids {
if cheapestBid == nil || cheapestBid != nil && bid.Amount < cheapestBid.Amount {
cheapestBid = &bid
}
}

return cheapestBid.Id.Provider, nil
}
29 changes: 0 additions & 29 deletions akash/client/provider_test.go

This file was deleted.

42 changes: 40 additions & 2 deletions akash/client/types/bids.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,48 @@ type BidWrapper struct {
type Bids []Bid

type Bid struct {
Id BidId `json:"bid_id"`
Amount int32 `json:"amount"`
Id BidId `json:"bid_id"`
Price BidPrice `json:"price"`
}

type BidId struct {
Provider string `json:"provider"`
}

type BidPrice struct {
Amount float32 `json:"amount,string"`
}

func (b Bids) GetProviderAddresses() []string {
addresses := make([]string, 0, len(b))

for _, bid := range b {
addresses = append(addresses, bid.Id.Provider)
}

return addresses
}

func (b Bids) FindByProvider(provider string) Bid {
for _, bid := range b {
if bid.Id.Provider == provider {
return bid
}
}

return Bid{}
}

// FindAllByProviders finds all the Bid structures that have any of the given providers.
// It returns a slice of all the Bid structures where the providers were found.
func (b Bids) FindAllByProviders(providers []string) Bids {
bids := make(Bids, 0)

for _, provider := range providers {
if bid := b.FindByProvider(provider); bid != (Bid{}) {
bids = append(bids, bid)
}
}

return bids
}
File renamed without changes.
56 changes: 56 additions & 0 deletions akash/extensions/comparable_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package extensions

func Union[T comparable](a []T, b []T) []T {
ch := make(chan T, len(b))

for _, bElem := range b {
go func(t T) {
if Contains(a, t) {
ch <- t
} else {
var zeroValue T
ch <- zeroValue
}
}(bElem)
}

finds := make([]T, 0)
var zeroValue T

for range b {
s := <-ch
if s != zeroValue {
finds = append(finds, s)
}
}

return finds
}

func Contains[T comparable](s []T, e T) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func ContainsAny[T comparable](a []T, b []T) bool {
ch := make(chan bool, len(b))

for _, str := range b {
go func(s T) {
ch <- Contains(a, s)
}(str)
}

for range b {
contains := <-ch
if contains {
return true
}
}

return false
}
51 changes: 51 additions & 0 deletions akash/extensions/comparable_extensions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package extensions

import "testing"

func TestContains(t *testing.T) {
testSlice := []string{"A", "B", "C", "D", "test", "F"}

t.Run("should return true if slice contains value", func(t *testing.T) {
if !Contains(testSlice, "test") {
t.Errorf("Expected slice to contain %s", testSlice)
}
})

t.Run("should return false if slice contains value", func(t *testing.T) {
if Contains(testSlice, "Non-existent test") {
t.Errorf("Expected slice not to contain %s", testSlice)
}
})
}

func TestContainsAny(t *testing.T) {
testSlice := []string{"A", "B", "C", "D", "test", "F"}

t.Run("should return true if slice contains any value", func(t *testing.T) {
if !ContainsAny(testSlice, []string{"yes", "test", "hello", "there"}) {
t.Errorf("Expected slice to contain %s", testSlice)
}
})

t.Run("should return false if slice contains any value", func(t *testing.T) {
if ContainsAny(testSlice, []string{"Non-existent test"}) {
t.Errorf("Expected slice not to contain %s", testSlice)
}
})
}

func TestUnion(t *testing.T) {
type Test struct{ x string }
testSlice := []Test{{"A"}, {"B"}, {"C"}, {"D"}, {"test"}, {"F"}}

t.Run("should return a slice containing finds", func(t *testing.T) {
finds := Union(testSlice, []Test{{"yes"}, {"test"}, {"A"}, {"there"}})
if len(finds) != 2 {
t.Errorf("Expected to find %d but found %d (%v)", 2, len(finds), finds)
}

if !Contains(testSlice, finds[0]) || !Contains(testSlice, finds[1]) {
t.Fail()
}
})
}
26 changes: 26 additions & 0 deletions akash/extensions/string_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package extensions

func FindAll(strings []string, e []string) []string {
ch := make(chan string, len(e))

for _, str := range e {
go func(s string) {
if Contains(strings, s) {
ch <- s
} else {
ch <- ""
}
}(str)
}

finds := make([]string, 0)

for range e {
s := <-ch
if s != "" {
finds = append(finds, s)
}
}

return finds
}
Loading

0 comments on commit 9801a0b

Please sign in to comment.