Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Node Driver Functionality #1

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
171 changes: 167 additions & 4 deletions driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package driver

import (
"context"
"errors"
"fmt"

"net/http"

"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state"
waldurclient "github.com/waldur/go-client"
)

const (
Expand All @@ -16,8 +21,17 @@ const (
type Driver struct {
*drivers.BaseDriver

ApiUrl string
ApiToken string
ApiUrl string
ApiToken string
ProjectUuid string
OfferingUuid string
FlavorUuid string
ImageUuid string
SystemVolumeSize int
SystemVolumeTypeUuid string
DataVolumeTypeUuid string
SubnetUuids []string
SecurityGroupUuid string
}

// NewDriver creates and returns a new instance of Waldur driver
Expand Down Expand Up @@ -49,13 +63,62 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Name: "waldur-proj-uuid",
Usage: "UUID of the project in Waldur",
},
mcnflag.StringFlag{
EnvVar: "WALDUR_OFFERING_UUID",
Name: "waldur-offering-uuid",
Usage: "UUID of the VM offering in Waldur",
},
mcnflag.StringFlag{
EnvVar: "WALDUR_FLAVOR_UUID",
Name: "waldur-flavor-uuid",
Usage: "UUID of the VM flavor in Waldur",
},
mcnflag.StringFlag{
EnvVar: "WALDUR_IMAGE_UUID",
Name: "waldur-image-uuid",
Usage: "UUID of the VM image in Waldur",
},
mcnflag.IntFlag{
EnvVar: "WALDUR_SYS_VOLUME_SIZE",
Name: "waldur-sys-volume-size",
Usage: "System volume size for Waldur VM (GB)",
},
mcnflag.StringFlag{
EnvVar: "WALDUR_SYS_VOLUME_TYPE_UUID",
Name: "waldur-sys-volume-type-uuid",
Usage: "UUID of the system volume type in Waldur",
},
mcnflag.StringFlag{
EnvVar: "WALDUR_DATA_VOLUME_TYPE_UUID",
Name: "waldur-data-volume-type-uuid",
Usage: "UUID of the data volume type in Waldur",
},
mcnflag.StringFlag{
EnvVar: "WALDUR_SEC_GROUP_UUID",
Name: "waldur-sec-group-uuid",
Usage: "UUID of the security group in Waldur",
},
mcnflag.StringSliceFlag{
EnvVar: "WALDUR_SUBNET_UUIDS",
Name: "waldur-subnet-uuids",
Usage: "List of UUIDs of subnets in Waldur",
},
}
}

// SetConfigFromFlags configures the driver with the command line arguments
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.ApiUrl = flags.String("waldur-api-url")
d.ApiToken = flags.String("waldur-api-token")
d.ProjectUuid = flags.String("waldur-proj-uuid")
d.OfferingUuid = flags.String("waldur-offering-uuid")
d.FlavorUuid = flags.String("waldur-flavor-uuid")
d.ImageUuid = flags.String("waldur-image-uuid")
d.SystemVolumeSize = flags.Int("waldur-sys-volume-size")
d.SystemVolumeTypeUuid = flags.String("waldur-sys-volume-type-uuid")
d.DataVolumeTypeUuid = flags.String("waldur-data-volume-type-uuid")
d.SecurityGroupUuid = flags.String("waldur-sec-group-uuid")
d.SubnetUuids = flags.StringSlice("waldur-subnet-uuids")

// Validation
if d.ApiUrl == "" {
Expand All @@ -66,16 +129,116 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return fmt.Errorf("Waldur requires the --waldur-api-token option")
}

if d.ProjectUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-proj-uuid option")
}
if d.OfferingUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-offering-uuid option")
}
if d.FlavorUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-flavor-uuid option")
}
if d.ImageUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-image-uuid option")
}
if d.SystemVolumeSize == 0 {
return fmt.Errorf("Waldur requires the --waldur-sys-volume-size to be greater than 5 GB")
}
if d.SystemVolumeTypeUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-sys-volume-type-uuid option")
}
if d.DataVolumeTypeUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-data-volume-type-uuid option")
}
if d.SecurityGroupUuid == "" {
return fmt.Errorf("Waldur requires the --waldur-sec-group-uuid option")
}
if d.SubnetUuids == nil {
d.SubnetUuids = []string{}
}

return nil
}

