@@ -51,22 +51,24 @@ func withExternalManaged() cloudstack.OptionFunc {
51
51
func (c * client ) GetOrCreateCluster (cluster * clusterv1.Cluster , csCluster * infrav1.CloudStackCluster , fd * infrav1.CloudStackFailureDomainSpec ) error {
52
52
// Get cluster
53
53
if csCluster .Status .CloudStackClusterID != "" {
54
- _ , count , err := c .cs .Kubernetes .GetKubernetesClusterByID (csCluster .Status .CloudStackClusterID , withExternalManaged ())
54
+ externalManagedCluster , count , err := c .cs .Kubernetes .GetKubernetesClusterByID (csCluster .Status .CloudStackClusterID , withExternalManaged ())
55
55
if err != nil {
56
56
return err
57
- }
58
- if count == 1 {
57
+ } else if count > 0 {
58
+ csCluster . Status . CloudStackClusterID = externalManagedCluster . Id
59
59
return nil
60
60
}
61
61
}
62
62
63
63
// Check if a cluster exists with the same name
64
- clusterName := fmt .Sprintf ("%s - %s" , cluster .GetName (), csCluster .GetName ())
65
- csUnmanagedCluster , count , err := c .cs .Kubernetes .GetKubernetesClusterByName (clusterName , withExternalManaged ())
64
+ clusterName := fmt .Sprintf ("%s - %s - %s " , cluster .GetName (), csCluster .GetName (), csCluster . GetUID ())
65
+ externalManagedCluster , count , err := c .cs .Kubernetes .GetKubernetesClusterByName (clusterName , withExternalManaged ())
66
66
if err != nil && ! strings .Contains (err .Error (), "No match found for " ) {
67
67
return err
68
68
}
69
- if count <= 0 {
69
+ if count > 0 {
70
+ csCluster .Status .CloudStackClusterID = externalManagedCluster .Id
71
+ } else if err == nil || (err != nil && strings .Contains (err .Error (), "No match found for " )) {
70
72
// Create cluster
71
73
domain := Domain {Path : rootDomain }
72
74
if csCluster .Spec .FailureDomains [0 ].Domain != "" {
@@ -91,27 +93,20 @@ func (c *client) GetOrCreateCluster(cluster *clusterv1.Cluster, csCluster *infra
91
93
setIfNotEmpty (csCluster .Spec .ControlPlaneEndpoint .Host , params .SetExternalloadbalanceripaddress )
92
94
params .SetClustertype ("ExternalManaged" )
93
95
94
- r , err := c .cs .Kubernetes .CreateKubernetesCluster (params )
96
+ cloudStackCKSCluster , err := c .cs .Kubernetes .CreateKubernetesCluster (params )
95
97
if err != nil {
96
98
return err
97
99
}
98
- csUnmanagedCluster , count , err = c .cs .Kubernetes .GetKubernetesClusterByID (r .Id )
99
- if err != nil {
100
- return err
101
- }
102
- if count == 0 {
103
- return errors .New ("cluster not found" )
104
- }
100
+ csCluster .Status .CloudStackClusterID = cloudStackCKSCluster .Id
105
101
}
106
- csCluster .Status .CloudStackClusterID = csUnmanagedCluster .Id
107
102
return nil
108
103
}
109
104
110
105
func (c * client ) DeleteCluster (csCluster * infrav1.CloudStackCluster ) error {
111
106
if csCluster .Status .CloudStackClusterID != "" {
112
107
csUnmanagedCluster , count , err := c .cs .Kubernetes .GetKubernetesClusterByID (csCluster .Status .CloudStackClusterID , withExternalManaged ())
113
- if err != nil {
114
- return err
108
+ if err != nil && strings . Contains ( err . Error (), " not found" ) {
109
+ return nil
115
110
}
116
111
if count != 0 {
117
112
params := c .cs .Kubernetes .NewDeleteKubernetesClusterParams (csUnmanagedCluster .Id )
@@ -121,7 +116,6 @@ func (c *client) DeleteCluster(csCluster *infrav1.CloudStackCluster) error {
121
116
}
122
117
}
123
118
csCluster .Status .CloudStackClusterID = ""
124
- return nil
125
119
}
126
120
return nil
127
121
}
0 commit comments