From d27a5aed7a040935145e6e67bcaef2c4b729c5e9 Mon Sep 17 00:00:00 2001 From: Manabu Mccloskey Date: Tue, 21 Nov 2023 19:00:26 +0000 Subject: [PATCH] implement shutdown --- pkg/controllers/localbuild/controller.go | 25 +++++++++++++++++++++--- pkg/controllers/localbuild/installer.go | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/controllers/localbuild/controller.go b/pkg/controllers/localbuild/controller.go index cc9f06ea..c87f3601 100644 --- a/pkg/controllers/localbuild/controller.go +++ b/pkg/controllers/localbuild/controller.go @@ -299,9 +299,13 @@ func (r *LocalbuildReconciler) ReconcileArgoAppsWithGitea(ctx context.Context, r } } - // TODO: this needs to be removed for local file syncs. - r.shouldShutdown = true - return ctrl.Result{}, nil + shutdown, err := r.shouldShutDown(ctx, resource) + if err != nil { + return ctrl.Result{Requeue: true}, err + } + r.shouldShutdown = shutdown + + return ctrl.Result{RequeueAfter: time.Second * 30}, nil } func (r *LocalbuildReconciler) reconcileEmbeddedApp(ctx context.Context, appName string, resource *v1alpha1.Localbuild) (ctrl.Result, error) { @@ -366,6 +370,21 @@ func (r *LocalbuildReconciler) reconcileEmbeddedApp(ctx context.Context, appName return ctrl.Result{}, nil } +func (r *LocalbuildReconciler) shouldShutDown(ctx context.Context, resource *v1alpha1.Localbuild) (bool, error) { + repos := &v1alpha1.GitRepositoryList{} + err := r.Client.List(ctx, repos, client.InNamespace(resource.Namespace)) + if err != nil { + return false, fmt.Errorf("getting repo list %w", err) + } + for i := range repos.Items { + repo := repos.Items[i] + if !repo.Status.Synced { + return false, nil + } + } + return true, nil +} + func GetEmbeddedRawInstallResources(name string) ([][]byte, error) { switch name { case "argocd": diff --git a/pkg/controllers/localbuild/installer.go b/pkg/controllers/localbuild/installer.go index 74277885..36298a51 100644 --- a/pkg/controllers/localbuild/installer.go +++ b/pkg/controllers/localbuild/installer.go @@ -86,7 +86,7 @@ func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, re _ = appsv1.AddToScheme(sch) if gvkObj, err := sch.New(gvk); err == nil { if gotObj, ok := gvkObj.(client.Object); ok { - if err := cli.Get(ctx, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, gotObj); err != nil { + if err := cli.Get(ctx, types.NamespacedName{Namespace: e.namespace, Name: obj.GetName()}, gotObj); err != nil { if err = controllerutil.SetControllerReference(resource, obj, sc); err != nil { log.Error(err, "Setting controller reference for deployment", obj.GetName(), obj) return ctrl.Result{}, err @@ -132,7 +132,7 @@ func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, re for { if gotObj, ok := gvkObj.(client.Object); ok { - if err := cli.Get(ctx, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, gotObj); err != nil { + if err := cli.Get(ctx, types.NamespacedName{Namespace: e.namespace, Name: obj.GetName()}, gotObj); err != nil { errCh <- err return }