Skip to content

Commit 2fa6bff

Browse files
committed
update doc
1 parent ed7ea5a commit 2fa6bff

File tree

4 files changed

+449
-77
lines changed

4 files changed

+449
-77
lines changed

cloudstack/resource_cloudstack_cni_configuration_test.go

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func TestAccCloudStackCniConfiguration_basic(t *testing.T) {
32-
var cniConfig cloudstack.CniConfiguration
32+
var cniConfig cloudstack.UserData
3333

3434
resource.Test(t, resource.TestCase{
3535
PreCheck: func() { testAccPreCheck(t) },
@@ -48,7 +48,7 @@ func TestAccCloudStackCniConfiguration_basic(t *testing.T) {
4848
})
4949
}
5050

51-
func testAccCheckCloudStackCniConfigurationExists(n string, cniConfig *cloudstack.CniConfiguration) resource.TestCheckFunc {
51+
func testAccCheckCloudStackCniConfigurationExists(n string, cniConfig *cloudstack.UserData) resource.TestCheckFunc {
5252
return func(s *terraform.State) error {
5353
rs, ok := s.RootModule().Resources[n]
5454
if !ok {
@@ -60,11 +60,19 @@ func testAccCheckCloudStackCniConfigurationExists(n string, cniConfig *cloudstac
6060
}
6161

6262
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
63-
config, _, err := cs.Configuration.GetCniConfigurationByID(rs.Primary.ID)
63+
p := cs.Configuration.NewListCniConfigurationParams()
64+
p.SetId(rs.Primary.ID)
65+
66+
resp, err := cs.Configuration.ListCniConfiguration(p)
6467
if err != nil {
6568
return err
6669
}
6770

71+
if resp.Count != 1 {
72+
return fmt.Errorf("CNI configuration not found")
73+
}
74+
75+
config := resp.CniConfiguration[0]
6876
if config.Id != rs.Primary.ID {
6977
return fmt.Errorf("CNI configuration not found")
7078
}
@@ -86,8 +94,11 @@ func testAccCheckCloudStackCniConfigurationDestroy(s *terraform.State) error {
8694
return fmt.Errorf("No CNI configuration ID is set")
8795
}
8896

89-
_, _, err := cs.Configuration.GetCniConfigurationByID(rs.Primary.ID)
90-
if err == nil {
97+
p := cs.Configuration.NewListCniConfigurationParams()
98+
p.SetId(rs.Primary.ID)
99+
100+
resp, err := cs.Configuration.ListCniConfiguration(p)
101+
if err == nil && resp.Count > 0 {
91102
return fmt.Errorf("CNI configuration %s still exists", rs.Primary.ID)
92103
}
93104
}
@@ -98,24 +109,34 @@ func testAccCheckCloudStackCniConfigurationDestroy(s *terraform.State) error {
98109
const testAccCloudStackCniConfiguration_basic = `
99110
resource "cloudstack_cni_configuration" "foo" {
100111
name = "test-cni-config"
101-
cni_config = <<EOF
102-
{
103-
"cniVersion": "0.4.0",
104-
"name": "test-network",
105-
"type": "bridge",
106-
"bridge": "cni0",
107-
"isGateway": true,
108-
"ipMasq": true,
109-
"ipam": {
110-
"type": "host-local",
111-
"subnet": "10.244.0.0/16",
112-
"routes": [
113-
{ "dst": "0.0.0.0/0" }
112+
cni_config = base64encode(jsonencode({
113+
"name": "test-network",
114+
"cniVersion": "0.4.0",
115+
"plugins": [
116+
{
117+
"type": "calico",
118+
"log_level": "info",
119+
"datastore_type": "kubernetes",
120+
"nodename": "KUBERNETES_NODE_NAME",
121+
"mtu": "CNI_MTU",
122+
"ipam": {
123+
"type": "calico-ipam"
124+
},
125+
"policy": {
126+
"type": "k8s"
127+
},
128+
"kubernetes": {
129+
"kubeconfig": "KUBECONFIG_FILEPATH"
130+
}
131+
},
132+
{
133+
"type": "portmap",
134+
"snat": true,
135+
"capabilities": {"portMappings": true}
136+
}
114137
]
115-
}
116-
}
117-
EOF
138+
}))
118139
119-
params = ["subnet", "gateway"]
140+
params = ["KUBERNETES_NODE_NAME", "CNI_MTU"]
120141
}
121142
`
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
layout: default
3+
page_title: "CloudStack: cloudstack_cni_configuration"
4+
sidebar_current: "docs-cloudstack-resource-cni_configuration"
5+
description: |-
6+
Creates and manages a CloudStack CNI (Container Network Interface) configuration
7+
---
8+
9+
# CloudStack: cloudstack_cni_configuration
10+
11+
A `cloudstack_cni_configuration` resource manages a Container Network Interface (CNI) configuration for CloudStack Kubernetes Service (CKS) clusters. CNI configurations define how network connectivity is provided to Kubernetes pods.
12+
13+
## Example Usage
14+
15+
### Basic Calico CNI Configuration
16+
17+
```hcl
18+
resource "cloudstack_cni_configuration" "calico" {
19+
name = "calico-cni-config"
20+
cni_config = base64encode(jsonencode({
21+
"name" = "k8s-pod-network",
22+
"cniVersion" = "0.3.1",
23+
"plugins" = [
24+
{
25+
"type" = "calico",
26+
"log_level" = "info",
27+
"datastore_type" = "kubernetes",
28+
"nodename" = "KUBERNETES_NODE_NAME",
29+
"mtu" = "CNI_MTU",
30+
"ipam" = {
31+
"type" = "calico-ipam"
32+
},
33+
"policy" = {
34+
"type" = "k8s"
35+
},
36+
"kubernetes" = {
37+
"kubeconfig" = "KUBECONFIG_FILEPATH"
38+
}
39+
},
40+
{
41+
"type" = "portmap",
42+
"snat" = true,
43+
"capabilities" = { "portMappings" = true }
44+
}
45+
]
46+
}))
47+
48+
params = [
49+
"KUBERNETES_NODE_NAME",
50+
"CNI_MTU",
51+
"KUBECONFIG_FILEPATH"
52+
]
53+
}
54+
```
55+
56+
### Flannel CNI Configuration
57+
58+
```hcl
59+
resource "cloudstack_cni_configuration" "flannel" {
60+
name = "flannel-cni-config"
61+
cni_config = base64encode(jsonencode({
62+
"name" = "cbr0",
63+
"cniVersion" = "0.3.1",
64+
"plugins" = [
65+
{
66+
"type" = "flannel",
67+
"delegate" = {
68+
"hairpinMode" = true,
69+
"isDefaultGateway" = true
70+
}
71+
},
72+
{
73+
"type" = "portmap",
74+
"capabilities" = {
75+
"portMappings" = true
76+
}
77+
}
78+
]
79+
}))
80+
81+
params = ["FLANNEL_NETWORK", "FLANNEL_SUBNET"]
82+
83+
domain_id = "domain-uuid"
84+
account = "admin"
85+
}
86+
```
87+
88+
## Argument Reference
89+
90+
The following arguments are supported:
91+
92+
### Required Arguments
93+
94+
* `name` - (Required) The name of the CNI configuration. Must be unique within the account/domain.
95+
* `cni_config` - (Required) The CNI configuration in base64-encoded JSON format. This should contain the complete CNI plugin configuration according to the CNI specification.
96+
97+
### Optional Arguments
98+
99+
* `params` - (Optional) A list of parameter names that can be substituted in the CNI configuration. These parameters can be provided with actual values when creating a Kubernetes cluster using `cni_config_details`.
100+
* `domain_id` - (Optional) The domain ID for the CNI configuration. If not specified, uses the default domain.
101+
* `account` - (Optional) The account name for the CNI configuration. If not specified, uses the account of the authenticated user.
102+
* `project_id` - (Optional) The project ID to assign the CNI configuration to.
103+
104+
## Attributes Reference
105+
106+
In addition to all arguments above, the following attributes are exported:
107+
108+
* `id` - The ID of the CNI configuration.
109+
* `created` - The timestamp when the CNI configuration was created.
110+
* `domain` - The domain name where the CNI configuration belongs.
111+
* `project` - The project name if the CNI configuration is assigned to a project.
112+
113+
## CNI Configuration Format
114+
115+
The `cni_config` should be a base64-encoded JSON string that follows the CNI specification. The configuration supports parameter substitution using placeholder names that can be defined in the `params` list.
116+
117+
### Parameter Substitution
118+
119+
Parameters in the CNI configuration can be specified as placeholders and will be replaced with actual values when the configuration is used in a Kubernetes cluster:
120+
121+
```json
122+
{
123+
"name": "k8s-pod-network",
124+
"cniVersion": "0.3.1",
125+
"plugins": [
126+
{
127+
"type": "calico",
128+
"nodename": "KUBERNETES_NODE_NAME",
129+
"mtu": "CNI_MTU"
130+
}
131+
]
132+
}
133+
```
134+
135+
The `KUBERNETES_NODE_NAME` and `CNI_MTU` placeholders will be replaced when creating a cluster using this configuration.
136+
137+
### Supported CNI Plugins
138+
139+
CloudStack supports various CNI plugins including:
140+
141+
* **Calico** - Provides networking and network policy for Kubernetes
142+
* **Flannel** - Simple overlay network for Kubernetes
143+
* **Weave** - Container networking solution
144+
* **Custom plugins** - Any CNI-compliant plugin can be configured
145+
146+
## Usage with Kubernetes Clusters
147+
148+
CNI configurations are used with Kubernetes clusters by referencing the configuration ID:
149+
150+
```hcl
151+
resource "cloudstack_kubernetes_cluster" "example" {
152+
name = "example-cluster"
153+
zone = "zone1"
154+
kubernetes_version = "1.25.0"
155+
service_offering = "Medium Instance"
156+
157+
cni_configuration_id = cloudstack_cni_configuration.calico.id
158+
cni_config_details = {
159+
"CNI_MTU" = "1450"
160+
"KUBERNETES_NODE_NAME" = "spec.nodeName"
161+
"KUBECONFIG_FILEPATH" = "/etc/cni/net.d/calico-kubeconfig"
162+
}
163+
}
164+
```
165+
166+
## Import
167+
168+
CNI configurations can be imported using the configuration ID:
169+
170+
```shell
171+
$ terraform import cloudstack_cni_configuration.example <CNI_CONFIGURATION_ID>
172+
```

0 commit comments

Comments
 (0)