Skip to content

Commit 8c0832f

Browse files
m4rCsiMarc Siegenthaler
andauthored
Allow setting labels on nodes with rke2 (#951)
* Add machine_labels to cluster_v2_rke_config_machine_pool * Clarified documentation for machine_labels and labels for cluster_v2_rke_config_machine_pool Co-authored-by: Marc Siegenthaler <marc.siegenthaler@sgdigital.com>
1 parent 81f8ff9 commit 8c0832f

4 files changed

Lines changed: 38 additions & 2 deletions

docs/resources/cluster_v2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ The following attributes are exported:
475475
* `unhealthy_node_timeout_seconds` - (Optional) Seconds an unhealthy node has to become active before it is replaced (int)
476476
* `max_unhealthy` - (Optional) Max unhealthy nodes for automated replacement to be allowed (string)
477477
* `unhealthy_range` - (Optional) Range of unhealthy nodes for automated replacement to be allowed (string)
478+
* `machine_labels` - (Optional) Labels for Machine pool nodes (map)
479+
* `labels` - (Optional) Labels for Machine Deployment Resource (map)
478480

479481
##### `machine_config`
480482

rancher2/schema_cluster_v2_rke_config_machine_pool.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package rancher2
22

33
import (
4+
"strings"
5+
46
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
57
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
68
)
@@ -138,6 +140,19 @@ func clusterV2RKEConfigMachinePoolFields() map[string]*schema.Schema {
138140
Optional: true,
139141
Description: "range of unhealthy nodes for automated replacement to be allowed",
140142
},
143+
"machine_labels": {
144+
Type: schema.TypeMap,
145+
Optional: true,
146+
Computed: true,
147+
Description: "Labels of the machine",
148+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
149+
// Supressing diff for labels containing cattle.io/
150+
if (strings.Contains(k, commonAnnotationLabelCattle) || strings.Contains(k, commonAnnotationLabelRancher)) && new == "" {
151+
return true
152+
}
153+
return false
154+
},
155+
},
141156
}
142157

143158
for k, v := range commonAnnotationLabelFields() {

rancher2/structure_cluster_v2_rke_config_machine_pool.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package rancher2
22

33
import (
4+
"time"
5+
46
provisionv1 "github.com/rancher/rancher/pkg/apis/provisioning.cattle.io/v1"
57
corev1 "k8s.io/api/core/v1"
68
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
79
"k8s.io/apimachinery/pkg/util/intstr"
8-
"time"
910
)
1011

1112
// Flatteners
@@ -65,6 +66,9 @@ func flattenClusterV2RKEConfigMachinePools(p []provisionv1.RKEMachinePool) []int
6566
if len(in.MachineDeploymentLabels) > 0 {
6667
obj["labels"] = toMapInterface(in.MachineDeploymentLabels)
6768
}
69+
if len(in.Labels) > 0 {
70+
obj["machine_labels"] = toMapInterface(in.Labels)
71+
}
6872
obj["paused"] = in.Paused
6973
if in.Quantity != nil {
7074
obj["quantity"] = int(*in.Quantity)
@@ -175,6 +179,9 @@ func expandClusterV2RKEConfigMachinePools(p []interface{}) []provisionv1.RKEMach
175179
if v, ok := in["labels"].(map[string]interface{}); ok && len(v) > 0 {
176180
obj.MachineDeploymentLabels = toMapString(v)
177181
}
182+
if v, ok := in["machine_labels"].(map[string]interface{}); ok && len(v) > 0 {
183+
obj.Labels = toMapString(v)
184+
}
178185
if v, ok := in["paused"].(bool); ok {
179186
obj.Paused = v
180187
}

rancher2/structure_cluster_v2_rke_config_machine_pool_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package rancher2
22

33
import (
4-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
54
"reflect"
65
"testing"
76
"time"
87

8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
910
provisionv1 "github.com/rancher/rancher/pkg/apis/provisioning.cattle.io/v1"
11+
rkev1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1"
1012
corev1 "k8s.io/api/core/v1"
1113
"k8s.io/apimachinery/pkg/util/intstr"
1214
)
@@ -62,6 +64,12 @@ func init() {
6264
"label_one": "one",
6365
"label_two": "two",
6466
},
67+
RKECommonNodeConfig: rkev1.RKECommonNodeConfig{
68+
Labels: map[string]string{
69+
"machine_label_one": "one",
70+
"machine_label_two": "two",
71+
},
72+
},
6573
Quantity: &quantity,
6674
Paused: true,
6775
RollingUpdate: testClusterV2RKEConfigMachinePoolRollingUpdateConf,
@@ -97,6 +105,10 @@ func init() {
97105
"label_one": "one",
98106
"label_two": "two",
99107
},
108+
"machine_labels": map[string]interface{}{
109+
"machine_label_one": "one",
110+
"machine_label_two": "two",
111+
},
100112
"quantity": 10,
101113
"paused": true,
102114
"rolling_update": testClusterV2RKEConfigMachinePoolRollingUpdateInterface,

0 commit comments

Comments
 (0)