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(EKS): nil checks for rawState #573

Open
wants to merge 3 commits into
base: v2.6.x
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion rafay/data_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/RafaySystems/rctl/pkg/config"
glogger "github.com/RafaySystems/rctl/pkg/log"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/go-yaml/yaml"
Expand Down Expand Up @@ -146,7 +147,11 @@ func dataEKSClusterRead(ctx context.Context, d *schema.ResourceData, m interface
if !ok {
v = []interface{}{}
}
c1, err := flattenEKSCluster(&clusterSpec, v, rawState.GetAttr("cluster"))
var clusterState cty.Value
if !rawState.IsNull() {
clusterState = rawState.GetAttr("cluster")
}
c1, err := flattenEKSCluster(&clusterSpec, v, clusterState)
log.Println("finished flatten eks cluster", c1)
if err != nil {
log.Printf("flatten eks cluster error %s", err.Error())
Expand Down
92 changes: 73 additions & 19 deletions rafay/resource_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,9 @@ func expandEKSClusterConfig(p []interface{}, rawConfig cty.Value) *EKSClusterCon
return obj
}
in := p[0].(map[string]interface{})
rawConfig = rawConfig.AsValueSlice()[0]
if !rawConfig.IsNull() && len(rawConfig.AsValueSlice()) > 0 {
rawConfig = rawConfig.AsValueSlice()[0]
}
if v, ok := in["kind"].(string); ok && len(v) > 0 {
obj.Kind = v
}
Expand Down Expand Up @@ -2188,10 +2190,18 @@ func expandEKSClusterConfig(p []interface{}, rawConfig cty.Value) *EKSClusterCon
obj.NodeGroups = expandNodeGroups(v)
}
if v, ok := in["vpc"].([]interface{}); ok && len(v) > 0 {
obj.VPC = expandVPC(v, rawConfig.GetAttr("vpc"))
var nRawConfig cty.Value
if !rawConfig.IsNull() {
nRawConfig = rawConfig.GetAttr("vpc")
}
obj.VPC = expandVPC(v, nRawConfig)
}
if v, ok := in["managed_nodegroups"].([]interface{}); ok && len(v) > 0 {
obj.ManagedNodeGroups = expandManagedNodeGroups(v, rawConfig.GetAttr("managed_nodegroups"))
var nRawConfig cty.Value
if !rawConfig.IsNull() {
nRawConfig = rawConfig.GetAttr("managed_nodegroups")
}
obj.ManagedNodeGroups = expandManagedNodeGroups(v, nRawConfig)
}
if v, ok := in["fargate_profiles"].([]interface{}); ok && len(v) > 0 {
obj.FargateProfiles = expandFargateProfiles(v)
Expand Down Expand Up @@ -2553,7 +2563,7 @@ func expandManagedNodeGroups(p []interface{}, rawConfig cty.Value) []*ManagedNod
for i := range p {
obj := &ManagedNodeGroup{}
in := p[i].(map[string]interface{})
nRawConfig := rawConfig.AsValueSlice()[i]
// nRawConfig := rawConfig.AsValueSlice()[i]
if v, ok := in["name"].(string); ok && len(v) > 0 {
obj.Name = v
}
Expand Down Expand Up @@ -2606,7 +2616,11 @@ func expandManagedNodeGroups(p []interface{}, rawConfig cty.Value) []*ManagedNod
obj.AMI = v
}
if v, ok := in["security_groups"].([]interface{}); ok && len(v) > 0 {
obj.SecurityGroups = expandManagedNodeGroupSecurityGroups(v, nRawConfig.GetAttr("security_groups"))
var nRawConfig cty.Value
if !rawConfig.IsNull() && i < len(rawConfig.AsValueSlice()) {
nRawConfig = rawConfig.AsValueSlice()[i].GetAttr("security_groups")
}
obj.SecurityGroups = expandManagedNodeGroupSecurityGroups(v, nRawConfig)
}
if v, ok := in["max_pods_per_node"].(int); ok {
obj.MaxPodsPerNode = &v
Expand Down Expand Up @@ -3098,19 +3112,27 @@ func expandManagedNodeGroupSecurityGroups(p []interface{}, rawConfig cty.Value)
return obj
}
in := p[0].(map[string]interface{})
rawConfig = rawConfig.AsValueSlice()[0]
if !rawConfig.IsNull() && len(rawConfig.AsValueSlice()) > 0 {
rawConfig = rawConfig.AsValueSlice()[0]
}

if v, ok := in["attach_ids"].([]interface{}); ok && len(v) > 0 {
obj.AttachIDs = toArrayString(v)
}

rawWithShared := rawConfig.GetAttr("with_shared")
var rawWithShared cty.Value
if !rawConfig.IsNull() {
rawWithShared = rawConfig.GetAttr("with_shared")
}
if !rawWithShared.IsNull() {
boolVal := rawWithShared.True()
obj.WithShared = &boolVal
}

rawWithLocal := rawConfig.GetAttr("with_local")
var rawWithLocal cty.Value
if !rawConfig.IsNull() {
rawWithLocal = rawConfig.GetAttr("with_shared")
}
if !rawWithLocal.IsNull() {
boolVal := rawWithLocal.True()
obj.WithLocal = &boolVal
Expand Down Expand Up @@ -3347,7 +3369,9 @@ func expandVPC(p []interface{}, rawConfig cty.Value) *EKSClusterVPC {
return obj
}
in := p[0].(map[string]interface{})
rawConfig = rawConfig.AsValueSlice()[0]
if !rawConfig.IsNull() && len(rawConfig.AsValueSlice()) > 0 {
rawConfig = rawConfig.AsValueSlice()[0]
}

if v, ok := in["id"].(string); ok && len(v) > 0 {
obj.ID = v
Expand Down Expand Up @@ -3376,7 +3400,10 @@ func expandVPC(p []interface{}, rawConfig cty.Value) *EKSClusterVPC {
if v, ok := in["shared_node_security_group"].(string); ok && len(v) > 0 {
obj.SharedNodeSecurityGroup = v
}
rawManageSharedNodeSecurityGroupRules := rawConfig.GetAttr("manage_shared_node_security_group_rules")
var rawManageSharedNodeSecurityGroupRules cty.Value
if !rawConfig.IsNull() {
rawManageSharedNodeSecurityGroupRules = rawConfig.GetAttr("manage_shared_node_security_group_rules")
}
if !rawManageSharedNodeSecurityGroupRules.IsNull() {
boolVal := rawManageSharedNodeSecurityGroupRules.True()
obj.ManageSharedNodeSecurityGroupRules = &boolVal
Expand Down Expand Up @@ -3822,7 +3849,9 @@ func flattenEKSCluster(in *EKSCluster, p []interface{}, rawState cty.Value) ([]i
if in == nil {
return nil, fmt.Errorf("empty cluster input")
}
rawState = rawState.AsValueSlice()[0]
if !rawState.IsNull() && len(rawState.AsValueSlice()) > 0 {
rawState = rawState.AsValueSlice()[0]
}

if len(in.Kind) > 0 {
obj["kind"] = in.Kind
Expand Down Expand Up @@ -3851,7 +3880,11 @@ func flattenEKSCluster(in *EKSCluster, p []interface{}, rawState cty.Value) ([]i
if !ok {
v = []interface{}{}
}
ret2, err = flattenEKSClusterSpec(in.Spec, v, rawState.GetAttr("spec"))
var nRawState cty.Value
if !rawState.IsNull() && len(rawState.AsValueSlice()) > 0 {
nRawState = rawState.GetAttr("spec")
}
ret2, err = flattenEKSClusterSpec(in.Spec, v, nRawState)
if err != nil {
log.Println("flattenEKSClusterSpec err")
return nil, err
Expand Down Expand Up @@ -3889,7 +3922,13 @@ func flattenEKSClusterSpec(in *EKSSpec, p []interface{}, rawState cty.Value) ([]
return nil, fmt.Errorf("%s", "flattenEKSClusterMetaData empty input")
}
obj := map[string]interface{}{}
rawState = rawState.AsValueSlice()[0]

if len(p) != 0 && p[0] != nil {
obj = p[0].(map[string]interface{})
}
if !rawState.IsNull() && len(rawState.AsValueSlice()) > 0 {
rawState = rawState.AsValueSlice()[0]
}

if len(in.Type) > 0 {
obj["type"] = in.Type
Expand All @@ -3914,7 +3953,11 @@ func flattenEKSClusterSpec(in *EKSSpec, p []interface{}, rawState cty.Value) ([]
if !ok {
v = []interface{}{}
}
obj["cni_params"] = flattenCNIParams(in.CniParams, v, rawState.GetAttr("cni_params"))
var nRawState cty.Value
if !rawState.IsNull() {
nRawState = rawState.GetAttr("cni_params")
}
obj["cni_params"] = flattenCNIParams(in.CniParams, v, nRawState)
}
if in.ProxyConfig != nil {
obj["proxy_config"] = flattenProxyConfig(in.ProxyConfig)
Expand All @@ -3939,7 +3982,9 @@ func flattenCNIParams(in *CustomCni, p []interface{}, rawState cty.Value) []inte
if len(p) != 0 && p[0] != nil {
obj = p[0].(map[string]interface{})
}
rawState = rawState.AsValueSlice()[0]
if !rawState.IsNull() && len(rawState.AsValueSlice()) > 0 {
rawState = rawState.AsValueSlice()[0]
}

if len(in.CustomCniCidr) > 0 {
obj["custom_cni_cidr"] = in.CustomCniCidr
Expand All @@ -3949,7 +3994,11 @@ func flattenCNIParams(in *CustomCni, p []interface{}, rawState cty.Value) []inte
if !ok {
v = []interface{}{}
}
obj["custom_cni_crd_spec"] = flattenCustomCNISpec(in.CustomCniCrdSpec, v, rawState.GetAttr("custom_cni_crd_spec"))
var nRawState cty.Value
if !rawState.IsNull() {
nRawState = rawState.GetAttr("custom_cni_crd_spec")
}
obj["custom_cni_crd_spec"] = flattenCustomCNISpec(in.CustomCniCrdSpec, v, nRawState)
}

return []interface{}{obj}
Expand All @@ -3960,9 +4009,11 @@ func flattenCustomCNISpec(in map[string][]CustomCniSpec, p []interface{}, rawSta

findLocalOrder := func(rawState cty.Value) []string {
var order []string
for _, crdSpec := range rawState.AsValueSlice() {
if subnetValue, ok := crdSpec.AsValueMap()["name"]; ok {
order = append(order, subnetValue.AsString())
if !rawState.IsNull() {
for _, crdSpec := range rawState.AsValueSlice() {
if subnetValue, ok := crdSpec.AsValueMap()["name"]; ok {
order = append(order, subnetValue.AsString())
}
}
}
return order
Expand Down Expand Up @@ -4349,6 +4400,9 @@ func flattenEKSClusterIAM(in *EKSClusterIAM, p []interface{}) ([]interface{}, er
}

func flattenIAMServiceAccountMetadata(in *EKSClusterIAMMeta, p []interface{}) []interface{} {
if in == nil {
return nil
}
obj := map[string]interface{}{}
if len(p) != 0 && p[0] != nil {
obj = p[0].(map[string]interface{})
Expand Down