Skip to content

Commit

Permalink
fix(node-funder): extract label-selector as a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa committed Aug 23, 2024
1 parent 7fe190f commit b38cda7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
18 changes: 13 additions & 5 deletions cmd/beekeeper/cmd/node_funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/ethersphere/beekeeper/pkg/config"
Expand All @@ -13,6 +14,8 @@ import (
"github.com/spf13/cobra"
)

const ingressNodeFunderLabelSelector string = "beekeeper.ethswarm.org/node-funder=true"

func (c *command) initNodeFunderCmd() (err error) {
const (
optionNameAddresses = "addresses"
Expand All @@ -23,6 +26,7 @@ func (c *command) initNodeFunderCmd() (err error) {
optionNameMinNative = "min-native"
optionNameMinSwarm = "min-swarm"
optionNameTimeout = "timeout"
optionNameLabelSelector = "label-selector"
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -81,7 +85,8 @@ func (c *command) initNodeFunderCmd() (err error) {
var nodeLister funder.NodeLister
// if addresses are provided, use them, not k8s client to list nodes
if cfg.Namespace != "" {
nodeLister = newNodeLister(c.k8sClient, c.log)
label := c.globalConfig.GetString(optionNameLabelSelector)
nodeLister = newNodeLister(c.k8sClient, label, c.log)
}

return funder.Fund(ctx, funder.Config{
Expand All @@ -105,6 +110,7 @@ func (c *command) initNodeFunderCmd() (err error) {
cmd.Flags().String(optionNameWalletKey, "", "Hex-encoded private key for the Bee node wallet. Required.")
cmd.Flags().Float64(optionNameMinNative, 0, "Minimum amount of chain native coins (xDAI) nodes should have.")
cmd.Flags().Float64(optionNameMinSwarm, 0, "Minimum amount of swarm tokens (xBZZ) nodes should have.")
cmd.Flags().String(optionNameLabelSelector, ingressNodeFunderLabelSelector, "Label selector for the ingress resources to be used for node-funder together with namespace. Empty string means no filtering.")
cmd.Flags().Duration(optionNameTimeout, 5*time.Minute, "Timeout.")

c.root.AddCommand(cmd)
Expand All @@ -114,12 +120,14 @@ func (c *command) initNodeFunderCmd() (err error) {

type nodeLister struct {
k8sClient *k8s.Client
label string
log logging.Logger
}

func newNodeLister(k8sClient *k8s.Client, l logging.Logger) *nodeLister {
func newNodeLister(k8sClient *k8s.Client, label string, l logging.Logger) *nodeLister {
return &nodeLister{
k8sClient: k8sClient,
label: label,
log: l,
}
}
Expand All @@ -133,12 +141,12 @@ func (nf *nodeLister) List(ctx context.Context, namespace string) (nodes []funde
return nil, fmt.Errorf("namespace not provided")
}

ingressHosts, err := nf.k8sClient.Ingress.ListAPINodesHosts(ctx, namespace)
ingressHosts, err := nf.k8sClient.Ingress.GetIngressHosts(ctx, namespace, nf.label)
if err != nil {
return nil, fmt.Errorf("list ingress api nodes hosts: %s", err.Error())
}

ingressRouteHosts, err := nf.k8sClient.IngressRoute.ListAPINodesHosts(ctx, namespace)
ingressRouteHosts, err := nf.k8sClient.IngressRoute.GetIngressHosts(ctx, namespace, nf.label)
if err != nil {
return nil, fmt.Errorf("list ingress route api nodes hosts: %s", err.Error())
}
Expand All @@ -147,7 +155,7 @@ func (nf *nodeLister) List(ctx context.Context, namespace string) (nodes []funde

for _, node := range ingressHosts {
nodes = append(nodes, funder.NodeInfo{
Name: node.Name,
Name: strings.TrimSuffix(node.Name, "-api"),
Address: fmt.Sprintf("http://%s", node.Host),
})
}
Expand Down
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ node-groups:
app.kubernetes.io/name: "bee"
app.kubernetes.io/part-of: "bee"
app.kubernetes.io/version: "latest"
beekeeper.ethswarm.org/node-funder: "true"
node-selector:
node-group: "private"
persistence-enabled: false
Expand Down
23 changes: 10 additions & 13 deletions pkg/k8s/customresource/ingressroute/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ingressroute
import (
"context"
"fmt"
"strings"

"github.com/ethersphere/beekeeper/pkg/k8s/ingress"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -82,10 +81,10 @@ func (c *Client) Delete(ctx context.Context, name, namespace string) (err error)
return
}

// ListAPINodesHosts list Ingresses that are nodes
func (c *Client) ListAPINodesHosts(ctx context.Context, namespace string) (nodes []ingress.NodeInfo, err error) {
// GetIngressHosts list Ingress Routes hosts using label as selector, for the given namespace. If label is empty, all Ingresses are listed.
func (c *Client) GetIngressHosts(ctx context.Context, namespace, label string) (nodes []ingress.NodeInfo, err error) {
ingressRoutes, err := c.clientset.IngressRoutes(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=bee",
LabelSelector: label,
})
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -96,15 +95,13 @@ func (c *Client) ListAPINodesHosts(ctx context.Context, namespace string) (nodes

if ingressRoutes != nil {
for _, ingressRoute := range ingressRoutes.Items {
if strings.HasSuffix(ingressRoute.Name, "-api") {
for _, route := range ingressRoute.Spec.Routes {
host := route.GetHost()
if host != "" {
nodes = append(nodes, ingress.NodeInfo{
Name: strings.TrimSuffix(ingressRoute.Name, "-api"),
Host: host,
})
}
for _, route := range ingressRoute.Spec.Routes {
host := route.GetHost()
if host != "" {
nodes = append(nodes, ingress.NodeInfo{
Name: ingressRoute.Name,
Host: host,
})
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions pkg/k8s/ingress/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ingress
import (
"context"
"fmt"
"strings"

v1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -76,10 +75,10 @@ func (c *Client) Delete(ctx context.Context, name, namespace string) (err error)
return
}

// ListAPINodesHosts list Ingresses that are nodes
func (c *Client) ListAPINodesHosts(ctx context.Context, namespace string) (nodes []NodeInfo, err error) {
// GetIngressHosts list Ingresses hosts using label as selector, for the given namespace. If label is empty, all Ingresses are listed.
func (c *Client) GetIngressHosts(ctx context.Context, namespace, label string) (nodes []NodeInfo, err error) {
ingreses, err := c.clientset.NetworkingV1().Ingresses(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/name=bee",
LabelSelector: label,
})
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -89,14 +88,12 @@ func (c *Client) ListAPINodesHosts(ctx context.Context, namespace string) (nodes
}

for _, ingress := range ingreses.Items {
if strings.HasSuffix(ingress.Name, "-api") {
for _, rule := range ingress.Spec.Rules {
if rule.Host != "" {
nodes = append(nodes, NodeInfo{
Name: strings.TrimSuffix(ingress.Name, "-api"),
Host: rule.Host,
})
}
for _, rule := range ingress.Spec.Rules {
if rule.Host != "" {
nodes = append(nodes, NodeInfo{
Name: ingress.Name,
Host: rule.Host,
})
}
}
}
Expand Down

0 comments on commit b38cda7

Please sign in to comment.