Skip to content

Commit

Permalink
add more UT
Browse files Browse the repository at this point in the history
Signed-off-by: haoqing0110 <qhao@redhat.com>
  • Loading branch information
haoqing0110 committed Aug 2, 2024
1 parent b5a03dc commit 19d6f36
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 425 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,59 +46,51 @@ func (d *managedClusterAddonConfigurationReconciler) mergeAddonConfig(
mca *addonv1alpha1.ManagedClusterAddOn, desiredConfigMap addonConfigMap) *addonv1alpha1.ManagedClusterAddOn {
mcaCopy := mca.DeepCopy()

mergedConfigs := make(map[addonv1alpha1.ConfigGroupResource][]addonv1alpha1.ConfigReference)
mergedConfigs := make(addonConfigMap)
// remove configs that are not desired
for _, config := range mcaCopy.Status.ConfigReferences {
if _, ok := desiredConfigMap[config.ConfigGroupResource]; ok {
if _, ok := mergedConfigs[config.ConfigGroupResource]; !ok {
mergedConfigs[config.ConfigGroupResource] = []addonv1alpha1.ConfigReference{}
gr := config.ConfigGroupResource
if _, ok := desiredConfigMap[gr]; ok {
if _, ok := mergedConfigs[gr]; !ok {
mergedConfigs[gr] = []addonv1alpha1.ConfigReference{}
}
mergedConfigs[config.ConfigGroupResource] = append(mergedConfigs[config.ConfigGroupResource], config)
}
}

mergedConfigsCopy := make(map[addonv1alpha1.ConfigGroupResource][]addonv1alpha1.ConfigReference)
for gvk := range mergedConfigs {
mergedConfigsCopy[gvk] = append(mergedConfigsCopy[gvk], mergedConfigs[gvk]...)
for i := range desiredConfigMap[gr] {
if desiredConfigMap[gr][i].DesiredConfig.ConfigReferent == config.DesiredConfig.ConfigReferent {
mergedConfigs[gr] = append(mergedConfigs[gr], config)
}
}
}
}

// append or update configs
for _, configArray := range desiredConfigMap {
seenConfigs := map[addonv1alpha1.ConfigReferent]struct{}{}
for _, config := range configArray {
// Initialize GVK
if len(seenConfigs) == 0 {
mergedConfigs[config.ConfigGroupResource] = []addonv1alpha1.ConfigReference{}
}
// Skip duplicates
if _, ok := seenConfigs[config.DesiredConfig.ConfigReferent]; ok {
continue
}

newConfig := addonv1alpha1.ConfigReference{
ConfigGroupResource: config.ConfigGroupResource,
DesiredConfig: config.DesiredConfig.DeepCopy(),
ConfigReferent: config.ConfigReferent,
LastObservedGeneration: 0,
for gr, configList := range desiredConfigMap {
for _, config := range configList {
if _, ok := mergedConfigs[gr]; !ok {
mergedConfigs[gr] = []addonv1alpha1.ConfigReference{}
}

// Only reset observed generation if the config was previously observed
for _, exConfig := range mergedConfigsCopy[config.ConfigGroupResource] {
if exConfig.DesiredConfig != nil && (exConfig.DesiredConfig.ConfigReferent == config.DesiredConfig.ConfigReferent) {
newConfig.LastObservedGeneration = exConfig.LastObservedGeneration
match := false
for i := range mergedConfigs[gr] {
if mergedConfigs[gr][i].ConfigGroupResource == config.ConfigGroupResource &&
mergedConfigs[gr][i].DesiredConfig.ConfigReferent == config.DesiredConfig.ConfigReferent {
match = true
mergedConfigs[gr][i].ConfigReferent = config.ConfigReferent
mergedConfigs[gr][i].DesiredConfig = config.DesiredConfig.DeepCopy()
}
}

mergedConfigs[config.ConfigGroupResource] = append(mergedConfigs[config.ConfigGroupResource], newConfig)
seenConfigs[config.DesiredConfig.ConfigReferent] = struct{}{}
if !match {
mergedConfigs[gr] = append(mergedConfigs[gr], config)
}
}
}

// sort by gvk and set the final config references
configRefs := []addonv1alpha1.ConfigReference{}
for gvk := range mergedConfigs {
for _, gvk := range mergedConfigs.orderedKeys() {
configRefs = append(configRefs, mergedConfigs[gvk]...)
}

mcaCopy.Status.ConfigReferences = configRefs
return mcaCopy
}
Loading

0 comments on commit 19d6f36

Please sign in to comment.