Skip to content

Commit

Permalink
Render descriptions in flytectl (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayitbeegh authored Aug 5, 2021
1 parent 2fd5e56 commit eaeb998
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 69 deletions.
65 changes: 47 additions & 18 deletions flytectl/cmd/get/execution_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import (
"io/ioutil"
"os"

"gopkg.in/yaml.v3"

cmdUtil "github.com/flyteorg/flytectl/pkg/commandutils"
"github.com/flyteorg/flyteidl/clients/go/coreutils"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"

"sigs.k8s.io/yaml"
)

// ExecutionConfig is duplicated struct from create with the same structure. This is to avoid the circular dependency.
// ExecutionConfig is duplicated struct from create with the same structure. This is to avoid the circular dependency. Only works with go-yaml.
// TODO : replace this with a cleaner design
type ExecutionConfig struct {
TargetDomain string `json:"targetDomain"`
TargetProject string `json:"targetProject"`
KubeServiceAcct string `json:"kubeServiceAcct"`
IamRoleARN string `json:"iamRoleARN"`
Workflow string `json:"workflow,omitempty"`
Task string `json:"task,omitempty"`
Version string `json:"version"`
Inputs map[string]interface{} `json:"inputs"`
IamRoleARN string `yaml:"iamRoleARN"`
Inputs map[string]yaml.Node `yaml:"inputs"`
KubeServiceAcct string `yaml:"kubeServiceAcct"`
TargetDomain string `yaml:"targetDomain"`
TargetProject string `yaml:"targetProject"`
Task string `yaml:"task,omitempty"`
Version string `yaml:"version"`
Workflow string `yaml:"workflow,omitempty"`
}

func WriteExecConfigToFile(executionConfig ExecutionConfig, fileName string) error {
Expand Down Expand Up @@ -78,16 +78,27 @@ func TaskInputs(task *admin.Task) map[string]*core.Variable {
return task.Closure.CompiledTask.Template.Interface.Inputs.Variables
}

func ParamMapForTask(task *admin.Task) (map[string]interface{}, error) {
func ParamMapForTask(task *admin.Task) (map[string]yaml.Node, error) {
taskInputs := TaskInputs(task)
paramMap := make(map[string]interface{}, len(taskInputs))
paramMap := make(map[string]yaml.Node, len(taskInputs))
for k, v := range taskInputs {
varTypeValue, err := coreutils.MakeDefaultLiteralForType(v.Type)
if err != nil {
fmt.Println("error creating default value for literal type ", v.Type)
return nil, err
}
if paramMap[k], err = coreutils.ExtractFromLiteral(varTypeValue); err != nil {
var nativeLiteral interface{}
if nativeLiteral, err = coreutils.ExtractFromLiteral(varTypeValue); err != nil {
return nil, err
}

if k == v.Description {
// a: # a isn't very helpful
paramMap[k], err = getCommentedYamlNode(nativeLiteral, "")
} else {
paramMap[k], err = getCommentedYamlNode(nativeLiteral, v.Description)
}
if err != nil {
return nil, err
}
}
Expand All @@ -105,24 +116,42 @@ func WorkflowParams(lp *admin.LaunchPlan) map[string]*core.Parameter {
return lp.Spec.DefaultInputs.Parameters
}

func ParamMapForWorkflow(lp *admin.LaunchPlan) (map[string]interface{}, error) {
func ParamMapForWorkflow(lp *admin.LaunchPlan) (map[string]yaml.Node, error) {
workflowParams := WorkflowParams(lp)
paramMap := make(map[string]interface{}, len(workflowParams))
paramMap := make(map[string]yaml.Node, len(workflowParams))
for k, v := range workflowParams {
varTypeValue, err := coreutils.MakeDefaultLiteralForType(v.Var.Type)
if err != nil {
fmt.Println("error creating default value for literal type ", v.Var.Type)
return nil, err
}
if paramMap[k], err = coreutils.ExtractFromLiteral(varTypeValue); err != nil {
var nativeLiteral interface{}
if nativeLiteral, err = coreutils.ExtractFromLiteral(varTypeValue); err != nil {
return nil, err
}
// Override if there is a default value
if paramsDefault, ok := v.Behavior.(*core.Parameter_Default); ok {
if paramMap[k], err = coreutils.ExtractFromLiteral(paramsDefault.Default); err != nil {
if nativeLiteral, err = coreutils.ExtractFromLiteral(paramsDefault.Default); err != nil {
return nil, err
}
}
if k == v.Var.Description {
// a: # a isn't very helpful
paramMap[k], err = getCommentedYamlNode(nativeLiteral, "")
} else {
paramMap[k], err = getCommentedYamlNode(nativeLiteral, v.Var.Description)
}

if err != nil {
return nil, err
}
}
return paramMap, nil
}

func getCommentedYamlNode(input interface{}, comment string) (yaml.Node, error) {
var node yaml.Node
err := node.Encode(input)
node.LineComment = comment
return node, err
}
31 changes: 29 additions & 2 deletions flytectl/cmd/get/launch_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ var launchplanColumns = []printer.Column{
{Header: "Type", JSONPath: "$.closure.compiledTask.template.type"},
{Header: "State", JSONPath: "$.spec.state"},
{Header: "Schedule", JSONPath: "$.spec.entityMetadata.schedule"},
{Header: "Inputs", JSONPath: "$.closure.expectedInputs.parameters." + printer.DefaultFormattedDescriptionsKey + ".var.description"},
{Header: "Outputs", JSONPath: "$.closure.expectedOutputs.variables." + printer.DefaultFormattedDescriptionsKey + ".description"},
}

// Column structure for get all launchplans
Expand All @@ -122,6 +124,21 @@ func LaunchplanToProtoMessages(l []*admin.LaunchPlan) []proto.Message {
return messages
}

func LaunchplanToTableProtoMessages(l []*admin.LaunchPlan) []proto.Message {
messages := make([]proto.Message, 0, len(l))
for _, m := range l {
m := proto.Clone(m).(*admin.LaunchPlan)
if m.Closure.ExpectedInputs != nil {
printer.FormatParameterDescriptions(m.Closure.ExpectedInputs.Parameters)
}
if m.Closure.ExpectedOutputs != nil {
printer.FormatVariableDescriptions(m.Closure.ExpectedOutputs.Variables)
}
messages = append(messages, m)
}
return messages
}

func getLaunchPlanFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
launchPlanPrinter := printer.Printer{}
var launchPlans []*admin.LaunchPlan
Expand All @@ -134,8 +151,13 @@ func getLaunchPlanFunc(ctx context.Context, args []string, cmdCtx cmdCore.Comman
return err
}
logger.Debugf(ctx, "Retrieved %v launch plans", len(launchPlans))
err = launchPlanPrinter.Print(config.GetConfig().MustOutputFormat(), launchplanColumns,
LaunchplanToProtoMessages(launchPlans)...)
if config.GetConfig().MustOutputFormat() == printer.OutputFormatTABLE {
err = launchPlanPrinter.Print(config.GetConfig().MustOutputFormat(), launchplanColumns,
LaunchplanToTableProtoMessages(launchPlans)...)
} else {
err = launchPlanPrinter.Print(config.GetConfig().MustOutputFormat(), launchplanColumns,
LaunchplanToProtoMessages(launchPlans)...)
}
if err != nil {
return err
}
Expand All @@ -148,8 +170,13 @@ func getLaunchPlanFunc(ctx context.Context, args []string, cmdCtx cmdCore.Comman
}

logger.Debugf(ctx, "Retrieved %v launch plans", len(launchPlans))
if config.GetConfig().MustOutputFormat() == printer.OutputFormatTABLE {
return launchPlanPrinter.Print(config.GetConfig().MustOutputFormat(), launchplansColumns,
LaunchplanToTableProtoMessages(launchPlans)...)
}
return launchPlanPrinter.Print(config.GetConfig().MustOutputFormat(), launchplansColumns,
LaunchplanToProtoMessages(launchPlans)...)

}

// FetchLPForName fetches the launchplan give it name.
Expand Down
Loading

0 comments on commit eaeb998

Please sign in to comment.