diff --git a/eks/coredns.go b/eks/coredns.go index 9e47a87..4c1da92 100644 --- a/eks/coredns.go +++ b/eks/coredns.go @@ -1,6 +1,8 @@ package eks import ( + "strings" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/kubergrunt/eksawshelper" @@ -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") diff --git a/kubectl/command.go b/kubectl/command.go index 187dbc3..04e0206 100644 --- a/kubectl/command.go +++ b/kubectl/command.go @@ -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 +}