Skip to content

Commit 1599593

Browse files
author
Per Goncalves da Silva
committed
exp: template + vap
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 21d67ff commit 1599593

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
)
@@ -363,6 +366,7 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp
363366
return nil
364367
}, helmclient.AppendInstallPostRenderer(post))
365368
if err != nil {
369+
_ = r.renderTemplates(chrt, values, post)
366370
log.FromContext(ctx).Error(err, "failed to install bundle")
367371
setInstalledStatusConditionFailed(ext, fmt.Sprintf("%s:%v", ocv1alpha1.ReasonInstallationFailed, olmv1error.AsOlmErr(err)))
368372
return ctrl.Result{}, err
@@ -799,3 +803,56 @@ func (r *ClusterExtensionReconciler) validateBundle(bundle *catalogmetadata.Bund
799803

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

0 commit comments

Comments
 (0)