Skip to content

Commit 67d6ca8

Browse files
authored
feat: relax the -p flag to allow files (cnoe-io#538)
Signed-off-by: Caleb Boylan <calebboylan@gmail.com>
1 parent 796a921 commit 67d6ca8

8 files changed

Lines changed: 91 additions & 19 deletions

File tree

api/v1alpha1/localbuild_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type EmbeddedArgoApplicationsPackageConfigSpec struct {
4242
type PackageConfigsSpec struct {
4343
Argo ArgoPackageConfigSpec `json:"argoPackageConfigs,omitempty"`
4444
EmbeddedArgoApplications EmbeddedArgoApplicationsPackageConfigSpec `json:"embeddedArgoApplicationsPackageConfigs,omitempty"`
45+
CustomPackageFiles []string `json:"customPackageFiles,omitempty"`
4546
CustomPackageDirs []string `json:"customPackageDirs,omitempty"`
4647
CustomPackageUrls []string `json:"customPackageUrls,omitempty"`
4748
// +kubebuilder:validation:Optional

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/build/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Build struct {
3434
kubeVersion string
3535
extraPortsMapping string
3636
registryConfig []string
37+
customPackageFiles []string
3738
customPackageDirs []string
3839
customPackageUrls []string
3940
packageCustomization map[string]v1alpha1.PackageCustomization
@@ -50,6 +51,7 @@ type NewBuildOptions struct {
5051
KubeVersion string
5152
ExtraPortsMapping string
5253
RegistryConfig []string
54+
CustomPackageFiles []string
5355
CustomPackageDirs []string
5456
CustomPackageUrls []string
5557
PackageCustomization map[string]v1alpha1.PackageCustomization
@@ -66,6 +68,7 @@ func NewBuild(opts NewBuildOptions) *Build {
6668
kubeVersion: opts.KubeVersion,
6769
extraPortsMapping: opts.ExtraPortsMapping,
6870
registryConfig: opts.RegistryConfig,
71+
customPackageFiles: opts.CustomPackageFiles,
6972
customPackageDirs: opts.CustomPackageDirs,
7073
customPackageUrls: opts.CustomPackageUrls,
7174
packageCustomization: opts.PackageCustomization,
@@ -258,6 +261,7 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error {
258261
Enabled: true,
259262
},
260263
CustomPackageDirs: b.customPackageDirs,
264+
CustomPackageFiles: b.customPackageFiles,
261265
CustomPackageUrls: b.customPackageUrls,
262266
CorePackageCustomization: b.packageCustomization,
263267
},

pkg/cmd/create/root.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,17 @@ func create(cmd *cobra.Command, args []string) error {
113113
return err
114114
}
115115

116-
var absDirPaths []string
116+
var localFiles []string
117+
var localDirs []string
117118
var remotePaths []string
118119

119120
if len(extraPackages) > 0 {
120-
r, l, pErr := helpers.ParsePackageStrings(extraPackages)
121+
r, f, d, pErr := helpers.ParsePackageStrings(extraPackages)
121122
if pErr != nil {
122123
return pErr
123124
}
124-
absDirPaths = l
125+
localFiles = f
126+
localDirs = d
125127
remotePaths = r
126128
}
127129

@@ -164,7 +166,8 @@ func create(cmd *cobra.Command, args []string) error {
164166
StaticPassword: devPassword,
165167
},
166168

167-
CustomPackageDirs: absDirPaths,
169+
CustomPackageFiles: localFiles,
170+
CustomPackageDirs: localDirs,
168171
CustomPackageUrls: remotePaths,
169172
ExitOnSync: exitOnSync,
170173
PackageCustomization: o,
@@ -204,7 +207,7 @@ func validate() error {
204207
}
205208
}
206209

207-
_, _, err = helpers.ParsePackageStrings(extraPackages)
210+
_, _, _, err = helpers.ParsePackageStrings(extraPackages)
208211
return err
209212
}
210213

pkg/cmd/helpers/validation.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func ValidateKubernetesYamlFile(absPath string) error {
3535
return nil
3636
}
3737

38-
func ParsePackageStrings(pkgStrings []string) ([]string, []string, error) {
39-
remote, local := make([]string, 0, 2), make([]string, 0, 2)
38+
func ParsePackageStrings(pkgStrings []string) ([]string, []string, []string, error) {
39+
remote, files, dirs := make([]string, 0, 2), make([]string, 0, 2), make([]string, 0, 2)
4040
for i := range pkgStrings {
4141
loc := pkgStrings[i]
4242
_, err := util.NewKustomizeRemote(loc)
@@ -47,13 +47,20 @@ func ParsePackageStrings(pkgStrings []string) ([]string, []string, error) {
4747

4848
absPath, err := getAbsPath(loc, true)
4949
if err == nil {
50-
local = append(local, absPath)
50+
dirs = append(dirs, absPath)
5151
continue
5252
}
53-
return nil, nil, err
53+
54+
absPath, err = getAbsPath(loc, false)
55+
if err == nil {
56+
files = append(files, absPath)
57+
continue
58+
}
59+
60+
return nil, nil, nil, err
5461
}
5562

56-
return remote, local, nil
63+
return remote, files, dirs, nil
5764
}
5865

5966
func getAbsPath(path string, isDir bool) (string, error) {
@@ -65,11 +72,13 @@ func getAbsPath(path string, isDir bool) (string, error) {
6572
if err != nil {
6673
return "", fmt.Errorf("failed to validate path %s : %w", absPath, err)
6774
}
75+
6876
if isDir && !f.IsDir() {
6977
return "", fmt.Errorf("given path is not a directory. %s", absPath)
7078
}
79+
7180
if !isDir && !f.Mode().IsRegular() {
72-
return "", fmt.Errorf("give path is not a file. %s", absPath)
81+
return "", fmt.Errorf("given path is not a file. %s", absPath)
7382
}
7483
return absPath, nil
7584
}

pkg/cmd/helpers/validation_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,38 @@ func TestParsePackageStrings(t *testing.T) {
4141
expectErr bool
4242
inputPaths []string
4343
remote int
44-
local int
44+
files int
45+
dirs int
4546
}{
46-
"allLocal": {expectErr: false, inputPaths: []string{"test-data", "."}, remote: 0, local: 2},
47+
"allDirs": {expectErr: false, inputPaths: []string{"test-data", "."}, remote: 0, files: 0, dirs: 2},
48+
"allFiles": {expectErr: false, inputPaths: []string{"test-data/valid.yaml"}, remote: 0, files: 1, dirs: 0},
4749
"allRemote": {expectErr: false, inputPaths: []string{
4850
"https://github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?timeout=120&ref=v3.3.1",
4951
"git@github.com:owner/repo//examples",
50-
}, remote: 2, local: 0},
52+
}, remote: 2, files: 0, dirs: 0},
5153
"mix": {expectErr: false, inputPaths: []string{
5254
"https://github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?timeout=120&ref=v3.3.1",
5355
"test-data",
54-
}, remote: 1, local: 1},
56+
"test-data/valid.yaml",
57+
}, remote: 1, files: 1, dirs: 1},
5558
"invalidLocalPath": {expectErr: true, inputPaths: []string{
5659
"does-not-exist",
57-
}, remote: 0, local: 0},
60+
}, remote: 0, files: 0, dirs: 0},
5861
"invalidRemotePath": {expectErr: true, inputPaths: []string{
5962
"https:// github.com/kubernetes-sigs/kustomize//examples",
60-
}, remote: 0, local: 0},
63+
}, remote: 0, files: 0, dirs: 0},
6164
}
6265

