Skip to content

Commit

Permalink
Matchable resource support in flytectl for task resource attributes (#63
Browse files Browse the repository at this point in the history
)

* Adding commands for task resource attributes support

Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored May 11, 2021
1 parent a0632f0 commit 661de8d
Show file tree
Hide file tree
Showing 82 changed files with 2,961 additions and 259 deletions.
1 change: 1 addition & 0 deletions flytectl/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bin
_test
./config.yaml
docs/build/*
cmd/get/temp-output-file
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package subcommand

//go:generate pflags TaskResourceAttrDeleteConfig --default-var DefaultTaskResourceDelConfig

// TaskResourceAttrDeleteConfig Matchable resource attributes configuration passed from command line
type TaskResourceAttrDeleteConfig struct {
AttrFile string `json:"attrFile" pflag:",attribute file name to be used for delete attribute for the resource type."`
}

var DefaultTaskResourceDelConfig = &TaskResourceAttrDeleteConfig{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package subcommand

//go:generate pflags TaskResourceAttrFetchConfig --default-var DefaultTaskResourceFetchConfig

type TaskResourceAttrFetchConfig struct {
AttrFile string `json:"attrFile" pflag:",attribute file name to be used for generating attribute for the resource type."`
}

var DefaultTaskResourceFetchConfig = &TaskResourceAttrFetchConfig{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package subcommand

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"

cmdUtil "github.com/flyteorg/flytectl/pkg/commandutils"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flytestdlib/logger"

"sigs.k8s.io/yaml"
)

// TaskResourceAttrFileConfig shadow config for TaskResourceAttribute.
// The shadow config is not using ProjectDomainAttribute/Workflowattribute directly inorder to simplify the inputs.
// As the same structure is being used for both ProjectDomainAttribute/Workflowattribute
type TaskResourceAttrFileConfig struct {
Project string `json:"project"`
Domain string `json:"domain"`
Workflow string `json:"workflow,omitempty"`
*admin.TaskResourceAttributes
}

// WriteConfigToFile used for marshaling the config to a file which can then be used for update/delete
func (t TaskResourceAttrFileConfig) WriteConfigToFile(fileName string) error {
d, err := yaml.Marshal(t)
if err != nil {
return fmt.Errorf("error: %v", err)
}
if _, err = os.Stat(fileName); err == nil {
if !cmdUtil.AskForConfirmation(fmt.Sprintf("warning file %v will be overwritten", fileName)) {
return fmt.Errorf("backup the file before continuing")
}
}
return ioutil.WriteFile(fileName, d, 0600)
}

// Dumps the json representation of the TaskResourceAttrFileConfig
func (t TaskResourceAttrFileConfig) String() string {
tj, err := json.Marshal(t)
if err != nil {
fmt.Println(err)
return "marshaling error"
}
return fmt.Sprintf("%s\n", tj)
}

// ReadConfigFromFile used for unmarshaling the config from a file which is used for update/delete
func (t *TaskResourceAttrFileConfig) ReadConfigFromFile(fileName string) error {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return fmt.Errorf("unable to read from %v yaml file", fileName)
}
if err = yaml.UnmarshalStrict(data, t); err != nil {
return err
}
return nil
}

// MatchableAttributeDecorator decorator over TaskResourceAttributes. Similar decorator would exist for other MatchingAttributes
func (t *TaskResourceAttrFileConfig) MatchableAttributeDecorator() *admin.MatchingAttributes {
return &admin.MatchingAttributes{
Target: &admin.MatchingAttributes_TaskResourceAttributes{
TaskResourceAttributes: t.TaskResourceAttributes,
},
}
}

func (t TaskResourceAttrFileConfig) DumpTaskResourceAttr(ctx context.Context, fileName string) {
// Dump empty task resource attr for editing
// Write config to file if filename provided in the command
if len(fileName) > 0 {
// Read the config from the file and update the TaskResourceAttrFileConfig with the TaskResourceAttrConfig
if err := t.WriteConfigToFile(fileName); err != nil {
fmt.Printf("error dumping in file due to %v", err)
logger.Warnf(ctx, "error dumping in file due to %v", err)
return
}
fmt.Printf("wrote the config to file %v", fileName)
} else {
fmt.Printf("%v", t)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package subcommand

//go:generate pflags TaskResourceAttrUpdateConfig --default-var DefaultTaskResourceUpdateConfig

// TaskResourceAttrUpdateConfig Matchable resource attributes configuration passed from command line
type TaskResourceAttrUpdateConfig struct {
AttrFile string `json:"attrFile" pflag:",attribute file name to be used for updating attribute for the resource type."`
}

var DefaultTaskResourceUpdateConfig = &TaskResourceAttrUpdateConfig{}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 661de8d

Please sign in to comment.