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

Fix machine selector config to allow kubelet arg list #1181

Merged
merged 2 commits into from
Aug 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 42 additions & 3 deletions rancher2/resource_rancher2_cluster_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ func resourceRancher2ClusterV2() *schema.Resource {
Importer: &schema.ResourceImporter{
State: resourceRancher2ClusterV2Import,
},
Schema: clusterV2Fields(),
Schema: clusterV2Fields(),
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceRancher2ClusterV2Resource().CoreConfigSchema().ImpliedType(),
Upgrade: resourceRancher2ClusterV2StateUpgradeV0,
Version: 0,
},
},
CustomizeDiff: func(d *schema.ResourceDiff, i interface{}) error {
if d.HasChange("rke_config") {
oldObj, newObj := d.GetChange("rke_config")
//return fmt.Errorf("\n%#v\n%#v\n", oldObj, newObj)
oldInterface, oldOk := oldObj.([]interface{})
newInterface, newOk := newObj.([]interface{})
if oldOk && newOk && len(newInterface) > 0 {
Expand All @@ -41,7 +48,6 @@ func resourceRancher2ClusterV2() *schema.Resource {
}
if d.HasChange("local_auth_endpoint") {
oldObj, newObj := d.GetChange("local_auth_endpoint")
//return fmt.Errorf("\n%#v\n%#v\n", oldObj, newObj)
oldInterface, oldOk := oldObj.([]interface{})
newInterface, newOk := newObj.([]interface{})
if oldOk && newOk && len(newInterface) > 0 {
Expand All @@ -64,6 +70,39 @@ func resourceRancher2ClusterV2() *schema.Resource {
}
}

func resourceRancher2ClusterV2Resource() *schema.Resource {
return &schema.Resource{
Schema: clusterV2FieldsV0(),
}
}

func resourceRancher2ClusterV2StateUpgradeV0(rawState map[string]any, meta interface{}) (map[string]any, error) {
if rkeConfigs, ok := rawState["rke_config"].([]any); ok && len(rkeConfigs) > 0 {
for i := range rkeConfigs {
if rkeConfig, ok := rkeConfigs[i].(map[string]any); ok && len(rkeConfig) > 0 {
if machineSelectorConfigs, ok := rkeConfig["machine_selector_config"].([]any); ok && len(machineSelectorConfigs) > 0 {

// upgrade all machine selector configs
for m := range machineSelectorConfigs {
if machineSelectorConfig, ok := machineSelectorConfigs[m].(map[string]any); ok && len(machineSelectorConfig) > 0 {

// machine selector config data found. Migrate state from map -> string
if config, ok := machineSelectorConfig["config"].(map[string]any); ok {
newValue := ""
if conf, err := mapInterfaceToYAML(config); err == nil {
newValue = conf
}
rawState["rke_config"].([]interface{})[i].(map[string]any)["machine_selector_config"].([]any)[m].(map[string]any)["config"] = newValue
}
}
}
}
}
}
}
return rawState, nil
}

func resourceRancher2ClusterV2Create(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
cluster, err := expandClusterV2(d)
Expand Down
120 changes: 120 additions & 0 deletions rancher2/schema_cluster_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,126 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func clusterV2FieldsV0() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Cluster V2 name",
},
"fleet_namespace": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "fleet-default",
},
"kubernetes_version": {
Type: schema.TypeString,
Required: true,
Description: "Cluster V2 kubernetes version",
},
"local_auth_endpoint": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Cluster V2 local auth endpoint",
Elem: &schema.Resource{
Schema: clusterV2LocalAuthEndpointFields(),
},
},
"rke_config": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Description: "Cluster V2 rke config",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigFieldsV0(),
},
},
"agent_env_vars": {
Type: schema.TypeList,
Optional: true,
Description: "Cluster V2 default agent env vars",
Elem: &schema.Resource{
Schema: envVarFields(),
},
},
"cloud_credential_secret_name": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 cloud credential secret name",
},
"cluster_agent_deployment_customization": {
Type: schema.TypeList,
Optional: true,
Description: "Optional customization for cluster agent",
Elem: &schema.Resource{
Schema: agentDeploymentCustomizationFields(),
},
},
"fleet_agent_deployment_customization": {
Type: schema.TypeList,
Optional: true,
Description: "Optional customization for fleet agent",
Elem: &schema.Resource{
Schema: agentDeploymentCustomizationFields(),
},
},
"default_pod_security_policy_template_name": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 default pod security policy template name",
},
"default_pod_security_admission_configuration_template_name": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 default pod security admission configuration template name",
},
"default_cluster_role_for_project_members": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 default cluster role for project members",
},
"enable_network_policy": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Enable k8s network policy",
},
// Computed attributes
"cluster_registration_token": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Sensitive: true,
Elem: &schema.Resource{
Schema: clusterRegistrationTokenFields(),
},
},
"kube_config": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"cluster_v1_id": {
Type: schema.TypeString,
Computed: true,
},
"resource_version": {
Type: schema.TypeString,
Computed: true,
},
}

