Skip to content

Commit

Permalink
add kubectl function with output. add check to schedule coredns function
Browse files Browse the repository at this point in the history
  • Loading branch information
ryehowell committed Jan 23, 2024
1 parent 7d27bab commit 5ad3309
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
27 changes: 22 additions & 5 deletions eks/coredns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eks

import (
"strings"

"github.com/gruntwork-io/go-commons/errors"

"github.com/gruntwork-io/kubergrunt/eksawshelper"
Expand Down Expand Up @@ -43,17 +45,32 @@ func ScheduleCoredns(
case Fargate:
logger.Info("Doing fargate annotation")

err = kubectl.RunKubectl(
// CoreDNS may or may not have the compute-type annotation by default. Check for existence.
out, err := kubectl.RunKubectlWithOutput(
kubectlOptions,
"patch", "deployment", "coredns",
"get", "deployment", "coredns",
"-n", "kube-system",
"--type", "json",
"--patch", `[{"op": "remove","path": "/spec/template/metadata/annotations/eks.amazonaws.com~1compute-type"}]`,
"-o", `-jsonpath='{.spec.template.metadata}'`,
)

if err != nil {
return errors.WithStackTrace(err)
}

// Only attempt to patch coredns deployment if the compute-type annotation is present. Else skip.
if strings.Contains(out, "compute-type") {
err = kubectl.RunKubectl(
kubectlOptions,
"patch", "deployment", "coredns",
"-n", "kube-system",
"--type", "json",
"--patch", `[{"op": "remove","path": "/spec/template/metadata/annotations/eks.amazonaws.com~1compute-type"}]`,
)

if err != nil {
return errors.WithStackTrace(err)
}
}

case EC2:
logger.Info("Doing ec2 annotation")

Expand Down
28 changes: 28 additions & 0 deletions kubectl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,31 @@ func RunKubectl(options *KubectlOptions, args ...string) error {
_, err := shell.RunShellCommandAndGetAndStreamOutput(shellOptions, "kubectl", cmdArgs...)
return err
}

func RunKubectlWithOutput(options *KubectlOptions, args ...string) (string, error) {
shellOptions := shell.NewShellOptions()
cmdArgs := []string{}
scheme := options.AuthScheme()
switch scheme {
case ConfigBased:
if options.ContextName != "" {
cmdArgs = append(cmdArgs, "--context", options.ContextName)
}
if options.ConfigPath != "" {
cmdArgs = append(cmdArgs, "--kubeconfig", options.ConfigPath)
}
default:
tmpfile, err := options.TempConfigFromAuthInfo()
if tmpfile != "" {
// Make sure to delete the tmp file at the end
defer os.Remove(tmpfile)
}
if err != nil {
return "ERROR", err
}
cmdArgs = append(cmdArgs, "--kubeconfig", tmpfile)
}
cmdArgs = append(cmdArgs, args...)
out, err := shell.RunShellCommandAndGetAndStreamOutput(shellOptions, "kubectl", cmdArgs...)
return out, err
}

0 comments on commit 5ad3309

Please sign in to comment.