Skip to content

Commit

Permalink
provider/cluster: close unsuccessful deployments
Browse files Browse the repository at this point in the history
refs #215
  • Loading branch information
boz committed Jul 18, 2018
1 parent e049ad8 commit ed5c58c
Show file tree
Hide file tree
Showing 31 changed files with 654 additions and 242 deletions.
6 changes: 3 additions & 3 deletions _run/kube/deployment.yml
Expand Up @@ -14,9 +14,9 @@ services:
profiles:
compute:
web:
cpu: 1
memory: 512
disk: 5
cpu: 0.1
memory: 512Mi
disk: 5Gi
placement:
westcoast:
attributes:
Expand Down
2 changes: 1 addition & 1 deletion _run/kube/run.sh
Expand Up @@ -38,7 +38,7 @@ case "$1" in
akash_provider provider run "$(cat "$DATA_ROOT/master.dc")" -k master --kube
;;
deploy)
akash deployment create deployment.yml -k master -w
akash deployment create deployment.yml -k master
;;
manifest)
akash deployment sendmani deployment.yml "$2" -k master
Expand Down
2 changes: 1 addition & 1 deletion _run/multi/run.sh
Expand Up @@ -29,7 +29,7 @@ case "$1" in
akash marketplace
;;
deploy)
akash deployment create deployment.yml -k master -w
akash deployment create deployment.yml -k master
;;
*)
echo "USAGE: $0 <init|send|query|marketplace|deploy>" >&2
Expand Down
2 changes: 1 addition & 1 deletion _run/single/run.sh
Expand Up @@ -38,7 +38,7 @@ case "$1" in
akash_provider provider run "$(cat "$DATA_ROOT/master.dc")" -k master
;;
deploy)
akash deployment create ../deployment.yml -k master -w
akash deployment create ../deployment.yml -k master
;;
*)
echo "USAGE: $0 <init|akashd|send|query|marketplace|provider|deploy>" >&2
Expand Down
2 changes: 1 addition & 1 deletion app/market/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/market/mocks/engine.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/market/mocks/facilitator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/akash/query_test.go
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestAccountQuery_NoNode(t *testing.T) {
testutil.Shrug(t, 283)
hexaddr := testutil.HexAddress(t)
args := []string{query.QueryCommand().Name(), "account", hexaddr}
base := baseCommand()
Expand All @@ -19,6 +20,7 @@ func TestAccountQuery_NoNode(t *testing.T) {
}

func TestDeploymentQuery_NoNode(t *testing.T) {
testutil.Shrug(t, 283)
hexaddr := testutil.HexDeploymentAddress(t)
args := []string{query.QueryCommand().Name(), "deployment", hexaddr}
base := baseCommand()
Expand All @@ -29,6 +31,7 @@ func TestDeploymentQuery_NoNode(t *testing.T) {
}

func TestOrderQuery_NoNode(t *testing.T) {
testutil.Shrug(t, 283)
hexaddr := testutil.HexDeploymentAddress(t)
args := []string{query.QueryCommand().Name(), "order", hexaddr}
base := baseCommand()
Expand All @@ -39,6 +42,7 @@ func TestOrderQuery_NoNode(t *testing.T) {
}

func TestProviderQuery_NoNode(t *testing.T) {
testutil.Shrug(t, 283)
hexaddr := testutil.HexDeploymentAddress(t)
args := []string{query.QueryCommand().Name(), "provider", hexaddr}
base := baseCommand()
Expand Down
1 change: 1 addition & 0 deletions cmd/akash/session/base.go
Expand Up @@ -22,6 +22,7 @@ const (
defaultKeyType = "ed25519"
defaultCodec = "english"
defaultPassword = "0123456789"
defaultHost = "localhost"
)

func SetupBaseCommand(cmd *cobra.Command) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/akash/session/flags.go
Expand Up @@ -32,7 +32,7 @@ func AddFlagWait(cmd *cobra.Command, flags *pflag.FlagSet) {
}

func AddFlagHost(cmd *cobra.Command, flags *pflag.FlagSet) {
flags.String(flagHost, "", "cluster host")
flags.String(flagHost, defaultHost, "cluster host")
viper.BindPFlag(flagHost, flags.Lookup(flagHost))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/akash/session/session.go
Expand Up @@ -262,7 +262,7 @@ func loadKeyManager(root string) (keys.Keybase, tmdb.DB, error) {
}

func (ctx *session) Host() string {
if len(ctx.cmd.Flag(flagHost).Value.String()) > 0 {
if ctx.cmd.Flag(flagHost).Value.String() != ctx.cmd.Flag(flagHost).DefValue {
return ctx.cmd.Flag(flagHost).Value.String()
}
return viper.GetString(flagHost)
Expand Down
2 changes: 1 addition & 1 deletion marketplace/mocks/handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 78 additions & 13 deletions provider/cluster/client.go
Expand Up @@ -4,19 +4,43 @@ import (
"bufio"
"context"
"io"
"sync"

"github.com/ovrclk/akash/types"
)

type Client interface {
Deploy(types.LeaseID, *types.ManifestGroup) error
TeardownLease(types.LeaseID) error
TeardownNamespace(string) error

Deployments() ([]Deployment, error)
LeaseStatus(types.LeaseID) (*types.LeaseStatusResponse, error)
ServiceStatus(types.LeaseID, string) (*types.ServiceStatusResponse, error)
ServiceLogs(context.Context, types.LeaseID, int64, bool) ([]*ServiceLog, error)

Inventory() ([]Node, error)
}

type Node interface {
ID() string
Available() types.ResourceUnit
}

type node struct {
id string
available types.ResourceUnit
}

func NewNode(id string, available types.ResourceUnit) Node {
return &node{id: id, available: available}
}

func (n *node) ID() string {
return n.id
}

func (n *node) Available() types.ResourceUnit {
return n.available
}

type Deployment interface {
Expand All @@ -30,7 +54,16 @@ type ServiceLog struct {
Scanner *bufio.Scanner
}

type nullClient int
const (
// 5 CPUs, 5Gi memory for null client.
nullClientCPU = 5
nullClientMemory = 5 * 1024 * 1024 * 1024
)

type nullClient struct {
leases map[string]*types.ManifestGroup
mtx sync.Mutex
}

func NewServiceLog(name string, stream io.ReadCloser) *ServiceLog {
return &ServiceLog{
Expand All @@ -41,33 +74,65 @@ func NewServiceLog(name string, stream io.ReadCloser) *ServiceLog {
}

func NullClient() Client {
return nullClient(0)
return &nullClient{
leases: make(map[string]*types.ManifestGroup),
mtx: sync.Mutex{},
}
}

func (nullClient) Deploy(_ types.LeaseID, _ *types.ManifestGroup) error {
func (c *nullClient) Deploy(lid types.LeaseID, mgroup *types.ManifestGroup) error {
c.mtx.Lock()
defer c.mtx.Unlock()
c.leases[lid.String()] = mgroup
return nil
}

func (nullClient) LeaseStatus(_ types.LeaseID) (*types.LeaseStatusResponse, error) {
return nil, nil
func (c *nullClient) LeaseStatus(lid types.LeaseID) (*types.LeaseStatusResponse, error) {
c.mtx.Lock()
defer c.mtx.Unlock()

mgroup, ok := c.leases[lid.String()]
if !ok {
return nil, nil
}

resp := &types.LeaseStatusResponse{}
for _, svc := range mgroup.Services {
resp.Services = append(resp.Services, &types.ServiceStatus{
Name: svc.Name,
Available: int32(svc.Count),
Total: int32(svc.Count),
})
}

return resp, nil
}

func (nullClient) ServiceStatus(_ types.LeaseID, _ string) (*types.ServiceStatusResponse, error) {
func (c *nullClient) ServiceStatus(_ types.LeaseID, _ string) (*types.ServiceStatusResponse, error) {
return nil, nil
}

func (nullClient) ServiceLogs(_ context.Context, _ types.LeaseID, _ int64, _ bool) ([]*ServiceLog, error) {
func (c *nullClient) ServiceLogs(_ context.Context, _ types.LeaseID, _ int64, _ bool) ([]*ServiceLog, error) {
return nil, nil
}

func (nullClient) TeardownLease(_ types.LeaseID) error {
return nil
}
func (c *nullClient) TeardownLease(lid types.LeaseID) error {
c.mtx.Lock()
defer c.mtx.Unlock()

func (nullClient) TeardownNamespace(_ string) error {
delete(c.leases, lid.String())
return nil
}

func (nullClient) Deployments() ([]Deployment, error) {
func (c *nullClient) Deployments() ([]Deployment, error) {
return nil, nil
}

func (c *nullClient) Inventory() ([]Node, error) {
return []Node{
NewNode("solo", types.ResourceUnit{
CPU: nullClientCPU,
Memory: nullClientMemory,
}),
}, nil
}
9 changes: 5 additions & 4 deletions provider/cluster/kube/apply.go
@@ -1,6 +1,7 @@
package kube

import (
akashv1 "github.com/ovrclk/akash/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -88,18 +89,18 @@ func prepareEnvironment(kc kubernetes.Interface, ns string) error {
return err
}

func applyManifest(c *client, b *manifestBuilder) error {
obj, err := c.mc.AkashV1().Manifests(c.ns).Get(b.name(), metav1.GetOptions{})
func applyManifest(kc akashv1.Interface, b *manifestBuilder) error {
obj, err := kc.AkashV1().Manifests(b.ns()).Get(b.name(), metav1.GetOptions{})
switch {
case err == nil:
obj, err = b.update(obj)
if err == nil {
_, err = c.mc.AkashV1().Manifests(c.ns).Update(obj)
_, err = kc.AkashV1().Manifests(b.ns()).Update(obj)
}
case errors.IsNotFound(err):
obj, err = b.create()
if err == nil {
_, err = c.mc.AkashV1().Manifests(c.ns).Create(obj)
_, err = kc.AkashV1().Manifests(b.ns()).Create(obj)
}
}
return err
Expand Down
12 changes: 9 additions & 3 deletions provider/cluster/kube/builder.go
Expand Up @@ -266,20 +266,26 @@ func lidNS(lid types.LeaseID) string {
// manifest
type manifestBuilder struct {
builder
mns string
}

func newManifestBuilder(lid types.LeaseID, group *types.ManifestGroup) *manifestBuilder {
func newManifestBuilder(ns string, lid types.LeaseID, group *types.ManifestGroup) *manifestBuilder {
return &manifestBuilder{
builder: builder{lid, group},
mns: ns,
}
}

func (b *manifestBuilder) ns() string {
return b.mns
}

func (b *manifestBuilder) create() (*akashv1.Manifest, error) {
return akashv1.NewManifest(b.ns(), &b.lid, b.group)
return akashv1.NewManifest(lidNS(b.lid), &b.lid, b.group)
}

func (b *manifestBuilder) update(obj *akashv1.Manifest) (*akashv1.Manifest, error) {
return akashv1.NewManifest(b.ns(), &b.lid, b.group)
return akashv1.NewManifest(lidNS(b.lid), &b.lid, b.group)
}

func (b *manifestBuilder) name() string {
Expand Down

0 comments on commit ed5c58c

Please sign in to comment.