Skip to content

Commit a42741a

Browse files
authored
fix: cluster_customization_defaults.kustomization returns inconsistency for same results (#321)
Fixes #318
1 parent 16b5f2b commit a42741a

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

akp/data_source_akp_kargoagent_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestAccKargoAgentDataSource(t *testing.T) {
2222
resource.TestCheckResourceAttr("data.akp_kargo_agent.test", "namespace", "akuity"),
2323
resource.TestCheckResourceAttr("data.akp_kargo_agent.test", "labels.app", "test"),
2424
resource.TestCheckResourceAttr("data.akp_kargo_agent.test", "annotations.app", "test"),
25-
resource.TestCheckResourceAttr("data.akp_kargo_agent.test", "spec.data.target_version", "0.5.54"),
2625
),
2726
},
2827
},

akp/data_source_akp_kargoagents_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ func testAccCheckKargoAgentAttributes(dataSourceName string, targetKargoAgentNam
6464
if err := resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("agents.%d.spec.data.size", i), "small")(s); err != nil {
6565
return err
6666
}
67-
if err := resource.TestCheckResourceAttr(dataSourceName, fmt.Sprintf("agents.%d.spec.data.target_version", i), "0.5.54")(s); err != nil {
68-
return err
69-
}
7067
return nil
7168
}
7269
}

akp/types/types.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"reflect"
78
"strconv"
89

910
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -12,6 +13,7 @@ import (
1213
tftypes "github.com/hashicorp/terraform-plugin-framework/types"
1314
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1415
"google.golang.org/protobuf/types/known/structpb"
16+
yamlv3 "gopkg.in/yaml.v3"
1517
appsv1 "k8s.io/api/apps/v1"
1618
"k8s.io/apimachinery/pkg/api/resource"
1719
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -87,7 +89,7 @@ func (a *ArgoCD) Update(ctx context.Context, diagnostics *diag.Diagnostics, cd *
8789
Subdomain: tftypes.StringValue(cd.Spec.InstanceSpec.Subdomain),
8890
DeclarativeManagementEnabled: tftypes.BoolValue(declarativeManagementEnabled),
8991
Extensions: toExtensionsTFModel(cd.Spec.InstanceSpec.Extensions),
90-
ClusterCustomizationDefaults: toClusterCustomizationTFModel(ctx, diagnostics, cd.Spec.InstanceSpec.ClusterCustomizationDefaults),
92+
ClusterCustomizationDefaults: a.toClusterCustomizationTFModel(ctx, diagnostics, cd.Spec.InstanceSpec.ClusterCustomizationDefaults),
9193
ImageUpdaterEnabled: tftypes.BoolValue(imageUpdaterEnabled),
9294
BackendIpAllowListEnabled: tftypes.BoolValue(backendIpAllowListEnabled),
9395
RepoServerDelegate: toRepoServerDelegateTFModel(cd.Spec.InstanceSpec.RepoServerDelegate),
@@ -765,7 +767,7 @@ func toManagedClusterTFModel(cluster *v1alpha1.ManagedCluster) *ManagedCluster {
765767
}
766768
}
767769

768-
func toClusterCustomizationTFModel(ctx context.Context, diagnostics *diag.Diagnostics, customization *v1alpha1.ClusterCustomization) tftypes.Object {
770+
func (a *ArgoCD) toClusterCustomizationTFModel(ctx context.Context, diagnostics *diag.Diagnostics, customization *v1alpha1.ClusterCustomization) tftypes.Object {
769771
if customization == nil {
770772
return tftypes.ObjectNull(clusterCustomizationAttrTypes)
771773
}
@@ -774,6 +776,22 @@ func toClusterCustomizationTFModel(ctx context.Context, diagnostics *diag.Diagno
774776
diagnostics.AddError("failed to convert json to yaml", err.Error())
775777
}
776778

779+
if !a.Spec.InstanceSpec.ClusterCustomizationDefaults.IsNull() && !a.Spec.InstanceSpec.ClusterCustomizationDefaults.IsUnknown() {
780+
var existingCustomization ClusterCustomization
781+
diagnostics.Append(a.Spec.InstanceSpec.ClusterCustomizationDefaults.As(ctx, &existingCustomization, basetypes.ObjectAsOptions{
782+
UnhandledNullAsEmpty: true,
783+
UnhandledUnknownAsEmpty: true,
784+
})...)
785+
786+
if !diagnostics.HasError() {
787+
existingYaml := existingCustomization.Kustomization.ValueString()
788+
newYaml := string(yamlData)
789+
if yamlEqual(existingYaml, newYaml) {
790+
yamlData = []byte(existingYaml)
791+
}
792+
}
793+
}
794+
777795
autoUpgradeDisabled := false
778796
if customization.AutoUpgradeDisabled != nil && *customization.AutoUpgradeDisabled {
779797
autoUpgradeDisabled = true
@@ -1603,3 +1621,14 @@ func areResourcesValid(min, max string) bool {
16031621
}
16041622
return minQ.Cmp(maxQ) <= 0
16051623
}
1624+
1625+
func yamlEqual(a, b string) bool {
1626+
var objA, objB any
1627+
if err := yamlv3.Unmarshal([]byte(a), &objA); err != nil {
1628+
return false
1629+
}
1630+
if err := yamlv3.Unmarshal([]byte(b), &objB); err != nil {
1631+
return false
1632+
}
1633+
return reflect.DeepEqual(objA, objB)
1634+
}

0 commit comments

Comments
 (0)