Skip to content

Commit

Permalink
fixing deepcopy issue and testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmayja committed Dec 8, 2023
1 parent 99c029a commit 0a1974c
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 149 deletions.
27 changes: 7 additions & 20 deletions api/v1/aerospikecluster_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1

import (
"fmt"
lib "github.com/aerospike/aerospike-management-lib"
"reflect"
"strings"

Expand All @@ -31,6 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/aerospike/aerospike-kubernetes-operator/pkg/merge"
lib "github.com/aerospike/aerospike-management-lib"
)

//nolint:lll // for readability
Expand Down Expand Up @@ -240,16 +240,13 @@ func (c *AerospikeCluster) updateRacksPodSpecFromGlobal(asLog logr.Logger) {

func (c *AerospikeCluster) updateRacksAerospikeConfigFromGlobal(asLog logr.Logger) error {
for idx := range c.Spec.RackConfig.Racks {
asLog.Info("#########printing spec aerospike config", "spec", c.Spec.AerospikeConfig.Value)
rack := &c.Spec.RackConfig.Racks[idx]

var (
m map[string]interface{}
err error
)

asconf := AerospikeConfigSpec{Value: map[string]interface{}{}}

if rack.InputAerospikeConfig != nil {
// Merge this rack's and global config.
m, err = merge.Merge(
Expand All @@ -260,52 +257,42 @@ func (c *AerospikeCluster) updateRacksAerospikeConfigFromGlobal(asLog logr.Logge
return err
}

asconf.Value = m

asLog.V(1).Info(
"Merged rack config from global aerospikeConfig", "rack id",
rack.ID, "rackAerospikeConfig", m, "globalAerospikeConfig",
c.Spec.AerospikeConfig,
)
} else {
// Use the global config.
lib.DeepCopy(&asconf.Value, &c.Spec.AerospikeConfig.Value)
//m = c.Spec.AerospikeConfig.Value
m = lib.DeepCopy(c.Spec.AerospikeConfig.Value).(map[string]interface{})
}

asLog.V(1).Info(
"Update rack aerospikeConfig from default aerospikeConfig",
"rackAerospikeConfig", asconf.Value,
"rackAerospikeConfig", m,
)

// Set defaults in updated rack config
// Above merge function may have overwritten defaults.
if err := c.setDefaultAerospikeConfigs(asLog, asconf, rack.ID); err != nil {
if err := c.setDefaultAerospikeConfigs(asLog, AerospikeConfigSpec{Value: m}, rack.ID); err != nil {
return err
}

asLog.V(1).Info(
"Update rack aerospikeConfig from default aerospikeConfig",
"rackAerospikeConfig", asconf.Value,
)

c.Spec.RackConfig.Racks[idx].AerospikeConfig = asconf

c.Spec.RackConfig.Racks[idx].AerospikeConfig.Value = m
}

return nil
}

func (c *AerospikeCluster) setDefaultAerospikeConfigs(asLog logr.Logger, configSpec AerospikeConfigSpec, rackID int) error {
func (c *AerospikeCluster) setDefaultAerospikeConfigs(asLog logr.Logger,
configSpec AerospikeConfigSpec, rackID int) error {
config := configSpec.Value

// namespace conf
if err := setDefaultNsConf(asLog, configSpec, c.Spec.RackConfig.Namespaces, rackID); err != nil {
return err
}

asLog.Info("after setDefaultNsConf", "c.Spec.AerospikeConfig", c.Spec.AerospikeConfig.Value, "rackID", rackID)

// service conf
if err := setDefaultServiceConf(asLog, configSpec, c.Name); err != nil {
return err
Expand Down
142 changes: 62 additions & 80 deletions api/v1/aerospikecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,87 +864,78 @@ func init() {
}

// CopySpecToStatus copy spec in status. Spec to Status DeepCopy doesn't work. It fails in reflect lib.
func CopySpecToStatus(spec *AerospikeClusterSpec) (*AerospikeClusterStatusSpec, error) { //nolint:dupl // not duplicate
func CopySpecToStatus(spec *AerospikeClusterSpec) (*AerospikeClusterStatusSpec, error) {
status := AerospikeClusterStatusSpec{}

status.Size = spec.Size
status.Image = spec.Image

// Storage
statusStorage := AerospikeStorageSpec{}
lib.DeepCopy(&statusStorage, &spec.Storage)
statusStorage := lib.DeepCopy(spec.Storage).(AerospikeStorageSpec)

status.Storage = statusStorage

if spec.AerospikeAccessControl != nil {
// AerospikeAccessControl
statusAerospikeAccessControl := &AerospikeAccessControlSpec{}
lib.DeepCopy(
statusAerospikeAccessControl, spec.AerospikeAccessControl,
)
statusAerospikeAccessControl := lib.DeepCopy(
*spec.AerospikeAccessControl,
).(AerospikeAccessControlSpec)

status.AerospikeAccessControl = statusAerospikeAccessControl
status.AerospikeAccessControl = &statusAerospikeAccessControl
}

// AerospikeConfig
statusAerospikeConfig := &AerospikeConfigSpec{}
lib.DeepCopy(
statusAerospikeConfig, spec.AerospikeConfig,
)
if spec.AerospikeConfig != nil {
// AerospikeConfig
statusAerospikeConfig := lib.DeepCopy(
*spec.AerospikeConfig,
).(AerospikeConfigSpec)

status.AerospikeConfig = statusAerospikeConfig
status.AerospikeConfig = &statusAerospikeConfig
}

if spec.ValidationPolicy != nil {
// ValidationPolicy
statusValidationPolicy := &ValidationPolicySpec{}
lib.DeepCopy(
statusValidationPolicy, spec.ValidationPolicy,
)
statusValidationPolicy := lib.DeepCopy(
*spec.ValidationPolicy,
).(ValidationPolicySpec)

status.ValidationPolicy = statusValidationPolicy
status.ValidationPolicy = &statusValidationPolicy
}

// RackConfig
statusRackConfig := RackConfig{}
lib.DeepCopy(&statusRackConfig, &spec.RackConfig)
statusRackConfig := lib.DeepCopy(spec.RackConfig).(RackConfig)
status.RackConfig = statusRackConfig

// AerospikeNetworkPolicy
statusAerospikeNetworkPolicy := AerospikeNetworkPolicy{}
lib.DeepCopy(
&statusAerospikeNetworkPolicy, &spec.AerospikeNetworkPolicy,
)
statusAerospikeNetworkPolicy := lib.DeepCopy(
spec.AerospikeNetworkPolicy,
).(AerospikeNetworkPolicy)

status.AerospikeNetworkPolicy = statusAerospikeNetworkPolicy

if spec.OperatorClientCertSpec != nil {
clientCertSpec := &AerospikeOperatorClientCertSpec{}
lib.DeepCopy(
clientCertSpec, spec.OperatorClientCertSpec,
)
clientCertSpec := lib.DeepCopy(
*spec.OperatorClientCertSpec,
).(AerospikeOperatorClientCertSpec)

status.OperatorClientCertSpec = clientCertSpec
status.OperatorClientCertSpec = &clientCertSpec
}

// Storage
statusPodSpec := AerospikePodSpec{}
lib.DeepCopy(&statusPodSpec, &spec.PodSpec)
statusPodSpec := lib.DeepCopy(spec.PodSpec).(AerospikePodSpec)
status.PodSpec = statusPodSpec

seedsFinderServices := SeedsFinderServices{}
lib.DeepCopy(
&seedsFinderServices, &spec.SeedsFinderServices,
)
seedsFinderServices := lib.DeepCopy(
spec.SeedsFinderServices,
).(SeedsFinderServices)

status.SeedsFinderServices = seedsFinderServices

// RosterNodeBlockList
if len(spec.RosterNodeBlockList) != 0 {
var rosterNodeBlockList []string

lib.DeepCopy(
&rosterNodeBlockList, &spec.RosterNodeBlockList,
)
rosterNodeBlockList := lib.DeepCopy(
spec.RosterNodeBlockList,
).([]string)

status.RosterNodeBlockList = rosterNodeBlockList
}
Expand All @@ -953,88 +944,79 @@ func CopySpecToStatus(spec *AerospikeClusterSpec) (*AerospikeClusterStatusSpec,
}

// CopyStatusToSpec copy status in spec. Status to Spec DeepCopy doesn't work. It fails in reflect lib.
func CopyStatusToSpec(status *AerospikeClusterStatusSpec) (*AerospikeClusterSpec, error) { //nolint:dupl // no need
func CopyStatusToSpec(status *AerospikeClusterStatusSpec) (*AerospikeClusterSpec, error) {
spec := AerospikeClusterSpec{}

spec.Size = status.Size
spec.Image = status.Image

// Storage
specStorage := AerospikeStorageSpec{}
lib.DeepCopy(&specStorage, &status.Storage)
specStorage := lib.DeepCopy(status.Storage).(AerospikeStorageSpec)
spec.Storage = specStorage

if status.AerospikeAccessControl != nil {
// AerospikeAccessControl
specAerospikeAccessControl := &AerospikeAccessControlSpec{}
lib.DeepCopy(
specAerospikeAccessControl, status.AerospikeAccessControl,
)
specAerospikeAccessControl := lib.DeepCopy(
status.AerospikeAccessControl,
).(*AerospikeAccessControlSpec)

spec.AerospikeAccessControl = specAerospikeAccessControl
}

// AerospikeConfig
specAerospikeConfig := &AerospikeConfigSpec{}
lib.DeepCopy(
specAerospikeConfig, status.AerospikeConfig,
)
if status.AerospikeConfig != nil {
specAerospikeConfig := lib.DeepCopy(
status.AerospikeConfig,
).(*AerospikeConfigSpec)

spec.AerospikeConfig = specAerospikeConfig
spec.AerospikeConfig = specAerospikeConfig
}

if status.ValidationPolicy != nil {
// ValidationPolicy
specValidationPolicy := &ValidationPolicySpec{}
lib.DeepCopy(
specValidationPolicy, status.ValidationPolicy,
)
specValidationPolicy := lib.DeepCopy(
*status.ValidationPolicy,
).(ValidationPolicySpec)

spec.ValidationPolicy = specValidationPolicy
spec.ValidationPolicy = &specValidationPolicy
}

// RackConfig
specRackConfig := RackConfig{}
lib.DeepCopy(&specRackConfig, &status.RackConfig)
specRackConfig := lib.DeepCopy(status.RackConfig).(RackConfig)

spec.RackConfig = specRackConfig

// AerospikeNetworkPolicy
specAerospikeNetworkPolicy := AerospikeNetworkPolicy{}
lib.DeepCopy(
&specAerospikeNetworkPolicy, &status.AerospikeNetworkPolicy,
)
specAerospikeNetworkPolicy := lib.DeepCopy(
status.AerospikeNetworkPolicy,
).(AerospikeNetworkPolicy)

spec.AerospikeNetworkPolicy = specAerospikeNetworkPolicy

if status.OperatorClientCertSpec != nil {
clientCertSpec := &AerospikeOperatorClientCertSpec{}
lib.DeepCopy(
clientCertSpec, status.OperatorClientCertSpec,
)
clientCertSpec := lib.DeepCopy(
*status.OperatorClientCertSpec,
).(AerospikeOperatorClientCertSpec)

spec.OperatorClientCertSpec = clientCertSpec
spec.OperatorClientCertSpec = &clientCertSpec
}

// Storage
specPodSpec := AerospikePodSpec{}
lib.DeepCopy(&specPodSpec, &status.PodSpec)
specPodSpec := lib.DeepCopy(status.PodSpec).(AerospikePodSpec)

spec.PodSpec = specPodSpec

seedsFinderServices := SeedsFinderServices{}
lib.DeepCopy(
&seedsFinderServices, &status.SeedsFinderServices,
)
seedsFinderServices := lib.DeepCopy(
status.SeedsFinderServices,
).(SeedsFinderServices)

spec.SeedsFinderServices = seedsFinderServices

// RosterNodeBlockList
if len(status.RosterNodeBlockList) != 0 {
var rosterNodeBlockList []string

lib.DeepCopy(
&rosterNodeBlockList, &status.RosterNodeBlockList,
)
rosterNodeBlockList := lib.DeepCopy(
status.RosterNodeBlockList,
).([]string)

spec.RosterNodeBlockList = rosterNodeBlockList
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1/aerospikeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *AerospikeConfigSpec) DeepCopy() *AerospikeConfigSpec {
dst := &AerospikeConfigSpec{
Value: map[string]interface{}{},
}
lib.DeepCopy(dst, c)
dst.Value = lib.DeepCopy(c.Value).(map[string]interface{})

return dst
}
7 changes: 3 additions & 4 deletions api/v1beta1/aerospikeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ func (c *AerospikeConfigSpec) UnmarshalJSON(b []byte) error {
}

func (c *AerospikeConfigSpec) DeepCopy() *AerospikeConfigSpec {
src := *c
dst := AerospikeConfigSpec{
dst := &AerospikeConfigSpec{
Value: map[string]interface{}{},
}
lib.DeepCopy(dst.Value, src.Value)
dst.Value = lib.DeepCopy(c.Value).(map[string]interface{})

return &dst
return dst
}
7 changes: 3 additions & 4 deletions controllers/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ func (r *SingleClusterReconciler) createConfigMapData(rack *asdbv1.Rack) (
func createPodSpecForRack(
aeroCluster *asdbv1.AerospikeCluster, rack *asdbv1.Rack,
) *asdbv1.AerospikePodSpec {
rackFullPodSpec := asdbv1.AerospikePodSpec{}
lib.DeepCopy(
&rackFullPodSpec, &aeroCluster.Spec.PodSpec,
)
rackFullPodSpec := lib.DeepCopy(
aeroCluster.Spec.PodSpec,
).(asdbv1.AerospikePodSpec)

rackFullPodSpec.Affinity = rack.PodSpec.Affinity
rackFullPodSpec.Tolerations = rack.PodSpec.Tolerations
Expand Down
4 changes: 4 additions & 0 deletions controllers/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,5 +1403,9 @@ func isFieldDynamic(dynamic map[string]bool, diff string) bool {
return true
}

if strings.Contains(key, "rack-id") {
return false
}

return dynamic[key]
}
Loading

0 comments on commit 0a1974c

Please sign in to comment.