Skip to content

Commit

Permalink
Update the kubernetes resource to include the option to add expecific…
Browse files Browse the repository at this point in the history
… cluster type (#164)

Signed-off-by: Alejandro J. Nuñez Madrazo <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Feb 8, 2023
1 parent 0674cb8 commit aff3fc3
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 11 deletions.
4 changes: 2 additions & 2 deletions civo/datasource_kubernetes_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func flattenKubernetesVersion(version, _ interface{}, _ map[string]interface{})
flattenedVersion := map[string]interface{}{}
flattenedVersion["version"] = s.Version
flattenedVersion["label"] = fmt.Sprintf("v%s", s.Version)
flattenedVersion["type"] = s.Type
flattenedVersion["type"] = s.ClusterType
flattenedVersion["default"] = s.Default
return flattenedVersion, nil
}
Expand All @@ -67,7 +67,7 @@ func kubernetesVersionSchema() map[string]*schema.Schema {
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the version - can be `stable`, `legacy` & etc",
Description: "The type of the cluster, can be `talos` or `k3s`",
},
"default": {
Type: schema.TypeBool,
Expand Down
4 changes: 2 additions & 2 deletions civo/datasource_kubernetes_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func testAccCheckDataSourceCivoKubernetesVersionFiltered(n string) resource.Test

for i := 0; i < total; i++ {
name := rs.Primary.Attributes[fmt.Sprintf("versions.%d.type", i)]
if !stringInSlice(name, []string{"stable"}) {
if !stringInSlice(name, []string{"talos"}) {
return fmt.Errorf("Type is not in expected test filter values")
}

Expand All @@ -117,7 +117,7 @@ func testAccDataSourceCivoKubernetesVersionConfigWhitFilter() string {
data "civo_kubernetes_version" "foobar" {
filter {
key = "type"
values = ["stable"]
values = ["talos"]
}
}
`
Expand Down
11 changes: 11 additions & 0 deletions civo/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func resourceKubernetesCluster() *schema.Resource {
Required: true,
Description: "The existing firewall ID to use for this cluster",
},
"cluster_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The type of cluster to create, valid options are `k3s` or `talos` the default is `k3s`",
},
// Computed resource
"installed_applications": applicationSchema(),
"pools": nodePoolSchema(),
Expand Down Expand Up @@ -270,6 +276,10 @@ func resourceKubernetesClusterCreate(ctx context.Context, d *schema.ResourceData
config.Applications = ""
}

if attr, ok := d.GetOk("cluster_type"); ok {
config.ClusterType = attr.(string)
}

if attr, ok := d.GetOk("firewall_id"); ok {
firewallID := attr.(string)
firewall, err := apiClient.FindFirewall(firewallID)
Expand Down Expand Up @@ -344,6 +354,7 @@ func resourceKubernetesClusterRead(_ context.Context, d *schema.ResourceData, m
d.Set("num_target_nodes", resp.NumTargetNode)
d.Set("target_nodes_size", resp.TargetNodeSize)
d.Set("kubernetes_version", resp.KubernetesVersion)
d.Set("cluster_type", resp.ClusterType)
d.Set("cni", resp.CNIPlugin)
d.Set("tags", strings.Join(resp.Tags, " ")) // space separated tags
d.Set("status", resp.Status)
Expand Down
2 changes: 2 additions & 0 deletions civo/resource_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestAccCivoKubernetesCluster_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resName, "master_ip"),
resource.TestCheckResourceAttrSet(resName, "dns_entry"),
resource.TestCheckResourceAttrSet(resName, "created_at"),
resource.TestCheckResourceAttrSet(resName, "cluster_type"),
),
},
},
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestAccCivoKubernetesClusterCNI(t *testing.T) {
resource.TestCheckResourceAttrSet(resName, "master_ip"),
resource.TestCheckResourceAttrSet(resName, "dns_entry"),
resource.TestCheckResourceAttrSet(resName, "created_at"),
resource.TestCheckResourceAttrSet(resName, "cluster_type"),
),
},
},
Expand Down
17 changes: 12 additions & 5 deletions examples/data-sources/civo_kubernetes_version/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
data "civo_kubernetes_version" "stable" {
filter {
key = "type"
values = ["stable"]
}
data "civo_kubernetes_version" "talos" {
filter {
key = "type"
values = ["talos"]
}
}

data "civo_kubernetes_version" "k3s" {
filter {
key = "type"
values = ["k3s"]
}
}
29 changes: 28 additions & 1 deletion examples/resources/civo_kubernetes_cluster/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "civo_firewall_rule" "kubernetes" {
action = "allow"
}

# Create a cluster
# Create a cluster without expecific cluster type by default is k3s
resource "civo_kubernetes_cluster" "my-cluster" {
name = "my-cluster"
applications = "Portainer,Linkerd:Linkerd & Jaeger"
Expand All @@ -39,3 +39,30 @@ resource "civo_kubernetes_cluster" "my-cluster" {
node_count = 3
}
}

# Create a cluster with k3s
resource "civo_kubernetes_cluster" "my-cluster" {
name = "my-cluster"
applications = "Portainer,Linkerd:Linkerd & Jaeger"
firewall_id = civo_firewall.my-firewall.id
cluster_type = "k3s"
pools {
label = "front-end" // Optional
size = element(data.civo_size.xsmall.sizes, 0).name
node_count = 3
}
}


# Create a cluster with talos
resource "civo_kubernetes_cluster" "my-cluster" {
name = "my-cluster"
applications = "Portainer,Linkerd:Linkerd & Jaeger"
firewall_id = civo_firewall.my-firewall.id
cluster_type = "talos"
pools {
label = "front-end" // Optional
size = element(data.civo_size.xsmall.sizes, 0).name
node_count = 3
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/civo/terraform-provider-civo

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/civo/civogo v0.3.23
github.com/civo/civogo v0.3.24
github.com/google/uuid v1.3.0
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/civo/civogo v0.3.23 h1:FTSad682Qu7CsjkNAYWpSe2CSR1NJOE2NRxc6gUzHDk=
github.com/civo/civogo v0.3.23/go.mod h1:SbS06e0JPgIF27r1sLC97gjU1xWmONQeHgzF1hfLpak=
github.com/civo/civogo v0.3.24 h1:7kXEhtVUFkWIunNePqPCcIUilOktnXLWrKfi5sDqBNQ=
github.com/civo/civogo v0.3.24/go.mod h1:SbS06e0JPgIF27r1sLC97gjU1xWmONQeHgzF1hfLpak=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down

0 comments on commit aff3fc3

Please sign in to comment.