Skip to content

Commit

Permalink
Adding additional tests for ociRepository chartRef
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Mar 12, 2024
1 parent e85adca commit 3a93f37
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ jobs:
kubectl -n install-create-target-ns get deployment install-create-target-ns-install-create-target-ns-podinfo
kubectl -n helm-system delete -f config/testdata/install-create-target-ns
- name: Run install from ocirepo test
run: |
kubectl -n helm-system apply -f config/testdata/install-from-ocirepo-source
kubectl -n helm-system wait helmreleases/podinfo-from-ocirepo --for=condition=ready --timeout=4m
kubectl -n helm-system delete -f config/testdata/install-from-ocirepo-source
- name: Run install fail test
run: |
test_name=install-fail
Expand Down
2 changes: 1 addition & 1 deletion api/v2beta2/reference_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type CrossNamespaceSourceReference struct {
APIVersion string `json:"apiVersion,omitempty"`

// Kind of the referent.
// +kubebuilder:validation:Enum=OCIRepository;HelmChart
// +kubebuilder:validation:Enum=OCIRepository
// +required
Kind string `json:"kind"`

Expand Down
1 change: 0 additions & 1 deletion config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,6 @@ spec:
description: Kind of the referent.
enum:
- OCIRepository
- HelmChart
type: string
name:
description: Name of the referent.
Expand Down
14 changes: 14 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ rules:
- helmcharts/status
verbs:
- get
- apiGroups:
- source.toolkit.fluxcd.io
resources:
- ocirepositories
verbs:
- get
- list
- watch
- apiGroups:
- source.toolkit.fluxcd.io
resources:
- ocirepositories/status
verbs:
- get
34 changes: 34 additions & 0 deletions config/testdata/install-from-ocirepo-source/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: OCIRepository
metadata:
name: podinfo-ocirepo
spec:
interval: 10m
url: oci://ghcr.io/stefanprodan/charts/podinfo
ref:
tag: 6.6.0
---
apiVersion: helm.toolkit.fluxcd.io/v2beta2
kind: HelmRelease
metadata:
name: podinfo-from-ocirepo
spec:
releaseName: podinfo
chartRef:
kind: OCIRepository
name: podinfo-ocirepo
interval: 50m
driftDetection:
mode: enabled
install:
timeout: 10m
remediation:
retries: 3
upgrade:
timeout: 10m
values:
resources:
requests:
cpu: 100m
memory: 64Mi
20 changes: 14 additions & 6 deletions internal/controller/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import (
// +kubebuilder:rbac:groups=helm.toolkit.fluxcd.io,resources=helmreleases/finalizers,verbs=get;create;update;patch;delete
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=helmcharts,verbs=get;list;watch
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=helmcharts/status,verbs=get
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=ocirepositories,verbs=get;list;watch
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=ocirepositories/status,verbs=get
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch

// HelmReleaseReconciler reconciles a HelmRelease object.
Expand Down Expand Up @@ -155,7 +157,7 @@ func (r *HelmReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

if !isValidChartRef(obj) {
return ctrl.Result{}, reconcile.TerminalError(fmt.Errorf("invalid HelmChart reference"))
return ctrl.Result{}, reconcile.TerminalError(fmt.Errorf("invalid Chart reference"))
}

// Initialize the patch helper with the current version of the object.
Expand Down Expand Up @@ -277,7 +279,7 @@ func (r *HelmReleaseReconciler) reconcileRelease(ctx context.Context, patchHelpe
return ctrl.Result{}, reconcile.TerminalError(err)
}

msg := fmt.Sprintf("could not get HelmChart object: %s", err.Error())
msg := fmt.Sprintf("could not get Source object: %s", err.Error())
conditions.MarkFalse(obj, meta.ReadyCondition, v2.ArtifactFailedReason, msg)
return ctrl.Result{}, err
}
Expand All @@ -289,13 +291,13 @@ func (r *HelmReleaseReconciler) reconcileRelease(ctx context.Context, patchHelpe
// Check if the source is ready.
if ready, msg := isSourceReady(source); !ready {
log.Info(msg)
conditions.MarkFalse(obj, meta.ReadyCondition, "HelmChartNotReady", msg)
conditions.MarkFalse(obj, meta.ReadyCondition, "SourceNotReady", msg)
// Do not requeue immediately, when the artifact is created
// the watcher should trigger a reconciliation.
return jitter.JitteredRequeueInterval(ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}), errWaitForChart
}
// Remove any stale corresponding Ready=False condition with Unknown.
if conditions.HasAnyReason(obj, meta.ReadyCondition, "HelmChartNotReady") {
if conditions.HasAnyReason(obj, meta.ReadyCondition, "SourceNotReady") {
conditions.MarkUnknown(obj, meta.ReadyCondition, meta.ProgressingReason, "reconciliation in progress")
}

Expand All @@ -315,7 +317,7 @@ func (r *HelmReleaseReconciler) reconcileRelease(ctx context.Context, patchHelpe
loadedChart, err := loader.SecureLoadChartFromURL(loader.NewRetryableHTTPClient(ctx, r.artifactFetchRetries), source.GetArtifact().URL, source.GetArtifact().Digest)
if err != nil {
if errors.Is(err, loader.ErrFileNotFound) {
msg := fmt.Sprintf("Chart not ready: artifact not found. Retrying in %s", r.requeueDependency.String())
msg := fmt.Sprintf("Source not ready: artifact not found. Retrying in %s", r.requeueDependency.String())
conditions.MarkFalse(obj, meta.ReadyCondition, v2.ArtifactFailedReason, msg)
log.Info(msg)
return ctrl.Result{RequeueAfter: r.requeueDependency}, errWaitForDependency
Expand Down Expand Up @@ -678,6 +680,9 @@ func (r *HelmReleaseReconciler) getSource(ctx context.Context, obj *v2.HelmRelea
return r.getHelmChartFromOCIRef(ctx, obj)
}
name, namespace = obj.Spec.ChartRef.Name, obj.Spec.ChartRef.Namespace
if namespace == "" {
namespace = obj.GetNamespace()
}
} else {
namespace, name = obj.Status.GetHelmChart()
}
Expand All @@ -696,7 +701,10 @@ func (r *HelmReleaseReconciler) getSource(ctx context.Context, obj *v2.HelmRelea
}

func (r *HelmReleaseReconciler) getHelmChartFromOCIRef(ctx context.Context, obj *v2.HelmRelease) (source.Source, error) {
namespace, name := obj.Spec.ChartRef.Name, obj.Spec.ChartRef.Namespace
name, namespace := obj.Spec.ChartRef.Name, obj.Spec.ChartRef.Namespace
if namespace == "" {
namespace = obj.GetNamespace()
}
ociRepoRef := types.NamespacedName{Namespace: namespace, Name: name}

if err := intacl.AllowsAccessTo(obj, sourcev1.OCIRepositoryKind, ociRepoRef); err != nil {
Expand Down
Loading

0 comments on commit 3a93f37

Please sign in to comment.