Skip to content

Commit

Permalink
Convert upgrader script used by in-place upgrades to a go binary to
Browse files Browse the repository at this point in the history
make the project more conformant with the rest of the codebase.

Signed-off-by: Rahul Ganesh <rahulgab@amazon.com>
  • Loading branch information
Rahul Ganesh committed Mar 28, 2024
1 parent 56f3514 commit a4dbffc
Show file tree
Hide file tree
Showing 25 changed files with 1,649 additions and 2 deletions.
1 change: 1 addition & 0 deletions projects/aws/upgrader/GOLANG_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.22
45 changes: 43 additions & 2 deletions projects/aws/upgrader/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BASE_DIRECTORY:=$(shell git rev-parse --show-toplevel)
GOLANG_VERSION=$(shell cat GOLANG_VERSION)

REPO=upgrader
REPO_OWNER=aws
Expand All @@ -12,15 +13,25 @@ EXCLUDE_FROM_UPGRADE_BUILDSPEC=true
# for the staging buildspec generation
BUILDSPEC_DEPENDS_ON_OVERRIDE=containerd_containerd_linux_amd64 containerd_containerd_linux_arm64 kubernetes_sigs_cri_tools

BINARY_TARGET_FILES=upgrader
SOURCE_PATTERNS=.
GO_MOD_PATHS=..

# force binaries to go to non-release branch bin folder
BINARIES_ARE_RELEASE_BRANCHED=false
GIT_CHECKOUT_TARGET=main.go
FAKE_GIT_REPO_TARGET=.git
REPO_NO_CLONE=true
HAS_LICENSES=false
SKIP_CHECKSUM_VALIDATION=true

BUILDSPECS=buildspec.yml buildspecs/combine-images.yml
BUILDSPEC_1_COMPUTE_TYPE=BUILD_GENERAL1_LARGE
BUILDSPEC_1_VARS_KEYS=IMAGE_PLATFORMS
BUILDSPEC_1_VARS_VALUES=IMAGE_PLATFORMS
BUILDSPEC_1_ARCH_TYPES=LINUX_CONTAINER ARM_CONTAINER
BUILDSPEC_2_DEPENDS_ON_OVERRIDE=aws_upgrader_linux_amd64 aws_upgrader_linux_arm64

REPO_NO_CLONE=true
HAS_LICENSES=false

BASE_IMAGE_NAME=eks-distro-minimal-base-nsenter
IMAGE_NAMES=upgrader
Expand All @@ -34,6 +45,36 @@ PROJECT_DEPENDENCIES=eksd/kubernetes/client eksd/kubernetes/server eksd/cni-plug

include $(BASE_DIRECTORY)/Common.mk

TOOLS_BIN_DIR := $(shell pwd)/hack/tools/bin
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen

GO=build::common::use_go_version $(GOLANG_VERSION)

build: unit-test

$(REPO):
@mkdir $@

$(GATHER_LICENSES_TARGETS): | $(FAKE_GIT_REPO_TARGET)

$(ATTRIBUTION_TARGETS): GIT_TAG

$(FAKE_GIT_REPO_TARGET):
@git init
@git remote add origin https://github.com/aws/eks-anywhere-build-tooling.git

$(TOOLS_BIN_DIR):
@mkdir -p $(TOOLS_BIN_DIR)

$(MOCKGEN): $(TOOLS_BIN_DIR)
GOBIN=$(TOOLS_BIN_DIR) go install github.com/golang/mock/mockgen@v1.6.0

unit-test: | $$(ENABLE_DOCKER)
go test ./...

.PHONY: mocks
mocks: $(MOCKGEN)
go generate ./...

########### DO NOT EDIT #############################
# To update call: make add-generated-help-block
Expand Down
43 changes: 43 additions & 0 deletions projects/aws/upgrader/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/aws/eks-anywhere-build-tooling/tools/version-tracker/pkg/util/logger"
)

var rootCmd = &cobra.Command{
Use: "upgrader",
Short: "EKS Anywhere InPlace upgrader",
Long: `Use EKS Anywhere InPlace upgrader to upgrade your nodes InPlace`,
PersistentPreRun: rootPersistentPreRun,
}

