Skip to content

Commit d2ffd2a

Browse files
committed
fix(argocd_cluster): use cluster list api to avoid 403 issues with cluster get api at cluster read time
Signed-off-by: Hugo HARS <[email protected]>
1 parent 76d2d70 commit d2ffd2a

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

argocd/resource_argocd_cluster.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func resourceArgoCDClusterCreate(ctx context.Context, d *schema.ResourceData, me
4343

4444
// Cluster are unique by "server address" so we should check there is no existing cluster with this address before
4545
existingClusters, err := si.ClusterClient.List(ctx, &clusterClient.ClusterQuery{
46+
// Starting argo-cd server v2.8.0 filtering on list api endpoint is fixed, else it is ignored, see:
47+
// - https://github.com/oboukili/terraform-provider-argocd/issues/266#issuecomment-1739122022
48+
// - https://github.com/argoproj/argo-cd/pull/13363
4649
Id: &clusterClient.ClusterID{
4750
Type: "server",
4851
Value: rtrimmedServer,
@@ -53,6 +56,7 @@ func resourceArgoCDClusterCreate(ctx context.Context, d *schema.ResourceData, me
5356
return errorToDiagnostics(fmt.Sprintf("failed to list existing clusters when creating cluster %s", cluster.Server), err)
5457
}
5558

59+
// Here we will filter ourselves on the list so that we are backward compatible for argo-cd server with version < v2.8.0 (see coment above)
5660
if len(existingClusters.Items) > 0 {
5761
for _, existingCluster := range existingClusters.Items {
5862
if rtrimmedServer == strings.TrimRight(existingCluster.Server, "/") {
@@ -103,7 +107,57 @@ func resourceArgoCDClusterRead(ctx context.Context, d *schema.ResourceData, meta
103107
return nil
104108
}
105109

106-
return argoCDAPIError("read", "cluster", d.Id(), err)
110+
// Fix for https://github.com/argoproj-labs/terraform-provider-argocd/issues/266
111+
// This fix is added here as a workaround to ensure backward compatibility, as
112+
// it is triggered only on the specific usecase where the issue happens.
113+
// Additional remarks about this code:
114+
// * it is a copy/paste of the code used by resourceArgoCDClusterCreate to check if
115+
// the cluster already exists (with some obvious changes to return value and mutex type)
116+
// * it should at term replace the `si.ClusterClient.Get` code for this method
117+
if strings.Contains(err.Error(), "PermissionDenied") {
118+
cluster, err := expandCluster(d)
119+
if err != nil {
120+
return errorToDiagnostics("failed to expand cluster", err)
121+
}
122+
123+
tokenMutexClusters.RLock()
124+
125+
rtrimmedServer := strings.TrimRight(cluster.Server, "/")
126+
127+
// Cluster are unique by "server address" so we should check there is no existing cluster with this address before
128+
existingClusters, err := si.ClusterClient.List(ctx, &clusterClient.ClusterQuery{
129+
// Starting argo-cd server v2.8.0 filtering on list api endpoint is fixed, else it is ignored, see:
130+
// - https://github.com/oboukili/terraform-provider-argocd/issues/266#issuecomment-1739122022
131+
// - https://github.com/argoproj/argo-cd/pull/13363
132+
Id: &clusterClient.ClusterID{
133+
Type: "server",
134+
Value: rtrimmedServer,
135+
},
136+
})
137+
138+
tokenMutexClusters.RUnlock()
139+
140+
if err != nil {
141+
return errorToDiagnostics(fmt.Sprintf("failed to list existing clusters when reading cluster %s", cluster.Server), err)
142+
}
143+
144+
// Here we will filter ourselves on the list so that we are backward compatible for argo-cd server with version < v2.8.0 (see coment above)
145+
if len(existingClusters.Items) > 0 {
146+
for _, existingCluster := range existingClusters.Items {
147+
if rtrimmedServer == strings.TrimRight(existingCluster.Server, "/") {
148+
// Cluster was found, return
149+
return nil
150+
}
151+
}
152+
}
153+
154+
// Cluster was not found, return with empty Id
155+
d.SetId("")
156+
157+
return nil
158+
} else {
159+
return argoCDAPIError("read", "cluster", d.Id(), err)
160+
}
107161
}
108162

109163
if err = flattenCluster(c, d); err != nil {

0 commit comments

Comments
 (0)