Skip to content

Commit

Permalink
image pull progress
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani committed Nov 15, 2024
1 parent 7c423d2 commit e61f88f
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 136 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
![coverage](https://krkn-chaos.github.io/krkn-lib-docs/coverage_badge_krknctl.svg)

![krknctl_logo.jpg](media/krknctl_logo.png)

# krknctl
## [krkn](https://github.com/krkn-chaos/krkn) chaos engineering tool CLI

Expand Down
20 changes: 0 additions & 20 deletions cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,6 @@ import (
"time"
)

func customCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Esempio di mappa di ID e descrizioni
completions := map[string]string{
"0": "questo rappresenta l'id del container 0",
"1": "questo rappresenta l'id del container 1",
"2": "questo rappresenta l'id del container 2",
"3": "questo rappresenta l'id del container 3",
}

var results []string
for id, description := range completions {
if strings.HasPrefix(id, toComplete) {
// Formatta l'output per mostrare l'ID seguito dalla descrizione
results = append(results, fmt.Sprintf("%s\t%s", id, description))
}
}

return results, cobra.ShellCompDirectiveNoFileComp
}

func NewAttachCmd(scenarioOrchestrator *scenario_orchestrator.ScenarioOrchestrator, config config.Config) *cobra.Command {
var command = &cobra.Command{
Use: "attach",
Expand Down
18 changes: 0 additions & 18 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ func NewDescribeCommand(factory *factory.ProviderFactory, config config.Config)
Long: `describes a scenario`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}
*/

dataSource := BuildDataSource(config, false, nil)
provider := GetProvider(false, factory)

Expand All @@ -40,15 +31,6 @@ func NewDescribeCommand(factory *factory.ProviderFactory, config config.Config)

},
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return err
}
*/

dataSource := BuildDataSource(config, false, nil)
spinner := NewSpinnerWithSuffix("fetching scenario details...")
spinner.Start()
Expand Down
9 changes: 0 additions & 9 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,6 @@ func NewGraphScaffoldCommand(factory *provider_factory.ProviderFactory, config c
Long: `Scaffolds a dependency graph based run`,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}
*/

dataSource := BuildDataSource(config, false, nil)
dataProvider := GetProvider(false, factory)

Expand Down
8 changes: 0 additions & 8 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ func NewListScenariosCommand(factory *provider_factory.ProviderFactory, config c
Long: `list available krkn-hub scenarios`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return err
}
*/
dataSource := BuildDataSource(config, false, nil)
provider := GetProvider(false, factory)
s := NewSpinnerWithSuffix("fetching scenarios...")
Expand Down
13 changes: 1 addition & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@ import (

func Execute(providerFactory *factory.ProviderFactory, scenarioOrchestrator *scenario_orchestrator.ScenarioOrchestrator, config config.Config) {

rootCmd := NewRootCommand(providerFactory, config)
// TODO: json output + offline repos
/*
var jsonFlag bool
var offlineFlag bool
var offlineRepoConfig string
rootCmd.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "Output in JSON")
rootCmd.PersistentFlags().BoolVarP(&offlineFlag, "offline", "o", false, "Offline mode")
rootCmd.PersistentFlags().StringVarP(&offlineRepoConfig, "offline-repo-config", "r", "", "Offline repository config file")
rootCmd.MarkFlagsRequiredTogether("offline", "offline-repo-config")
*/

rootCmd := NewRootCommand(config)
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh]",
Short: "Genera script di completamento per bash o zsh",
Expand Down
56 changes: 17 additions & 39 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
DisableFlagParsing: false,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}
*/

dataSource := BuildDataSource(config, false, nil)
provider := GetProvider(false, factory)

Expand All @@ -46,14 +37,6 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
},

PreRunE: func(cmd *cobra.Command, args []string) error {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return err
}
*/
dataSource := BuildDataSource(config, false, nil)
provider := GetProvider(false, factory)
scenarioDetail, err := provider.GetScenarioDetail(args[0], dataSource)
Expand All @@ -79,27 +62,20 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: datasource offline TBD
/*
offline, err := cmd.Flags().GetBool("offline")
offlineRepo, err := cmd.Flags().GetString("offline-repo-config")
if err != nil {
return err
}
*/

