Skip to content

Commit

Permalink
Merge pull request #26 from ernoaapa/updates-and-improvements
Browse files Browse the repository at this point in the history
Updates and improvements
  • Loading branch information
ernoaapa authored Feb 12, 2018
2 parents 52fd3de + 8f77298 commit 41b038d
Show file tree
Hide file tree
Showing 112 changed files with 6,479 additions and 1,291 deletions.
24 changes: 24 additions & 0 deletions cmd/eliotd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ernoaapa/eliot/pkg/controller"
"github.com/ernoaapa/eliot/pkg/device"
"github.com/ernoaapa/eliot/pkg/discovery"
"github.com/ernoaapa/eliot/pkg/profile"
"github.com/ernoaapa/eliot/pkg/version"
log "github.com/sirupsen/logrus"
"github.com/thejerf/suture"
Expand Down Expand Up @@ -39,6 +40,11 @@ func main() {
EnvVar: "ELIOT_CONTAINERD",
Value: "/run/containerd/containerd.sock",
},
cli.DurationFlag{
Name: "timeout, t",
Usage: "total timeout for runtime requests",
EnvVar: "ELIOT_TIMEOUT",
},
cli.BoolTFlag{
Name: "lifecycle-controller",
Usage: "Enable container lifecycle controller",
Expand All @@ -60,6 +66,17 @@ func main() {
Usage: "Enable discover GRPC server over zeroconf",
EnvVar: "ELIOT_DISCOVERY",
},
cli.BoolFlag{
Name: "profile",
Usage: "Turn on pprof profiling",
EnvVar: "ELIOT_PROFILE",
},
cli.StringFlag{
Name: "profile-address",
Usage: "The http address for the pprof server",
EnvVar: "ELIOT_PROFILE_ADDRESS",
Value: "0.0.0.0:8000",
},
cli.StringFlag{
Name: "labels",
Usage: "Comma separated list of device labels. E.g. --labels device=rpi3,location=home,environment=testing",
Expand All @@ -82,6 +99,13 @@ func main() {
supervisor := suture.NewSimple("eliotd")
serviceCount := 0

if clicontext.BoolT("profile") {
profileAddr := clicontext.String("profile-address")
log.Infof("profiling enabled, address: %s", profileAddr)
supervisor.Add(profile.NewServer(profileAddr))
serviceCount++
}

if clicontext.Bool("grpc-api") {
log.Infoln("grpc-api enabled")
supervisor.Add(api.NewServer(grpcListen, client, device))
Expand Down
3 changes: 2 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ernoaapa/eliot/pkg/discovery"
"github.com/ernoaapa/eliot/pkg/printers"
"github.com/ernoaapa/eliot/pkg/sync"
"github.com/ernoaapa/eliot/pkg/utils"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -139,7 +140,7 @@ func GetConfigProvider(clicontext *cli.Context) *config.Provider {
for _, device := range devices {
endpoints = append(endpoints, config.Endpoint{
Name: device.Hostname,
URL: device.GetPrimaryEndpoint(),
URL: utils.GetFirst(device.Addresses, ""),
})
}
provider.OverrideEndpoints(endpoints)
Expand Down
10 changes: 5 additions & 5 deletions pkg/discovery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"time"

"github.com/ernoaapa/eliot/pkg/model"
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
"github.com/grandcat/zeroconf"
"github.com/pkg/errors"
)

// Devices return list of DeviceInfos synchronously with given timeout
func Devices(timeout time.Duration) (devices []model.DeviceInfo, err error) {
results := make(chan model.DeviceInfo)
func Devices(timeout time.Duration) (devices []*device.Info, err error) {
results := make(chan *device.Info)
defer close(results)

go func() {
Expand All @@ -29,7 +29,7 @@ func Devices(timeout time.Duration) (devices []model.DeviceInfo, err error) {
}

// DevicesAsync search for devices in network asynchronously for given timeout
func DevicesAsync(results chan<- model.DeviceInfo, timeout time.Duration) error {
func DevicesAsync(results chan<- *device.Info, timeout time.Duration) error {
resolver, err := zeroconf.NewResolver(nil)
if err != nil {
return errors.Wrapf(err, "Failed to initialize new zeroconf resolver")
Expand All @@ -38,7 +38,7 @@ func DevicesAsync(results chan<- model.DeviceInfo, timeout time.Duration) error
entries := make(chan *zeroconf.ServiceEntry)
go func(entries <-chan *zeroconf.ServiceEntry) {
for entry := range entries {
results <- MapToInternalModel(entry)
results <- MapToAPIModel(entry)
}
}(entries)

Expand Down
19 changes: 13 additions & 6 deletions pkg/discovery/mapping.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package discovery

import (
"net"
"strings"

"github.com/ernoaapa/eliot/pkg/model"
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
"github.com/grandcat/zeroconf"
)

// MapToInternalModel takes zeroconf entry and maps it to internal DeviceInfo model
func MapToInternalModel(entry *zeroconf.ServiceEntry) model.DeviceInfo {
func MapToAPIModel(entry *zeroconf.ServiceEntry) *device.Info {
version := "unknown"

for _, val := range entry.Text {
Expand All @@ -18,10 +18,17 @@ func MapToInternalModel(entry *zeroconf.ServiceEntry) model.DeviceInfo {
}
}

return model.DeviceInfo{
return &device.Info{
Hostname: entry.HostName,
Addresses: append(entry.AddrIPv4, entry.AddrIPv6...),
GrpcPort: entry.Port,
Addresses: addressesToString(append(entry.AddrIPv4, entry.AddrIPv6...)),
GrpcPort: int64(entry.Port),
Version: version,
}
}

func addressesToString(addresses []net.IP) (result []string) {
for _, ip := range addresses {
result = append(result, ip.String())
}
return result
}
8 changes: 6 additions & 2 deletions pkg/discovery/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMapToInternalModel(t *testing.T) {
result := MapToInternalModel(&zeroconf.ServiceEntry{
result := MapToAPIModel(&zeroconf.ServiceEntry{
HostName: "hostname",
AddrIPv4: []net.IP{net.IPv4zero},
AddrIPv6: []net.IP{net.IPv6loopback},
Expand All @@ -18,5 +18,9 @@ func TestMapToInternalModel(t *testing.T) {

assert.Equal(t, "hostname", result.Hostname)
assert.Equal(t, "1.2.3-abcd", result.Version)
assert.Equal(t, []net.IP{net.IPv4zero, net.IPv6loopback}, result.Addresses)
assert.Equal(t, addressesToString([]net.IP{net.IPv4zero, net.IPv6loopback}), result.Addresses)
}

func TestAddressesToString(t *testing.T) {
assert.Equal(t, []string{"0.0.0.0", "::1"}, addressesToString([]net.IP{net.IPv4zero, net.IPv6loopback}))
}
9 changes: 0 additions & 9 deletions pkg/model/device.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"fmt"
"net"
)

Expand Down Expand Up @@ -49,11 +48,3 @@ type DeviceState struct {
type PodState struct {
ID string `validate:"required,gt=0"`
}

// GetPrimaryEndpoint return primary GRPC endpoint address
func (d DeviceInfo) GetPrimaryEndpoint() string {
if len(d.Addresses) == 0 {
return ""
}
return fmt.Sprintf("%s:%d", d.Addresses[0], d.GrpcPort)
}
6 changes: 3 additions & 3 deletions pkg/printers/humanreadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/ernoaapa/eliot/pkg/printers/humanreadable"
"github.com/ernoaapa/eliot/pkg/utils"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -79,15 +79,15 @@ func getKeys(source map[string]int) (result []string) {
}

// PrintDevices writes list of Devices in human readable table format to the writer
func (p *HumanReadablePrinter) PrintDevices(devices []model.DeviceInfo, writer io.Writer) error {
func (p *HumanReadablePrinter) PrintDevices(devices []*device.Info, writer io.Writer) error {
if len(devices) == 0 {
fmt.Fprintf(writer, "\n\t(No devices)\n\n")
return nil
}
fmt.Fprintln(writer, "\nHOSTNAME\tENDPOINT\tVERSION")

for _, device := range devices {
_, err := fmt.Fprintf(writer, "%s\t%s\t%s\n", device.Hostname, device.GetPrimaryEndpoint(), device.Version)
_, err := fmt.Fprintf(writer, "%s\t%s\t%s\n", device.Hostname, utils.GetFirst(device.Addresses, ""), device.Version)
if err != nil {
return errors.Wrapf(err, "Error while writing device row")
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/printers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
)

// ResourcePrinter is an interface that knows how to print runtime objects.
type ResourcePrinter interface {
PrintPods([]*pods.Pod, io.Writer) error
PrintDevices([]model.DeviceInfo, io.Writer) error
PrintDevices([]*device.Info, io.Writer) error
PrintDevice(*device.Info, io.Writer) error
PrintPod(*pods.Pod, io.Writer) error
PrintConfig(*config.Config, io.Writer) error
Expand Down
7 changes: 3 additions & 4 deletions pkg/printers/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -57,11 +56,11 @@ func testYamlPrintPods(t *testing.T, printer ResourcePrinter) {
func testYamlPrintDevices(t *testing.T, printer ResourcePrinter) {
var buffer bytes.Buffer

data := []model.DeviceInfo{
data := []*device.Info{
{
Hostname: "foobar",
Labels: map[string]string{
"env": "test",
Labels: []*device.Label{
{Key: "env", Value: "test"},
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/printers/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
device "github.com/ernoaapa/eliot/pkg/api/services/device/v1"
pods "github.com/ernoaapa/eliot/pkg/api/services/pods/v1"
"github.com/ernoaapa/eliot/pkg/config"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
Expand All @@ -29,7 +28,7 @@ func (p *YamlPrinter) PrintPods(pods []*pods.Pod, w io.Writer) error {
}

// PrintDevices takes list of devices and prints to Writer in YAML format
func (p *YamlPrinter) PrintDevices(devices []model.DeviceInfo, w io.Writer) error {
func (p *YamlPrinter) PrintDevices(devices []*device.Info, w io.Writer) error {
if err := writeAsYml(devices, w); err != nil {
return errors.Wrap(err, "Failed to write devices yaml")
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/profile/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package profile

import (
"context"
"net/http"
_ "net/http/pprof"

log "github.com/sirupsen/logrus"
)

type Server struct {
httpServer *http.Server
}

// NewServer creates new profiling server
func NewServer(addr string) *Server {
return &Server{
httpServer: &http.Server{Addr: addr},
}
}

// Serve starts http server which serve the pperf
func (s *Server) Serve() {
err := s.httpServer.ListenAndServe()
if err != nil {
log.Panicf("Failed to start profiling http server: %s", err)
}
}

// Stop the http server
func (s *Server) Stop() {
err := s.httpServer.Shutdown(context.Background())
if err != nil {
log.Panicf("Failed to stop profiling http server: %s", err)
}
}
8 changes: 8 additions & 0 deletions pkg/utils/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ func RotateRBy(a *[]string, i int) {
x, b := (*a)[:(len(*a)-i)], (*a)[(len(*a)-i):]
*a = append(b, x...)
}

// GetFirst return first element from the list or default if list is empty
func GetFirst(l []string, d string) string {
if len(l) == 0 {
return d
}
return l[0]
}
13 changes: 7 additions & 6 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ github.com/urfave/cli v1.20.0
github.com/stretchr/testify v1.1.4
github.com/davecgh/go-spew v1.1.0
github.com/pmezard/go-difflib v1.0.0
github.com/containerd/containerd 556c46d0a9755f5b1620cc51cc5826593bf6e5d8
github.com/containerd/containerd 83cf37155670575ec0782bb7940b53876c151c40
github.com/sirupsen/logrus v1.0.3
golang.org/x/crypto 81e90905daefcd6fd217b62423c0908922eadb30
golang.org/x/sys bb24a47a89eac6c1227fbcb2ae37a8b9ed323366
github.com/Microsoft/go-winio v0.4.5
github.com/Microsoft/go-winio v0.4.7
github.com/containerd/fifo fbfb6a11ec671efbe94ad1c12c2e98773f19e1e6
github.com/gogo/protobuf v0.5
github.com/golang/protobuf 1643683e1b54a9e88ad26d98f81400c8c9d9f4f9
github.com/opencontainers/go-digest v1.0.0-rc1
github.com/opencontainers/image-spec v1.0.1
github.com/opencontainers/runc v1.0.0-rc4
github.com/opencontainers/runc 9f9c96235cc97674e935002fc3d78361b696a69e
github.com/opencontainers/runtime-spec v1.0.1
golang.org/x/net 66aacef3dd8a676686c7ae3716979581e8b03c47
google.golang.org/grpc v1.7.1
Expand All @@ -22,7 +22,7 @@ golang.org/x/text 19e51611da83d6be54ddafce4a4af510cb3e9ea4
github.com/docker/go-events 9461782956ad83b30282bf90e31fa6a70c255ba9
golang.org/x/sync 450f422ab23cf9881c94e2db30cac0eb1b7cf80c
github.com/containerd/continuity cf279e6ac893682272b4479d4c67fd3abf878b4e
gopkg.in/go-playground/validator.v9 v9.8.0
gopkg.in/go-playground/validator.v9 v9.9.4
github.com/go-playground/universal-translator v0.16.0
github.com/go-playground/locales v0.11.2
gopkg.in/yaml.v2 eb3733d160e74a9c7e442f435eb3bea458e1d19f
Expand All @@ -33,10 +33,11 @@ github.com/willf/pad b3d7806010228b1a954b4d9c4ee4cf6cb476b063
github.com/mattn/go-isatty v0.0.3
github.com/mattn/go-colorable v0.0.9
github.com/rs/xid v1.1
github.com/miekg/dns b38dc3dcb734f6ca3a830a1f92edf188330956c5
github.com/miekg/dns v1.0.4
github.com/cenkalti/backoff v1.1.0
github.com/containerd/typeurl f6943554a7e7e88b3c14aad190bf05932da84788
github.com/docker/docker v17.05.0-ce
k8s.io/kubernetes v1.9.0-alpha.2
github.com/thejerf/suture v2.0.1
github.com/ghodss/yaml v1.0.0
github.com/ghodss/yaml v1.0.0
github.com/syndtr/gocapability db04d3cc01c8b54962a58ec7e491717d06cfcc16
3 changes: 0 additions & 3 deletions vendor/github.com/Microsoft/go-winio/file.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 41b038d

Please sign in to comment.