Skip to content

Commit

Permalink
chore: remove code duplication (#1372)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
thisthat and bacherfl committed May 4, 2023
1 parent dadd70b commit da66c80
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions operator/controllers/common/helperfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,32 @@ func copyMap[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {

func GetTaskDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string) (*klcv1alpha3.KeptnTaskDefinition, error) {
definition := &klcv1alpha3.KeptnTaskDefinition{}
err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: namespace}, definition)
if err != nil {
log.Info("Failed to get KeptnTaskDefinition from application namespace", "KeptnTaskDefinition", definitionName, "namespace", namespace)
if k8serrors.IsNotFound(err) {
if err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: KLTNamespace}, definition); err != nil {
log.Info("Failed to get KeptnTaskDefinition from default KLT namespace", "KeptnTaskDefinition", definitionName)
return nil, err
}
return definition, nil
}
if err := getObject(k8sclient, log, ctx, definitionName, namespace, definition); err != nil {
return nil, err
}
return definition, nil
}

func GetEvaluationDefinition(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string) (*klcv1alpha3.KeptnEvaluationDefinition, error) {
definition := &klcv1alpha3.KeptnEvaluationDefinition{}
if err := getObject(k8sclient, log, ctx, definitionName, namespace, definition); err != nil {
return nil, err
}
return definition, nil
}

func getObject(k8sclient client.Client, log logr.Logger, ctx context.Context, definitionName string, namespace string, definition client.Object) error {
err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: namespace}, definition)
if err != nil {
log.Info("Failed to get KeptnEvaluationDefinition from application namespace", "KeptnEvaluationDefinition", definitionName, "namespace", namespace)
log.Info("Failed to get resource from application namespace", "resource type", fmt.Sprintf("%T", definition), "Definition name", definitionName, "namespace", namespace)
if k8serrors.IsNotFound(err) {
if err := k8sclient.Get(ctx, types.NamespacedName{Name: definitionName, Namespace: KLTNamespace}, definition); err != nil {
log.Info("Failed to get KeptnEvaluationDefinition from default KLT namespace", "KeptnEvaluationDefinition", definitionName)
return nil, err
log.Info("Failed to get resource from default KLT namespace", "resource type", fmt.Sprintf("%T", definition), "definition name", definitionName)
return err
}
return definition, nil
return nil
}
return nil, err
return err
}
return definition, nil
return nil
}

0 comments on commit da66c80

Please sign in to comment.