From 686b40293734609a1b2ad9523f0a74a392717411 Mon Sep 17 00:00:00 2001 From: Ilya Lesikov Date: Thu, 21 Apr 2022 15:32:37 +0300 Subject: [PATCH] feat(kube-run): pod and container name can be used in --overrides %container_name% and %pod_name% now replaced with container/pod name in `kube-run --overrides`. Signed-off-by: Ilya Lesikov --- cmd/werf/kube_run/kube_run.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/werf/kube_run/kube_run.go b/cmd/werf/kube_run/kube_run.go index bd94a4e179..5f328179a2 100644 --- a/cmd/werf/kube_run/kube_run.go +++ b/cmd/werf/kube_run/kube_run.go @@ -168,7 +168,7 @@ func NewCmd() *cobra.Command { common.SetupPlatform(&commonCmdData, 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)") + 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.ExtraOptions, "extra-options", "", os.Getenv("WERF_EXTRA_OPTIONS"), "Pass extra options to \"kubectl run\" command (default $WERF_EXTRA_OPTIONS)") cmd.Flags().BoolVarP(&cmdData.Rm, "rm", "", common.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", "", common.GetBoolEnvironmentDefaultFalse("WERF_RM_WITH_NAMESPACE"), "Remove also a namespace after command completion (default $WERF_RM_WITH_NAMESPACE or false if not specified)") @@ -272,6 +272,8 @@ 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) @@ -698,3 +700,8 @@ func addImagePullSecret(secret string, overrides map[string]interface{}) (map[st overrides["spec"].(map[string]interface{})["imagePullSecrets"] = append(overrides["spec"].(map[string]interface{})["imagePullSecrets"].([]interface{}), newImagePullSecret) return overrides, nil } + +func templateOverrides(line, podName, containerName string) string { + result := strings.ReplaceAll(line, "%container_name%", containerName) + return strings.ReplaceAll(result, "%pod_name%", podName) +}