Skip to content

Commit 4640b18

Browse files
author
Per Goncalves da Silva
committed
exp: template + vap
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 73fdc37 commit 4640b18

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed
File renamed without changes.
File renamed without changes.

internal/action/helm.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package action
2+
3+
import (
4+
"helm.sh/helm/v3/pkg/chart"
5+
"helm.sh/helm/v3/pkg/release"
6+
7+
"github.com/operator-framework/helm-operator-plugins/pkg/client"
8+
)
9+
10+
type ActionClient struct {
11+
client.ActionInterface
12+
}
13+
14+
func (a ActionClient) Install(name, namespace string, chrt *chart.Chart, vals map[string]interface{}, opts ...client.InstallOption) (*release.Release, error) {
15+
rel, err := a.ActionInterface.Install(name, namespace, chrt, vals, opts...)
16+
return rel, err
17+
}
18+
19+
func (a ActionClient) Upgrade(name, namespace string, chrt *chart.Chart, vals map[string]interface{}, opts ...client.UpgradeOption) (*release.Release, error) {
20+
re, err := a.ActionInterface.Upgrade(name, namespace, chrt, vals, opts...)
21+
return re, err
22+
}

internal/controllers/clusterextension_controller.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
olmv1error "github.com/operator-framework/operator-controller/internal/action/error"
25+
"gopkg.in/yaml.v3"
26+
"helm.sh/helm/v3/pkg/engine"
2427
"io"
28+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2529
"sort"
2630
"strings"
2731
"sync"
@@ -64,7 +68,7 @@ import (
6468
"github.com/operator-framework/operator-registry/alpha/property"
6569
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
6670
registryv1handler "github.com/operator-framework/rukpak/pkg/handler"
67-
crdupgradesafety "github.com/operator-framework/rukpak/pkg/preflights/crdupgradesafety"
71+
"github.com/operator-framework/rukpak/pkg/preflights/crdupgradesafety"
6872
rukpaksource "github.com/operator-framework/rukpak/pkg/source"
6973
"github.com/operator-framework/rukpak/pkg/storage"
7074
"github.com/operator-framework/rukpak/pkg/util"
@@ -74,7 +78,6 @@ import (
7478
catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter"
7579
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
7680
"github.com/operator-framework/operator-controller/internal/conditionsets"
77-
olmv1error "github.com/operator-framework/operator-controller/internal/error"
7881
"github.com/operator-framework/operator-controller/internal/httputil"
7982
"github.com/operator-framework/operator-controller/internal/labels"
8083
)
@@ -359,6 +362,7 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp
359362
return nil
360363
}, helmclient.AppendInstallPostRenderer(post))
361364
if err != nil {
365+
_ = r.renderTemplates(chrt, values, post)
362366
log.FromContext(ctx).Error(err, "failed to install bundle")
363367
setInstalledStatusConditionFailed(ext, fmt.Sprintf("%s:%v", ocv1alpha1.ReasonInstallationFailed, olmv1error.AsOlmErr(err)))
364368
return ctrl.Result{}, err
@@ -788,3 +792,56 @@ func (r *ClusterExtensionReconciler) validateBundle(bundle *catalogmetadata.Bund
788792

789793
return nil
790794
}
795+
796+
func (r *ClusterExtensionReconciler) renderTemplates(chart *chart.Chart, values map[string]interface{}, post postrender.PostRenderer) error {
797+
options := chartutil.ReleaseOptions{}
798+
799+
// Combine chart values with release options
800+
valuesToRender, err := chartutil.ToRenderValues(chart, values, options, nil)
801+
if err != nil {
802+
return err
803+
}
804+
805+
// Render templates
806+
rendered, err := engine.Render(chart, valuesToRender)
807+
if err != nil {
808+
return err
809+
}
810+
811+
for _, tmpl := range rendered {
812+
// apply post renderer
813+
buf := bytes.NewBufferString(tmpl)
814+
modified, err := post.Run(buf)
815+
if err != nil {
816+
return err
817+
}
818+
819+
// extract object
820+
var obj map[string]interface{}
821+
if err := yaml.Unmarshal(modified.Bytes(), &obj); err != nil {
822+
return err
823+
}
824+
825+
resource := &unstructured.Unstructured{Object: obj}
826+
827+
// try to create (dry run)
828+
err = r.Client.Create(context.Background(), resource, client.DryRunAll)
829+
if !k8serrors.IsAlreadyExists(err) {
830+
return err
831+
}
832+
833+
key := client.ObjectKeyFromObject(resource)
834+
existingResource := resource.DeepCopy()
835+
err = r.Client.Get(context.Background(), key, existingResource)
836+
if err != nil {
837+
return err
838+
}
839+
840+
// try to update (dry run)
841+
resource.SetResourceVersion(existingResource.GetResourceVersion())
842+
if err := r.Client.Update(context.Background(), resource, client.DryRunAll); !k8serrors.IsNotFound(err) {
843+
return err
844+
}
845+
}
846+
return nil
847+
}

0 commit comments

Comments
 (0)