Skip to content

Commit

Permalink
support remote repository
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey committed May 14, 2024
1 parent 830c5ea commit b64b627
Show file tree
Hide file tree
Showing 23 changed files with 1,046 additions and 146 deletions.
17 changes: 16 additions & 1 deletion api/v1alpha1/custom_package_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
CNOEURIScheme = "cnoe://"
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
type CustomPackage struct {
Expand All @@ -30,12 +34,23 @@ type CustomPackageSpec struct {
GitServerAuthSecretRef SecretReference `json:"gitServerAuthSecretRef"`
// 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"`
InternalGitServeURL string `json:"internalGitServeURL"`
RemoteRepository RemoteRepositorySpec `json:"remoteRepository"`
// Replicate specifies whether to replicate remote or local contents to the local gitea server.
// +kubebuilder:default:=false
Replicate bool `json:"replicate"`
}

// RemoteRepositorySpec specifies information about remote repositories.
type RemoteRepositorySpec struct {
CloneSubmodules bool `json:"cloneSubmodules"`
Path string `json:"path"`
// Url specifies the url to the repository containing the ArgoCD application file
Url string `json:"url"`
// Ref specifies the specific ref supported by git fetch
Ref string `json:"ref"`
}

type ArgoCDPackageSpec struct {
// ApplicationFile specifies the absolute path to the ArgoCD application file
ApplicationFile string `json:"applicationFile"`
Expand Down
8 changes: 6 additions & 2 deletions api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const (
GitProviderGitea = "gitea"
GitProviderGitHub = "github"
GiteaAdminUserName = "giteaAdmin"
SourceTypeLocal = "local"
SourceTypeRemote = "remote"
SourceTypeEmbedded = "embedded"
)

type GitRepositorySpec struct {
Expand All @@ -27,9 +30,10 @@ type GitRepositorySource struct {
// 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
Path string `json:"path"`
Path string `json:"path"`
RemoteRepository RemoteRepositorySpec `json:"remoteRepository"`
// Type is the source type.
// +kubebuilder:validation:Enum:=local;embedded
// +kubebuilder:validation:Enum:=local;embedded;remote
// +kubebuilder:default:=embedded
Type string `json:"type"`
}
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/localbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type PackageConfigsSpec struct {
Argo ArgoPackageConfigSpec `json:"argoPackageConfigs,omitempty"`
EmbeddedArgoApplications EmbeddedArgoApplicationsPackageConfigSpec `json:"embeddedArgoApplicationsPackageConfigs,omitempty"`
CustomPackageDirs []string `json:"customPackageDirs,omitempty"`
CustomPackageUrls []string `json:"customPackageUrls,omitempty"`
}

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

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

43 changes: 29 additions & 14 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,42 @@ type Build struct {
kubeVersion string
extraPortsMapping string
customPackageDirs []string
customPackageUrls []string
packageCustomization map[string]v1alpha1.PackageCustomization
exitOnSync bool
scheme *runtime.Scheme
CancelFunc context.CancelFunc
}

func NewBuild(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, cfg util.CorePackageTemplateConfig,
customPackageDirs []string, exitOnSync bool, scheme *runtime.Scheme, ctxCancel context.CancelFunc,
packageCustomization map[string]v1alpha1.PackageCustomization) *Build {
type NewBuildOptions struct {
Name string
TemplateData util.CorePackageTemplateConfig
KindConfigPath string
KubeConfigPath string
KubeVersion string
ExtraPortsMapping string
CustomPackageDirs []string
CustomPackageUrls []string
PackageCustomization map[string]v1alpha1.PackageCustomization
ExitOnSync bool
Scheme *runtime.Scheme
CancelFunc context.CancelFunc
}

func NewBuild(opts NewBuildOptions) *Build {
return &Build{
name: name,
kindConfigPath: kindConfigPath,
kubeConfigPath: kubeConfigPath,
kubeVersion: kubeVersion,
extraPortsMapping: extraPortsMapping,
customPackageDirs: customPackageDirs,
packageCustomization: packageCustomization,
exitOnSync: exitOnSync,
scheme: scheme,
cfg: cfg,
CancelFunc: ctxCancel,
name: opts.Name,
kindConfigPath: opts.KindConfigPath,
kubeConfigPath: opts.KubeConfigPath,
kubeVersion: opts.KubeVersion,
extraPortsMapping: opts.ExtraPortsMapping,
customPackageDirs: opts.CustomPackageDirs,
customPackageUrls: opts.CustomPackageUrls,
packageCustomization: opts.PackageCustomization,
exitOnSync: opts.ExitOnSync,
scheme: opts.Scheme,
cfg: opts.TemplateData,
CancelFunc: opts.CancelFunc,
}
}

Expand Down Expand Up @@ -178,6 +192,7 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
PackageCustomization: b.packageCustomization,
},
CustomPackageDirs: b.customPackageDirs,
CustomPackageUrls: b.customPackageUrls,
},
}

Expand Down
39 changes: 29 additions & 10 deletions pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ func create(cmd *cobra.Command, args []string) error {
}

var absDirPaths []string
var remotePaths []string

if len(extraPackagesDirs) > 0 {
p, err := helpers.GetAbsFilePaths(extraPackagesDirs, true)
if err != nil {
return err
r, l, pErr := helpers.ParsePackageStrings(extraPackagesDirs)
if pErr != nil {
return pErr
}
absDirPaths = p
absDirPaths = l
remotePaths = r
}

o := make(map[string]v1alpha1.PackageCustomization)
Expand All @@ -106,17 +109,31 @@ func create(cmd *cobra.Command, args []string) error {
exitOnSync = !noExit
}

b := build.NewBuild(
buildName, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping,
util.CorePackageTemplateConfig{
opts := build.NewBuildOptions{
Name: buildName,
KubeVersion: kubeVersion,
KubeConfigPath: kubeConfigPath,
KindConfigPath: kindConfigPath,
ExtraPortsMapping: extraPortsMapping,

TemplateData: util.CorePackageTemplateConfig{
Protocol: protocol,
Host: host,
IngressHost: ingressHost,
Port: port,
UsePathRouting: pathRouting,
},
absDirPaths, exitOnSync, k8s.GetScheme(), ctxCancel, o,
)

CustomPackageDirs: absDirPaths,
CustomPackageUrls: remotePaths,
ExitOnSync: exitOnSync,
PackageCustomization: o,

Scheme: k8s.GetScheme(),
CancelFunc: ctxCancel,
}

b := build.NewBuild(opts)

if err := b.Run(ctx, recreateCluster); err != nil {
return err
Expand Down Expand Up @@ -153,7 +170,9 @@ func validate() error {
return pErr
}
}
return nil

_, _, err = helpers.ParsePackageStrings(extraPackagesDirs)
return err
}

func getPackageCustomFile(input string) (v1alpha1.PackageCustomization, error) {
Expand Down
9 changes: 6 additions & 3 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/build"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -59,8 +58,12 @@ func getSecretsE(cmd *cobra.Command, args []string) error {
defer ctxCancel()
kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")

b := build.NewBuild("", "", kubeConfigPath, "", "",
util.CorePackageTemplateConfig{}, []string{}, false, k8s.GetScheme(), ctxCancel, nil)
opts := build.NewBuildOptions{}
opts.KubeConfigPath = kubeConfigPath
opts.Scheme = k8s.GetScheme()
opts.CancelFunc = ctxCancel

b := build.NewBuild(opts)

kubeConfig, err := b.GetKubeConfig()
if err != nil {
Expand Down
58 changes: 43 additions & 15 deletions pkg/cmd/helpers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"

"github.com/cnoe-io/idpbuilder/pkg/util"
"sigs.k8s.io/kustomize/kyaml/kio"
)

Expand Down Expand Up @@ -34,27 +35,54 @@ func ValidateKubernetesYamlFile(absPath string) error {
return nil
}

func ParsePackageStrings(pkgStrings []string) ([]string, []string, error) {
remote := make([]string, 0, 2)
local := make([]string, 0, 2)
for i := range pkgStrings {
loc := pkgStrings[i]
_, err := util.NewKustomizeRemote(loc)
if err == nil {
remote = append(remote, loc)
continue
}

absPath, err := getAbsPath(loc, true)
if err == nil {
local = append(local, absPath)
continue
}
return nil, nil, err
}

return remote, local, nil
}

func getAbsPath(path string, isDir bool) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("failed to validate path %s : %w", path, err)
}
f, err := os.Stat(absPath)
if err != nil {
return "", fmt.Errorf("failed to validate path %s : %w", absPath, err)
}
if isDir && !f.IsDir() {
return "", fmt.Errorf("given path is not a directory. %s", absPath)
}
if !isDir && !f.Mode().IsRegular() {
return "", fmt.Errorf("give path is not a file. %s", absPath)
}
return absPath, nil
}

func GetAbsFilePaths(paths []string, isDir bool) ([]string, error) {
out := make([]string, 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)
absPath, err := getAbsPath(paths[i], isDir)
if err != nil {
return nil, fmt.Errorf("failed to validate path %s : %w", absPath, err)
}
if isDir && !f.IsDir() {
return nil, fmt.Errorf("given path is not a directory. %s", absPath)
}
if !isDir && !f.Mode().IsRegular() {
return nil, fmt.Errorf("give path is not a file. %s", absPath)
return nil, err
}

out[i] = absPath
}

return out, nil
}
Loading

0 comments on commit b64b627

Please sign in to comment.