4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
+ "reflect"
7
8
"strconv"
8
9
9
10
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -12,6 +13,7 @@ import (
12
13
tftypes "github.com/hashicorp/terraform-plugin-framework/types"
13
14
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
14
15
"google.golang.org/protobuf/types/known/structpb"
16
+ yamlv3 "gopkg.in/yaml.v3"
15
17
appsv1 "k8s.io/api/apps/v1"
16
18
"k8s.io/apimachinery/pkg/api/resource"
17
19
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -87,7 +89,7 @@ func (a *ArgoCD) Update(ctx context.Context, diagnostics *diag.Diagnostics, cd *
87
89
Subdomain : tftypes .StringValue (cd .Spec .InstanceSpec .Subdomain ),
88
90
DeclarativeManagementEnabled : tftypes .BoolValue (declarativeManagementEnabled ),
89
91
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 ),
91
93
ImageUpdaterEnabled : tftypes .BoolValue (imageUpdaterEnabled ),
92
94
BackendIpAllowListEnabled : tftypes .BoolValue (backendIpAllowListEnabled ),
93
95
RepoServerDelegate : toRepoServerDelegateTFModel (cd .Spec .InstanceSpec .RepoServerDelegate ),
@@ -765,7 +767,7 @@ func toManagedClusterTFModel(cluster *v1alpha1.ManagedCluster) *ManagedCluster {
765
767
}
766
768
}
767
769
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 {
769
771
if customization == nil {
770
772
return tftypes .ObjectNull (clusterCustomizationAttrTypes )
771
773
}
@@ -774,6 +776,22 @@ func toClusterCustomizationTFModel(ctx context.Context, diagnostics *diag.Diagno
774
776
diagnostics .AddError ("failed to convert json to yaml" , err .Error ())
775
777
}
776
778
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
+
777
795
autoUpgradeDisabled := false
778
796
if customization .AutoUpgradeDisabled != nil && * customization .AutoUpgradeDisabled {
779
797
autoUpgradeDisabled = true
@@ -1603,3 +1621,14 @@ func areResourcesValid(min, max string) bool {
1603
1621
}
1604
1622
return minQ .Cmp (maxQ ) <= 0
1605
1623
}
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