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

Add server provisioner #73

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions charts/region/crds/region.unikorn-cloud.org_servers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ spec:
- jsonPath: .metadata.creationTimestamp
name: age
type: date
- jsonPath: .status.privateIP
name: privateIP
type: string
- jsonPath: .status.publicIP
name: publicIP
type: string
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/unikorn/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ type ServerList struct {
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="status",type="string",JSONPath=".status.conditions[?(@.type==\"Available\")].reason"
// +kubebuilder:printcolumn:name="age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="privateIP",type="string",JSONPath=".status.privateIP"
// +kubebuilder:printcolumn:name="publicIP",type="string",JSONPath=".status.publicIP"
type Server struct {
metav1.TypeMeta `json:",inline"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/providers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ type Provider interface {
CreateSecurityGroupRule(ctx context.Context, identity *unikornv1.Identity, securityGroup *unikornv1.SecurityGroup, rule *unikornv1.SecurityGroupRule) error
// DeleteSecurityGroupRule deletes a security group rule.
DeleteSecurityGroupRule(ctx context.Context, identity *unikornv1.Identity, securityGroup *unikornv1.SecurityGroup, rule *unikornv1.SecurityGroupRule) error
// CreateServer creates a new server.
CreateServer(ctx context.Context, identity *unikornv1.Identity, server *unikornv1.Server) error
// DeleteServer deletes a server.
DeleteServer(ctx context.Context, identity *unikornv1.Identity, server *unikornv1.Server) error
}
52 changes: 52 additions & 0 deletions pkg/providers/openstack/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/keypairs"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/quotasets"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servergroups"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"

Expand Down Expand Up @@ -267,3 +268,54 @@ func (c *ComputeClient) UpdateQuotas(ctx context.Context, projectID string) erro

return quotasets.Update(ctx, c.client, projectID, opts).Err
}

func (c *ComputeClient) CreateServer(ctx context.Context, name, imageID, flavorID, keyName string, networkIDs []string, serverGroupID *string, metadata map[string]string) (*servers.Server, error) {
tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, "POST /compute/v2/servers/")
defer span.End()

schedulerHintOpts := servers.SchedulerHintOpts{}

if serverGroupID != nil {
schedulerHintOpts.Group = *serverGroupID
}

networks := make([]servers.Network, len(networkIDs))
for i, id := range networkIDs {
networks[i] = servers.Network{UUID: id}
}

serverCreateOpts := servers.CreateOpts{
Name: name,
ImageRef: imageID,
FlavorRef: flavorID,
Networks: networks,
Metadata: metadata,
}

createOpts := keypairs.CreateOptsExt{
CreateOptsBuilder: serverCreateOpts,
KeyName: keyName,
}

return servers.Create(ctx, c.client, createOpts, schedulerHintOpts).Extract()
}

func (c *ComputeClient) DeleteServer(ctx context.Context, id string) error {
tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, fmt.Sprintf("DELETE /compute/v2/servers/%s", id))
defer span.End()

return servers.Delete(ctx, c.client, id).ExtractErr()
}

func (c *ComputeClient) GetServer(ctx context.Context, id string) (*servers.Server, error) {
tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, fmt.Sprintf("GET /compute/v2/servers/%s", id))
defer span.End()

return servers.Get(ctx, c.client, id).Extract()
}
4 changes: 4 additions & 0 deletions pkg/providers/openstack/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ var (
// ErrResourceNotFound is returned when a named resource cannot
// be looked up (we have to do it ourselves) and it cannot be found.
ErrResourceNotFound = errors.New("requested resource not found")

// ErrResourceDependency is returned when a resource is in unexpected
// state or condition.
ErrResouceDependency = errors.New("resource dependency error")
)
61 changes: 61 additions & 0 deletions pkg/providers/openstack/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (
gophercloud "github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/provider"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/groups"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/rules"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -363,3 +365,62 @@ func (c *NetworkClient) DeleteSecurityGroupRule(ctx context.Context, securityGro

return rules.Delete(ctx, c.client, ruleID).Err
}

// CreateFloatingIP creates a floating IP.
func (c *NetworkClient) CreateFloatingIP(ctx context.Context, portID string) (*floatingips.FloatingIP, error) {
externalNetworks, err := c.ExternalNetworks(ctx)
if err != nil {
return nil, err
}

tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, "POST /network/v2.0/floatingips", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()

opts := &floatingips.CreateOpts{
FloatingNetworkID: externalNetworks[0].ID,
PortID: portID,
}

floatingIP, err := floatingips.Create(ctx, c.client, opts).Extract()
if err != nil {
return nil, err
}

return floatingIP, nil
}

// DeleteFloatingIP deletes a floating IP.
func (c *NetworkClient) DeleteFloatingIP(ctx context.Context, id string) error {
tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, fmt.Sprintf("DELETE /network/v2.0/floatingips/%s", id), trace.WithSpanKind(trace.SpanKindClient))
defer span.End()

return floatingips.Delete(ctx, c.client, id).Err
}

// ListServerPorts returns a list of ports for a server.
func (c *NetworkClient) ListServerPorts(ctx context.Context, serverID string) ([]ports.Port, error) {
tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, "GET /network/v2.0/ports", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()

listOpts := ports.ListOpts{
DeviceID: serverID,
}

allPages, err := ports.List(c.client, listOpts).AllPages(ctx)
if err != nil {
return nil, err
}

allPorts, err := ports.ExtractPorts(allPages)
if err != nil {
return nil, err
}

return allPorts, nil
}
Loading
Loading