Skip to content

Commit

Permalink
cofigure custom server_group_id for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
furkhat committed Mar 17, 2022
1 parent 4be4e82 commit 87df0c2
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 5 deletions.
35 changes: 35 additions & 0 deletions docs/data-sources/sshkey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
page_title: "MetaKube: metakube_sshkey"
---

# metakube_sshkey

Get sshkey record's public key and fingerprint data. Useful if you want to reference existing key.

## Example Usage

Dump public key into a file `keydata`.

```hcl
data "metakube_sshkey" "example" {
project_id = "foo"
name = "work-laptop"
}
resource "local_file" "key_data" {
content = "data.metakube_sshkey.example.public_key"
filename = "${path.module}/keydata"
}
```
## Argument Reference

The following arguments are supported:

* `project_id` - (Optional) MetaKube Project ID.
* `name` - (Optional) Name of the sshkey record.

## Attributes Reference

The only attribute exported is:
* `public_key`: The ssh key public key.
* `fingerprint`: The ssh key fingerprint.
3 changes: 2 additions & 1 deletion docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ When set, start time and length must be configured.
* `security_group` - (Optional) When specified, all worker nodes will be attached to this security group. If not specified, a security group will be created.
* `network` - (Optional) When specified, all worker nodes will be attached to this network. If not specified, a network, subnet & router will be created.
* `subnet_id` - (Optional) When specified, all worker nodes will be attached to this subnet of specified network. If not specified, a network, subnet & router will be created.
* `subnet_cidr` - Change this to configure a different internal IP range for Nodes. Default: `192.168.1.0/24`.
* `subnet_cidr` - (Optional) Change this to configure a different internal IP range for Nodes. Default: `192.168.1.0/24`.
When using password based auth
* `server_group_id` - (Optional) Server group id to use for all machines within a cluster. You can use openstack server groups to group or seperate servers using soft/hard affinity/anti-affinity rules. When not set explicitly, the default soft anti-affinity server group will be created and used.
* `tenant` - (Optional) The project to use for billing. You can set it using environment variable `OS_PROJECT_NAME`. Must be omit if application credentials are used.
* `username` - (Optional) The account's username. You can set it using environment variable `OS_USERNAME`. Must be omit if application credentials are used.
* `password` - (Optional) The account's password. You can set it using environment variable `OS_PASSWORD`. Must be omit if application credentials are used.
Expand Down
12 changes: 9 additions & 3 deletions examples/openstack/advanced/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
terraform {
required_providers {
metakube = {
source = "syseleven/metakube"
}
metakube = {
source = "syseleven/metakube"
}
openstack = {
source = "terraform-provider-openstack/openstack"
}
Expand Down Expand Up @@ -91,6 +91,11 @@ resource "openstack_networking_subnet_v2" "subnet_1" {
"37.123.105.117"]
}

resource "openstack_compute_servergroup_v2" "anti-affinity" {
name = "test-anti-affinity"
policies = ["anti-affinity"]
}

# Set up a router to allow access through public internet.
data "openstack_networking_network_v2" "external" {
name = var.floating_ip_pool
Expand Down Expand Up @@ -160,6 +165,7 @@ resource "metakube_cluster" "cluster" {
network = openstack_networking_network_v2.network_1.name
subnet_id = openstack_networking_subnet_v2.subnet_1.id
subnet_cidr = openstack_networking_secgroup_rule_v2.allow_higher_ports.remote_ip_prefix
server_group_id = openstack_compute_servergroup_v2.anti-affinity.id
}
}

Expand Down
6 changes: 6 additions & 0 deletions metakube/datasource_metakube_sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func dataSourceMetakubeSSHKey() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"fingerprint": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
Expand All @@ -50,6 +55,7 @@ func metakubeDataSourceSSHKeyRead(ctx context.Context, d *schema.ResourceData, m
d.Set("public_key", r.Spec.PublicKey)
d.Set("name", name)
d.Set("project_id", prj)
d.Set("fingerprint", r.Spec.Fingerprint)
return nil
}
}
Expand Down
2 changes: 2 additions & 0 deletions metakube/resource_metakube_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ type clusterOpenstackPreservedValues struct {
openstackUsername interface{}
openstackPassword interface{}
openstackTenant interface{}
openstackServerGroupID interface{}
openstackApplicationCredentialsID interface{}
openstackApplicationCredentialsSecret interface{}
}
Expand All @@ -550,6 +551,7 @@ func readClusterPreserveValues(d *schema.ResourceData) clusterPreserveValues {
openstackUsername: d.Get(key("openstack.0.username")),
openstackPassword: d.Get(key("openstack.0.password")),
openstackTenant: d.Get(key("openstack.0.tenant")),
openstackServerGroupID: d.Get(key("openstack.0.server_group_id")),
openstackApplicationCredentialsID: d.Get(key("openstack.0.application_credentials_id")),
openstackApplicationCredentialsSecret: d.Get(key("openstack.0.application_credentials_secret")),
}
Expand Down
6 changes: 6 additions & 0 deletions metakube/resource_metakube_cluster_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,11 @@ func metakubeResourceClusterOpenstackCloudSpecFields() map[string]*schema.Schema
ForceNew: true,
Description: "Change this to configure a different internal IP range for Nodes. Default: 192.168.1.0/24",
},
"server_group_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Server group to use for all machines within a cluster",
},
}
}
13 changes: 13 additions & 0 deletions metakube/resource_metakube_cluster_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,14 @@ func flattenOpenstackSpec(values *clusterOpenstackPreservedValues, in *models.Op
att["subnet_cidr"] = in.SubnetCIDR
}

