Skip to content

Commit

Permalink
add a flag to specify if the cli should exit once packages are synced
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu Mccloskey <[email protected]>
  • Loading branch information
nabuskey committed Dec 14, 2023
1 parent a9b2378 commit 44db422
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha1/custom_package_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type ArgoCDPackageSpec struct {
}

type CustomPackageStatus struct {
// A Custom package is considered synced when the in-cluster repository url is set as the repository URL
// This only applies for a package that references local directories
Synced bool `json:"synced,omitempty"`
GitRepositoryRefs []ObjectRef `json:"gitRepositoryRefs,omitempty"`
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"context"
"fmt"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/globals"
"github.com/cnoe-io/idpbuilder/pkg/controllers"
Expand All @@ -28,18 +29,20 @@ type Build struct {
kubeVersion string
extraPortsMapping string
customPackageDirs []string
exitOnSync bool
scheme *runtime.Scheme
CancelFunc context.CancelFunc
}

func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, customPackageDirs []string, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, customPackageDirs []string, exitOnSync bool, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
return &Build{
name: name,
kindConfigPath: kindConfigPath,
kubeConfigPath: kubeConfigPath,
kubeVersion: kubeVersion,
extraPortsMapping: extraPortsMapping,
customPackageDirs: customPackageDirs,
exitOnSync: exitOnSync,
scheme: scheme,
CancelFunc: ctxCancel,
}
Expand Down Expand Up @@ -95,7 +98,7 @@ func (b *Build) ReconcileCRDs(ctx context.Context, kubeClient client.Client) err
}

func (b *Build) RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error) error {
return controllers.RunControllers(ctx, mgr, exitCh, b.CancelFunc)
return controllers.RunControllers(ctx, mgr, exitCh, b.CancelFunc, b.exitOnSync)
}

func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
Expand Down
11 changes: 9 additions & 2 deletions pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
extraPortsMapping string
kindConfigPath string
extraPackagesDirs []string
noExit bool
)

var CreateCmd = &cobra.Command{
Expand All @@ -39,7 +40,8 @@ func init() {
CreateCmd.PersistentFlags().StringVar(&kubeVersion, "kubeVersion", "v1.26.3", "Version of the kind kubernetes cluster to create.")
CreateCmd.PersistentFlags().StringVar(&extraPortsMapping, "extraPorts", "", "List of extra ports to expose on the docker container and kubernetes cluster as nodePort (e.g. \"22:32222,9090:39090,etc\").")
CreateCmd.PersistentFlags().StringVar(&kindConfigPath, "kindConfig", "", "Path of the kind config file to be used instead of the default.")
CreateCmd.Flags().StringSliceVarP(&extraPackagesDirs, "package-dir", "p", []string{}, "paths to custom packages")
CreateCmd.Flags().StringSliceVarP(&extraPackagesDirs, "package-dir", "p", []string{}, "Paths to custom packages")
CreateCmd.Flags().BoolVarP(&noExit, "no-exit", "n", true, "When set, idpbuilder will not exit after all packages are synced. Useful for continuously syncing local directories.")

zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := zap.Options{
Expand Down Expand Up @@ -71,7 +73,12 @@ func create(cmd *cobra.Command, args []string) error {
absDirPaths = p
}

b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping, absDirPaths, k8s.GetScheme(), ctxCancel)
exitOnSync := true
if cmd.Flags().Changed("no-exit") {
exitOnSync = !noExit
}

b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping, absDirPaths, exitOnSync, k8s.GetScheme(), ctxCancel)

if err := b.Run(ctx, recreateCluster); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/gitserver/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func create(cmd *cobra.Command, args []string) error {

// Run controllers
managerExit := make(chan error)
if err := controllers.RunControllers(ctx, mgr, managerExit, ctxCancel); err != nil {
if err := controllers.RunControllers(ctx, mgr, managerExit, ctxCancel, true); err != nil {
setupLog.Error(err, "Running controllers")
return err
}
Expand Down
15 changes: 11 additions & 4 deletions pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package custompackage
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"

argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"os"
"path/filepath"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -82,6 +83,7 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
appName := app.GetName()
if resource.Spec.Replicate {
repoRefs := make([]v1alpha1.ObjectRef, 0, 1)
synced := true
if app.Spec.HasMultipleSources() {
for j := range app.Spec.Sources {
s := app.Spec.Sources[j]
Expand All @@ -90,6 +92,9 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
return res, sErr
}
if repo != nil {
if synced {
synced = repo.Status.InternalGitRepositoryUrl != ""
}
s.RepoURL = repo.Status.InternalGitRepositoryUrl
repoRefs = append(repoRefs, v1alpha1.ObjectRef{
Namespace: repo.Namespace,
Expand All @@ -105,6 +110,7 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
return res, sErr
}
if repo != nil {
synced = repo.Status.InternalGitRepositoryUrl != ""
s.RepoURL = repo.Status.InternalGitRepositoryUrl
repoRefs = append(repoRefs, v1alpha1.ObjectRef{
Namespace: repo.Namespace,
Expand All @@ -114,6 +120,7 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
}
}
resource.Status.GitRepositoryRefs = repoRefs
resource.Status.Synced = synced
}

foundAppObj := argov1alpha1.Application{}
Expand Down
23 changes: 20 additions & 3 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package localbuild
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"os"
"path/filepath"
"strings"
"time"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/globals"
Expand Down Expand Up @@ -51,6 +52,7 @@ type LocalbuildReconciler struct {
client.Client
Scheme *runtime.Scheme
CancelFunc context.CancelFunc
ExitOnSync bool
shouldShutdown bool
}

Expand Down Expand Up @@ -375,20 +377,35 @@ func (r *LocalbuildReconciler) reconcileEmbeddedApp(ctx context.Context, appName
}

func (r *LocalbuildReconciler) shouldShutDown(ctx context.Context, resource *v1alpha1.Localbuild) (bool, error) {
if len(resource.Spec.PackageConfigs.CustomPackageDirs) > 0 {
if !r.ExitOnSync {
return false, nil
}

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)
return false, fmt.Errorf("listing repositories %w", err)
}
for i := range repos.Items {
repo := repos.Items[i]
if !repo.Status.Synced {
return false, nil
}
}

pkgs := &v1alpha1.CustomPackageList{}
err = r.Client.List(ctx, pkgs, client.InNamespace(resource.Namespace))
if err != nil {
return false, fmt.Errorf("listing custom packages %w", err)
}

for i := range pkgs.Items {
pkg := pkgs.Items[i]
if !pkg.Status.Synced {
return false, nil
}
}

return true, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ spec:
type: object
type: array
synced:
description: A Custom package is considered synced when the in-cluster
repository url is set as the repository URL This only applies for
a package that references local directories
type: boolean
type: object
type: object
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"

"github.com/cnoe-io/idpbuilder/pkg/controllers/custompackage"

"github.com/cnoe-io/idpbuilder/pkg/controllers/gitrepository"
Expand All @@ -10,13 +11,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
)

func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, ctxCancel context.CancelFunc) error {
func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, ctxCancel context.CancelFunc, exitOnSync bool) error {
log := log.FromContext(ctx)

// Run Localbuild controller
if err := (&localbuild.LocalbuildReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ExitOnSync: exitOnSync,
CancelFunc: ctxCancel,
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create localbuild controller")
Expand Down

0 comments on commit 44db422

Please sign in to comment.