Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
[fix/cleanup] This PR fixes using of multiple sources and cleans up H…
Browse files Browse the repository at this point in the history
…elm provisioner

- Create a new fs from source, only if destination is specified. This allows
  specifying multiple sources.
- Remove helm provisioner related manifests and controller.

Signed-off-by: Varsha Prasad Narsing <[email protected]>
  • Loading branch information
varshaprasad96 committed Sep 20, 2023
1 parent 7136f73 commit f4dbc9b
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 318 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM gcr.io/distroless/static:debug-nonroot

WORKDIR /

COPY helm helm
COPY core core
COPY unpack unpack
COPY webhooks webhooks
Expand Down
1 change: 0 additions & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ repo = {
'binaries': {
'core': 'core',
'crdvalidator': 'crd-validation-webhook',
'helm': 'helm-provisioner',
'webhooks': 'rukpak-webhooks',
},
'starting_debug_port': 10000,
Expand Down
40 changes: 1 addition & 39 deletions cmd/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func main() {
dependentSelector := labels.NewSelector().Add(*dependentRequirement)

cfg := ctrl.GetConfigOrDie()
fmt.Println("systemNs:!!!!", systemNamespace)
if systemNamespace == "" {
systemNamespace = util.PodNamespace()
}
Expand All @@ -127,7 +126,7 @@ func main() {
opts.Scheme = scheme
opts.Namespace = systemNamespace
})
fmt.Println("systemNsCluster!!!!!!!", systemNsCluster)

if err != nil {
setupLog.Error(err, "unable to create system namespace cluster")
os.Exit(1)
Expand Down Expand Up @@ -228,38 +227,9 @@ func main() {
os.Exit(1)
}

// commonBundleProvisionerOptions := []bundle.Option{
// bundle.WithUnpacker(unpacker),
// bundle.WithFinalizers(bundleFinalizers),
// bundle.WithStorage(bundleStorage),
// }

cfgGetter := helmclient.NewActionConfigGetter(mgr.GetConfig(), mgr.GetRESTMapper(), mgr.GetLogger())
acg := helmclient.NewActionClientGetter(cfgGetter)
deployer := v1alpha2deployer.NewDefaultHelmDeployerWithOpts(v1alpha2deployer.WithActionClientGetter(acg), v1alpha2deployer.WithReleaseNamespace(systemNamespace))
// commonBDProvisionerOptions := []bundledeployment.Option{
// bundledeployment.WithReleaseNamespace(systemNamespace),
// bundledeployment.WithActionClientGetter(acg),
// bundledeployment.WithStorage(bundleStorage),
// }

// if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNamespace, append(
// commonBundleProvisionerOptions,
// bundle.WithProvisionerID(plain.ProvisionerID),
// bundle.WithHandler(bundle.HandlerFunc(plain.HandleBundle)),
// )...); err != nil {
// setupLog.Error(err, "unable to create controller", "controller", rukpakv1alpha1.BundleKind, "provisionerID", plain.ProvisionerID)
// os.Exit(1)
// }

// if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNamespace, append(
// commonBundleProvisionerOptions,
// bundle.WithProvisionerID(registry.ProvisionerID),
// bundle.WithHandler(bundle.HandlerFunc(registry.HandleBundle)),
// )...); err != nil {
// setupLog.Error(err, "unable to create controller", "controller", rukpakv1alpha1.BundleKind, "provisionerID", registry.ProvisionerID)
// os.Exit(1)
// }

if err := v1alpha2bd.SetupWithManager(mgr,
systemNsCluster.GetCache(),
Expand All @@ -270,14 +240,6 @@ func main() {
os.Exit(1)
}

// if err := bundledeployment.SetupWithManager(mgr, append(
// commonBDProvisionerOptions,
// bundledeployment.WithProvisionerID(plain.ProvisionerID),
// bundledeployment.WithHandler(bundledeployment.HandlerFunc(plain.HandleBundleDeployment)),
// )...); err != nil {
// setupLog.Error(err, "unable to create controller", "controller", rukpakv1alpha1.BundleDeploymentKind, "provisionerID", plain.ProvisionerID)
// os.Exit(1)
// }
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/operator-framework/api v0.17.4-0.20230223191600-0131a6301e42
github.com/operator-framework/helm-operator-plugins v0.0.11
github.com/operator-framework/operator-registry v1.28.0
github.com/otiai10/copy v1.11.0
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -134,7 +135,6 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/operator-framework/operator-lib v0.11.0 // indirect
github.com/otiai10/copy v1.2.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
10 changes: 3 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,9 @@ github.com/operator-framework/operator-lib v0.11.0 h1:eYzqpiOfq9WBI4Trddisiq/X9B
github.com/operator-framework/operator-lib v0.11.0/go.mod h1:RpyKhFAoG6DmKTDIwMuO6pI3LRc8IE9rxEYWy476o6g=
github.com/operator-framework/operator-registry v1.28.0 h1:vtmd2WgJxkx7vuuOxW4k5Le/oo0SfonSeJVMU3rKIfk=
github.com/operator-framework/operator-registry v1.28.0/go.mod h1:UYw3uaZyHwHgnczLRYmUqMpgRgP2EfkqOsaR+LI+nK8=
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc=
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/otiai10/copy v1.11.0 h1:OKBD80J/mLBrwnzXqGtFCzprFSGioo30JcmR4APsNwc=
github.com/otiai10/copy v1.11.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

v1alpha2deployer "github.com/operator-framework/rukpak/internal/controllers/v1alpha2/deployer"
v1alpha2source "github.com/operator-framework/rukpak/internal/controllers/v1alpha2/source"
Expand All @@ -50,7 +51,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/source"
crsource "sigs.k8s.io/controller-runtime/pkg/source"
)

Expand Down Expand Up @@ -100,7 +100,6 @@ func WithDeployer(u v1alpha2deployer.Deployer) Option {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (b *bundleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
fmt.Println("reconciling")
log := log.FromContext(ctx)

existingBD := &v1alpha2.BundleDeployment{}
Expand All @@ -116,6 +115,7 @@ func (b *bundleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Req

// Skip reconciling if `spec.paused` is set.
if existingBD.Spec.Paused {
log.Info("bundledeployment has been paused for reconciliation", "bundle deployment name", existingBD.Name)
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -192,7 +192,6 @@ func (b *bundleDeploymentReconciler) reconcile(ctx context.Context, bd *v1alpha2
return ctrl.Result{}, fmt.Errorf("unkown deploy state %q for bundle deployment %s: %v", deployRes.State, bd.GetName(), bd.Generation)
}

fmt.Println("deplpoy done")
for _, obj := range deployRes.AppliedObjects {
uMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
Expand Down
51 changes: 42 additions & 9 deletions internal/controllers/v1alpha2/source/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
sshgit "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-logr/logr"
"github.com/operator-framework/rukpak/api/v1alpha2"
"github.com/operator-framework/rukpak/internal/util"
"github.com/spf13/afero"
"golang.org/x/crypto/ssh"
corev1 "k8s.io/api/core/v1"
Expand All @@ -33,6 +34,10 @@ type Git struct {
Log logr.Logger
}

const (
gitCachePath = "var/cache/git/"
)

func (r *Git) Unpack(ctx context.Context, bundeDepName string, bundleSrc v1alpha2.BundleDeplopymentSource, base afero.Fs, opts UnpackOption) (*Result, error) {
// Validate inputs
if err := r.validate(bundleSrc); err != nil {
Expand Down Expand Up @@ -72,20 +77,40 @@ func (r *Git) Unpack(ctx context.Context, bundeDepName string, bundleSrc v1alpha
// create a destination path to clone the repository to.
// destination would be <bd-name>/bd.spec.sources.destination.
// verify if path already exists if so, clean up.
if err := base.RemoveAll(bundleSrc.Destination); err != nil {
return nil, fmt.Errorf("error removing contents from local destination %v", err)
if bundleSrc.Destination != "" {
if err := base.RemoveAll(bundleSrc.Destination); err != nil {
return nil, fmt.Errorf("error removing contents from local destination %v", err)
}
if err := base.MkdirAll(bundleSrc.Destination, 0755); err != nil {
return nil, fmt.Errorf("error creating storagepath %q", err)
}
}

if err := util.CreateDirPath(base, gitCachePath); err != nil {
return nil, err
}
if err := base.MkdirAll(bundleSrc.Destination, 0755); err != nil {
return nil, fmt.Errorf("error creating storagepath %q", err)
defer deleteCacheDir(base)

// clone to local but in a cache dir.
repo, err := git.PlainCloneContext(ctx, filepath.Join(bundeDepName, gitCachePath), false, &cloneOpts)
if err != nil {
return nil, fmt.Errorf("bundle unpack git clone error: %v - %s", err, progress.String())
}

// refers to the full local path where contents need to be stored.
storagePath := filepath.Join(bundeDepName, filepath.Clean(bundleSrc.Destination))

// clone to local.
repo, err := git.PlainCloneContext(ctx, storagePath, false, &cloneOpts)
if err != nil {
return nil, fmt.Errorf("bundle unpack git clone error: %v - %s", err, progress.String())
cacheSrcPath := filepath.Join(bundeDepName, gitCachePath)
if gitsource.Directory != "" {
directory := filepath.Clean(gitsource.Directory)
if directory[:3] == "../" || directory[0] == '/' {
return nil, fmt.Errorf("get subdirectory %q for repository %q: %s", gitsource.Directory, gitsource.Repository, "directory can not start with '../' or '/'")
}
cacheSrcPath = filepath.Join(cacheSrcPath, directory)
}

if err := util.CopyDir(base, cacheSrcPath, storagePath); err != nil {
return nil, fmt.Errorf("copying contents from cache to local dir: %v", err)
}

commitHash, err := repo.ResolveRevision("HEAD")
Expand All @@ -102,10 +127,18 @@ func (r *Git) Unpack(ctx context.Context, bundeDepName string, bundleSrc v1alpha
Kind: v1alpha2.SourceTypeGit,
Git: resolvedGit,
}

return &Result{ResolvedSource: resolvedSource, State: StateUnpacked, Message: "Successfully unpacked git bundle"}, nil
}

func deleteCacheDir(fs afero.Fs) error {
paths := strings.Split(gitCachePath, string(os.PathSeparator))
if len(paths) <= 0 {
// shouldn't happen
return fmt.Errorf("unable to find cache directory: %s", gitCachePath)
}
return fs.RemoveAll(paths[0])
}

func (r *Git) validate(bundleSrc v1alpha2.BundleDeplopymentSource) error {
if bundleSrc.Kind != v1alpha2.SourceTypeGit {
return fmt.Errorf("bundle source type %q not supported", bundleSrc.Kind)
Expand Down
20 changes: 0 additions & 20 deletions internal/controllers/v1alpha2/source/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -42,7 +41,6 @@ func (i *Image) Unpack(ctx context.Context, bdName string, bdSrc v1alpha2.Bundle
if err := i.validate(&bdSrc, opts); err != nil {
return nil, fmt.Errorf("validation unsuccessful during unpacking %v", err)
}

// storage path to store contents in local directory.
storagePath := filepath.Join(bdName, filepath.Clean(bdSrc.Destination))
return i.unpack(ctx, bdName, storagePath, bdSrc, base, opts)
Expand Down Expand Up @@ -332,21 +330,3 @@ func pendingImagePodResult(pod *corev1.Pod) *Result {
}
return &Result{State: StateUnpackPending, Message: strings.Join(messages, "; ")}
}

// Perform a base64 encoding to get the directoryName to store caches
func getCacheDirName(bdName string, bd v1alpha2.BundleDeplopymentSource) string {
switch bd.Kind {
case v1alpha2.SourceTypeImage:
return encode(bdName, string(bd.Kind), bd.Image.ImageRef)
case v1alpha2.SourceTypeGit:
return encode(bdName, string(bd.Kind), bd.Git.Repository)
case v1alpha2.SourceTypeHTTP:
return encode(bdName, string(bd.Kind), bd.HTTP.URL)
default:
return ""
}
}

func encode(str1, str2, str3 string) string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s-%s-%s", str1, str2, str3)))
}
23 changes: 23 additions & 0 deletions internal/util/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"path/filepath"
"strings"
"testing/fstest"

cp "github.com/otiai10/copy"
"github.com/spf13/afero"
)

// FilesOnlyFilesystem is an fs.FS implementation that treats non-regular files
Expand Down Expand Up @@ -88,3 +91,23 @@ func (f baseDirFS) Open(name string) (fs.File, error) {
}
return nil, fs.ErrNotExist
}

// CopyDir copies contents from one directory to another in afero.Fs
func CopyDir(fs afero.Fs, srcPath, destPath string) error {
return cp.Copy(srcPath, destPath, cp.Options{
OnDirExists: func(src, dest string) cp.DirExistsAction {
return cp.Merge
},
})
}

func CreateDirPath(fs afero.Fs, path string) error {
if err := fs.RemoveAll(path); err != nil {
return fmt.Errorf("error removing existing contents from local path: %s: %v", path, err)
}

if err := fs.MkdirAll(path, 0755); err != nil {
return fmt.Errorf("error creating path %s: %v", path, err)
}
return nil
}
20 changes: 0 additions & 20 deletions manifests/base/provisioners/helm/kustomization.yaml

This file was deleted.

Loading

0 comments on commit f4dbc9b

Please sign in to comment.