Skip to content

Commit

Permalink
Refactor image pulling logic to support different pull commands
Browse files Browse the repository at this point in the history
- Updated the image pulling mechanism to conditionally format the pull command based on the command type (e.g., crictl).
- Improved code readability by reducing redundancy in command construction.

This change enhances flexibility in handling image pulls across different environments.

Signed-off-by: LazyBusyYang <[email protected]>
  • Loading branch information
LazyBusyYang committed Jan 19, 2025
1 parent 22971d7 commit be282e4
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions cmd/kk/pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,30 @@ func (images *Images) PullImages(runtime connector.Runtime, kubeConf *common.Kub

host := runtime.RemoteHost()

for _, image := range images.Images {
switch {
case host.IsRole(common.Master) && image.Group == kubekeyapiv1alpha2.Master && image.Enable,
host.IsRole(common.Worker) && image.Group == kubekeyapiv1alpha2.Worker && image.Enable,
(host.IsRole(common.Master) || host.IsRole(common.Worker)) && image.Group == kubekeyapiv1alpha2.K8s && image.Enable,
host.IsRole(common.ETCD) && image.Group == kubekeyapiv1alpha2.Etcd && image.Enable:

logger.Log.Messagef(host.GetName(), "downloading image: %s", image.ImageName())
if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("env PATH=$PATH %s pull %s --platform %s", pullCmd, image.ImageName(), host.GetArch()), false); err != nil {
return errors.Wrap(err, "pull image failed")
}
default:
continue
}

}
return nil
for _, image := range images.Images {
switch {
case host.IsRole(common.Master) && image.Group == kubekeyapiv1alpha2.Master && image.Enable,
host.IsRole(common.Worker) && image.Group == kubekeyapiv1alpha2.Worker && image.Enable,
(host.IsRole(common.Master) || host.IsRole(common.Worker)) && image.Group == kubekeyapiv1alpha2.K8s && image.Enable,
host.IsRole(common.ETCD) && image.Group == kubekeyapiv1alpha2.Etcd && image.Enable:

logger.Log.Messagef(host.GetName(), "downloading image: %s", image.ImageName())

var pullCommand string
if pullCmd == "crictl" {
pullCommand = fmt.Sprintf("env PATH=$PATH %s pull %s", pullCmd, image.ImageName())
} else {
pullCommand = fmt.Sprintf("env PATH=$PATH %s pull %s --platform %s", pullCmd, image.ImageName(), host.GetArch())
}

if _, err := runtime.GetRunner().SudoCmd(pullCommand, false); err != nil {
return errors.Wrap(err, "pull image failed")
}
default:
continue
}
}
return nil
}

// DefaultRegistry is used to get default registry address.
Expand Down

0 comments on commit be282e4

Please sign in to comment.