Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom packages #100

Merged
merged 7 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions api/v1alpha1/custom_package_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
type CustomPackage struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec CustomPackageSpec `json:"spec,omitempty"`
Status CustomPackageStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true
type CustomPackageList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []CustomPackage `json:"items"`
}

// CustomPackageSpec controls the installation of the custom applications.
type CustomPackageSpec struct {
// Replicate specifies whether to replicate remote or local contents to the local gitea server.
// +kubebuilder:default:=false
Replicate bool `json:"replicate"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be defaulted to something, no? I assume it is probably a no by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it defaults to false via zero value. I'll add an annotation to be explicit.

// GitServerURL specifies the base URL for the git server for API calls.
// for example, http://gitea.cnoe.localtest.me:8880
GitServerURL string `json:"gitServerURL"`
// InternalGitServeURL specifies the base URL for the git server accessible within the cluster.
// for example, http://my-gitea-http.gitea.svc.cluster.local:3000
InternalGitServeURL string `json:"internalGitServeURL"`
GitServerAuthSecretRef SecretReference `json:"gitServerAuthSecretRef"`

ArgoCD ArgoCDPackageSpec `json:"argoCD,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PackageSpec maybe? this leaves room for us to hook in other packaging tools later on too, without having to changeg the spec

}

type ArgoCDPackageSpec struct {
// ApplicationFile specifies the absolute path to the ArgoCD application file
ApplicationFile string `json:"applicationFile"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}

type CustomPackageStatus struct {
Synced bool `json:"synced,omitempty"`
GitRepositoryRefs []ObjectRef `json:"gitRepositoryRefs,omitempty"`
}

type ObjectRef struct {
APIVersion string `json:"apiVersion,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Kind string `json:"kind,omitempty"`
UID string `json:"uid,omitempty"`
}
4 changes: 3 additions & 1 deletion api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type GitRepositorySpec struct {
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern=`^https?:\/\/.+$`
GitURL string `json:"gitURL"`
// InternalGitURL is the base URL of Git server accessible within the cluster only.
InternalGitURL string `json:"internalGitURL"`
// SecretRef is the reference to secret that contain Git server credentials
// +kubebuilder:validation:Optional
SecretRef SecretReference `json:"secretRef"`
Expand All @@ -18,7 +20,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
1 change: 1 addition & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ func init() {
SchemeBuilder.Register(&Localbuild{}, &LocalbuildList{})
SchemeBuilder.Register(&GitServer{}, &GitServerList{})
SchemeBuilder.Register(&GitRepository{}, &GitRepositoryList{})
SchemeBuilder.Register(&CustomPackage{}, &CustomPackageList{})
}
1 change: 1 addition & 0 deletions api/v1alpha1/localbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type PackageConfigsSpec struct {
GitConfig GitConfigSpec `json:"gitConfig,omitempty"`
Argo ArgoPackageConfigSpec `json:"argoPackageConfigs,omitempty"`
EmbeddedArgoApplications EmbeddedArgoApplicationsPackageConfigSpec `json:"embeddedArgoApplicationsPackageConfigs,omitempty"`
CustomPackageDirs []string `json:"customPackageDirs,omitempty"`
}

type LocalbuildSpec struct {
Expand Down
135 changes: 133 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.

10 changes: 8 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 @@ -158,10 +160,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(),
},
CustomPackageDirs: b.customPackageDirs,
},
}
return nil
})
if err != nil {
return fmt.Errorf("creating localbuild resource: %w", err)
}

if err != nil {
setupLog.Error(err, "Error creating localbuild resource")
Expand Down
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 absDirPaths []string
if len(extraPackagesDirs) > 0 {
p, err := getPackageAbsDirs(extraPackagesDirs)
if err != nil {
return err
}
absDirPaths = p
}

b := build.NewBuild(buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping, absDirPaths, 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