Files
kubevela/pkg/commands/dryrun.go
Zheng Xi Zhou 2a943c9429 Using Terraform as IaC module in KubeVela (#863)
* Integrate Terraform into KubeVela

Integrated Terrafrom into KubeVela to enable it to deploy
infrastruce resouces by `vela up`

* extend Terraform modules/files as WorkloadDefinition

stop printing terraform log to console

Support one workload consumes two cloud services

Refactor Terraform plugin based on Application Object

add testcase

* refactor code per reviewer's comments

fix rebase issue

* find lost code back

* refactor code to make the modification as little as to that of branch master

* remove blank lines from imports
2021-01-28 18:36:52 +08:00

104 lines
2.7 KiB
Go

package commands
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
corev1alpha2 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/appfile"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
)
type dryRunOptions struct {
cmdutil.IOStreams
applicationFile string
}
// NewDryRunCommand creates `dry-run` command
func NewDryRunCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
o := &dryRunOptions{IOStreams: ioStreams}
cmd := &cobra.Command{
Use: "dry-run",
DisableFlagsInUseLine: true,
Short: "Dry Run an application, and output the conversion result to stdout",
Long: "Dry Run an application, and output the conversion result to stdout",
Example: "vela dry-run",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return c.SetConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
newClient, err := client.New(c.Config, client.Options{Scheme: c.Schema})
if err != nil {
return err
}
dm, err := discoverymapper.New(c.Config)
if err != nil {
return err
}
app, err := readApplicationFromFile(o.applicationFile)
if err != nil {
return errors.WithMessagef(err, "read application file: %s", o.applicationFile)
}
parser := appfile.NewApplicationParser(newClient, dm)
appFile, err := parser.GenerateAppFile(app.Name, app)
if err != nil {
return errors.WithMessage(err, "generate appFile")
}
ac, comps, err := parser.GenerateApplicationConfiguration(appFile, app.Namespace)
if err != nil {
return errors.WithMessage(err, "generate OAM objects")
}
var outs = []interface{}{ac}
for index := range comps {
outs = append(outs, comps[index])
}
result, err := yaml.Marshal(outs)
if err != nil {
return errors.WithMessage(err, "marshal result object in yaml format")
}
o.Info(string(result))
return nil
},
}
cmd.Flags().StringVarP(&o.applicationFile, "file", "f", "./app.yaml", "application file name")
cmd.SetOut(ioStreams.Out)
return cmd
}
func readApplicationFromFile(filename string) (*corev1alpha2.Application, error) {
fileContent, err := ioutil.ReadFile(filepath.Clean(filename))
if err != nil {
return nil, err
}
fileType := filepath.Ext(filename)
switch fileType {
case ".yaml", ".yml":
fileContent, err = yaml.YAMLToJSON(fileContent)
if err != nil {
return nil, err
}
}
app := new(corev1alpha2.Application)
err = json.Unmarshal(fileContent, app)
return app, err
}