Skip to content

Commit

Permalink
dcomprasion.StructsDiff was implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
worryg0d authored and ribaraka committed Feb 26, 2024
1 parent 944f92a commit 13f37bd
Show file tree
Hide file tree
Showing 21 changed files with 713 additions and 130 deletions.
12 changes: 6 additions & 6 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@
"filename": "apis/clusters/v1beta1/cadence_types.go",
"hashed_secret": "a242f4a16b957f7ff99eb24e189e94d270d2348b",
"is_verified": false,
"line_number": 291
"line_number": 292
},
{
"type": "Secret Keyword",
"filename": "apis/clusters/v1beta1/cadence_types.go",
"hashed_secret": "a57ce131bd944bdf8ba2f2f93e179dc416ed0315",
"is_verified": false,
"line_number": 300
"line_number": 301
}
],
"apis/clusters/v1beta1/cassandra_types.go": [
Expand Down Expand Up @@ -319,21 +319,21 @@
"filename": "apis/clusters/v1beta1/postgresql_types.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 355
"line_number": 354
},
{
"type": "Secret Keyword",
"filename": "apis/clusters/v1beta1/postgresql_types.go",
"hashed_secret": "a3d7d4a96d18c8fc5a1cf9c9c01c45b4690b4008",
"is_verified": false,
"line_number": 361
"line_number": 360
},
{
"type": "Secret Keyword",
"filename": "apis/clusters/v1beta1/postgresql_types.go",
"hashed_secret": "a57ce131bd944bdf8ba2f2f93e179dc416ed0315",
"is_verified": false,
"line_number": 481
"line_number": 480
}
],
"apis/clusters/v1beta1/redis_types.go": [
Expand Down Expand Up @@ -1146,5 +1146,5 @@
}
]
},
"generated_at": "2024-02-21T14:33:43Z"
"generated_at": "2024-02-26T10:23:28Z"
}
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/cadence_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type CadenceSpec struct {
//+kubebuilder:validation:MaxItems:=1
TargetPrimaryCadence []*CadenceDependencyTarget `json:"targetPrimaryCadence,omitempty"`

ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty"`
ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty" dcomparisonSkip:"true"`

UseCadenceWebAuth bool `json:"useCadenceWebAuth"`
UseHTTPAPI bool `json:"useHttpApi,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions apis/clusters/v1beta1/cassandra_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type CassandraSpec struct {
PasswordAndUserAuth bool `json:"passwordAndUserAuth,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
PCICompliance bool `json:"pciCompliance,omitempty"`
UserRefs References `json:"userRefs,omitempty"`
ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty"`
UserRefs References `json:"userRefs,omitempty" dcomparisonSkip:"true"`
ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty" dcomparisonSkip:"true"`
}

// CassandraStatus defines the observed state of Cassandra
Expand Down Expand Up @@ -171,7 +171,7 @@ type DebeziumCassandraSpec struct {
KafkaVPCType string `json:"kafkaVpcType"`
KafkaTopicPrefix string `json:"kafkaTopicPrefix"`
KafkaDataCentreID string `json:"kafkaCdcId,omitempty"`
ClusterRef *clusterresourcesv1beta1.ClusterRef `json:"clusterRef,omitempty"`
ClusterRef *clusterresourcesv1beta1.ClusterRef `json:"clusterRef,omitempty" dcomparisonSkip:"true"`
Version string `json:"version"`
}

Expand Down
4 changes: 2 additions & 2 deletions apis/clusters/v1beta1/kafka_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type KafkaSpec struct {
ClientBrokerAuthWithMTLS bool `json:"clientBrokerAuthWithMtls,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
PCICompliance bool `json:"pciCompliance,omitempty"`
UserRefs References `json:"userRefs,omitempty"`
UserRefs References `json:"userRefs,omitempty" dcomparisonSkip:"true"`

// Provision additional dedicated nodes for Apache Zookeeper to run on.
// Zookeeper nodes will be co-located with Kafka if this is not provided
Expand All @@ -86,7 +86,7 @@ type KafkaSpec struct {
KarapaceRestProxy []*KarapaceRestProxy `json:"karapaceRestProxy,omitempty"`
KarapaceSchemaRegistry []*KarapaceSchemaRegistry `json:"karapaceSchemaRegistry,omitempty"`
Kraft []*Kraft `json:"kraft,omitempty"`
ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty"`
ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty" dcomparisonSkip:"true"`
}

