Skip to content

Commit

Permalink
feat: restart pods in bee cluster and update k8s to 0.30.3 (#429)
Browse files Browse the repository at this point in the history
* feat: restart pods in namespace

* feat: restart pods using cluster name

* chore: bump k8s version to 0.30.3

* chore: bump version to 1.23 and golangci-lint to 1.61

* chore: bump beekeeper version to v0.20.0
  • Loading branch information
gacevicljubisa authored Nov 5, 2024
1 parent 18d41f1 commit 1864bb3
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 503 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ jobs:
run: git config --global core.autocrlf false
- name: Lint
if: matrix.os == 'ubuntu-latest'
uses: golangci/golangci-lint-action@v4
uses: golangci/golangci-lint-action@v6
with:
version: v1.56.2
version: v1.61.0
args: --timeout 10m
skip-cache: false
- name: Vet
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22 AS build
FROM golang:1.23 AS build

WORKDIR /src
# enable modules caching in separate layer
Expand All @@ -8,7 +8,7 @@ COPY . ./

RUN make binary

FROM debian:12.5-slim
FROM debian:12.7-slim

ENV DEBIAN_FRONTEND noninteractive

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GO ?= go
GOLANGCI_LINT ?= golangci-lint
GOLANGCI_LINT_VERSION ?= v1.51.2
GOLANGCI_LINT_VERSION ?= v1.61.0

COMMIT ?= "$(shell git describe --long --dirty --always --match "" || true)"
LDFLAGS ?= -s -w -X github.com/ethersphere/beekeeper.commit=$(COMMIT)
Expand Down
6 changes: 3 additions & 3 deletions cmd/beekeeper/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *command) deleteCluster(ctx context.Context, clusterName string, cfg *co
return fmt.Errorf("cluster %s not defined", clusterName)
}

cluster := configureCluster(clusterConfig, c)
cluster := initializeCluster(clusterConfig, c)