for k, v := range commonAnnotationLabelFields() {
s[k] = v
}

return s
}

func clusterV2Fields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"name": {
Expand Down
154 changes: 154 additions & 0 deletions rancher2/schema_cluster_v2_rke_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,160 @@ import (

//Types

func clusterV2RKEConfigFieldsV0() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"additional_manifest": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 additional manifest",
},
"local_auth_endpoint": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Deprecated: "Use rancher2_cluster_v2.local_auth_endpoint instead",
Description: "Cluster V2 local auth endpoint",
Elem: &schema.Resource{
Schema: clusterV2LocalAuthEndpointFields(),
},
},
"upgrade_strategy": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Cluster V2 upgrade strategy",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigUpgradeStrategyFields(),
},
},
"chart_values": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 chart values. It should be in YAML format",
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v, ok := val.(string)
if !ok || len(v) == 0 {
return
}
_, err := ghodssyamlToMapInterface(v)
if err != nil {
errs = append(errs, fmt.Errorf("%q must be in yaml format, error: %v", key, err))
return
}
return
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "" || new == "" {
return false
}
oldMap, _ := ghodssyamlToMapInterface(old)
newMap, _ := ghodssyamlToMapInterface(new)
return reflect.DeepEqual(oldMap, newMap)
},
},
"machine_global_config": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster V2 machine global config",
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v, ok := val.(string)
if !ok || len(v) == 0 {
return
}
_, err := ghodssyamlToMapInterface(v)
if err != nil {
errs = append(errs, fmt.Errorf("%q must be in yaml format, error: %v", key, err))
return
}
return
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "" || new == "" {
return false
}
oldMap, _ := ghodssyamlToMapInterface(old)
newMap, _ := ghodssyamlToMapInterface(new)
return reflect.DeepEqual(oldMap, newMap)
},
},
"machine_pools": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Cluster V2 machine pools",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigMachinePoolFields(),
},
},
"machine_pool_defaults": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Default values for machine pool configurations if unset",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigMachinePoolDefaultFields(),
},
},
"machine_selector_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Cluster V2 machine selector config",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigSystemConfigFieldsV0(),
},
},
"registries": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Cluster V2 registries",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigRegistryFields(),
},
},
"etcd": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Description: "Cluster V2 etcd",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigETCDFields(),
},
},
"rotate_certificates": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Cluster V2 certificate rotation",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigRotateCertificatesFields(),
},
},
"etcd_snapshot_create": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Cluster V2 etcd snapshot create",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigETCDSnapshotCreateFields(),
},
},
"etcd_snapshot_restore": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Cluster V2 etcd snapshot restore",
Elem: &schema.Resource{
Schema: clusterV2RKEConfigETCDSnapshotRestoreFields(),
},
},
}

return s
}

func clusterV2RKEConfigFields() map[string]*schema.Schema {
s := map[string]*schema.Schema{
"additional_manifest": {
Expand Down