Skip to content

Commit 37a5725

Browse files
committed
Be a bit more defensive in the error checks
1 parent c49b161 commit 37a5725

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

internal/controller/argo.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,10 @@ func createOrUpdateArgoCD(client dynamic.Interface, fullClient kubernetes.Interf
411411

412412
if !haveArgo(client, name, namespace) {
413413
// create it
414-
obj, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(argo)
414+
obj, errConvert := runtime.DefaultUnstructuredConverter.ToUnstructured(argo)
415+
if errConvert != nil {
416+
return fmt.Errorf("failed to convert ArgoCD to unstructured for create: %v", errConvert)
417+
}
415418
newArgo := &unstructured.Unstructured{Object: obj}
416419
_, err = client.Resource(gvr).Namespace(namespace).Create(context.TODO(), newArgo, metav1.CreateOptions{})
417420
} else { // update it
@@ -420,7 +423,10 @@ func createOrUpdateArgoCD(client dynamic.Interface, fullClient kubernetes.Interf
420423
return fmt.Errorf("failed to get existing ArgoCD %s/%s: %v", namespace, name, err)
421424
}
422425
argo.SetResourceVersion(oldArgo.GetResourceVersion())
423-
obj, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(argo)
426+
obj, errConvert := runtime.DefaultUnstructuredConverter.ToUnstructured(argo)
427+
if errConvert != nil {
428+
return fmt.Errorf("failed to convert ArgoCD to unstructured for update: %v", errConvert)
429+
}
424430
newArgo := &unstructured.Unstructured{Object: obj}
425431

426432
_, err = client.Resource(gvr).Namespace(namespace).Update(context.TODO(), newArgo, metav1.UpdateOptions{})
@@ -708,9 +714,10 @@ func commonApplicationSourceHelm(p *api.Pattern, prefix string) *argoapi.Applica
708714
valueFiles := newApplicationValueFiles(p, prefix)
709715
sharedValueFiles, err := getSharedValueFiles(p, prefix)
710716
if err != nil {
711-
fmt.Printf("Could not fetch sharedValueFiles: %s", err)
717+
log.Printf("Could not fetch sharedValueFiles: %s", err)
718+
} else {
719+
valueFiles = append(valueFiles, sharedValueFiles...)
712720
}
713-
valueFiles = append(valueFiles, sharedValueFiles...)
714721

715722
return &argoapi.ApplicationSourceHelm{
716723
ValueFiles: valueFiles,
@@ -913,9 +920,12 @@ func getApplication(client argoclient.Interface, name, namespace string) (*argoa
913920

914921
func createApplication(client argoclient.Interface, app *argoapi.Application, namespace string) error {
915922
saved, err := client.ArgoprojV1alpha1().Applications(namespace).Create(context.Background(), app, metav1.CreateOptions{})
923+
if err != nil {
924+
return err
925+
}
916926
yamlOutput, _ := objectYaml(saved)
917927
log.Printf("Created: %s\n", yamlOutput)
918-
return err
928+
return nil
919929
}
920930

921931
func updateApplication(client argoclient.Interface, target, current *argoapi.Application, namespace string) (bool, error) {

internal/controller/pattern_controller.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,11 @@ func (r *PatternReconciler) deleteSpokeApps(targetApp, app *argoapi.Application,
599599
log.Printf("Deletion phase: %s - checking if all child applications are gone from spoke", api.DeleteSpokeChildApps)
600600

601601
// Update application with deletePattern=DeleteSpokeChildApps to trigger spoke child deletion
602-
if changed, _ := updateApplication(r.argoClient, targetApp, app, namespace); changed {
602+
changed, errUpdate := updateApplication(r.argoClient, targetApp, app, namespace)
603+
if errUpdate != nil {
604+
return fmt.Errorf("failed to update application %q for spoke child deletion: %v", app.Name, errUpdate)
605+
}
606+
if changed {
603607
return fmt.Errorf("updated application %q for spoke child deletion", app.Name)
604608
}
605609

@@ -660,7 +664,11 @@ func (r *PatternReconciler) deleteHubApps(targetApp, app *argoapi.Application, n
660664
}
661665

662666
// Update application with deletePattern=DeleteHubChildApps to trigger hub child app deletion
663-
if changed, _ := updateApplication(r.argoClient, targetApp, app, namespace); changed {
667+
changed, errUpdate := updateApplication(r.argoClient, targetApp, app, namespace)
668+
if errUpdate != nil {
669+
return fmt.Errorf("failed to update application %q for hub deletion: %v", app.Name, errUpdate)
670+
}
671+
if changed {
664672
return fmt.Errorf("updated application %q for hub deletion", app.Name)
665673
}
666674

@@ -731,7 +739,11 @@ func (r *PatternReconciler) finalizeObject(instance *api.Pattern) error {
731739

732740
// Phase 2: Delete app of apps from spoke
733741
if qualifiedInstance.Status.DeletionPhase == api.DeleteSpoke {
734-
if changed, _ := updateApplication(r.argoClient, targetApp, app, ns); changed {
742+
changed, errUpdate := updateApplication(r.argoClient, targetApp, app, ns)
743+
if errUpdate != nil {
744+
return fmt.Errorf("failed to update application %q for spoke app of apps deletion: %v", app.Name, errUpdate)
745+
}
746+
if changed {
735747
return fmt.Errorf("updated application %q for spoke app of apps deletion", app.Name)
736748
}
737749

0 commit comments

Comments
 (0)