Skip to content

Commit f84f494

Browse files
wait for mgmt cluster healthy and kubeconfig implemented
1 parent 4ac75a1 commit f84f494

File tree

5 files changed

+113
-3
lines changed

5 files changed

+113
-3
lines changed

internal/models/ekscluster/spec.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/resources/ekscluster/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ const (
6969
releaseVersionKey = "release_version"
7070
readyCondition = "Ready"
7171
errorSeverity = "ERROR"
72+
waitForKubeconfig = "wait_for_kubeconfig"
73+
kubeconfigKey = "kubeconfig"
7274
)

internal/resources/ekscluster/data_source.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"log"
1111
"strconv"
1212
"time"
13+
// "os"
14+
// "strings"
1315

1416
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1517
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -20,6 +22,9 @@ import (
2022
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper"
2123
eksmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/ekscluster"
2224
"github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/common"
25+
26+
clustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/cluster"
27+
configModels "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/kubeconfig"
2328
)
2429

2530
func DataSourceTMCEKSCluster() *schema.Resource {
@@ -79,6 +84,35 @@ func dataSourceTMCEKSClusterRead(ctx context.Context, d *schema.ResourceData, m
7984
return true, nil
8085
}
8186

87+
// if val := os.Getenv("WAIT_FOR_HEALTHY"); strings.EqualFold(val, "TRUE") || val == "" {
88+
if isWaitForKubeconfig(d) {
89+
clusFullName := &clustermodel.VmwareTanzuManageV1alpha1ClusterFullName { Name: resp.EksCluster.Spec.AgentName, OrgID: clusterFn.OrgID, ManagementClusterName: "eks", ProvisionerName: "eks" }
90+
clusterResp, err := config.TMCConnection.ClusterResourceService.ManageV1alpha1ClusterResourceServiceGet(clusFullName)
91+
if err != nil {
92+
log.Printf("Unable to get Tanzu Mission Control cluster entry, name : %s", clusterFn.Name)
93+
}
94+
95+
if !isManagemetClusterHealthy(clusterResp) {
96+
log.Printf("[DEBUG] waiting for cluster(%s) to be in Healthy status", clusterFn.Name)
97+
return true, nil
98+
}
99+
100+
fn := &configModels.VmwareTanzuManageV1alpha1ClusterFullName{
101+
ManagementClusterName: "eks",
102+
ProvisionerName: "eks",
103+
Name: resp.EksCluster.Spec.AgentName,
104+
OrgID: "",
105+
}
106+
resp, err := config.TMCConnection.KubeConfigResourceService.KubeconfigServiceGet(fn)
107+
108+
if kubeConfigReady(err, resp) {
109+
if err = d.Set(kubeconfigKey, resp.Kubeconfig); err != nil {
110+
log.Printf("Failed to set Kubeconfig")
111+
return true, nil
112+
}
113+
}
114+
}
115+
82116
return false, nil
83117
}
84118

@@ -124,6 +158,14 @@ func dataSourceTMCEKSClusterRead(ctx context.Context, d *schema.ResourceData, m
124158
return diags
125159
}
126160

