Skip to content

Commit

Permalink
Add server userdata to support server configuration and init scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nsricardor committed Dec 3, 2024
1 parent 3debb83 commit fd1ddfd
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 55 deletions.
5 changes: 5 additions & 0 deletions charts/region/crds/region.unikorn-cloud.org_servers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ spec:
- value
type: object
type: array
userData:
description: UserData contains configuration information or scripts
to use upon launch.
format: byte
type: string
required:
- flavorID
- image
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/unikorn/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,8 @@ type ServerSpec struct {
PublicIPAllocation *ServerPublicIPAllocationSpec `json:"publicIPAllocation,omitempty"`
// Networks is the server network configuration.
Networks []ServerNetworkSpec `json:"networks,omitempty"`
// UserData contains configuration information or scripts to use upon launch.
UserData []byte `json:"userData,omitempty"`
}

type ServerSecurityGroupSpec struct {
Expand Down
18 changes: 18 additions & 0 deletions pkg/handler/server/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func convert(in *unikornv1.Server) *openapi.ServerRead {
Networks: convertServerNetworks(in.Spec.Networks),
PublicIPAllocation: convertServerPublicIPAllocation(in.Spec.PublicIPAllocation),
SecurityGroups: convertServerSecurityGroups(in.Spec.SecurityGroups),
UserData: convertServerUserData(in.Spec.UserData),
},
Status: openapi.ServerReadStatus{
PrivateIP: in.Status.PrivateIP,
Expand Down Expand Up @@ -121,6 +122,14 @@ func convertServerSecurityGroup(in *unikornv1.ServerSecurityGroupSpec) openapi.S
}
}

func convertServerUserData(in []byte) *[]byte {
if in == nil {
return nil
}

return &in
}

type generator struct {
// client allows Kubernetes API access.
client client.Client
Expand Down Expand Up @@ -164,6 +173,7 @@ func (g *generator) generate(ctx context.Context, in *openapi.ServerWrite) (*uni
PublicIPAllocation: g.generatePublicIPAllocation(in.Spec.PublicIPAllocation),
SecurityGroups: g.generateSecurityGroups(in.Spec.SecurityGroups),
Networks: g.generateNetworks(in.Spec.Networks),
UserData: g.generateUserData(in.Spec.UserData),
},
}

Expand Down Expand Up @@ -228,3 +238,11 @@ func (g *generator) generateNetworks(in openapi.ServerNetworkList) []unikornv1.S

return out
}

func (g *generator) generateUserData(in *[]byte) []byte {
if in == nil {
return nil
}

return *in
}
107 changes: 54 additions & 53 deletions pkg/openapi/schema.go

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

8 changes: 8 additions & 0 deletions pkg/openapi/server.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,10 @@ components:
$ref: '#/components/schemas/serverPublicIPAllocation'
networks:
$ref: '#/components/schemas/serverNetworkList'
userData:
description: UserData contains base64-encoded configuration information or scripts to use upon launch.
type: string
format: byte
serverNetworkList:
description: A list of networks.
type: array
Expand Down Expand Up @@ -1296,6 +1300,10 @@ components:
$ref: '#/components/schemas/serverPublicIPAllocation'
networks:
$ref: '#/components/schemas/serverNetworkList'
userData:
description: UserData contains base64-encoded configuration information or scripts to use upon launch.
type: string
format: byte
serverWrite:
description: A server request.
type: object
Expand Down
6 changes: 6 additions & 0 deletions pkg/openapi/types.go

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

3 changes: 2 additions & 1 deletion pkg/providers/openstack/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ 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, securityGroupIDs []string, serverGroupID *string, metadata map[string]string) (*servers.Server, error) {
func (c *ComputeClient) CreateServer(ctx context.Context, name, imageID, flavorID, keyName string, networkIDs, securityGroupIDs []string, serverGroupID *string, metadata map[string]string, userData []byte) (*servers.Server, error) {
tracer := otel.GetTracerProvider().Tracer(constants.Application)

_, span := tracer.Start(ctx, "POST /compute/v2/servers/")
Expand All @@ -293,6 +293,7 @@ func (c *ComputeClient) CreateServer(ctx context.Context, name, imageID, flavorI
Networks: networks,
Metadata: metadata,
SecurityGroups: securityGroupIDs,
UserData: userData,
}

createOpts := keypairs.CreateOptsExt{
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ func (p *Provider) createServer(ctx context.Context, computeService *ComputeClie
securityGroupIDs[i] = sg.ID
}

providerServer, err := computeService.CreateServer(ctx, server.Labels[coreconstants.NameLabel], image.ID, flavor.ID, *identity.Spec.SSHKeyName, networkIDs, securityGroupIDs, identity.Spec.ServerGroupID, metadata)
providerServer, err := computeService.CreateServer(ctx, server.Labels[coreconstants.NameLabel], image.ID, flavor.ID, *identity.Spec.SSHKeyName, networkIDs, securityGroupIDs, identity.Spec.ServerGroupID, metadata, server.Spec.UserData)
if err != nil {
return err
}
Expand Down

0 comments on commit fd1ddfd

Please sign in to comment.