Skip to content

Commit

Permalink
Added support for getting cluster nodes details and migration status. (
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadkesarwani authored Jan 4, 2024
1 parent 762a4c3 commit 6330c9c
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/service/ocean/providers/aws/listmigrations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/ocean"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/aws"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
"log"
)

func main() {
// All clients require a Session. The Session provides the client with
// shared configuration such as account and credentials.
// A Session should be shared where possible to take advantage of
// configuration and credential caching. See the session package for
// more information.
sess := session.New()

// Create a new instance of the service's client with a Session.
// Optional spotinst.Config values can also be provided as variadic
// arguments to the New function. This option allows you to provide
// service specific configuration.
svc := ocean.New(sess)

// Create a new context.
ctx := context.Background()

// Lists migrations.
out, err := svc.CloudProviderAWS().ListMigrations(ctx, &aws.ReadMigrationInput{
ClusterID: spotinst.String("o-12345"),
})
if err != nil {
log.Fatalf("spotinst: failed to list migrations: %v", err)
}

// Output all migrations, if any.
if len(out.Migration) > 0 {
for _, node := range out.Migration {
log.Printf("Migration %s",
stringutil.Stringify(node))
}
}
}
46 changes: 46 additions & 0 deletions examples/service/ocean/providers/aws/migrationstatus/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/ocean"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/aws"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
"log"
)

func main() {
// All clients require a Session. The Session provides the client with
// shared configuration such as account and credentials.
// A Session should be shared where possible to take advantage of
// configuration and credential caching. See the session package for
// more information.
sess := session.New()

// Create a new instance of the service's client with a Session.
// Optional spotinst.Config values can also be provided as variadic
// arguments to the New function. This option allows you to provide
// service specific configuration.
svc := ocean.New(sess)

// Create a new context.
ctx := context.Background()

//Lists migration status
out, err := svc.CloudProviderAWS().MigrationStatus(ctx, &aws.ReadMigrationStatusInput{
ClusterID: spotinst.String("o-12345"),
MigrationID: spotinst.String("owm-12345678"),
})
if err != nil {
log.Fatalf("spotinst: failed to fetch migration status: %v", err)
}

// Output showing migration status, if any.
if len(out.MigrationStatus) > 0 {
for _, node := range out.MigrationStatus {
log.Printf("MigrationStatus %s",
stringutil.Stringify(node))
}
}
}
46 changes: 46 additions & 0 deletions examples/service/ocean/providers/aws/readnodes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"github.com/spotinst/spotinst-sdk-go/service/ocean"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/aws"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
"log"
)

func main() {
// All clients require a Session. The Session provides the client with
// shared configuration such as account and credentials.
// A Session should be shared where possible to take advantage of
// configuration and credential caching. See the session package for
// more information.
sess := session.New()

// Create a new instance of the service's client with a Session.
// Optional spotinst.Config values can also be provided as variadic
// arguments to the New function. This option allows you to provide
// service specific configuration.
svc := ocean.New(sess)

// Create a new context.
ctx := context.Background()

// Lists nodes.
out, err := svc.CloudProviderAWS().ReadClusterNodes(ctx, &aws.ReadClusterNodeInput{
ClusterID: spotinst.String("o-12345"),
LaunchSpecId: spotinst.String("ols-12345"),
})
if err != nil {
log.Fatalf("spotinst: failed to list nodes: %v", err)
}

// Output all nodes, if any.
if len(out.ClusterNode) > 0 {
for _, node := range out.ClusterNode {
log.Printf("Node %s",
stringutil.Stringify(node))
}
}
}
123 changes: 123 additions & 0 deletions service/ocean/providers/aws/clusternodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package aws

import (
"context"
"encoding/json"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/client"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/uritemplates"
"io/ioutil"
"net/http"
"time"
)

