Skip to content

Commit

Permalink
allow for custom packages to be installed
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu Mccloskey <[email protected]>
  • Loading branch information
nabuskey committed Dec 5, 2023
1 parent 9084ef8 commit e81f85c
Show file tree
Hide file tree
Showing 12 changed files with 534 additions and 30 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GitRepositorySpec struct {
type GitRepositorySource struct {
// +kubebuilder:validation:Enum:=argocd;backstage;crossplane;gitea;nginx
// +kubebuilder:validation:Optional
EmbeddedAppName string `json:"embeddedAppName"`
EmbeddedAppName string `json:"embeddedAppName,omitempty"`
// Path is the absolute path to directory that contains Kustomize structure or raw manifests.
// This is required when Type is set to local.
// +kubebuilder:validation:Optional
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/localbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type PackageConfigsSpec struct {
GitConfig GitConfigSpec `json:"gitConfig,omitempty"`
Argo ArgoPackageConfigSpec `json:"argoPackageConfigs,omitempty"`
EmbeddedArgoApplications EmbeddedArgoApplicationsPackageConfigSpec `json:"embeddedArgoApplicationsPackageConfigs,omitempty"`
CustomPackages []CustomPackageSpec `json:"customPackages,omitempty"`
}

// CustomPackageSpec controls the installation of the custom argocd applications.
type CustomPackageSpec struct {
// Directory specifies the absolute path to the directory which contains ArgoCD Application manifests
Directory string `json:"directory,omitempty"`
}

type LocalbuildSpec struct {
Expand Down
24 changes: 22 additions & 2 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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 @@ -27,17 +27,19 @@ type Build struct {
kubeConfigPath string
kubeVersion string
extraPortsMapping string
customPackageDirs []string
scheme *runtime.Scheme
CancelFunc context.CancelFunc
}

func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, customPackageDirs []string, scheme *runtime.Scheme, ctxCancel context.CancelFunc) *Build {
return &Build{
name: name,
kindConfigPath: kindConfigPath,
kubeConfigPath: kubeConfigPath,
kubeVersion: kubeVersion,
extraPortsMapping: extraPortsMapping,
customPackageDirs: customPackageDirs,
scheme: scheme,
CancelFunc: ctxCancel,
}
Expand Down Expand Up @@ -144,6 +146,8 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
},
}

pkgs, err := getPackages(b.customPackageDirs)

setupLog.Info("Creating localbuild resource")
_, err = controllerutil.CreateOrUpdate(ctx, kubeClient, &localBuild, func() error {
localBuild.Spec = v1alpha1.LocalbuildSpec{
Expand All @@ -158,10 +162,14 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
// hint: for the old behavior, replace Type value below with globals.GitServerResourcename()
Type: globals.GiteaResourceName(),
},
CustomPackages: pkgs,
},
}
return nil
})
if err != nil {
return fmt.Errorf("creating localbuild resource: %w", err)
}

if err != nil {
setupLog.Error(err, "Error creating localbuild resource")
Expand All @@ -172,3 +180,12 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
close(managerExit)
return err
}

func getPackages(srcDirs []string) ([]v1alpha1.CustomPackageSpec, error) {
out := make([]v1alpha1.CustomPackageSpec, len(srcDirs), len(srcDirs))
for i := range srcDirs {
out[i] = v1alpha1.CustomPackageSpec{Directory: srcDirs[i]}
}

return out, nil
}
34 changes: 33 additions & 1 deletion pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
kubeVersion string
extraPortsMapping string
kindConfigPath string
extraPackagesDirs []string
)

var CreateCmd = &cobra.Command{
Expand All @@ -38,6 +39,7 @@ 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")

zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
opts := zap.Options{
Expand All @@ -60,10 +62,40 @@ func create(cmd *cobra.Command, args []string) error {
os.Exit(1)
}

b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping, k8s.GetScheme(), ctxCancel)
var absPaths []string
if len(extraPackagesDirs) > 0 {
p, err := getPackageAbsDirs(extraPackagesDirs)
if err != nil {
return err
}
absPaths = p
}

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

if err := b.Run(ctx, recreateCluster); err != nil {
return err
}
return nil
}

func getPackageAbsDirs(paths []string) ([]string, error) {
out := make([]string, len(paths), len(paths))
for i := range paths {
path := paths[i]
absPath, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("failed to validate path %s : %w", path, err)
}
f, err := os.Stat(absPath)
if err != nil {
return nil, fmt.Errorf("failed to validate path %s : %w", absPath, err)
}
if !f.IsDir() {
return nil, fmt.Errorf("given path is not a directory. %s", absPath)
}
out[i] = absPath
}

return out, nil
}
Loading

0 comments on commit e81f85c

Please sign in to comment.