Skip to content

Commit

Permalink
no pointer, separate func
Browse files Browse the repository at this point in the history
  • Loading branch information
matthagenbuch committed May 20, 2024
1 parent b25e45f commit 461925e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apis/v1beta1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type OpenTelemetryCollectorSpec struct {
// +optional
// +kubebuilder:default:=3
// +kubebuilder:validation:Minimum:=1
ConfigVersions *int32 `json:"configVersions,omitempty"`
ConfigVersions int `json:"configVersions,omitempty"`
// Ingress is used to specify how OpenTelemetry Collector is exposed. This
// functionality is only available if one of the valid modes is set.
// Valid modes are: deployment, daemonset and statefulset.
Expand Down
5 changes: 0 additions & 5 deletions apis/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5543,7 +5543,7 @@ spec:
x-kubernetes-preserve-unknown-fields: true
configVersions:
default: 3
format: int32
minimum: 1
type: integer
configmaps:
items:
Expand Down
20 changes: 13 additions & 7 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,32 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
if err != nil {
return nil, fmt.Errorf("error listing ConfigMaps: %w", err)
}
ownedConfigMaps := r.getConfigMapsToReconcile(params.OtelCol.Spec.ConfigVersions, configMapList)
for i := range ownedConfigMaps {
ownedObjects[ownedConfigMaps[i].GetUID()] = &ownedConfigMaps[i]
}

return ownedObjects, nil
}

// only return collector ConfigMaps older than spec.ConfigVersions
// getConfigMapsToReconcile returns a list of ConfigMaps to reconcile based on the number of ConfigMaps to keep.
// It keeps the newest ConfigMap, the `configVersionsToKeep` next newest ConfigMaps, and returns the remainder.
func (r *OpenTelemetryCollectorReconciler) getConfigMapsToReconcile(configVersionsToKeep int, configMapList *corev1.ConfigMapList) []corev1.ConfigMap {
ownedConfigMaps := []corev1.ConfigMap{}
sort.Slice(configMapList.Items, func(i, j int) bool {
iTime := configMapList.Items[i].GetCreationTimestamp().Time
jTime := configMapList.Items[j].GetCreationTimestamp().Time
// sort the ConfigMaps newest to oldest
return iTime.After(jTime)
})
configVersionsToKeep := 3
if params.OtelCol.Spec.ConfigVersions != nil {
configVersionsToKeep = max(1, int(*params.OtelCol.Spec.ConfigVersions))
}

for i := range configMapList.Items {
if i > configVersionsToKeep {
ownedObjects[configMapList.Items[i].GetUID()] = &configMapList.Items[i]
ownedConfigMaps = append(ownedConfigMaps, configMapList.Items[i])
}
}

return ownedObjects, nil
return ownedConfigMaps
}

func (r *OpenTelemetryCollectorReconciler) getParams(instance v1beta1.OpenTelemetryCollector) (manifests.Params, error) {
Expand Down

0 comments on commit 461925e

Please sign in to comment.