161+
func isManagemetClusterHealthy(cluster *clustermodel.VmwareTanzuManageV1alpha1ClusterGetClusterResponse) bool {
162+
return cluster.Cluster.Status.Health != nil && *cluster.Cluster.Status.Health == clustermodel.VmwareTanzuManageV1alpha1CommonClusterHealthHEALTHY
163+
}
164+
165+
func kubeConfigReady(err error, resp *configModels.VmwareTanzuManageV1alpha1ClusterKubeconfigGetKubeconfigResponse) bool {
166+
return err == nil && *resp.Status == configModels.VmwareTanzuManageV1alpha1ClusterKubeconfigGetKubeconfigResponseStatusREADY
167+
}
168+
127169
func setResourceData(d *schema.ResourceData, eksCluster *eksmodel.VmwareTanzuManageV1alpha1EksclusterEksCluster, remoteNodepools []*eksmodel.VmwareTanzuManageV1alpha1EksclusterNodepoolNodepool) error {
128170
status := map[string]interface{}{
129171
// TODO: add condition

internal/resources/ekscluster/data_source_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build ekscluster
2-
// +build ekscluster
3-
41
/*
52
Copyright 2022 VMware, Inc. All Rights Reserved.
63
SPDX-License-Identifier: MPL-2.0
@@ -14,6 +11,7 @@ import (
1411
"github.com/stretchr/testify/require"
1512

1613
eksmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/ekscluster"
14+
clustermodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/cluster"
1715
)
1816

1917
func TestNodepoolPosMap(t *testing.T) {
@@ -62,3 +60,45 @@ func TestNodepoolPosMap(t *testing.T) {
6260
})
6361
}
6462
}
63+
64+
func TestIsManagemetClusterHealthy(t *testing.T) {
65+
tests := []struct {
66+
name string
67+
cluster *clustermodel.VmwareTanzuManageV1alpha1ClusterGetClusterResponse
68+
response bool
69+
err error
70+
}{
71+
{
72+
name: "Not healthy",
73+
cluster: &clustermodel.VmwareTanzuManageV1alpha1ClusterGetClusterResponse{
74+
Cluster: &clustermodel.VmwareTanzuManageV1alpha1ClusterCluster{
75+
Status: &clustermodel.VmwareTanzuManageV1alpha1ClusterStatus{
76+
Health: clustermodel.NewVmwareTanzuManageV1alpha1CommonClusterHealth(clustermodel.VmwareTanzuManageV1alpha1CommonClusterHealthUNHEALTHY),
77+
},
78+
},
79+
},
80+
response: false,
81+
err: nil,
82+
},
83+
{
84+
name: "Healthy",
85+
cluster: &clustermodel.VmwareTanzuManageV1alpha1ClusterGetClusterResponse{
86+
Cluster: &clustermodel.VmwareTanzuManageV1alpha1ClusterCluster{
87+
Status: &clustermodel.VmwareTanzuManageV1alpha1ClusterStatus{
88+
Health: clustermodel.NewVmwareTanzuManageV1alpha1CommonClusterHealth(clustermodel.VmwareTanzuManageV1alpha1CommonClusterHealthHEALTHY),
89+
},
90+
},
91+
},
92+
response: true,
93+
err: nil,
94+
},
95+
}
96+
97+
for _, test := range tests {
98+
t.Run(test.name, func(t *testing.T) {
99+
if test.response != isManagemetClusterHealthy(test.cluster) {
100+
t.Errorf("expected function output to match")
101+
}
102+
})
103+
}
104+
}

internal/resources/ekscluster/resource_ekscluster.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ var clusterSchema = map[string]*schema.Schema{
8686
return true
8787
},
8888
},
89+
waitForKubeconfig: {
90+
Type: schema.TypeBool,
91+
Description: "Wait until pinniped extension is ready to provide kubeconfig",
92+
Default: false,
93+
Optional: true,
94+
},
95+
kubeconfigKey: {
96+
Type: schema.TypeString,
97+
Description: "Kubeconfig for connecting to newly created cluster base64 encoded. This will only be returned if you have elected to wait for kubeconfig.",
98+
Computed: true,
99+
},
89100
}
90101

91102
var clusterSpecSchema = &schema.Schema{
@@ -958,3 +969,12 @@ func flattenEniConfig(item *eksmodel.VmwareTanzuManageV1alpha1EksclusterEniConfi
958969

959970
return data
960971
}
972+
973+
func isWaitForKubeconfig(data *schema.ResourceData) bool {
974+
v := data.Get(waitForKubeconfig)
975+
if v != nil {
976+
return v.(bool)
977+
}
978+
979+
return false
980+
}

0 commit comments

Comments
 (0)