Skip to content

Commit

Permalink
Merge pull request #1 from bringg/add_consul
Browse files Browse the repository at this point in the history
add consul backend
  • Loading branch information
Shareed2k authored Feb 15, 2021
2 parents d50e1ae + 0ef3c9a commit 98cffed
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ I wanted a simple, fast all in one tool to search for instance by key in few clo
## Backend providers

* Amazon EC2
* Consul by HashiCorp
* Google Cloud Compute
* Kubernetes Pods

Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func init() {
Root.PersistentFlags().StringVarP(&backendsString, "backends", "b", backendsString, "")

Root.AddCommand(newCompletionCmd(os.Stdout))
Root.AddCommand(helpBackends)

Root.AddCommand(helpCommand)
helpCommand.AddCommand(helpFlags)
Expand Down
14 changes: 12 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ go 1.15

require (
github.com/Rican7/conjson v0.1.0
github.com/armon/go-metrics v0.3.6 // indirect
github.com/aws/aws-sdk-go-v2 v1.1.0
github.com/aws/aws-sdk-go-v2/config v1.1.0
github.com/aws/aws-sdk-go-v2/service/ec2 v1.1.0
github.com/fatih/color v1.10.0
github.com/golangci/golangci-lint v1.36.0
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.4 // indirect
github.com/hashicorp/consul/api v1.8.1
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/json-iterator/go v1.1.10
github.com/miekg/dns v1.1.31 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5
github.com/mitchellh/go-testing-interface v1.14.0 // indirect
github.com/mitchellh/mapstructure v1.3.3
github.com/olekukonko/tablewriter v0.0.0-20180130162743-b8a9be070da4
github.com/pkg/errors v0.9.1
github.com/rclone/rclone v1.54.0
github.com/sirupsen/logrus v1.7.0
Expand Down
82 changes: 82 additions & 0 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/backend/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package all
import (
// part of registry
_ "github.com/bringg/honey/pkg/backend/aws"
_ "github.com/bringg/honey/pkg/backend/consul"
_ "github.com/bringg/honey/pkg/backend/gcp"
_ "github.com/bringg/honey/pkg/backend/k8s"
)
106 changes: 106 additions & 0 deletions pkg/backend/consul/consul.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package consul

import (
"context"
"fmt"

"github.com/hashicorp/consul/api"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct"
"github.com/sirupsen/logrus"

"github.com/bringg/honey/pkg/place"
)

const Name = "consul"

var (
log = logrus.WithField("backend", Name)
)

type (
Backend struct {
opt Options
client *api.Client
}

// Options defines the configuration for this backend
Options struct {
}
)

// Register with Backend
func init() {
place.Register(&place.RegInfo{
Name: Name,
Description: "Consul by HashiCorp",
NewBackend: NewBackend,
Options: []place.Option{},
})
}

func NewBackend(ctx context.Context, m configmap.Mapper) (place.Backend, error) {
// Parse config into Options struct
opt := new(Options)
err := configstruct.Set(m, opt)
if err != nil {
return nil, err
}

// Get a new client
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
return nil, err
}

return &Backend{
opt: *opt,
client: client,
}, nil
}

func (b *Backend) Name() string {
return Name
}

func (b *Backend) CacheKeyName(pattern string) string {
return fmt.Sprintf("%s", pattern)
}

func (b *Backend) List(ctx context.Context, pattern string) (place.Printable, error) {
nodes, _, err := b.client.Catalog().Nodes(&api.QueryOptions{
Filter: fmt.Sprintf(`Node contains "%s"`, pattern),
})
if err != nil {
return nil, err
}

instances := make([]*place.Instance, len(nodes))
for i, node := range nodes {
hc, _, err := b.client.Health().Node(node.Node, &api.QueryOptions{})
if err != nil {
return nil, err
}

privateIP := node.Address
publicIP := ""
if wan, ok := node.TaggedAddresses["wan"]; ok && privateIP != wan {
publicIP = wan
}

instances[i] = &place.Instance{
Model: place.Model{
BackendName: Name,
ID: node.ID,
Name: node.Node,
Type: "node",
Status: hc.AggregatedStatus(),
PrivateIP: privateIP,
PublicIP: publicIP,
},
Raw: node,
}
}

return instances, nil
}
10 changes: 5 additions & 5 deletions pkg/backend/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var (

type (
Backend struct {
c *kubernetes.Clientset
opt Options
client *kubernetes.Clientset
opt Options
}

Options struct {
Expand Down Expand Up @@ -104,8 +104,8 @@ func NewBackend(ctx context.Context, m configmap.Mapper) (place.Backend, error)
}

return &Backend{
c: clientset,
opt: *opt,
client: clientset,
opt: *opt,
}, nil
}

Expand All @@ -130,7 +130,7 @@ func (b *Backend) List(ctx context.Context, pattern string) (place.Printable, er
return nil, errors.Wrap(err, "failed to compile regular expression from query")
}

pods, err := b.c.
pods, err := b.client.
CoreV1().
Pods(ns).
List(ctx, metav1.ListOptions{})
Expand Down

0 comments on commit 98cffed

Please sign in to comment.