if in.ServerGroupID != "" {
att["server_group_id"] = in.ServerGroupID
}

if values != nil {
if _, ok := att["server_group_id"]; !ok && values.openstackServerGroupID != nil {
att["server_group_id"] = values.openstackServerGroupID
}
if values.openstackTenant != nil {
att["tenant"] = values.openstackTenant
}
Expand Down Expand Up @@ -595,6 +602,12 @@ func expandOpenstackCloudSpec(p []interface{}) *models.OpenstackCloudSpec {
}
}

if v, ok := in["server_group_id"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.ServerGroupID = vv
}
}

if v, ok := in["application_credentials_id"]; ok {
if vv, ok := v.(string); ok && vv != "" {
obj.ApplicationCredentialID = vv
Expand Down
38 changes: 38 additions & 0 deletions metakube/resource_metakube_cluster_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func TestFlattenOpenstackCloudSpec(t *testing.T) {
SecurityGroups: "SecurityGroups",
SubnetID: "SubnetID",
TenantID: "TenantID",
ServerGroupID: "ServerGroupID",
},
clusterOpenstackPreservedValues{
openstackApplicationCredentialsID: "id",
Expand All @@ -209,6 +210,7 @@ func TestFlattenOpenstackCloudSpec(t *testing.T) {
"network": "Network",
"security_group": "SecurityGroups",
"subnet_id": "SubnetID",
"server_group_id": "ServerGroupID",
},
},
},
Expand All @@ -224,6 +226,7 @@ func TestFlattenOpenstackCloudSpec(t *testing.T) {
Tenant: "",
TenantID: "TenantID",
Username: "",
ServerGroupID: "ServerGroupID",
},
clusterOpenstackPreservedValues{
openstackUsername: "Username",
Expand All @@ -239,6 +242,39 @@ func TestFlattenOpenstackCloudSpec(t *testing.T) {
"network": "Network",
"security_group": "SecurityGroups",
"subnet_id": "SubnetID",
"server_group_id": "ServerGroupID",
},
},
},

{
&models.OpenstackCloudSpec{
FloatingIPPool: "FloatingIPPool",
Network: "Network",
Password: "",
RouterID: "RouterID",
SecurityGroups: "SecurityGroups",
SubnetID: "SubnetID",
Tenant: "",
TenantID: "TenantID",
Username: "",
},
clusterOpenstackPreservedValues{
openstackUsername: "Username",
openstackPassword: "Password",
openstackTenant: "Tenant",
openstackServerGroupID: "ServerGroupID",
},
[]interface{}{
map[string]interface{}{
"username": "Username",
"password": "Password",
"tenant": "Tenant",
"floating_ip_pool": "FloatingIPPool",
"network": "Network",
"security_group": "SecurityGroups",
"subnet_id": "SubnetID",
"server_group_id": "ServerGroupID",
},
},
},
Expand Down Expand Up @@ -556,13 +592,15 @@ func TestExpandOpenstackCloudSpec(t *testing.T) {
"floating_ip_pool": "FloatingIPPool",
"application_credentials_id": "id",
"application_credentials_secret": "secret",
"server_group_id": "ServerGroupID",
},
},
&models.OpenstackCloudSpec{
Domain: "Default",
FloatingIPPool: "FloatingIPPool",
ApplicationCredentialID: "id",
ApplicationCredentialSecret: "secret",
ServerGroupID: "ServerGroupID",
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion metakube/resource_metakube_node_deployment_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func metakubeResourceSystemLabelOrTag(key string) bool {
for _, s := range []string{"labels.%", "metakube", "system/", "kubernetes.io"} {
for _, s := range []string{"labels.%", "metakube", "system-", "system/", "kubernetes.io"} {
if strings.Contains(key, s) {
return true
}
Expand Down

0 comments on commit 87df0c2

Please sign in to comment.