Skip to content

Commit

Permalink
address some review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey committed May 23, 2024
1 parent 774391a commit 1a3b020
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 30 deletions.
9 changes: 5 additions & 4 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/helpers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion pkg/controllers/gitrepository/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion pkg/controllers/gitrepository/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
28 changes: 18 additions & 10 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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),
},
}
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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":
Expand Down
10 changes: 9 additions & 1 deletion pkg/controllers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1a3b020

Please sign in to comment.