github.com/banzaicloud/operator-tools@v0.28.10/pkg/helm/templatereconciler/chart.go (about)

     1  // Copyright © 2020 Banzai Cloud
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package templatereconciler
    16  
    17  import (
    18  	"emperror.dev/errors"
    19  	"helm.sh/helm/v3/pkg/chartutil"
    20  	"k8s.io/apimachinery/pkg/runtime"
    21  	"sigs.k8s.io/yaml"
    22  
    23  	"github.com/banzaicloud/operator-tools/pkg/helm"
    24  	"github.com/banzaicloud/operator-tools/pkg/reconciler"
    25  	"github.com/banzaicloud/operator-tools/pkg/utils"
    26  )
    27  
    28  func orderedChartObjectsWithState(releaseData *ReleaseData, scheme *runtime.Scheme, caps chartutil.Capabilities) ([]runtime.Object, reconciler.DesiredState, error) {
    29  	objects, err := chartObjects(releaseData, scheme, caps)
    30  	if err != nil {
    31  		return nil, nil, err
    32  	}
    33  
    34  	utils.RuntimeObjects(objects).Sort(utils.InstallResourceOrder)
    35  
    36  	return objects, reconciler.StatePresent, nil
    37  }
    38  
    39  func chartObjects(releaseData *ReleaseData, scheme *runtime.Scheme, caps chartutil.Capabilities) ([]runtime.Object, error) {
    40  	chartDefaultValues, err := helm.GetDefaultValues(releaseData.Chart)
    41  	if err != nil {
    42  		return nil, errors.WrapIff(err, "could not get chart default values for %s", releaseData.ChartName)
    43  	}
    44  
    45  	chartDefaultValuesYaml := helm.Strimap{}
    46  	if err := yaml.Unmarshal(chartDefaultValues, &chartDefaultValuesYaml); err != nil {
    47  		return nil, errors.WrapIff(err, "could not marshal default values for %s", releaseData.ChartName)
    48  	}
    49  
    50  	objects, err := helm.Render(releaseData.Chart, helm.MergeMaps(chartDefaultValuesYaml, releaseData.Values), helm.ReleaseOptions{
    51  		Name:      releaseData.ReleaseName,
    52  		IsInstall: true,
    53  		IsUpgrade: false,
    54  		Namespace: releaseData.Namespace,
    55  		Scheme: scheme,
    56  		Capabilities: caps,
    57  	}, releaseData.ChartName)
    58  	if err != nil {
    59  		return nil, errors.WrapIff(err, "could not render %s helm manifest objects", releaseData.ChartName)
    60  	}
    61  
    62  	return objects, nil
    63  }