Skip to content

Commit

Permalink
Gen2 CAPI cloudspace provisioning (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: ansalamdaniel <[email protected]>
  • Loading branch information
ansalamdaniel authored Sep 3, 2024
1 parent 9323ea2 commit 0d90d48
Show file tree
Hide file tree
Showing 9 changed files with 1,137 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/cloudspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ output "csphase" {

- `api_server_endpoint` (String) Kubernetes api server URL
- `bids` (Attributes Set) (see [below for nested schema](#nestedatt--bids))
- `deployment_type` (String) deployment type for the cloudspace (gen1|gen2)
- `first_ready_timestamp` (String) The time when the cloudspace was first ready.
- `hacontrol_plane` (Boolean) High Availability Kubernetes (replicated control plane for redundancy). This is a critical feature for production workloads.
- `health` (String) Health indicates if CloudSpace has a working APIServer and available nodes
Expand Down
1 change: 1 addition & 0 deletions docs/resources/cloudspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resource "spot_cloudspace" "example" {
### Optional

- `cloudspace_name` (String) The name of the cloudspace.
- `deployment_type` (String) The name of the cloudspace.
- `hacontrol_plane` (Boolean) High Availability Kubernetes (replicated control plane for redundancy). This is a critical feature for production workloads.
- `name` (String) The name of the cloudspace.
- `preemption_webhook` (String) Webhook URL for preemption notifications.
Expand Down
3 changes: 3 additions & 0 deletions examples/cloudspace/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ resource "spot_cloudspace" "example" {
hacontrol_plane = false
preemption_webhook = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
wait_until_ready = true
# deployment_type field is optional and the default is "gen1".
# Supported values: gen1, gen2
deployment_type = "gen1"
}

# Creates a spot node pool with an autoscaling pool of 3-8 servers of class gp.vs1.large-dfw.
Expand Down
1,042 changes: 1,036 additions & 6 deletions go.work.sum

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions internal/provider/cloudspace_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"github.com/rackerlabs/terraform-provider-spot/internal/provider/datasource_cloudspace"
)

var _ datasource.DataSource = (*cloudspaceDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*cloudspaceDataSource)(nil)
var (
_ datasource.DataSource = (*cloudspaceDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*cloudspaceDataSource)(nil)
)

func NewCloudspaceDataSource() datasource.DataSource {
return &cloudspaceDataSource{}
Expand Down Expand Up @@ -97,6 +99,7 @@ func (d *cloudspaceDataSource) Read(ctx context.Context, req datasource.ReadRequ
data.Reason = types.StringValue(cloudspace.Status.Reason)
data.HacontrolPlane = types.BoolValue(cloudspace.Spec.HAControlPlane)
data.FirstReadyTimestamp = types.StringValue(cloudspace.Status.FirstReadyTimestamp.Format(time.RFC3339))
data.DeploymentType = types.StringValue(cloudspace.Spec.DeploymentType)
if cloudspace.Spec.Webhook != "" {
// even if we dont set string value it becomes "" by default
// assume it as Null if it is not set
Expand Down
20 changes: 15 additions & 5 deletions internal/provider/cloudspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
ngpcv1 "github.com/RSS-Engineering/ngpc-cp/api/v1"
)

var _ resource.Resource = (*cloudspaceResource)(nil)
var _ resource.ResourceWithConfigure = (*cloudspaceResource)(nil)
var _ resource.ResourceWithImportState = (*cloudspaceResource)(nil)
var _ resource.ResourceWithModifyPlan = (*cloudspaceResource)(nil)
var (
_ resource.Resource = (*cloudspaceResource)(nil)
_ resource.ResourceWithConfigure = (*cloudspaceResource)(nil)
_ resource.ResourceWithImportState = (*cloudspaceResource)(nil)
_ resource.ResourceWithModifyPlan = (*cloudspaceResource)(nil)
)

func NewCloudspaceResource() resource.Resource {
return &cloudspaceResource{}
Expand Down Expand Up @@ -69,7 +71,7 @@ func (r *cloudspaceResource) ModifyPlan(ctx context.Context, req resource.Modify
resp.Diagnostics.AddWarning("Failed to validate region", err.Error())
} else {
var validRegion bool
var regionNames = make([]string, len(regionsList))
regionNames := make([]string, len(regionsList))
for i, region := range regionsList {
if region.Name == regionVal.ValueString() {
validRegion = true
Expand Down Expand Up @@ -125,6 +127,7 @@ func (r *cloudspaceResource) Create(ctx context.Context, req resource.CreateRequ
Cloud: "default",
HAControlPlane: data.HacontrolPlane.ValueBool(),
Webhook: data.PreemptionWebhook.ValueString(),
DeploymentType: data.DeploymentType.ValueString(),
},
}
tflog.Info(ctx, "Creating cloudspace", map[string]any{"name": cloudspace.ObjectMeta.Name})
Expand Down Expand Up @@ -237,6 +240,11 @@ func (r *cloudspaceResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

if plan.DeploymentType.ValueString() != state.DeploymentType.ValueString() {
resp.Diagnostics.AddError("Update to the deployment_type is not allowed", fmt.Sprintf("%s to %s is not allowed", state.DeploymentType.ValueString(), plan.DeploymentType.ValueString()))
return
}

resourceVersionBytes, diags := req.Private.GetKey(ctx, keyResourceVersion)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -259,6 +267,7 @@ func (r *cloudspaceResource) Update(ctx context.Context, req resource.UpdateRequ
Cloud: "default",
HAControlPlane: plan.HacontrolPlane.ValueBool(),
Webhook: plan.PreemptionWebhook.ValueString(),
DeploymentType: plan.DeploymentType.ValueString(),
},
}
tflog.Debug(ctx, "Updating cloudspace", map[string]any{"name": name, "namespace": namespace})
Expand Down Expand Up @@ -333,6 +342,7 @@ func setCloudspaceState(ctx context.Context, cloudspace *ngpcv1.CloudSpace, stat
state.CloudspaceName = types.StringValue(cloudspace.ObjectMeta.Name)
state.Region = types.StringValue(cloudspace.Spec.Region)
state.HacontrolPlane = types.BoolValue(cloudspace.Spec.HAControlPlane)
state.DeploymentType = types.StringValue(cloudspace.Spec.DeploymentType)
if cloudspace.Spec.Webhook != "" {
// even if we dont set string value it becomes "" by default
// assume it as Null if it is not set
Expand Down

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

16 changes: 16 additions & 0 deletions internal/provider/resource_cloudspace/cloudspace_resource_gen.go

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

54 changes: 54 additions & 0 deletions provider_code_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,53 @@
}
]
}
},
{
"name": "deployment_type",
"string": {
"default": {
"static": "gen1"
},
"computed_optional_required": "computed_optional",
"description": "The name of the cloudspace.",
"plan_modifiers": [
{
"custom": {
"imports": [
{
"path": "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
}
],
"schema_definition": "stringplanmodifier.RequiresReplace()"
}
},
{
"custom": {
"imports": [
{
"path": "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
}
],
"schema_definition": "stringplanmodifier.UseStateForUnknown()"
}
}
],
"validators": [
{
"custom": {
"imports": [
{
"path": "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
},
{
"path": "regexp"
}
],
"schema_definition": "stringvalidator.RegexMatches(regexp.MustCompile(`^gen[12]$`), \"deployment_type must be gen1 (or) gen2\")"
}
}
]
}
}
]
}
Expand Down Expand Up @@ -1079,6 +1126,13 @@
"computed_optional_required": "computed",
"description": "Reason contains the reason why the CloudSpace is in a certain phase."
}
},
{
"name": "deployment_type",
"string": {
"computed_optional_required": "computed",
"description": "deployment type for the cloudspace (gen1|gen2)"
}
}
]
}
Expand Down

0 comments on commit 0d90d48

Please sign in to comment.