Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 122 additions & 1 deletion oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package oracle
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"

"github.com/libopenstorage/cloudops"
"github.com/oracle/oci-go-sdk/v65/common"
Expand Down Expand Up @@ -37,13 +39,16 @@ const (
MetadataUserDataKey = "user_data"
metadataTenancyIDKey = "oke-tenancy-id"
metadataPoolIDKey = "oke-pool-id"
metadataClusterIDKey = "oke-cluster-id"
envPrefix = "PX_ORACLE"
envInstanceID = "INSTANCE_ID"
envRegion = "INSTANCE_REGION"
envAvailabilityDomain = "INSTNACE_AVAILABILITY_DOMAIN"
envCompartmentID = "COMPARTMENT_ID"
envTenancyID = "TENANCY_ID"
envPoolID = "POOL_ID"
envClusterID = "CLUSTER_ID"
defaultTimeout = 5 * time.Minute
)

type oracleOps struct {
Expand All @@ -55,6 +60,7 @@ type oracleOps struct {
compartmentID string
tenancyID string
poolID string
clusterID string
volumeAttachmentMapping map[string]*string
storage core.BlockstorageClient
compute core.ComputeClient
Expand Down Expand Up @@ -125,6 +131,11 @@ func getInfoFromEnv(oracleOps *oracleOps) error {
if err != nil {
return err
}

oracleOps.clusterID, err = cloudops.GetEnvValueStrict(envClusterID)
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -202,7 +213,7 @@ func GetMetadata() (map[string]interface{}, error) {
}

func getInfoFromMetadata(oracleOps *oracleOps) error {
var tenancyID, poolID string
var tenancyID, poolID, clusterID string
var ok bool
metadata, err := GetMetadata()
if err != nil {
Expand All @@ -217,13 +228,17 @@ func getInfoFromMetadata(oracleOps *oracleOps) error {
if poolID, ok = okeMetadata[metadataPoolIDKey].(string); !ok {
return fmt.Errorf("can not get pool ID from oracle metadata service. error: [%v]", err)
}
if clusterID, ok = okeMetadata[metadataClusterIDKey].(string); !ok {
return fmt.Errorf("can not get cluster ID from oracle metadata service. error: [%v]", err)
}
}
} else {
return fmt.Errorf("can not get OKE metadata from oracle metadata service. error: [%v]", err)
}
}
oracleOps.tenancyID = tenancyID
oracleOps.poolID = poolID
oracleOps.clusterID = clusterID
if oracleOps.instance, ok = metadata[metadataInstanceIDkey].(string); !ok {
return fmt.Errorf("can not get instance id from oracle metadata service. error: [%v]", err)
}
Expand Down Expand Up @@ -457,6 +472,111 @@ func (o *oracleOps) Delete(volumeID string) error {
return nil
}

func (o *oracleOps) SetInstanceGroupSize(instanceGroupID string, count int64, timeout time.Duration) error {

if timeout == 0*time.Second {
timeout = defaultTimeout
}

instanceGroupSize := int(count)

//get nodepool by ID to be updated
nodePoolReq := containerengine.ListNodePoolsRequest{CompartmentId: &o.compartmentID, Name: &instanceGroupID, ClusterId: &o.clusterID}
nodePools, err := o.containerEngine.ListNodePools(context.Background(), nodePoolReq)
if err != nil {
return err
}

if len(nodePools.Items) == 0 {
return errors.New("No node pool found with name " + instanceGroupID)
}
numberOfDomains := len(nodePools.Items[0].NodeConfigDetails.PlacementConfigs)
totalClusterSize := numberOfDomains * instanceGroupSize
logrus.Println("Setting instanceGroupSize to ", totalClusterSize, " in total ", numberOfDomains, " regions.")

//get all availabliity domain
nodePoolPlacementConfigDetails := make([]containerengine.NodePoolPlacementConfigDetails, numberOfDomains)

for i, placementConfigs := range nodePools.Items[0].NodeConfigDetails.PlacementConfigs {
nodePoolPlacementConfigDetails[i].AvailabilityDomain = placementConfigs.AvailabilityDomain
nodePoolPlacementConfigDetails[i].SubnetId = placementConfigs.SubnetId
}

//update node pools
req := containerengine.UpdateNodePoolRequest{
NodePoolId: nodePools.Items[0].Id, //get node pool id
UpdateNodePoolDetails: containerengine.UpdateNodePoolDetails{
NodeConfigDetails: &containerengine.UpdateNodePoolNodeConfigDetails{
Size: &totalClusterSize,
PlacementConfigs: nodePoolPlacementConfigDetails,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we are not making any changes to placementConfig and it's not a required (must) field. We can skip creating a duplicate nodePoolPlacementConfigDetails, if oracle SDK allows that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodePoolPlacementConfigDetails is required to equally balance creation and deletion of instances in each availability domain. We need to provide one placement configuration for each availability domain in which we intend to launch a node.

},
},
}

resp, err := o.containerEngine.UpdateNodePool(context.Background(), req)
if err != nil {
return err
}

err = o.waitTillWorkStatusIsSucceeded(resp.OpcRequestId, resp.OpcWorkRequestId, timeout)
if err != nil {
return err
}

return nil
}

func (o *oracleOps) waitTillWorkStatusIsSucceeded(opcRequestID, opcWorkRequestID *string, timeout time.Duration) error {
workReq := containerengine.GetWorkRequestRequest{OpcRequestId: opcRequestID,
WorkRequestId: opcWorkRequestID}

f := func() (interface{}, bool, error) {
workResp, err := o.containerEngine.GetWorkRequest(context.Background(), workReq)
if err != nil {
return nil, true, err
}

if workResp.Status == containerengine.WorkRequestStatusSucceeded {
return workResp.Status, false, nil
}

logrus.Debugf("Work status is in [%s] state", workResp.Status)
return nil, true, fmt.Errorf("Work status is in [%s] state", workResp.Status)
}
_, err := task.DoRetryWithTimeout(f, timeout, 10*time.Second)
return err
}

func (o *oracleOps) GetInstanceGroupSize(instanceGroupID string) (int64, error) {

var count int64

nodePoolReq := containerengine.ListNodePoolsRequest{CompartmentId: &o.compartmentID, Name: &instanceGroupID, ClusterId: &o.clusterID}
nodePools, err := o.containerEngine.ListNodePools(context.Background(), nodePoolReq)
if err != nil {
return 0, err
}

if len(nodePools.Items) == 0 {
return 0, errors.New("No node pool found with name " + instanceGroupID)
}

req := containerengine.GetNodePoolRequest{NodePoolId: nodePools.Items[0].Id}
Comment thread
nikita-bhatia marked this conversation as resolved.

resp, err := o.containerEngine.GetNodePool(context.Background(), req)

if err != nil {
return 0, err
}

for _, node := range resp.Nodes {
if node.LifecycleState == containerengine.NodeLifecycleStateActive {
count++
}
}
return count, nil
}

// Attach volumeID, accepts attachOptions as opaque data
// Return attach path.
func (o *oracleOps) Attach(volumeID string, options map[string]string) (string, error) {
Expand Down Expand Up @@ -606,4 +726,5 @@ func (o *oracleOps) GetDeviceID(vol interface{}) (string, error) {
return *d.Id, nil
}
return "", fmt.Errorf("invalid type: %v given to GetDeviceID", vol)

}
5 changes: 3 additions & 2 deletions oracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func TestAll(t *testing.T) {
t.Skipf("skipping Oracle tests as environment is not set...\n")
}

compartmentID, _ := cloudops.GetEnvValueStrict(fmt.Sprintf("%s_%s", envPrefix, envCompartmentID))
availabilityDomain, _ := cloudops.GetEnvValueStrict(fmt.Sprintf("%s_%s", envPrefix, envAvailabilityDomain))

compartmentID, _ := cloudops.GetEnvValueStrict(fmt.Sprintf("%s", envCompartmentID))
availabilityDomain, _ := cloudops.GetEnvValueStrict(fmt.Sprintf("%s", envAvailabilityDomain))
oracleVol := &core.Volume{
SizeInGBs: common.Int64(newDiskSizeInGB),
CompartmentId: common.String(compartmentID),
Expand Down