Skip to content

Commit

Permalink
Fix kubectl run overrides (#33)
Browse files Browse the repository at this point in the history
* fix: changed kubectl run override type

From the default `merge` to `strategic`

* fix: used override instead of deprecated flag

* feat: added --overrides flag to kurun run

* chore: replaced empty value with nil
  • Loading branch information
akijakya authored Mar 6, 2023
1 parent c211d5c commit 6cd7598
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"os/exec"
"strings"

jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

func NewRunCommand(rootParams *rootCommandParams) *cobra.Command {
var serviceAccount string
var overrides string
var podEnv []string

cmd := &cobra.Command{
Expand Down Expand Up @@ -46,7 +48,18 @@ func NewRunCommand(rootParams *rootCommandParams) *cobra.Command {

podName := strings.Split(image, ":")[0]

podOverride := map[string]interface{}{
kubectlArgs := []string{
"run", podName,
mode,
"--image=docker.io/library/" + image,
"--quiet",
"--image-pull-policy=IfNotPresent",
"--restart=Never",
"--rm",
"--override-type=strategic",
}

limitsPatch := map[string]interface{}{
"spec": corev1.PodSpec{
Containers: []corev1.Container{
{
Expand All @@ -63,26 +76,32 @@ func NewRunCommand(rootParams *rootCommandParams) *cobra.Command {
},
}

overrides, err := json.Marshal(podOverride)
limitsOverride, err := json.Marshal(limitsPatch)
if err != nil {
return err
}

kubectlArgs := []string{
"run", podName,
mode,
"--image=docker.io/library/" + image,
"--quiet",
"--image-pull-policy=IfNotPresent",
"--restart=Never",
"--rm",
"--overrides=" + string(overrides),
}
combinedOverride := limitsOverride

if serviceAccount != "" {
kubectlArgs = append(kubectlArgs, fmt.Sprintf("--serviceaccount=%s", serviceAccount))
serviceAccountPatch := fmt.Sprintf(`{"spec":{"serviceAccount":"%s"}}`, serviceAccount)
serviceAccountOverride, err := jsonpatch.MergeMergePatches(combinedOverride, []byte(serviceAccountPatch))
if err != nil {
return err
}
combinedOverride = serviceAccountOverride
}

if overrides != "" {
overridesOverride, err := jsonpatch.MergeMergePatches(combinedOverride, []byte(overrides))
if err != nil {
return err
}
combinedOverride = overridesOverride
}

kubectlArgs = append(kubectlArgs, fmt.Sprintf("--overrides=%s", string(combinedOverride)))

for _, e := range podEnv {
kubectlArgs = append(kubectlArgs, fmt.Sprintf("--env=%s", e))
}
Expand All @@ -103,7 +122,8 @@ func NewRunCommand(rootParams *rootCommandParams) *cobra.Command {
},
}
cmd.PersistentFlags().StringVar(&serviceAccount, "serviceaccount", "", "Service account to set for the pod")
cmd.PersistentFlags().StringArrayVarP(&podEnv, "env", "e", []string{}, "Environment variables to pass to the pod's containers")
cmd.PersistentFlags().StringVar(&overrides, "overrides", "", "An inline JSON override for the generated pod object, e.g. '{\"metadata\":{\"name\":\"my-pod\"}}'")
cmd.PersistentFlags().StringArrayVarP(&podEnv, "env", "e", nil, "Environment variables to pass to the pod's containers")

return cmd
}

0 comments on commit 6cd7598

Please sign in to comment.