From 2e6a6ccba9778547dbedb924e99546f94d63be59 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Fri, 24 Nov 2023 12:03:03 +0100 Subject: [PATCH] cluster improvements --- internal/model/cluster.go | 20 +--------- internal/model/cluster_cloud_settings.go | 20 ++++++++++ internal/model/provider.go | 4 ++ internal/resource/cluster/cluster.go | 50 ++++++++++++------------ 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/internal/model/cluster.go b/internal/model/cluster.go index 64aba18..8471c4a 100644 --- a/internal/model/cluster.go +++ b/internal/model/cluster.go @@ -25,22 +25,6 @@ type Cluster struct { Bindings *ClusterBindings `tfsdk:"bindings"` } -func (c *Cluster) CloudSettingsAttributes() *console.CloudSettingsAttributes { - if IsCloud(c.Cloud.ValueString(), CloudAWS) { - return &console.CloudSettingsAttributes{Aws: c.CloudSettings.AWS.Attributes()} - } - - if IsCloud(c.Cloud.ValueString(), CloudAzure) { - return &console.CloudSettingsAttributes{Azure: c.CloudSettings.Azure.Attributes()} - } - - if IsCloud(c.Cloud.ValueString(), CloudGCP) { - return &console.CloudSettingsAttributes{Gcp: c.CloudSettings.GCP.Attributes()} - } - - return nil -} - type ClusterNodePool struct { Name types.String `tfsdk:"name"` MinSize types.Int64 `tfsdk:"min_size"` @@ -73,14 +57,14 @@ func (c *Cluster) TagsAttribute(ctx context.Context, d diag.Diagnostics) (result return } -func (c *Cluster) CreateAttributes(ctx context.Context, d diag.Diagnostics) console.ClusterAttributes { +func (c *Cluster) Attributes(ctx context.Context, d diag.Diagnostics) console.ClusterAttributes { return console.ClusterAttributes{ Name: c.Name.ValueString(), Handle: c.Handle.ValueStringPointer(), ProviderID: c.ProviderId.ValueStringPointer(), Version: c.Version.ValueStringPointer(), Protect: c.Protect.ValueBoolPointer(), - CloudSettings: c.CloudSettingsAttributes(), + CloudSettings: c.CloudSettings.Attributes(), NodePools: c.NodePoolsAttribute(), ReadBindings: c.Bindings.ReadAttributes(), WriteBindings: c.Bindings.WriteAttributes(), diff --git a/internal/model/cluster_cloud_settings.go b/internal/model/cluster_cloud_settings.go index 2f37fb4..b9a7609 100644 --- a/internal/model/cluster_cloud_settings.go +++ b/internal/model/cluster_cloud_settings.go @@ -12,6 +12,26 @@ type ClusterCloudSettings struct { BYOK *ClusterCloudSettingsBYOK `tfsdk:"byok"` } +func (c *ClusterCloudSettings) Attributes() *console.CloudSettingsAttributes { + if c == nil { + return nil + } + + if c.AWS != nil { + return &console.CloudSettingsAttributes{Aws: c.AWS.Attributes()} + } + + if c.Azure != nil { + return &console.CloudSettingsAttributes{Azure: c.Azure.Attributes()} + } + + if c.GCP != nil { + return &console.CloudSettingsAttributes{Gcp: c.GCP.Attributes()} + } + + return nil +} + type ClusterCloudSettingsAWS struct { Region types.String `tfsdk:"region"` } diff --git a/internal/model/provider.go b/internal/model/provider.go index d8525fa..b22adee 100644 --- a/internal/model/provider.go +++ b/internal/model/provider.go @@ -45,6 +45,10 @@ type ProviderCloudSettings struct { } func (p *ProviderCloudSettings) Attributes() *console.CloudProviderSettingsAttributes { + if p == nil { + return nil + } + if p.GCP != nil { return &console.CloudProviderSettingsAttributes{Aws: p.AWS.Attributes()} } diff --git a/internal/resource/cluster/cluster.go b/internal/resource/cluster/cluster.go index beb866e..d29b78e 100644 --- a/internal/resource/cluster/cluster.go +++ b/internal/resource/cluster/cluster.go @@ -71,14 +71,14 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re Computed: true, }, "version": schema.StringAttribute{ - Description: "", - MarkdownDescription: "", + Description: "Desired Kubernetes version for this cluster. Leave empty for bring your own cluster.", + MarkdownDescription: "Desired Kubernetes version for this cluster. Leave empty for bring your own cluster.", Optional: true, Computed: true, }, "provider_id": schema.StringAttribute{ - Description: "", - MarkdownDescription: "", + Description: "Provider used to create this cluster. Leave empty for bring your own cluster.", + MarkdownDescription: "Provider used to create this cluster. Leave empty for bring your own cluster.", Optional: true, PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, }, @@ -103,38 +103,40 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re PlanModifiers: []planmodifier.Object{objectplanmodifier.RequiresReplace()}, }, "node_pools": schema.ListNestedAttribute{ - Description: "", - MarkdownDescription: "", + Description: "List of node pool specs managed by this cluster. Leave empty for bring your own cluster.", + MarkdownDescription: "List of node pool specs managed by this cluster. Leave empty for bring your own cluster.", Optional: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "name": schema.StringAttribute{ - Description: "", - MarkdownDescription: "", + Description: "Node pool name. Must be unique.", + MarkdownDescription: "Node pool name. Must be unique.", Required: true, }, "min_size": schema.Int64Attribute{ - Description: "", - MarkdownDescription: "", + Description: "Minimum number of instances in this node pool.", + MarkdownDescription: "Minimum number of instances in this node pool.", Required: true, }, "max_size": schema.Int64Attribute{ - Description: "", - MarkdownDescription: "", + Description: "Maximum number of instances in this node pool.", + MarkdownDescription: "Maximum number of instances in this node pool.", Required: true, }, "instance_type": schema.StringAttribute{ - Description: "", - MarkdownDescription: "", + Description: "The type of node to use. Usually cloud-specific.", + MarkdownDescription: "The type of node to use. Usually cloud-specific.", Required: true, }, "labels": schema.MapAttribute{ - ElementType: types.StringType, - Optional: true, + Description: "Kubernetes labels to apply to the nodes in this pool. Useful for node selectors.", + MarkdownDescription: "Kubernetes labels to apply to the nodes in this pool. Useful for node selectors.", + ElementType: types.StringType, + Optional: true, }, "taints": schema.ListNestedAttribute{ - Description: "", - MarkdownDescription: "", + Description: "Any taints you'd want to apply to a node, i.e. for preventing scheduling on spot instances.", + MarkdownDescription: "Any taints you'd want to apply to a node, i.e. for preventing scheduling on spot instances.", Optional: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ @@ -159,13 +161,13 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re Optional: true, Attributes: map[string]schema.Attribute{ "aws": schema.SingleNestedAttribute{ - Description: "", - MarkdownDescription: "", + Description: "AWS node pool customizations.", + MarkdownDescription: "AWS node pool customizations.", Optional: true, Attributes: map[string]schema.Attribute{ "launch_template_id": schema.StringAttribute{ - Description: "", - MarkdownDescription: "", + Description: "Custom launch template for your nodes. Useful for Golden AMI setups.", + MarkdownDescription: "Custom launch template for your nodes. Useful for Golden AMI setups.", Optional: true, }, }, @@ -177,7 +179,7 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re }, }, "protect": schema.BoolAttribute{ - Description: "If set to `true` then this cluster cannot be deleted.", + Description: "If set to \"true\" then this cluster cannot be deleted.", MarkdownDescription: "If set to `true` then this cluster cannot be deleted.", Optional: true, Computed: true, @@ -275,7 +277,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest return } - result, err := r.client.CreateCluster(ctx, data.CreateAttributes(ctx, resp.Diagnostics)) + result, err := r.client.CreateCluster(ctx, data.Attributes(ctx, resp.Diagnostics)) if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create cluster, got error: %s", err)) return