type ClusterNodes struct {
InstanceId *string `json:"instanceId,omitempty"`
InstanceType *string `json:"instanceType,omitempty"`
AvailabilityZone *string `json:"availabilityZone,omitempty"`
LaunchSpecId *string `json:"launchSpecId,omitempty"`
LaunchSpecName *string `json:"launchSpecName,omitempty"`
LifeCycle *string `json:"lifeCycle,omitempty"`
PublicIp *string `json:"publicIp,omitempty"`
NodeName *string `json:"nodeName,omitempty"`
RegistrationStatus *string `json:"registrationStatus,omitempty"`
WorkloadRequestedMilliCpu *int `json:"workloadRequestedMilliCpu,omitempty"`
WorkloadRequestedMemoryInMiB *int `json:"workloadRequestedMemoryInMiB,omitempty"`
WorkloadRequestedGpu *int `json:"workloadRequestedGpu,omitempty"`
HeadroomRequestedMilliCpu *int `json:"headroomRequestedMilliCpu,omitempty"`
HeadroomRequestedMemoryInMiB *int `json:"headroomRequestedMemoryInMiB,omitempty"`
HeadroomRequestedGpu *int `json:"headroomRequestedGpu,omitempty"`
AllocatableMilliCpu *int `json:"allocatableMilliCpu,omitempty"`
AllocatableMemoryInMiB *float64 `json:"allocatableMemoryInMiB,omitempty"`
AllocatableGpu *int `json:"allocatableGpu,omitempty"`

// Read-only fields.
CreatedAt *time.Time `json:"createdAt,omitempty"`

// forceSendFields is a list of field names (e.g. "Keys") to
// unconditionally include in API requests. By default, fields with
// empty values are omitted from API requests. However, any non-pointer,
// non-interface field appearing in ForceSendFields will be sent to the
// server regardless of whether the field is empty or not. This may be
// used to include empty fields in Patch requests.
forceSendFields []string

// nullFields is a list of field names (e.g. "Keys") to include in API
// requests with the JSON null value. By default, fields with empty
// values are omitted from API requests. However, any field with an
// empty value appearing in NullFields will be sent to the server as
// null. It is an error if a field in this list has a non-empty value.
// This may be used to include null fields in Patch requests.
nullFields []string
}
type ReadClusterNodeInput struct {
ClusterID *string `json:"clusterId,omitempty"`
LaunchSpecId *string `json:"launchSpecId,omitempty"`
InstanceId *string `json:"instanceId,omitempty"`
}

type ReadClusterNodeOutput struct {
ClusterNode []*ClusterNodes `json:"clusterNode,omitempty"`
}

func (s *ServiceOp) ReadClusterNodes(ctx context.Context, input *ReadClusterNodeInput) (*ReadClusterNodeOutput, error) {
path, err := uritemplates.Expand("/ocean/aws/k8s/cluster/{oceanClusterId}/nodes", uritemplates.Values{
"oceanClusterId": spotinst.StringValue(input.ClusterID),
})
if err != nil {
return nil, err
}
r := client.NewRequest(http.MethodGet, path)
if input.LaunchSpecId != nil {
r.Params["launchSpecId"] = []string{spotinst.StringValue(input.LaunchSpecId)}
}
if input.InstanceId != nil {
r.Params["instanceId"] = []string{spotinst.StringValue(input.InstanceId)}
}
resp, err := client.RequireOK(s.Client.Do(ctx, r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
gs, err := nodesFromHttpResponse(resp)
if err != nil {
return nil, err
}

return &ReadClusterNodeOutput{ClusterNode: gs}, nil
}

func nodesFromHttpResponse(resp *http.Response) ([]*ClusterNodes, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nodesFromJSON(body)
}

func nodesFromJSON(in []byte) ([]*ClusterNodes, error) {
var rw client.Response
if err := json.Unmarshal(in, &rw); err != nil {
return nil, err
}
out := make([]*ClusterNodes, len(rw.Response.Items))
if len(out) == 0 {
return out, nil
}
for i, rb := range rw.Response.Items {
b, err := nodeFromJSON(rb)
if err != nil {
return nil, err
}
out[i] = b
}
return out, nil
}

func nodeFromJSON(in []byte) (*ClusterNodes, error) {
b := new(ClusterNodes)
if err := json.Unmarshal(in, b); err != nil {
return nil, err
}
return b, nil
}
Loading

0 comments on commit 6330c9c

Please sign in to comment.