-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement grid-compose #1130
base: development
Are you sure you want to change the base?
implement grid-compose #1130
Conversation
grid-compose/cmd/root.go
Outdated
rootCmd.AddCommand(downCmd) | ||
} | ||
|
||
type App struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the right place, should be in internal/app.go or similar
grid-compose/cmd/root.go
Outdated
Specs types.Specs | ||
} | ||
|
||
func NewApp(net, mne, filePath string) (App, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
factory function should be in internal/app.go and use better names eg mnemonic instead of mne
grid-compose/cmd/root.go
Outdated
}, nil | ||
} | ||
|
||
func LoadSpecsFromFile(filePath string) (types.Specs, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create a loadSpecsFromReader with io.Reader and don't bother about paths, opening/closing the files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and of course, not the right place, internal/app.go
grid-compose/cmd/root.go
Outdated
return specs, nil | ||
} | ||
|
||
func Execute() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this to the beginning of the file
grid-compose/cmd/root.go
Outdated
|
||
var ( | ||
app App | ||
configFile string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configPath
grid-compose/cmd/root.go
Outdated
Short: "Grid-Compose is a tool for running multi-vm applications on TFGrid defined using a Yaml formatted file.", | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
app, err = NewApp(network, mnemonic, configFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if app, err := ... ; err != nil {
log.Fatal ...
}
grid-compose/cmd/up.go
Outdated
}, | ||
} | ||
|
||
func up(ctx context.Context) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move that into internal/app.go and make it a method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and same for the rest..
grid-compose/cmd/up.go
Outdated
} | ||
|
||
for key, val := range results { | ||
fmt.Printf("%s vm addresses:\n", key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why mixing logging with printing?
grid-compose/internal/utils.go
Outdated
"github.com/threefoldtech/zos/pkg/gridtypes/zos" | ||
) | ||
|
||
func GetProjectName(key string, twinId uint32) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method on the app
grid-compose/internal/utils.go
Outdated
return fmt.Sprintf("compose/%v/%v", twinId, key) | ||
} | ||
|
||
func GetVmAddresses(vm workloads.VM) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move into vmaddresses.go in internal
grid-compose/internal/utils.go
Outdated
return res | ||
} | ||
|
||
func GetRandomMyceliumIPSeed() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move into internal/mycelium_seed.go
grid-compose/pkg/types/types.go
Outdated
type Service struct { | ||
Flist string `yaml:"flist"` | ||
Entrypoint string `yaml:"entrypoint,omitempty"` | ||
Environment KVMap `yaml:"environment"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for the new type here map[string]string, specially if you don't have any new methods reacting on that type and doesn't provide any type safety
grid-compose/pkg/types/types.go
Outdated
|
||
type Service struct { | ||
Flist string `yaml:"flist"` | ||
Entrypoint string `yaml:"entrypoint,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is required
grid-compose/pkg/types/types.go
Outdated
Test []string `yaml:"test"` | ||
Interval string `yaml:"interval"` | ||
Timeout string `yaml:"timeout"` | ||
Retries int `yaml:"retries"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think of the type used, is that retries going to be a negative at anypoint? use uint or uint8 instead
grid-compose/pkg/types/types.go
Outdated
} | ||
|
||
type HealthCheck struct { | ||
Test []string `yaml:"test"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that a test command or what exactly?add a comment next to it
grid-compose/pkg/types/types.go
Outdated
|
||
type KVMap map[string]string | ||
|
||
func (m *KVMap) UnmarshalYAML(value *yaml.Node) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this
grid-compose/cmd/down.go
Outdated
projectName := internal.GetProjectName(key, app.Client.TwinID) | ||
log.Info().Str("projectName", projectName).Msg("canceling deployments") | ||
if err := app.Client.CancelByProjectName(projectName); err != nil { | ||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrap the error here was e.g fmt.Errorf("failed to cancel project %s: %w", projectName, err)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to to a method on App struct in app.go
grid-compose/cmd/down.go
Outdated
}, | ||
} | ||
|
||
func down() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to a method on App struct in internal/app.go
grid-compose/cmd/ps.go
Outdated
}, | ||
} | ||
|
||
func ps(ctx context.Context) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to a method on the app
grid-compose/cmd/ps.go
Outdated
// try to get contracts with the old project name format "<name>" | ||
contracts, err := t.ContractsGetter.ListContractsOfProjectName(projectName, true) | ||
if err != nil { | ||
return workloads.Deployment{}, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider returning a wrapper error with more context
grid-compose/cmd/version.go
Outdated
"github.com/spf13/cobra" | ||
) | ||
|
||
var version = "v0.0.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check how to properly do the versioning https://github.com/threefoldtech/tfgrid-sdk-go/blob/0c4a82b2e3fe68c8b70d4fac89bb3e31fecf8566/grid-cli/cmd/version.go
grid-compose/pkg/types/types.go
Outdated
@@ -0,0 +1,70 @@ | |||
package types | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these fields require validation e.g valid flist url, positive disk space, valid node id, depends_on a service and so on.
grid-compose/internal/utils.go
Outdated
} | ||
|
||
func GetVmAddresses(vm workloads.VM) string { | ||
var res string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a string Builder for this https://pkg.go.dev/strings#Builder
Please review the project structure, cobra commands should ONLY invoke methods defined on the app, don't leak any code logic into your commands. make sure the input is validated with decent error messages as well. |
still needs to handle the dependency resolution "and guard against cycles" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good job ya eyad
i have some comments on the project structure
grid-compose/cmd/down.go
Outdated
}, | ||
} | ||
|
||
func init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move registering commands to the init() in root.go
grid-compose/cmd/ps.go
Outdated
|
||
func init() { | ||
psCmd.PersistentFlags().BoolP("verbose", "v", false, "all information about deployed services") | ||
psCmd.PersistentFlags().StringP("output", "o", "", "output result to a file") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can drop this and simply use bash redirecting. grid-compose ps > dep.json
grid-compose/cmd/ps.go
Outdated
Run: func(cmd *cobra.Command, args []string) { | ||
flags := cmd.Flags() | ||
|
||
if err := app.Ps(cmd.Context(), flags); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invoke the flags here, and pass to the method only the values it needs
grid-compose/cmd/root.go
Outdated
|
||
var ( | ||
app *internal.App | ||
configPath string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid the global variable, we could add the app to the context. it will shared between the commands
grid-compose/cmd/root.go
Outdated
} | ||
|
||
func init() { | ||
network = os.Getenv("NETWORK") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move getting the env to the Run in rootCmd
grid-compose/internal/app.go
Outdated
@@ -0,0 +1,372 @@ | |||
package internal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a better structure for this file could be a separate package internal/app
have the app.go
file for the app struct & factory method. and a file for each command with its utils ps.go
, down.go
, ...
grid-compose/internal/app.go
Outdated
|
||
// App is the main application struct that holds the client and the config data | ||
type App struct { | ||
Client *deployer.TFPluginClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it needed to be public?
grid-compose/internal/app.go
Outdated
// App is the main application struct that holds the client and the config data | ||
type App struct { | ||
Client *deployer.TFPluginClient | ||
Config *config.Config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it is not used anywhere? if so remove it
grid-compose/internal/app.go
Outdated
|
||
if !verbose { | ||
if !dlAdded { | ||
output.WriteString(fmt.Sprintf("%-15s | %-15s | %-15s | %-15s | %-10s | %s\n", contractDlData.Name, vm.NetworkName, vm.Name, vm.Mounts[0].DiskName, wl.Result.State, utils.GetVmAddresses(vm))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this formatting be abstracted? get the result as a map/array then pass it to some method maybe in pkg/logging/print_resutl.go
it will be easier to make changes or even use a different pkg for formatting
grid-compose/internal/app.go
Outdated
} | ||
|
||
// freeCRU is not in NodeFilter? | ||
var freeMRU, freeSRU uint64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes freeCRU is not a filter. due to cores overprovisioning mulitple vms can use the same cpu cores, use totalCRU instead just making sure the node has the cores you need
refactor: refactor folder structure and add more examples
refactor: refcator project structure
tests: test each example manually docs: populate readme file, add future_work, add config.md, and extend cases
…e and move its components to app
…dependency_resolution implementation of a docker-compose-like cli for managing deployment of services simultaneously
…into development_compose_init
refactor: move config to pkg and create storage parser validation: add extensive validation for services and config
Let's unblock this |
Description
Building the grid-compose cli tool by implementing several commands adapted by docker-compose(up, down, version, etc)
Changes
Related Issues
Checklist