Skip to content

Commit 9506032

Browse files
Merge pull request #538 from darkdoc/fix-cardill-issue
fix cardill issue
2 parents 2231850 + c37f25a commit 9506032

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

internal/controller/argo.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,20 +411,31 @@ 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
418-
oldArgo, _ := getArgoCD(client, name, namespace)
421+
oldArgo, errGet := getArgoCDFunc(client, name, namespace)
422+
if errGet != nil {
423+
return fmt.Errorf("failed to get existing ArgoCD %s/%s: %v", namespace, name, errGet)
424+
}
419425
argo.SetResourceVersion(oldArgo.GetResourceVersion())
420-
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+
}
421430
newArgo := &unstructured.Unstructured{Object: obj}
422431

423432
_, err = client.Resource(gvr).Namespace(namespace).Update(context.TODO(), newArgo, metav1.UpdateOptions{})
424433
}
425434
return err
426435
}
427436

437+
var getArgoCDFunc = getArgoCD
438+
428439
func getArgoCD(client dynamic.Interface, name, namespace string) (*argooperator.ArgoCD, error) {
429440
gvr := schema.GroupVersionResource{Group: ArgoCDGroup, Version: ArgoCDVersion, Resource: ArgoCDResource}
430441
argo := &argooperator.ArgoCD{}
@@ -705,9 +716,10 @@ func commonApplicationSourceHelm(p *api.Pattern, prefix string) *argoapi.Applica
705716
valueFiles := newApplicationValueFiles(p, prefix)
706717
sharedValueFiles, err := getSharedValueFiles(p, prefix)
707718
if err != nil {
708-
fmt.Printf("Could not fetch sharedValueFiles: %s", err)
719+
log.Printf("Could not fetch sharedValueFiles: %s", err)
720+
} else {
721+
valueFiles = append(valueFiles, sharedValueFiles...)
709722
}
710-
valueFiles = append(valueFiles, sharedValueFiles...)
711723

712724
return &argoapi.ApplicationSourceHelm{
713725
ValueFiles: valueFiles,
@@ -910,9 +922,12 @@ func getApplication(client argoclient.Interface, name, namespace string) (*argoa
910922

911923
func createApplication(client argoclient.Interface, app *argoapi.Application, namespace string) error {
912924
saved, err := client.ArgoprojV1alpha1().Applications(namespace).Create(context.Background(), app, metav1.CreateOptions{})
925+
if err != nil {
926+
return err
927+
}
913928
yamlOutput, _ := objectYaml(saved)
914929
log.Printf("Created: %s\n", yamlOutput)
915-
return err
930+
return nil
916931
}
917932

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

internal/controller/argo_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,40 @@ var _ = Describe("CreateOrUpdateArgoCD", func() {
998998
Expect(argoCD.GetResourceVersion()).To(Equal("1")) // Ensure it has been updated
999999
})
10001000
})
1001+
1002+
Context("when there is an error in the getArgoCD fn but there is an argocd", func() {
1003+
1004+
BeforeEach(func() {
1005+
argoCD := &unstructured.Unstructured{
1006+
Object: map[string]any{
1007+
"apiVersion": "argoproj.io/v1beta1",
1008+
"kind": "ArgoCD",
1009+
"metadata": map[string]any{
1010+
"name": name,
1011+
"namespace": namespace,
1012+
"resourceVersion": "1",
1013+
},
1014+
},
1015+
}
1016+
_, err := dynamicClient.Resource(gvr).Namespace(namespace).Create(context.TODO(), argoCD, metav1.CreateOptions{})
1017+
Expect(err).ToNot(HaveOccurred())
1018+
getArgoCDFunc = func(_ dynamic.Interface, _, _ string) (*argooperator.ArgoCD, error) {
1019+
return nil, fmt.Errorf("forced error")
1020+
}
1021+
})
1022+
1023+
It("should propagate the error and not update the existing argocd", func() {
1024+
err := createOrUpdateArgoCD(dynamicClient, nil, name, namespace)
1025+
Expect(err).To(HaveOccurred())
1026+
1027+
argoCD, err := dynamicClient.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
1028+
1029+
Expect(err).NotTo(HaveOccurred())
1030+
Expect(argoCD).ToNot(BeNil())
1031+
Expect(argoCD.GetResourceVersion()).To(Equal("1"))
1032+
1033+
})
1034+
})
10011035
})
10021036

10031037
var _ = Describe("CompareApplication", func() {

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)