Skip to content

Commit

Permalink
Merge pull request #23 from pluralsh/update/cluster
Browse files Browse the repository at this point in the history
feat: Update cluster schema
  • Loading branch information
michaeljguarino authored Jan 8, 2024
2 parents b11f150 + 1d25454 commit d9024e2
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 153 deletions.
24 changes: 10 additions & 14 deletions example/byok/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
plural = {
source = "pluralsh/plural"
source = "pluralsh/plural"
version = "0.0.1"
}
}
Expand All @@ -12,22 +12,18 @@ provider "plural" {
}

resource "plural_cluster" "byok_workload_cluster" {
name = "workload-cluster-tf"
handle = "wctf"
cloud = "byok"
protect = "false"
cloud_settings = {
byok = {
kubeconfig = {
# Required, can be sourced from environment variables
}
}
name = "workload-cluster-tf"
handle = "wctf"
protect = "false"
kubeconfig = {
# Required, can be sourced from environment variables
# export PLURAL_KUBE_CONFIG_PATH to read from local file
}
tags = {
"managed-by" = "terraform-provider-plural"
}
}

data "plural_cluster" "byok_workload_cluster" {
handle = "wctf"
}
#data "plural_cluster" "byok_workload_cluster" {
# handle = "wctf"
#}
4 changes: 2 additions & 2 deletions internal/resource/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest
return
}

if common.IsCloud(data.Cloud.ValueString(), common.CloudBYOK) {
if common.IsCloud(data.Cloud.ValueString(), common.CloudBYOK) && data.HasKubeconfig() {
if result.CreateCluster.DeployToken == nil {
resp.Diagnostics.AddError("Client Error", "Unable to fetch cluster deploy token")
return
}

handler, err := NewOperatorHandler(ctx, &data.CloudSettings.BYOK.Kubeconfig, r.consoleUrl)
handler, err := NewOperatorHandler(ctx, data.GetKubeconfig(), r.consoleUrl)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to init operator handler, got error: %s", err))
return
Expand Down
19 changes: 18 additions & 1 deletion internal/resource/cluster_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type cluster struct {
Bindings *common.ClusterBindings `tfsdk:"bindings"`
NodePools types.Map `tfsdk:"node_pools"`
CloudSettings *ClusterCloudSettings `tfsdk:"cloud_settings"`
Kubeconfig *Kubeconfig `tfsdk:"kubeconfig"`
}

func (c *cluster) NodePoolsAttribute(ctx context.Context, d diag.Diagnostics) []*console.NodePoolAttributes {
Expand Down Expand Up @@ -110,6 +111,22 @@ func (c *cluster) FromCreate(cc *console.CreateCluster, ctx context.Context, d d
c.NodePools = common.ClusterNodePoolsFrom(cc.CreateCluster.NodePools, c.NodePools, ctx, d)
}

func (c *cluster) HasKubeconfig() bool {
return c.Kubeconfig != nil || (c.CloudSettings != nil && c.CloudSettings.BYOK != nil && c.CloudSettings.BYOK.Kubeconfig != nil)
}

func (c *cluster) GetKubeconfig() *Kubeconfig {
if !c.HasKubeconfig() {
return nil
}

if c.Kubeconfig != nil {
return c.Kubeconfig
}

return c.CloudSettings.BYOK.Kubeconfig
}

type ClusterCloudSettings struct {
AWS *ClusterCloudSettingsAWS `tfsdk:"aws"`
Azure *ClusterCloudSettingsAzure `tfsdk:"azure"`
Expand Down Expand Up @@ -178,7 +195,7 @@ func (c *ClusterCloudSettingsGCP) Attributes() *console.GcpCloudAttributes {
}

type ClusterCloudSettingsBYOK struct {
Kubeconfig Kubeconfig `tfsdk:"kubeconfig"`
Kubeconfig *Kubeconfig `tfsdk:"kubeconfig"`
}

type Kubeconfig struct {
Expand Down
Loading

0 comments on commit d9024e2

Please sign in to comment.