// delete node groups
for ngName, v := range clusterConfig.GetNodeGroups() {
Expand Down Expand Up @@ -131,7 +131,7 @@ func (c *command) setupCluster(ctx context.Context, clusterName string, cfg *con
fundOpts = ensureFundingDefaults(clusterConfig.Funding.Export(), c.log)
}

cluster = configureCluster(clusterConfig, c)
cluster = initializeCluster(clusterConfig, c)

nodeResultChan := make(chan nodeResult)
defer close(nodeResultChan)
Expand Down Expand Up @@ -183,7 +183,7 @@ func ensureFundingDefaults(fundOpts orchestration.FundingOptions, log logging.Lo
return fundOpts
}

func configureCluster(clusterConfig config.Cluster, c *command) orchestration.Cluster {
func initializeCluster(clusterConfig config.Cluster, c *command) orchestration.Cluster {
clusterOpts := clusterConfig.Export()
clusterOpts.SwapClient = c.swapClient
clusterOpts.K8SClient = c.k8sClient
Expand Down
4 changes: 4 additions & 0 deletions cmd/beekeeper/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func newCommand(opts ...option) (c *command, err error) {
return nil, err
}

if err := c.initRestartCmd(); err != nil {
return nil, err
}

if err := c.initPrintCmd(); err != nil {
return nil, err
}
Expand Down
94 changes: 94 additions & 0 deletions cmd/beekeeper/cmd/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cmd

import (
"context"
"errors"
"fmt"
"time"

"github.com/ethersphere/beekeeper/pkg/config"
"github.com/spf13/cobra"
)

func (c *command) initRestartCmd() (err error) {
const (
optionNameClusterName = "cluster-name"
optionNameLabelSelector = "label-selector"
optionNameNamespace = "namespace"
optionNameTimeout = "timeout"
)

cmd := &cobra.Command{
Use: "restart",
Short: "Restart pods in a cluster or namespace",
Long: `Restarts pods by deleting them. Uses cluster name as the primary scope or falls back to namespace, with optional label filtering.`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := context.WithTimeout(cmd.Context(), c.globalConfig.GetDuration(optionNameTimeout))
defer cancel()

clusterName := c.globalConfig.GetString(optionNameClusterName)
namespace := c.globalConfig.GetString(optionNameNamespace)

if clusterName == "" && namespace == "" {
return errors.New("either cluster name or namespace must be provided")
}

if clusterName != "" {
if err := c.restartCluster(ctx, clusterName, c.config); err != nil {
return fmt.Errorf("restarting cluster %s: %w", clusterName, err)
}
return nil
}

if err := c.k8sClient.Pods.DeletePods(ctx, namespace, c.globalConfig.GetString(optionNameLabelSelector)); err != nil {
return fmt.Errorf("restarting pods in namespace %s: %w", namespace, err)
}

return nil
},
PreRunE: c.preRunE,
}

cmd.Flags().String(optionNameClusterName, "", "Kubernetes cluster to operate on (overrides namespace).")
cmd.Flags().StringP(optionNameNamespace, "n", "", "Namespace to delete pods from (used if cluster name is not set).")
cmd.Flags().String(optionNameLabelSelector, "", "Label selector for resources in the namespace. Ignored if cluster name is set.")
cmd.Flags().Duration(optionNameTimeout, 5*time.Minute, "Operation timeout (e.g., 5s, 10m, 1.5h).")

c.root.AddCommand(cmd)

return nil
}

func (c *command) restartCluster(ctx context.Context, clusterName string, cfg *config.Config) (err error) {
c.log.Infof("restarting cluster %s", clusterName)

clusterConfig, ok := cfg.Clusters[clusterName]
if !ok {
return fmt.Errorf("cluster config %s not defined", clusterName)
}

cluster, err := c.setupCluster(ctx, clusterName, c.config, false)
if err != nil {
return fmt.Errorf("setting up cluster %s: %w", clusterName, err)
}

nodes := cluster.NodeNames()

count := 0

for _, node := range nodes {
podName := fmt.Sprintf("%s-0", node) // Suffix "-0" added as StatefulSet names pods based on replica count.
ok, err := c.k8sClient.Pods.Delete(ctx, podName, clusterConfig.GetNamespace())
if err != nil {
return fmt.Errorf("deleting pod %s in namespace %s: %w", node, clusterConfig.GetNamespace(), err)
}
if ok {
count++
c.log.Debugf("pod %s in namespace %s deleted", podName, clusterConfig.GetNamespace())
}
}

c.log.Infof("cluster %s restarted %d/%d nodes", clusterName, count, len(nodes))

return nil
}
51 changes: 28 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/ethersphere/beekeeper

go 1.22

toolchain go1.22.0
go 1.23

replace github.com/codahale/hdrhistogram => github.com/HdrHistogram/hdrhistogram-go v1.1.2

Expand All @@ -11,7 +9,7 @@ require (
github.com/ethersphere/bee/v2 v2.2.0
github.com/ethersphere/bmt v0.1.4
github.com/ethersphere/ethproxy v0.0.5
github.com/ethersphere/node-funder v0.2.0
github.com/ethersphere/node-funder v0.2.1
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0
github.com/google/uuid v1.6.0
Expand All @@ -23,12 +21,12 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/uber/jaeger-client-go v2.30.0+incompatible
golang.org/x/crypto v0.23.0
golang.org/x/crypto v0.24.0
golang.org/x/sync v0.7.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.22.16
k8s.io/apimachinery v0.22.16
k8s.io/client-go v0.22.16
k8s.io/api v0.30.3
k8s.io/apimachinery v0.30.3
k8s.io/client-go v0.30.3
)

require (
Expand All @@ -49,21 +47,24 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethersphere/go-sw3-abi v0.6.5 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-chi/chi v1.5.4 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -72,13 +73,15 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/reedsolomon v1.11.8 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.33.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/miekg/dns v1.1.58 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -95,6 +98,7 @@ require (
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down Expand Up @@ -122,24 +126,25 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.20.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20211110012726-3cc51fd1e909 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 1864bb3

Please sign in to comment.