(*scenarioOrchestrator).PrintContainerRuntime()
spinner := NewSpinnerWithSuffix("validating input...")
dataSource := BuildDataSource(config, false, nil)

// Starts validating input message
spinner.Start()

runDetached := false

provider := GetProvider(false, factory)
scenarioDetail, err := provider.GetScenarioDetail(args[0], dataSource)
if err != nil {
return err
}
spinner.Stop()

if err != nil {
return err
Expand Down Expand Up @@ -180,37 +156,39 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
volumes[fileSrcDst[0]] = fileSrcDst[1]
}

/*
if value == nil && field.Required == false {
fmt.Println(fmt.Sprintf("%s: nil default but not required", *field.Name))
*/

}

}
// stops the spinner before printing the input table to not disrupt it
spinner.Stop()

tbl := NewEnvironmentTable(environment)
tbl.Print()
fmt.Print("\n")
// restarts the spinner to present image pull progress
spinner.Suffix = "pulling scenario image..."
spinner.Start()

//WIP
socket, err := (*scenarioOrchestrator).GetContainerRuntimeSocket(nil)
if err != nil {
return err
}
startTime := time.Now()
containerName := utils.GenerateContainerName(config, scenarioDetail.Name, nil)
if runDetached == false {
_, err := color.New(color.FgGreen, color.Underline).Println("hit CTRL+C to terminate the scenario")
if err != nil {
return err
}
_, err = (*scenarioOrchestrator).RunAttached(config.GetQuayImageUri()+":"+scenarioDetail.Name, containerName, *socket, environment, false, volumes, os.Stdout, os.Stderr)
commChan := make(chan *string)
go func() {
for msg := range commChan {
spinner.Suffix = *msg
}
spinner.Stop()
}()
_, err = (*scenarioOrchestrator).RunAttached(config.GetQuayImageUri()+":"+scenarioDetail.Name, containerName, *socket, environment, false, volumes, os.Stdout, os.Stderr, &commChan)
if err != nil {
return err
}
} else {
containerId, _, err := (*scenarioOrchestrator).Run(config.GetQuayImageUri()+":"+scenarioDetail.Name, containerName, *socket, environment, false, volumes)
containerId, _, err := (*scenarioOrchestrator).Run(config.GetQuayImageUri()+":"+scenarioDetail.Name, containerName, *socket, environment, false, volumes, nil)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func NewSpinnerWithSuffix(suffix string) *spinner.Spinner {
return s
}

func NewRootCommand(factory *factory.ProviderFactory, krknctl_config config.Config) *cobra.Command {
func NewRootCommand(krknctlConfig config.Config) *cobra.Command {
var rootCmd = &cobra.Command{
Use: "krknctl",
Short: "krkn CLI",
Long: `krkn Command Line Interface`,
Version: krknctl_config.Version,
Version: krknctlConfig.Version,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/scenario_orchestrator/common_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func CommonRunGraph(scenarios models.ScenarioSet, resolvedGraph models.ResolvedG

go func() {
defer wg.Done()
_, _ = orchestrator.RunAttached(scenario.Image, containerName, containerRuntimeUri, env, cache, volumes, file, file)
_, _ = orchestrator.RunAttached(scenario.Image, containerName, containerRuntimeUri, env, cache, volumes, file, file, nil)
}()

}
Expand All @@ -60,9 +60,9 @@ func CommonRunGraph(scenarios models.ScenarioSet, resolvedGraph models.ResolvedG
commChannel <- nil
}

func CommonRunAttached(image string, containerName string, containerRuntimeUri string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, c ScenarioOrchestrator) (*string, error) {
func CommonRunAttached(image string, containerName string, containerRuntimeUri string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, c ScenarioOrchestrator, commChan *chan *string) (*string, error) {

containerId, ctx, err := c.Run(image, containerName, containerRuntimeUri, env, cache, volumeMounts)
containerId, ctx, err := c.Run(image, containerName, containerRuntimeUri, env, cache, volumeMounts, commChan)
if err != nil {
return nil, err
}
Expand Down
25 changes: 14 additions & 11 deletions pkg/scenario_orchestrator/docker/scenario_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ScenarioOrchestrator struct {
ContainerRuntime orchestrator_models.ContainerRuntime
}

func (c *ScenarioOrchestrator) Run(image string, containerName string, containerRuntimeUri string, env map[string]string, cache bool, volumeMounts map[string]string) (*string, *context.Context, error) {
func (c *ScenarioOrchestrator) Run(image string, containerName string, containerRuntimeUri string, env map[string]string, cache bool, volumeMounts map[string]string, commChan *chan *string) (*string, *context.Context, error) {

ctx, err := c.Connect(containerRuntimeUri)
if err != nil {
Expand All @@ -47,7 +47,7 @@ func (c *ScenarioOrchestrator) Run(image string, containerName string, container
return nil, nil, err
}
if cache == false || exists == false {
err := pullImage(ctx, cli, image)
err := pullImage(ctx, cli, image, commChan)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -86,11 +86,6 @@ func (c *ScenarioOrchestrator) Run(image string, containerName string, container
return &resp.ID, &ctxWithClient, nil
}

func (c *ScenarioOrchestrator) RunSerialPlan() {
//TODO implement me
panic("implement me")
}

func (c *ScenarioOrchestrator) Attach(containerId *string, ctx *context.Context, signalChannel chan os.Signal, stdout io.Writer, stderr io.Writer) (bool, error) {
cli, err := dockerClientFromContext(*ctx)
if err != nil {
Expand Down Expand Up @@ -218,7 +213,7 @@ func ImageExists(ctx context.Context, cli *client.Client, imageName, expectedDig
return false, nil
}

func pullImage(ctx context.Context, cli *client.Client, imageName string) error {
func pullImage(ctx context.Context, cli *client.Client, imageName string, commChan *chan *string) error {
reader, err := cli.ImagePull(ctx, imageName, images.PullOptions{})
if err != nil {
return err
Expand All @@ -243,11 +238,19 @@ func pullImage(ctx context.Context, cli *client.Client, imageName string) error
totalSize = int(message.Progress.Total)
}
if message.Progress.Current > 0 {
fmt.Println(fmt.Sprintf("Downloading image %s: %d/%d MB ", imageName, int(message.Progress.Current/1024/1024), totalSize/1024/1024))
if commChan != nil {
message := fmt.Sprintf("Downloading image %s %s ", imageName, message.Progress.String())
*commChan <- &message
}

}
}
}

if commChan != nil {
close(*commChan)
}

return nil
}

Expand Down Expand Up @@ -383,8 +386,8 @@ func (c *ScenarioOrchestrator) AttachWait(containerId *string, ctx *context.Cont
return &interrupted, nil
}

func (c *ScenarioOrchestrator) RunAttached(image string, containerName string, containerRuntimeUri string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer) (*string, error) {
containerId, err := scenario_orchestrator.CommonRunAttached(image, containerName, containerRuntimeUri, env, cache, volumeMounts, stdout, stderr, c)
func (c *ScenarioOrchestrator) RunAttached(image string, containerName string, containerRuntimeUri string, env map[string]string, cache bool, volumeMounts map[string]string, stdout io.Writer, stderr io.Writer, commChan *chan *string) (*string, error) {
containerId, err := scenario_orchestrator.CommonRunAttached(image, containerName, containerRuntimeUri, env, cache, volumeMounts, stdout, stderr, c, commChan)
return containerId, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestscenarioOrchestrator_Run(t *testing.T) {
assert.NotNil(t, socket)

fmt.Println("CONTAINER SOCKET -> " + *socket)
containerId, err := cm.RunAttached(conf.GetQuayImageUri()+":"+scenario.Name, scenario.Name, *socket, env, false, map[string]string{}, os.Stdout, os.Stderr)
containerId, err := cm.RunAttached(conf.GetQuayImageUri()+":"+scenario.Name, scenario.Name, *socket, env, false, map[string]string{}, os.Stdout, os.Stderr, nil)
if err != nil {
fmt.Println("ERROR -> " + err.Error())
}
Expand Down
Loading

0 comments on commit e61f88f

Please sign in to comment.