github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/builder/builder.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package builder
    21  
    22  import (
    23  	"bufio"
    24  	"embed"
    25  	"encoding/json"
    26  	"strings"
    27  
    28  	"github.com/leaanthony/debme"
    29  	"k8s.io/apimachinery/pkg/util/yaml"
    30  
    31  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    32  	"github.com/1aal/kubeblocks/pkg/gotemplate"
    33  )
    34  
    35  var (
    36  	//go:embed template/*
    37  	cueTemplate embed.FS
    38  )
    39  
    40  func newBuildTemplate(templateName string) (string, error) {
    41  	tmplFs, _ := debme.FS(cueTemplate, "template")
    42  	if tmlBytes, err := tmplFs.ReadFile(templateName); err != nil {
    43  		return "", err
    44  	} else {
    45  		return string(tmlBytes), nil
    46  	}
    47  }
    48  
    49  func BuildFromTemplate(values *gotemplate.TplValues, templateName string) (string, error) {
    50  	tpl, err := newBuildTemplate(templateName)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  
    55  	engine := gotemplate.NewTplEngine(values, nil, templateName, nil, nil)
    56  	rendered, err := engine.Render(tpl)
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  	return rendered, nil
    61  }
    62  
    63  func BuildResourceFromYaml[T any](obj T, bYaml string) (*T, error) {
    64  	var ret map[string]interface{}
    65  
    66  	content, err := yaml.NewYAMLReader(bufio.NewReader(strings.NewReader(bYaml))).Read()
    67  	if err != nil {
    68  		return nil, cfgcore.WrapError(err, "failed to read the cluster yaml")
    69  	}
    70  	err = yaml.Unmarshal(content, &ret)
    71  	if err != nil {
    72  		return nil, cfgcore.WrapError(err, "failed to unmarshal the cluster yaml")
    73  	}
    74  
    75  	contentToJSON, err := yaml.ToJSON(content)
    76  	if err != nil {
    77  		return nil, cfgcore.WrapError(err, "Unable to convert configuration to json")
    78  	}
    79  	if err := json.Unmarshal(contentToJSON, &obj); err != nil {
    80  		return nil, cfgcore.WrapError(err, "failed to unmarshal the cluster")
    81  	}
    82  	return &obj, nil
    83  }