Skip to content

Commit

Permalink
Add eksctl utils update-kube-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Mar 7, 2019
1 parent 758af46 commit 357da51
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/addons/default/kube_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package defaultaddons

import (
"fmt"
"strings"

"github.com/kris-nova/logger"
"github.com/pkg/errors"

"github.com/weaveworks/eksctl/pkg/printers"

apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

const (
// KubeProxy is the name of the kube-proxy addon
KubeProxy = "kube-proxy"
)

// UpdateKubeProxyImageTag updates image tag for kube-system:damoneset/kube-proxy based to match controlPlaneVersion
func UpdateKubeProxyImageTag(clientSet kubernetes.Interface, controlPlaneVersion string, dryRun bool) error {
printer := printers.NewJSONPrinter()

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

if err := printer.LogObj(logger.Debug, KubeProxy+" [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, KubeProxy)
}

desiredTag := "v" + controlPlaneVersion

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

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

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

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

logger.Info("%q is now up-to-date", KubeProxy)
return nil
}
83 changes: 83 additions & 0 deletions pkg/ctl/utils/update_kube_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package utils

import (
"os"

"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

defaultaddons "github.com/weaveworks/eksctl/pkg/addons/default"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/eks"
)

func updateKubeProxyCmd(g *cmdutils.Grouping) *cobra.Command {
p := &api.ProviderConfig{}
cfg := api.NewClusterConfig()

cmd := &cobra.Command{
Use: "update-kube-proxy",
Short: "Update kube-proxy add-on to ensure image matches Kubernetes control plane version",
Run: func(cmd *cobra.Command, args []string) {
if err := doUpdateKubeProxy(p, cfg, cmdutils.GetNameArg(args), cmd); err != nil {
logger.Critical("%s\n", err.Error())
os.Exit(1)
}
},
}

group := g.New(cmd)

group.InFlagSet("General", func(fs *pflag.FlagSet) {
fs.StringVarP(&cfg.Metadata.Name, "name", "n", "", "EKS cluster name (required)")
cmdutils.AddRegionFlag(fs, p)
cmdutils.AddConfigFileFlag(&clusterConfigFile, fs)
})

cmdutils.AddCommonFlagsForAWS(group, p, false)

group.AddTo(cmd)

return cmd
}

func doUpdateKubeProxy(p *api.ProviderConfig, cfg *api.ClusterConfig, nameArg string, cmd *cobra.Command) error {
if err := api.Register(); err != nil {
return err
}

if err := cmdutils.LoadMetadata(p, cfg, clusterConfigFile, nameArg, cmd); err != nil {
return err
}

ctl := eks.New(p, cfg)
meta := cfg.Metadata

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(p)
}
logger.Info("using region %s", meta.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}

if err := ctl.GetCredentials(cfg); err != nil {
return errors.Wrapf(err, "getting credentials for cluster %q", meta.Name)
}

clientSet, err := ctl.NewStdClientSet(cfg)
if err != nil {
return err
}

cprv, err := ctl.ControlPlaneReleaseVersion(clientSet)
if err != nil {
return err
}

return defaultaddons.UpdateKubeProxyImageTag(clientSet, cprv, false)
}
5 changes: 5 additions & 0 deletions pkg/ctl/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
)

var (
clusterConfigFile = ""
)

// Command will create the `utils` commands
func Command(g *cmdutils.Grouping) *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -22,6 +26,7 @@ func Command(g *cmdutils.Grouping) *cobra.Command {
cmd.AddCommand(writeKubeconfigCmd(g))
cmd.AddCommand(describeStacksCmd(g))
cmd.AddCommand(updateClusterStackCmd(g))
cmd.AddCommand(updateKubeProxyCmd(g))

return cmd
}

0 comments on commit 357da51

Please sign in to comment.