Skip to content

Commit

Permalink
Added the custom CNI to the Kubernetes cluster (#116)
Browse files Browse the repository at this point in the history
* Added the option to customize the CNI in the Kubernetes cluster
* Fixed error in the code
* Remove the default from the CNI
  • Loading branch information
alejandrojnm committed Jan 14, 2022
1 parent fa2e2e6 commit 5cbd3b8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
12 changes: 12 additions & 0 deletions civo/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func resourceKubernetesCluster() *schema.Resource {
Computed: true,
Description: "The version of k3s to install (optional, the default is currently the latest available)",
},
"cni": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The cni for the k3s to install (the default is `flannel`) valid options are `calico` or `flannel`",
ValidateFunc: utils.ValidateCNIName,
},
"tags": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -298,6 +305,10 @@ func resourceKubernetesClusterCreate(d *schema.ResourceData, m interface{}) erro
config.Tags = ""
}

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

if attr, ok := d.GetOk("applications"); ok {
if utils.CheckAPPName(attr.(string), apiClient) {
config.Applications = attr.(string)
Expand Down Expand Up @@ -380,6 +391,7 @@ func resourceKubernetesClusterRead(d *schema.ResourceData, m interface{}) error
d.Set("num_target_nodes", resp.NumTargetNode)
d.Set("target_nodes_size", resp.TargetNodeSize)
d.Set("kubernetes_version", resp.KubernetesVersion)
d.Set("cni", resp.CNIPlugin)
d.Set("tags", strings.Join(resp.Tags, " ")) // space separated tags
d.Set("status", resp.Status)
d.Set("ready", resp.Ready)
Expand Down
67 changes: 67 additions & 0 deletions civo/resource_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,63 @@ func TestAccCivoKubernetesClusterSize_update(t *testing.T) {
})
}

func TestAccCivoKubernetesClusterCNI(t *testing.T) {
var kubernetes civogo.KubernetesCluster

// generate a random name for each test run
resName := "civo_kubernetes_cluster.foobar"
var kubernetesClusterName = acctest.RandomWithPrefix("tf-test") + ".example"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCivoInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCivoKubernetesClusterConfigCNI(kubernetesClusterName),
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the widget object
testAccCheckCivoKubernetesClusterResourceExists(resName, &kubernetes),
// verify remote values
testAccCheckCivoKubernetesClusterValues(&kubernetes, kubernetesClusterName),
// verify local values
resource.TestCheckResourceAttr(resName, "name", kubernetesClusterName),
resource.TestCheckResourceAttr(resName, "num_target_nodes", "2"),
resource.TestCheckResourceAttr(resName, "target_nodes_size", "g2.small"),
resource.TestCheckResourceAttr(resName, "cni", "calico"),
resource.TestCheckResourceAttrSet(resName, "kubeconfig"),
resource.TestCheckResourceAttrSet(resName, "api_endpoint"),
resource.TestCheckResourceAttrSet(resName, "master_ip"),
resource.TestCheckResourceAttrSet(resName, "dns_entry"),
resource.TestCheckResourceAttrSet(resName, "built_at"),
resource.TestCheckResourceAttrSet(resName, "created_at"),
),
},
{
// use a dynamic configuration with the random name from above
Config: testAccCheckCivoKubernetesClusterConfigCNI(kubernetesClusterName),
Check: resource.ComposeTestCheckFunc(
// query the API to retrieve the widget object
testAccCheckCivoKubernetesClusterResourceExists(resName, &kubernetes),
// verify remote values
testAccCheckCivoKubernetesClusterValues(&kubernetes, kubernetesClusterName),
// verify local values
resource.TestCheckResourceAttr(resName, "name", kubernetesClusterName),
resource.TestCheckResourceAttr(resName, "num_target_nodes", "4"),
resource.TestCheckResourceAttr(resName, "target_nodes_size", "g2.small"),
resource.TestCheckResourceAttr(resName, "cni", "calico"),
resource.TestCheckResourceAttrSet(resName, "kubeconfig"),
resource.TestCheckResourceAttrSet(resName, "api_endpoint"),
resource.TestCheckResourceAttrSet(resName, "master_ip"),
resource.TestCheckResourceAttrSet(resName, "dns_entry"),
resource.TestCheckResourceAttrSet(resName, "built_at"),
resource.TestCheckResourceAttrSet(resName, "created_at"),
),
},
},
})
}

func TestAccCivoKubernetesClusterTags_update(t *testing.T) {
var kubernetes civogo.KubernetesCluster

Expand Down Expand Up @@ -237,3 +294,13 @@ resource "civo_kubernetes_cluster" "foobar" {
tags = "foo"
}`, name)
}

func testAccCheckCivoKubernetesClusterConfigCNI(name string) string {
return fmt.Sprintf(`
resource "civo_kubernetes_cluster" "foobar" {
name = "%s"
num_target_nodes = 2
tags = "foo"
cni = "calico"
}`, name)
}
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/aws/aws-sdk-go v1.29.22 // indirect
github.com/civo/civogo v0.2.57
github.com/civo/civogo v0.2.59
github.com/fatih/color v1.9.0 // indirect
github.com/google/uuid v1.2.0
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/civo/civogo v0.2.52 h1:oeMmeGuJOZFJ+uruu13ywCWOxSa2+Lyk+ePc6rxC8gY=
github.com/civo/civogo v0.2.52/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.57 h1:wdZQeivTk/vFaeJBQdFLZobsK6qE/Pb3XR3fxk5DCZU=
github.com/civo/civogo v0.2.57/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/civo/civogo v0.2.59 h1:nJkwotbnGQxvamXMlI7ux3DyBS2h7g4HwzJu+AWGcTE=
github.com/civo/civogo v0.2.59/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
22 changes: 22 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ func ValidateName(v interface{}, k string) (ws []string, es []error) {
return warns, errs
}

func ValidateCNIName(v interface{}, k string) (ws []string, es []error) {
var errs []error
var warns []string
value, ok := v.(string)
if !ok {
errs = append(errs, fmt.Errorf("expected CNI to be string"))
return warns, errs
}
whiteSpace := regexp.MustCompile(`\s+`)
if whiteSpace.Match([]byte(value)) {
errs = append(errs, fmt.Errorf("CNI cannot contain whitespace. Got %s", value))
return warns, errs
}

if value != "flannel" && value != "cilium" {
errs = append(errs, fmt.Errorf("CNI plugin provided isn't valid/supported"))
return warns, errs
}

return warns, errs
}

func ValidateNameSize(v interface{}, k string) (ws []string, es []error) {
var errs []error
var warns []string
Expand Down

0 comments on commit 5cbd3b8

Please sign in to comment.