// Create creates a host in Waldur using the driver's config
func (d *Driver) Create() error {
// Implement your provider's logic to create a node
log.Infof("Creating instance for %s...", d.MachineName)

// TODO
projectUri := fmt.Sprintf("%s/api/projects/%s/", d.ApiUrl, d.ProjectUuid)
offeringUri := fmt.Sprintf("%s/api/marketplace-public-offerings/%s/", d.ApiUrl, d.OfferingUuid)
flavorUri := fmt.Sprintf("%s/api/openstack-flavors/%s/", d.ApiUrl, d.FlavorUuid)
imageUri := fmt.Sprintf("%s/api/openstack-images/%s/", d.ApiUrl, d.ImageUuid)
systemVolumeTypeUri := fmt.Sprintf("%s/api/openstack-volume-types/%s/", d.ApiUrl, d.SystemVolumeTypeUuid)
dataVolumeTypeUri := fmt.Sprintf("%s/api/openstack-volume-types/%s/", d.ApiUrl, d.DataVolumeTypeUuid)
subnets := make([]map[string]string, len(d.SubnetUuids))
defaultSecGroupUri := fmt.Sprintf("%s/api/openstack-security-groups/%s/", d.ApiUrl, d.SecurityGroupUuid)
securityGroups := make([]map[string]string, 1)
securityGroups[0] = map[string]string{
"url": defaultSecGroupUri,
}

for i, subnet := range d.SubnetUuids {
subnetUri := fmt.Sprintf("%s/api/openstack-subnets/%s/", d.ApiUrl, subnet)
subnets[i] = map[string]string{
"subnet": subnetUri,
}
}
var attributes interface{} = map[string]interface{}{
"name": d.GetMachineName(),
"flavor": flavorUri,
"image": imageUri,
"system_volume_size": d.SystemVolumeSize * 1024,
"system_volume_type": systemVolumeTypeUri,
"data_volume_type": dataVolumeTypeUri,
"ports": subnets,
"security_groups": securityGroups,
// TODO: add floating_ips
// "floating_ips": floating_ips,
}

acceptingTermsOfService := true

limits := map[string]int{}

hc := http.Client{}
auth, err := waldurclient.NewTokenAuth(d.ApiToken)
if err != nil {
log.Errorf("Error while creating token auth %s", err)
return err
}

client, err := waldurclient.NewClientWithResponses(d.ApiUrl, waldurclient.WithHTTPClient(&hc), waldurclient.WithRequestEditorFn(auth.Intercept))
if err != nil {
log.Errorf("Error creating Waldur client %s", err)
return err
}
requestType := waldurclient.RequestTypesCreate

payload := waldurclient.MarketplaceOrdersCreateJSONRequestBody{
AcceptingTermsOfService: &acceptingTermsOfService,
Attributes: &attributes,
Limits: &limits,
Offering: offeringUri,
Project: projectUri,
Type: &requestType,
}

ctx := context.Background()
resp, err := client.MarketplaceOrdersCreateWithResponse(ctx, payload)

if err != nil {
log.Errorf("Error calling API: %v", err)
return err
}

if resp.StatusCode() != 201 {
responseBody := string(resp.Body[:])
log.Errorf("Unable to create an instance %s, code %d, details", d.MachineName, resp.StatusCode(), responseBody)
msg := fmt.Sprintf("Unable to create an instance %s, code %d", d.MachineName, resp.StatusCode())
return errors.New(msg)
}

log.Infof("Successfully created instance %s", d.MachineName)
return nil
}

Expand Down
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
module github.com/waldur/waldur-rancher-node-driver

go 1.23.7
go 1.24.1

replace github.com/docker/docker => github.com/moby/moby v1.4.2-0.20170731201646-1009e6a40b29

require github.com/docker/machine v0.16.2
require (
github.com/docker/machine v0.16.2
github.com/waldur/go-client v0.0.0-20250311163445-e98305ef85df
)

require (
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
github.com/docker/docker v0.0.0-00010101000000-000000000000 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/docker/docker v28.0.1+incompatible // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.10.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/term v0.30.0 // indirect
Expand Down
17 changes: 15 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg=
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/machine v0.16.2 h1:jyF9k3Zg+oIGxxSdYKPScyj3HqFZ6FjgA/3sblcASiU=
github.com/docker/machine v0.16.2/go.mod h1:I8mPNDeK1uH+JTcUU7X0ZW8KiYz0jyAgNaeSJ1rCfDI=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/moby/moby v1.4.2-0.20170731201646-1009e6a40b29 h1:afhHvUb+SpLlBXWf7FzbZdrpISvFgHZPpXGaSna+b60=
github.com/moby/moby v1.4.2-0.20170731201646-1009e6a40b29/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc=
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/waldur/go-client v0.0.0-20250311163445-e98305ef85df h1:9YHwVjo4Z1myElyK+r/WIS+tjkpyoEE/C20Q/7gpMjw=
github.com/waldur/go-client v0.0.0-20250311163445-e98305ef85df/go.mod h1:mFYRgtDg17/iN4+byM/7p2qk+BUUGtVaD8v5rOTjYQA=
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func main() {
plugin.RegisterDriver(NewDriver("", ""))
plugin.RegisterDriver(driver.NewDriver("", ""))
}