func init() {
rootCmd.PersistentFlags().IntP("verbosity", "v", 0, "Set the log level verbosity")
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.Fatalf("failed to bind flags for root: %v", err)
}
}

func rootPersistentPreRun(cmd *cobra.Command, args []string) {
if err := initLogger(); err != nil {
log.Fatal(err)
}
}

func initLogger() error {
if err := logger.Init(viper.GetInt("verbosity")); err != nil {
return fmt.Errorf("failed init zap logger in root command: %v", err)
}

return nil
}

func Execute() error {
return rootCmd.Execute()
}
36 changes: 36 additions & 0 deletions projects/aws/upgrader/cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"context"
"fmt"
"log"

"github.com/aws/eks-anywhere-build-tooling/projects/aws/upgrader/upgrade"
"github.com/spf13/cobra"
)

var upgradeStatusCmd = &cobra.Command{
Use: "status",
Short: "Upgrade status",
Long: "Use InPlace Upgrader upgrade status to get status of upgraded components on the node",
RunE: func(cmd *cobra.Command, args []string) error {
err := upgradeStatus(cmd.Context())
if err != nil {
log.Fatalf("upgrade status failed: %v", err)
}
return nil
},
}

func init() {
upgradeCmd.AddCommand(upgradeStatusCmd)
}

func upgradeStatus(ctx context.Context) error {
upg := upgrade.NewUpgrader()
if err := upg.LogStatusAndCleanup(ctx); err != nil {
return fmt.Errorf("fetching upgrade status on the node: %v", err)
}

return nil
}
15 changes: 15 additions & 0 deletions projects/aws/upgrader/cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/spf13/cobra"
)

var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade command",
Long: "Use InPlace Upgrader upgrade to run different upgrade commands on the node",
}

func init() {
rootCmd.AddCommand(upgradeCmd)
}
35 changes: 35 additions & 0 deletions projects/aws/upgrader/cmd/upgrade_cniplugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"context"
"fmt"
"log"

"github.com/aws/eks-anywhere-build-tooling/projects/aws/upgrader/upgrade"
"github.com/spf13/cobra"
)

var upgradeCniPluginsCmd = &cobra.Command{
Use: "cni-plugins",
Short: "Upgrade cni-plugins",
Long: "Use InPlace Upgrader upgrade cni-plugins to upgrade cni-plugins on the node",
RunE: func(cmd *cobra.Command, args []string) error {
err := upgradeCniPlugins(cmd.Context())
if err != nil {
log.Fatalf("upgrade cni-plugins failed: %v", err)
}
return nil
},
}

func init() {
upgradeCmd.AddCommand(upgradeCniPluginsCmd)
}

func upgradeCniPlugins(ctx context.Context) error {
upg := upgrade.NewUpgrader()
if err := upg.CniPluginsUpgrade(ctx); err != nil {
return fmt.Errorf("upgrading Cni-Plugins on the node: %v", err)
}
return nil
}
36 changes: 36 additions & 0 deletions projects/aws/upgrader/cmd/upgrade_containerd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"context"
"fmt"
"log"

"github.com/aws/eks-anywhere-build-tooling/projects/aws/upgrader/upgrade"
"github.com/spf13/cobra"
)

var upgradeContainerdCmd = &cobra.Command{
Use: "containerd",
Short: "Upgrade containerd",
Long: "Use InPlace Upgrader upgrade containerd to upgrade containerd on the node",
RunE: func(cmd *cobra.Command, args []string) error {
err := upgradeContainerd(cmd.Context())
if err != nil {
log.Fatalf("upgrade containerd failed: %v", err)
}
return nil
},
}

func init() {
upgradeCmd.AddCommand(upgradeContainerdCmd)
}

func upgradeContainerd(ctx context.Context) error {
upg := upgrade.NewUpgrader()
if err := upg.ContainerdUpgrade(ctx); err != nil {
return fmt.Errorf("upgrading containerd on node: %v", err)
}

return nil
}
36 changes: 36 additions & 0 deletions projects/aws/upgrader/cmd/upgrade_kubelet_kubectl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"context"
"fmt"
"log"

