diff --git a/pkg/cmd/get/secrets.go b/pkg/cmd/get/secrets.go index f0449c2b..cd97ce0b 100644 --- a/pkg/cmd/get/secrets.go +++ b/pkg/cmd/get/secrets.go @@ -58,10 +58,11 @@ func getSecretsE(cmd *cobra.Command, args []string) error { defer ctxCancel() kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config") - opts := build.NewBuildOptions{} - opts.KubeConfigPath = kubeConfigPath - opts.Scheme = k8s.GetScheme() - opts.CancelFunc = ctxCancel + opts := build.NewBuildOptions{ + KubeConfigPath: kubeConfigPath, + Scheme: k8s.GetScheme(), + CancelFunc: ctxCancel, + } b := build.NewBuild(opts) diff --git a/pkg/cmd/helpers/validation.go b/pkg/cmd/helpers/validation.go index 6c9cea51..87a9e42f 100644 --- a/pkg/cmd/helpers/validation.go +++ b/pkg/cmd/helpers/validation.go @@ -36,8 +36,7 @@ func ValidateKubernetesYamlFile(absPath string) error { } func ParsePackageStrings(pkgStrings []string) ([]string, []string, error) { - remote := make([]string, 0, 2) - local := make([]string, 0, 2) + remote, local := make([]string, 0, 2), make([]string, 0, 2) for i := range pkgStrings { loc := pkgStrings[i] _, err := util.NewKustomizeRemote(loc) diff --git a/pkg/controllers/custompackage/controller.go b/pkg/controllers/custompackage/controller.go index 3a701f7e..b53d9f1e 100644 --- a/pkg/controllers/custompackage/controller.go +++ b/pkg/controllers/custompackage/controller.go @@ -281,19 +281,19 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { } func (r *Reconciler) getArgoCDAppFile(ctx context.Context, resource *v1alpha1.CustomPackage) ([]byte, error) { - if resource.Spec.RemoteRepository.Url != "" { - cloneDir := util.RepoDir(resource.Spec.RemoteRepository.Url, r.TempDir) - st := r.RepoMap.LoadOrStore(resource.Spec.RemoteRepository.Url, cloneDir) - st.MU.Lock() - wt, _, err := util.CloneRemoteRepoToDir(ctx, resource.Spec.RemoteRepository, 1, false, cloneDir, "") - defer st.MU.Unlock() - if err != nil { - return nil, fmt.Errorf("cloning repo, %s: %w", resource.Spec.RemoteRepository.Url, err) - } - return util.ReadWorktreeFile(wt, resource.Spec.ArgoCD.ApplicationFile) + if resource.Spec.RemoteRepository.Url == "" { + return os.ReadFile(resource.Spec.ArgoCD.ApplicationFile) } - return os.ReadFile(resource.Spec.ArgoCD.ApplicationFile) + cloneDir := util.RepoDir(resource.Spec.RemoteRepository.Url, r.TempDir) + st := r.RepoMap.LoadOrStore(resource.Spec.RemoteRepository.Url, cloneDir) + st.MU.Lock() + wt, _, err := util.CloneRemoteRepoToDir(ctx, resource.Spec.RemoteRepository, 1, false, cloneDir, "") + defer st.MU.Unlock() + if err != nil { + return nil, fmt.Errorf("cloning repo, %s: %w", resource.Spec.RemoteRepository.Url, err) + } + return util.ReadWorktreeFile(wt, resource.Spec.ArgoCD.ApplicationFile) } func localRepoName(appName, dir string) string { diff --git a/pkg/controllers/gitrepository/gitea.go b/pkg/controllers/gitrepository/gitea.go index 6dcdcb1b..2a9db6c9 100644 --- a/pkg/controllers/gitrepository/gitea.go +++ b/pkg/controllers/gitrepository/gitea.go @@ -98,7 +98,14 @@ func (g *giteaProvider) getRepository(ctx context.Context, repo *v1alpha1.GitRep }, nil } -func (g *giteaProvider) updateRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials, tmpDir string, repoMap *util.RepoMap) error { +func (g *giteaProvider) updateRepoContent( + ctx context.Context, + repo *v1alpha1.GitRepository, + repoInfo repoInfo, + creds gitProviderCredentials, + tmpDir string, + repoMap *util.RepoMap, +) error { switch repo.Spec.Source.Type { case v1alpha1.SourceTypeLocal, v1alpha1.SourceTypeEmbedded: return reconcileLocalRepoContent(ctx, repo, repoInfo, creds, g.Scheme, g.config, tmpDir, repoMap) diff --git a/pkg/controllers/gitrepository/github.go b/pkg/controllers/gitrepository/github.go index 58f129c5..2a2ac0c3 100644 --- a/pkg/controllers/gitrepository/github.go +++ b/pkg/controllers/gitrepository/github.go @@ -102,7 +102,14 @@ func (g *gitHubProvider) setProviderCredentials(ctx context.Context, repo *v1alp return g.gitHubClient.setToken(creds.accessToken) } -func (g *gitHubProvider) updateRepoContent(ctx context.Context, repo *v1alpha1.GitRepository, repoInfo repoInfo, creds gitProviderCredentials, tmpDir string, repoMap *util.RepoMap) error { +func (g *gitHubProvider) updateRepoContent( + ctx context.Context, + repo *v1alpha1.GitRepository, + repoInfo repoInfo, + creds gitProviderCredentials, + tmpDir string, + repoMap *util.RepoMap, +) error { return reconcileLocalRepoContent(ctx, repo, repoInfo, creds, g.Scheme, g.config, tmpDir, repoMap) } diff --git a/pkg/controllers/localbuild/controller.go b/pkg/controllers/localbuild/controller.go index 700e2035..cd867b14 100644 --- a/pkg/controllers/localbuild/controller.go +++ b/pkg/controllers/localbuild/controller.go @@ -8,8 +8,10 @@ import ( "strings" "time" + argocdapp "github.com/cnoe-io/argocd-api/api/argo/application" "github.com/cnoe-io/idpbuilder/pkg/util" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" argov1alpha1 "github.com/cnoe-io/argocd-api/api/argo/application/v1alpha1" "github.com/cnoe-io/idpbuilder/api/v1alpha1" @@ -142,8 +144,8 @@ func (r *LocalbuildReconciler) ReconcileArgoAppsWithGitea(ctx context.Context, r } } - for i := range resource.Spec.PackageConfigs.CustomPackageDirs { - result, err := r.reconcileCustomPkgDir(ctx, resource, resource.Spec.PackageConfigs.CustomPackageDirs[i]) + for _, s := range resource.Spec.PackageConfigs.CustomPackageDirs { + result, err := r.reconcileCustomPkgDir(ctx, resource, s) if err != nil { return result, err } @@ -318,11 +320,10 @@ func (r *LocalbuildReconciler) reconcileCustomPkgUrl(ctx context.Context, resour return ctrl.Result{}, fmt.Errorf("getting yaml files from repo, %s: %w", pkgUrl, err) } - for i := range yamlFiles { - n := yamlFiles[i] - b, fErr := util.ReadWorktreeFile(wt, n) + for _, yamlFile := range yamlFiles { + b, fErr := util.ReadWorktreeFile(wt, yamlFile) if fErr != nil { - logger.V(1).Info("processing", "file", n, "err", fErr) + logger.V(1).Info("processing", "file", yamlFile, "err", fErr) continue } @@ -332,12 +333,12 @@ func (r *LocalbuildReconciler) reconcileCustomPkgUrl(ctx context.Context, resour continue } - if gvk.Kind == "Application" && gvk.Group == "argoproj.io" { + if isSupportedArgoCDTypes(gvk) { appName := o.GetName() appNS := o.GetNamespace() customPkg := &v1alpha1.CustomPackage{ ObjectMeta: metav1.ObjectMeta{ - Name: getCustomPackageName(filepath.Base(n), appName), + Name: getCustomPackageName(filepath.Base(yamlFile), appName), Namespace: globals.GetProjectNamespace(resource.Name), }, } @@ -366,7 +367,7 @@ func (r *LocalbuildReconciler) reconcileCustomPkgUrl(ctx context.Context, resour Namespace: resource.Status.Gitea.AdminUserSecretNamespace, }, ArgoCD: v1alpha1.ArgoCDPackageSpec{ - ApplicationFile: n, + ApplicationFile: yamlFile, Name: appName, Namespace: appNS, }, @@ -414,7 +415,7 @@ func (r *LocalbuildReconciler) reconcileCustomPkgDir(ctx context.Context, resour if fErr != nil { continue } - if gvk.Kind == "Application" && gvk.Group == "argoproj.io" { + if isSupportedArgoCDTypes(gvk) { appName := o.GetName() appNS := o.GetNamespace() customPkg := &v1alpha1.CustomPackage{ @@ -527,6 +528,13 @@ func getCustomPackageName(fileName, appName string) string { return fmt.Sprintf("%s-%s", strings.ToLower(s[0]), appName) } +func isSupportedArgoCDTypes(gvk *schema.GroupVersionKind) bool { + if gvk == nil { + return false + } + return gvk.Kind == argocdapp.ApplicationSingular && gvk.Group == argocdapp.Group +} + func GetEmbeddedRawInstallResources(name string, templateData any, config v1alpha1.PackageCustomization, scheme *runtime.Scheme) ([][]byte, error) { switch name { case "argocd": diff --git a/pkg/controllers/run.go b/pkg/controllers/run.go index 2df33c3b..19eae91f 100644 --- a/pkg/controllers/run.go +++ b/pkg/controllers/run.go @@ -12,7 +12,15 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" ) -func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, ctxCancel context.CancelFunc, exitOnSync bool, cfg util.CorePackageTemplateConfig, tmpDir string) error { +func RunControllers( + ctx context.Context, + mgr manager.Manager, + exitCh chan error, + ctxCancel context.CancelFunc, + exitOnSync bool, + cfg util.CorePackageTemplateConfig, + tmpDir string, +) error { logger := log.FromContext(ctx) repoMap := util.NewRepoLock()