6366
for k := range cases {
6467
c := cases[k]
65-
remote, local, err := ParsePackageStrings(c.inputPaths)
68+
remote, files, dirs, err := ParsePackageStrings(c.inputPaths)
6669
if cases[k].expectErr {
6770
assert.NotNil(t, err)
6871
} else {
6972
assert.Nil(t, err)
7073
}
7174
assert.Equal(t, c.remote, len(remote))
72-
assert.Equal(t, c.local, len(local))
75+
assert.Equal(t, c.files, len(files))
76+
assert.Equal(t, c.dirs, len(dirs))
7377
}
7478
}

pkg/controllers/localbuild/controller.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ func (r *LocalbuildReconciler) ReconcileArgoAppsWithGitea(ctx context.Context, r
255255
}
256256
}
257257

258+
for _, s := range resource.Spec.PackageConfigs.CustomPackageFiles {
259+
result, err := r.reconcileCustomPkgFile(ctx, resource, s)
260+
if err != nil {
261+
return result, err
262+
}
263+
}
264+
258265
for _, s := range resource.Spec.PackageConfigs.CustomPackageUrls {
259266
result, err := r.reconcileCustomPkgUrl(ctx, resource, s)
260267
if err != nil {
@@ -564,6 +571,41 @@ func (r *LocalbuildReconciler) reconcileCustomPkgDir(ctx context.Context, resour
564571
return ctrl.Result{}, nil
565572
}
566573

574+
func (r *LocalbuildReconciler) reconcileCustomPkgFile(ctx context.Context, resource *v1alpha1.Localbuild, pkgFile string) (ctrl.Result, error) {
575+
logger := log.FromContext(ctx)
576+
577+
file, err := os.Open(pkgFile)
578+
defer file.Close()
579+
if err != nil {
580+
return ctrl.Result{}, fmt.Errorf("opening file, %s: %w", pkgFile, err)
581+
}
582+
583+
fType, err := file.Stat()
584+
if err != nil {
585+
return ctrl.Result{}, fmt.Errorf("getting file info, %s: %w", pkgFile, err)
586+
}
587+
588+
if !fType.Mode().IsRegular() {
589+
return ctrl.Result{}, fmt.Errorf("file is not a regular file, %s: %w", pkgFile, err)
590+
}
591+
592+
if !util.IsYamlFile(file.Name()) {
593+
return ctrl.Result{}, fmt.Errorf("file is not a yaml file, %s: %w", pkgFile, err)
594+
}
595+
596+
b, fErr := os.ReadFile(pkgFile)
597+
if fErr != nil {
598+
return ctrl.Result{}, fmt.Errorf("reading file, %s: %w", pkgFile, err)
599+
}
600+
601+
rErr := r.reconcileCustomPkg(ctx, resource, b, pkgFile, nil)
602+
if rErr != nil {
603+
logger.Error(rErr, "reconciling custom pkg", "file", pkgFile)
604+
}
605+
606+
return ctrl.Result{}, nil
607+
}
608+
567609
func (r *LocalbuildReconciler) reconcileGitRepo(ctx context.Context, resource *v1alpha1.Localbuild, repoType, repoName, embeddedName, absPath string) (*v1alpha1.GitRepository, error) {
568610
repo := &v1alpha1.GitRepository{
569611
ObjectMeta: metav1.ObjectMeta{

pkg/controllers/resources/idpbuilder.cnoe.io_localbuilds.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ spec:
7171
items:
7272
type: string
7373
type: array
74+
customPackageFiles:
75+
items:
76+
type: string
77+
type: array
7478
customPackageUrls:
7579
items:
7680
type: string

0 commit comments

Comments
 (0)