Files
kubevela/pkg/commands/dryrun.go
T
wangyike 5c33598fe9 modify controller,webhook,api,chart
solve failed test

add compatibility test for old crd

add app ns for cli

modify compatibility test

solve compatibility problem

add testing for  GetDefinition func with cluster scope CRD

generate code for compatibility-test

move testdata generate to makefile

optimize ci pipeline for compatibility-test
2021-03-09 10:10:48 +08:00

107 lines
2.9 KiB
Go

package commands
import (
"context"
"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"
oamutil "github.com/oam-dev/kubevela/pkg/oam/util"
)
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)
ctx := oamutil.SetNnamespaceInCtx(context.Background(), app.Namespace)
appFile, err := parser.GenerateAppFile(ctx, 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
}