Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for updating coredns #678

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pkg/addons/default/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"k8s.io/apimachinery/pkg/watch"

"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/printers"
k8s "k8s.io/client-go/kubernetes"
)

const (
Expand All @@ -25,6 +27,9 @@ const (
// KubeDNS is the name of the kube-dns addon
KubeDNS = "kube-dns"

// CoreDNSVersion Current latest coredns version supported
CoreDNSVersion = "v1.2.2"

componentLabel = "eks.amazonaws.com/component"

coreDNSImagePrefix = "602401143452.dkr.ecr."
Expand Down Expand Up @@ -153,3 +158,55 @@ func InstallCoreDNS(rawClient kubernetes.RawClientInterface, region string, wait
logger.Info("%q is now up-to-date", CoreDNS)
return nil
}

// UpdateCoreDNSImageTag updates image tag for kube-system:deployment/coredns based to match the latest release
func UpdateCoreDNSImageTag(clientSet k8s.Interface, dryRun bool) error {
printer := printers.NewJSONPrinter()

d, err := clientSet.AppsV1().Deployments(metav1.NamespaceSystem).Get(CoreDNS, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
logger.Warning("%q was not found", CoreDNS)
return nil
}
return errors.Wrapf(err, "getting %q", CoreDNS)
}
if numContainers := len(d.Spec.Template.Spec.Containers); !(numContainers >= 1) {
return fmt.Errorf("%s has %d containers, expected at least 1", CoreDNS, numContainers)
}

if err := printer.LogObj(logger.Debug, CoreDNS+" [current] = \\\n%s\n", d); err != nil {
return err
}

image := &d.Spec.Template.Spec.Containers[0].Image
imageParts := strings.Split(*image, ":")

if len(imageParts) != 2 {
return fmt.Errorf("unexpected image format %q for %q", *image, CoreDNS)
}

if imageParts[1] == CoreDNSVersion {
logger.Debug("imageParts = %v, desiredTag = %s", imageParts, CoreDNSVersion)
logger.Info("%q is already up-to-date", CoreDNS)
return nil
}

if dryRun {
logger.Critical("%q is not up-to-date", CoreDNS)
return nil
}

imageParts[1] = CoreDNSVersion
*image = strings.Join(imageParts, ":")

if err := printer.LogObj(logger.Debug, CoreDNS+" [updated] = \\\n%s\n", d); err != nil {
return err
}
if _, err := clientSet.AppsV1().Deployments(metav1.NamespaceSystem).Update(d); err != nil {
return err
}

logger.Info("%q is now up-to-date", CoreDNS)
return nil
}
41 changes: 40 additions & 1 deletion pkg/addons/default/coredns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
)

var _ = Describe("default addons - coredns", func() {
Describe("can update coredns add-on", func() {
Describe("can update from kubedns to coredns add-on", func() {
var (
rawClient *testutils.FakeRawClient
ct *testutils.CollectionTracker
Expand Down Expand Up @@ -104,4 +105,42 @@ var _ = Describe("default addons - coredns", func() {
})

})

Describe("can update coredns", func() {
var (
clientSet *fake.Clientset
)

check := func(imageTag string) {
coreDNS, err := clientSet.AppsV1().Deployments(metav1.NamespaceSystem).Get(CoreDNS, metav1.GetOptions{})

Expect(err).ToNot(HaveOccurred())
Expect(coreDNS).ToNot(BeNil())
Expect(coreDNS.Spec.Template.Spec.Containers).To(HaveLen(1))

Expect(coreDNS.Spec.Template.Spec.Containers[0].Image).To(
Equal("602401143452.dkr.ecr.eu-west-1.amazonaws.com/eks/coredns:" + imageTag),
)
}

BeforeEach(func() {
clientSet, _ = testutils.NewFakeClientSetWithSamples("testdata/sample-1.11.json")
})

It("can load 1.11 sample", func() {
check("v1.1.3")
})

It("can update based to latest version", func() {
err := UpdateCoreDNSImageTag(clientSet, false)
Expect(err).ToNot(HaveOccurred())
check("v1.2.2")
})

It("can dry-run update to latest version", func() {
err := UpdateCoreDNSImageTag(clientSet, true)
Expect(err).ToNot(HaveOccurred())
check("v1.1.3")
})
})
})
Loading