type Kraft struct {
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/kafkaconnect_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ExternalCluster struct {

type ManagedCluster struct {
TargetKafkaClusterID string `json:"targetKafkaClusterId,omitempty"`
ClusterRef *clusterresource.ClusterRef `json:"clusterRef,omitempty"`
ClusterRef *clusterresource.ClusterRef `json:"clusterRef,omitempty" dcomparisonSkip:"true"`

// Available options are KAFKA_VPC, VPC_PEERED, SEPARATE_VPC
KafkaConnectVPCType string `json:"kafkaConnectVpcType"`
Expand Down
4 changes: 2 additions & 2 deletions apis/clusters/v1beta1/opensearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ type OpenSearchSpec struct {
AlertingPlugin bool `json:"alertingPlugin,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
PCICompliance bool `json:"pciCompliance,omitempty"`
UserRefs References `json:"userRefs,omitempty"`
UserRefs References `json:"userRefs,omitempty" dcomparisonSkip:"true"`
//+kubuilder:validation:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty" dcomparisonSkip:"true"`
//+kubuilder:validation:MaxItems:=1
IngestNodes []*OpenSearchIngestNodes `json:"ingestNodes,omitempty"`
}
Expand Down
4 changes: 2 additions & 2 deletions apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ type PgSpec struct {
DataCentres []*PgDataCentre `json:"dataCentres,omitempty"`
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*Reference `json:"userRefs,omitempty"`
UserRefs []*Reference `json:"userRefs,omitempty" dcomparisonSkip:"true"`
//+kubebuilder:validate:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty" dcomparisonSkip:"true"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="extensions cannot be changed after it is set"
Extensions PgExtensions `json:"extensions,omitempty"`
}
Expand Down
4 changes: 2 additions & 2 deletions apis/clusters/v1beta1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ type RedisSpec struct {
//+kubebuilder:validation:MaxItems:=2
DataCentres []*RedisDataCentre `json:"dataCentres"`

ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty"`
UserRefs References `json:"userRefs,omitempty"`
ResizeSettings GenericResizeSettings `json:"resizeSettings,omitempty" dcomparisonSkip:"true"`
UserRefs References `json:"userRefs,omitempty" dcomparisonSkip:"true"`
}

type RedisDataCentreStatus struct {
Expand Down
3 changes: 0 additions & 3 deletions apis/clusters/v1beta1/zookeeper_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ type ZookeeperSpec struct {

// ZookeeperStatus defines the observed state of Zookeeper
type ZookeeperStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

ClusterStatus `json:",inline"`
DefaultUserSecretRef *Reference `json:"defaultUserSecretRef,omitempty"`
}
Expand Down
3 changes: 1 addition & 2 deletions config/crd/bases/clusters.instaclustr.com_cadences.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ spec:
type: object
maxItems: 1
type: array
pciComplianceMode:
pciCompliance:
type: boolean
privateNetwork:
type: boolean
Expand Down Expand Up @@ -393,7 +393,6 @@ spec:
type: string
required:
- dataCentres
- pciComplianceMode
- useCadenceWebAuth
type: object
status:
Expand Down
2 changes: 0 additions & 2 deletions config/crd/bases/clusters.instaclustr.com_cassandras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ spec:
type: array
version:
type: string
required:
- pciCompliance
type: object
status:
description: CassandraStatus defines the observed state of Cassandra
Expand Down
7 changes: 0 additions & 7 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,6 @@ func (r *CassandraReconciler) newSyncJob(c *v1beta1.Cassandra) scheduler.Job {
} else if c.Status.CurrentClusterOperationStatus == models.NoOperation &&
c.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
!equals {
k8sData, err := removeRedundantFieldsFromSpec(c.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
return err
}

l.Info(msgExternalChanges, "instaclustr data", iCassandra.Spec, "k8s resource spec", string(k8sData))

patch := c.NewPatch()
c.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
74 changes: 16 additions & 58 deletions controllers/clusters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"

"github.com/go-logr/logr"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -169,74 +168,33 @@ func getSortedAppVersions(versions []*models.AppVersions, appType string) []*ver
return nil
}

func removeRedundantFieldsFromSpec(k8sSpec any, ignoreFields ...string) ([]byte, error) {
k8sSpecJson, err := json.Marshal(k8sSpec)
if err != nil {
return nil, err
}

if len(ignoreFields) == 0 {
return k8sSpecJson, nil
}

k8sSpecMap := map[string]any{}
err = json.Unmarshal(k8sSpecJson, &k8sSpecMap)

if err != nil {
return nil, err
}

for _, field := range ignoreFields {
delete(k8sSpecMap, field)
}

k8sSpecJson, err = json.Marshal(k8sSpecMap)
if err != nil {
return nil, err
}
return k8sSpecJson, nil
type objectDiff struct {
Field string `json:"field"`
K8sValue any `json:"k8sValue"`
InstaclustrValue any `json:"instaclustrValue"`
}

func createSpecDifferenceMessage(k8sSpec, iSpec any) (string, error) {
k8sData, err := removeRedundantFieldsFromSpec(k8sSpec, "userRefs")
func createSpecDifferenceMessage[T any](k8sSpec, iSpec T) (string, error) {
diffs, err := dcomparison.StructsDiff(models.SpecPath, k8sSpec, iSpec)
if err != nil {
return "", err
return "", fmt.Errorf("failed to create spec difference message, err: %w", err)
}

iData, err := json.Marshal(iSpec)
if err != nil {
return "", err
}

var k8sSpecMap map[string]any
err = json.Unmarshal(k8sData, &k8sSpecMap)
if err != nil {
return "", err
objectDiffs := make([]objectDiff, 0, len(diffs))
for _, diff := range diffs {
objectDiffs = append(objectDiffs, objectDiff{
Field: diff.Field,
K8sValue: diff.Value1,
InstaclustrValue: diff.Value2,
})
}

var iSpecMap map[string]any
err = json.Unmarshal(iData, &iSpecMap)
b, err := json.Marshal(objectDiffs)
if err != nil {
return "", err
}

diffs := dcomparison.MapsDiff(models.SpecPath, k8sSpecMap, iSpecMap)

return fmt.Sprintf("%s Diffs: %s", models.ExternalChangesBaseMessage, prepareDiffMessage(diffs)), nil
}

func prepareDiffMessage(diffs dcomparison.ObjectDiffs) string {
var diffMessages []string
for _, diff := range diffs {
diffMessages = append(diffMessages, fmt.Sprintf(
"{field: %s, k8sValue: %v, instaclustrValue: %v}",
diff.Field,
diff.Value1,
diff.Value2,
))
}

return strings.Join(diffMessages, ", ")
return fmt.Sprintf("%s Diffs: %s", models.ExternalChangesBaseMessage, b), nil
}

var msgDeleteClusterWithTwoFactorDelete = "Please confirm cluster deletion via email or phone. " +
Expand Down
7 changes: 0 additions & 7 deletions controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,6 @@ func (r *KafkaConnectReconciler) newSyncJob(kc *v1beta1.KafkaConnect) scheduler.
} else if kc.Status.CurrentClusterOperationStatus == models.NoOperation &&
kc.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
!equals {
k8sData, err := removeRedundantFieldsFromSpec(kc.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
return err
}

l.Info(msgExternalChanges, "instaclustr data", iKC.Spec, "k8s resource spec", string(k8sData))

patch := kc.NewPatch()
kc.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
7 changes: 0 additions & 7 deletions controllers/clusters/opensearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,6 @@ func (r *OpenSearchReconciler) newSyncJob(o *v1beta1.OpenSearch) scheduler.Job {
} else if o.Status.CurrentClusterOperationStatus == models.NoOperation &&
o.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
!equals {
k8sData, err := removeRedundantFieldsFromSpec(o.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
return err
}

l.Info(msgExternalChanges, "instaclustr data", iO.Spec, "k8s resource spec", string(k8sData))

patch := o.NewPatch()
o.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
7 changes: 0 additions & 7 deletions controllers/clusters/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,6 @@ func (r *PostgreSQLReconciler) newWatchStatusJob(pg *v1beta1.PostgreSQL) schedul
} else if pg.Status.CurrentClusterOperationStatus == models.NoOperation &&
pg.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
!equals {
k8sData, err := removeRedundantFieldsFromSpec(pg.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
return err
}

l.Info(msgExternalChanges, "instaclustr data", iPg.Spec, "k8s resource spec", string(k8sData))

patch := pg.NewPatch()
pg.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
7 changes: 0 additions & 7 deletions controllers/clusters/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,6 @@ func (r *RedisReconciler) newSyncJob(redis *v1beta1.Redis) scheduler.Job {
} else if redis.Status.CurrentClusterOperationStatus == models.NoOperation &&
redis.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
!equals {
k8sData, err := removeRedundantFieldsFromSpec(redis.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
return err
}

l.Info(msgExternalChanges, "instaclustr data", iRedis.Spec, "k8s resource spec", string(k8sData))

patch := redis.NewPatch()
redis.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
7 changes: 0 additions & 7 deletions controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,6 @@ func (r *ZookeeperReconciler) newWatchStatusJob(zook *v1beta1.Zookeeper) schedul
} else if zook.Status.CurrentClusterOperationStatus == models.NoOperation &&
zook.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
!equals {
k8sData, err := removeRedundantFieldsFromSpec(zook.Spec, "userRefs")
if err != nil {
l.Error(err, "Cannot remove redundant fields from k8s Spec")
return err
}

l.Info(msgExternalChanges, "instaclustr data", iZook.Spec, "k8s resource spec", string(k8sData))

patch := zook.NewPatch()
zook.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
7 changes: 3 additions & 4 deletions pkg/utils/dcomparison/map_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package dcomparison provides a solution for deeply comparing two maps,
// including their nested maps and slices. It is designed to identify differences
// between two maps that can contain a variety of data types, such as strings,
// integers, other maps, and slices.
// Package dcomparison provides a solution for deeply comparing two objects (struct, maps).
// It is designed to identify differences between two objects that may contain a variety of
// data types, such as strings, integers, other maps, and slices.
package dcomparison

import (
Expand Down
Loading

0 comments on commit 13f37bd

Please sign in to comment.