Skip to content

Commit

Permalink
feat(kube-run): support %container_image% in overrides (#6553)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Zaytsev <alexandr.zaytsev@flant.com>
Co-authored-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
  • Loading branch information
nervgh and alexey-igrychev authored Jan 21, 2025
1 parent 77f8fa8 commit c3ab1aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.tags
/.vscode
/.idea
/.run

/docs/vendor
/docs/.ruby-version
Expand Down
23 changes: 17 additions & 6 deletions cmd/werf/kube_run/kube_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
commonCmdData.SetupPlatform(cmd)

cmd.Flags().StringVarP(&cmdData.Pod, "pod", "", os.Getenv("WERF_POD"), "Set created pod name (default $WERF_POD or autogenerated if not specified)")
cmd.Flags().StringVarP(&cmdData.Overrides, "overrides", "", os.Getenv("WERF_OVERRIDES"), "Inline JSON to override/extend any fields in created Pod, e.g. to add imagePullSecrets field (default $WERF_OVERRIDES). %pod_name% and %container_name% will be replaced with names of a created pod and a container.")
cmd.Flags().StringVarP(&cmdData.Overrides, "overrides", "", os.Getenv("WERF_OVERRIDES"), "Inline JSON to override/extend any fields in created Pod, e.g. to add imagePullSecrets field (default $WERF_OVERRIDES). %pod_name%, %container_name%, and %container_image% will be replaced with the names of the created pod, container, and container image, respectively.")
cmd.Flags().StringVarP(&cmdData.RunExtraOptions, "extra-options", "", os.Getenv("WERF_EXTRA_OPTIONS"), "Pass extra options to \"kubectl run\" command, which will create a Pod (default $WERF_EXTRA_OPTIONS)")
cmd.Flags().BoolVarP(&cmdData.Rm, "rm", "", util.GetBoolEnvironmentDefaultTrue("WERF_RM"), "Remove pod and other created resources after command completion (default $WERF_RM or true if not specified)")
cmd.Flags().BoolVarP(&cmdData.RmWithNamespace, "rm-with-namespace", "", util.GetBoolEnvironmentDefaultFalse("WERF_RM_WITH_NAMESPACE"), "Remove also a namespace after command completion (default $WERF_RM_WITH_NAMESPACE or false if not specified)")
Expand Down Expand Up @@ -269,8 +269,6 @@ func runMain(ctx context.Context) error {
}
secret := pod

cmdData.Overrides = templateOverrides(cmdData.Overrides, pod, pod)

_, werfConfig, err := common.GetRequiredWerfConfig(ctx, &commonCmdData, giterminismManager, common.GetWerfConfigOptions(&commonCmdData, false))
if err != nil {
return fmt.Errorf("unable to load werf config: %w", err)
Expand Down Expand Up @@ -378,6 +376,12 @@ func run(ctx context.Context, pod, secret, namespace string, werfConfig *config.
return err
}

cmdData.Overrides = templateOverrides(cmdData.Overrides, map[string]string{
"%pod_name%": pod,
"%container_name%": pod,
"%container_image%": image,
})

var dockerAuthConf imgtypes.DockerAuthConfig
var namedRef reference.Named
if cmdData.AutoPullSecret {
Expand Down Expand Up @@ -912,9 +916,16 @@ func addImagePullSecret(secret string, podOverrides *corev1.Pod) error {
return nil
}

func templateOverrides(line, podName, containerName string) string {
result := strings.ReplaceAll(line, "%container_name%", containerName)
return strings.ReplaceAll(result, "%pod_name%", podName)
func templateOverrides(line string, replacements map[string]string) string {
rules := make([]string, 0)

for pattern, replacement := range replacements {
rules = append(rules, pattern, replacement)
}

replacer := strings.NewReplacer(rules...)

return replacer.Replace(line)
}

func validateCopyFrom() error {
Expand Down
5 changes: 3 additions & 2 deletions docs/_includes/reference/cli/werf_kube_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ werf kube-run [options] [IMAGE_NAME] [-- COMMAND ARG...]
deploy.namespace custom template from werf.yaml or $WERF_NAMESPACE)
--overrides=''
Inline JSON to override/extend any fields in created Pod, e.g. to add imagePullSecrets
field (default $WERF_OVERRIDES). %pod_name% and %container_name% will be replaced with
names of a created pod and a container.
field (default $WERF_OVERRIDES). %pod_name%, %container_name%, and %container_image%
will be replaced with the names of the created pod, container, and container image,
respectively.
--platform=[]
Enable platform emulation when building images with werf, format: OS/ARCH[/VARIANT]
($WERF_PLATFORM or $DOCKER_DEFAULT_PLATFORM by default)
Expand Down

0 comments on commit c3ab1aa

Please sign in to comment.