"github.com/aws/eks-anywhere-build-tooling/projects/aws/upgrader/upgrade"
"github.com/spf13/cobra"
)

var upgradeKubeletKubectlCmd = &cobra.Command{
Use: "kubelet-kubectl",
Short: "Upgrade kubelet-kubectl",
Long: "Use InPlace Upgrader upgrade kubelet-kubectl to upgrade kubelet and kubectl on the node",
RunE: func(cmd *cobra.Command, args []string) error {
err := upgradeKubeletAndKubectl(cmd.Context())
if err != nil {
log.Fatalf("upgrade kubelet-kubectl failed: %v", err)
}
return nil
},
}

func init() {
upgradeCmd.AddCommand(upgradeKubeletKubectlCmd)
}

func upgradeKubeletAndKubectl(ctx context.Context) error {
upg := upgrade.NewUpgrader()
if err := upg.KubeletKubectlUpgrade(ctx); err != nil {
return fmt.Errorf("upgrading kubelet and kubectl on node: %v", err)
}

return nil
}
74 changes: 74 additions & 0 deletions projects/aws/upgrader/cmd/upgrade_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"context"
"errors"
"fmt"
"log"

"github.com/aws/eks-anywhere-build-tooling/projects/aws/upgrader/upgrade"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var upgradeNodeCmd = &cobra.Command{
Use: "node",
Short: "Upgrade node",
Long: "Use InPlace Upgrader upgrade node to upgrade kubeadm on the node",
RunE: func(cmd *cobra.Command, args []string) error {
var err error

nodeType := viper.GetString("type")
k8sVersion := viper.GetString("k8sVersion")
if k8sVersion == "" {
return errors.New("k8sVersion flag has to be set for upgrade node command")
}

etcdVersion := viper.GetString("etcdVersion")
if etcdVersion == "" {
etcdVersion = "NO_UPDATE"
}

err = upgradeNode(cmd.Context(), nodeType, k8sVersion, etcdVersion)
if err != nil {
log.Fatalf("upgrade node failed: %v", err)
}
return nil
},
}

func init() {
var err error

upgradeCmd.AddCommand(upgradeNodeCmd)
upgradeNodeCmd.Flags().String("type", "", "Node type flag")
upgradeNodeCmd.Flags().String("k8sVersion", "", "kubernetes version flag")
upgradeNodeCmd.Flags().String("etcdVersion", "", "etcd version flag")
err = viper.BindPFlags(upgradeNodeCmd.Flags())
if err != nil {
log.Fatalf("Error initializing flags: %v", err)
}
}

func upgradeNode(ctx context.Context, nodeType, k8sVersion, etcdVersion string) error {
upg := upgrade.NewUpgrader(upgrade.WithKubernetesVersion(k8sVersion), upgrade.WithEtcdVersion(etcdVersion))

switch nodeType {
case "FirstCP":
if err := upg.KubeAdmInFirstCP(ctx); err != nil {
return fmt.Errorf("upgrading kubeadm in first controlplane node: %v", err)
}
case "RestCP":
if err := upg.KubeAdmInRestCP(ctx); err != nil {
return fmt.Errorf("upgrading kubeadm in controlplane node: %v", err)
}
case "Worker":
if err := upg.KubeAdmInWorker(ctx); err != nil {
return fmt.Errorf("upgrading kubeadm in worker node: %v", err)
}
default:
return fmt.Errorf("invalid node type, please specify one of the three types: FirstCP, RestCP or Worker")
}

return nil
}
39 changes: 39 additions & 0 deletions projects/aws/upgrader/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module github.com/aws/eks-anywhere-build-tooling/projects/aws/upgrader

go 1.22

require (
github.com/aws/eks-anywhere-build-tooling/tools/version-tracker v0.0.0-20240324014530-904f2adc3ec4
github.com/golang/mock v1.6.0
github.com/onsi/gomega v1.32.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/zapr v1.2.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit a4dbffc

Please sign in to comment.