Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the custom CNI to the Kubernetes cluster #116

Merged
merged 3 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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