Skip to content

Commit

Permalink
issue-544, refactor externalChanges message
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksiienkoMykyta authored and testisnullus committed Dec 6, 2023
1 parent 64d2342 commit 8dd9a0e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 7 deletions.
12 changes: 12 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_kafkas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ spec:
status:
description: KafkaStatus defines the observed state of Kafka
properties:
availableUsers:
items:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: array
cdcid:
type: string
currentClusterOperationStatus:
Expand Down
8 changes: 7 additions & 1 deletion controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,13 @@ func (r *CassandraReconciler) newWatchStatusJob(cassandra *v1beta1.Cassandra) sc
cassandra.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
cassandra.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!cassandra.Spec.IsEqual(iCassandra.Spec) {
l.Info(msgExternalChanges, "instaclustr data", iCassandra.Spec, "k8s resource spec", cassandra.Spec)
k8sData, err := removeRedundantFieldsFromSpec(cassandra.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 := cassandra.NewPatch()
cassandra.Annotations[models.ExternalChangesAnnotation] = models.True
Expand Down
30 changes: 29 additions & 1 deletion controllers/clusters/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,36 @@ 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
}

func createSpecDifferenceMessage(k8sSpec, iSpec any) (string, error) {
k8sData, err := json.Marshal(k8sSpec)
k8sData, err := removeRedundantFieldsFromSpec(k8sSpec, "userRefs")
if err != nil {
return "", err
}
Expand Down
8 changes: 7 additions & 1 deletion controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,13 @@ func (r *KafkaConnectReconciler) newWatchStatusJob(kc *v1beta1.KafkaConnect) sch
if iKC.Status.CurrentClusterOperationStatus == models.NoOperation &&
kc.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!kc.Spec.IsEqual(iKC.Spec) {
l.Info(msgExternalChanges, "instaclustr data", iKC.Spec, "k8s resource spec", kc.Spec)
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
8 changes: 7 additions & 1 deletion controllers/clusters/opensearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,13 @@ func (r *OpenSearchReconciler) newWatchStatusJob(o *v1beta1.OpenSearch) schedule
if iO.Status.CurrentClusterOperationStatus == models.NoOperation &&
o.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!o.Spec.IsEqual(iO.Spec) {
l.Info(msgExternalChanges, "instaclustr data", iO.Spec, "k8s resource spec", o.Spec)
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
8 changes: 7 additions & 1 deletion controllers/clusters/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,13 @@ func (r *PostgreSQLReconciler) newWatchStatusJob(pg *v1beta1.PostgreSQL) schedul
if iPg.Status.CurrentClusterOperationStatus == models.NoOperation &&
pg.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!pg.Spec.IsEqual(iPg.Spec) {
l.Info(msgExternalChanges, "instaclustr data", iPg.Spec, "k8s resource spec", pg.Spec)
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
9 changes: 8 additions & 1 deletion controllers/clusters/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,14 @@ func (r *RedisReconciler) newWatchStatusJob(redis *v1beta1.Redis) scheduler.Job
if iRedis.Status.CurrentClusterOperationStatus == models.NoOperation &&
redis.Annotations[models.UpdateQueuedAnnotation] != models.True &&
!redis.Spec.IsEqual(iRedis.Spec) {
l.Info(msgExternalChanges, "instaclustr data", iRedis.Spec, "k8s resource spec", redis.Spec)

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
8 changes: 7 additions & 1 deletion controllers/clusters/zookeeper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,13 @@ func (r *ZookeeperReconciler) newWatchStatusJob(zook *v1beta1.Zookeeper) schedul

if iZook.Status.CurrentClusterOperationStatus == models.NoOperation &&
!zook.Spec.IsEqual(iZook.Spec) {
l.Info(msgExternalChanges, "instaclustr data", iZook.Spec, "k8s resource spec", zook.Spec)
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

0 comments on commit 8dd9a0e

Please sign in to comment.