-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rebase ENG-8444 splitting main package
PR-34 from kelly deng Signed-off-by: Raj Dharwadkar <[email protected]>
- Loading branch information
1 parent
c8a6831
commit 7621946
Showing
22 changed files
with
1,824 additions
and
1,427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
certs/ | ||
/hegel | ||
/cmd/hegel/hegel | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
binary := cmd/hegel | ||
all: ${binary} | ||
.PHONY: ${binary} gen test | ||
${binary}: grpc/protos/hegel | ||
${binary}: | ||
CGO_ENABLED=0 GOOS=$$GOOS go build -ldflags="-X main.GitRev=$(shell git rev-parse --short HEAD)" -o $@ ./$@ | ||
gen: grpc/protos/hegel/hegel.pb.go | ||
grpc/protos/hegel/hegel.pb.go: grpc/protos/hegel/hegel.proto | ||
protoc --go_out=plugins=grpc:./ grpc/protos/hegel/hegel.proto | ||
goimports -w $@ | ||
ifeq ($(CI),drone) | ||
run: ${binary} | ||
${binary} | ||
test: | ||
go test -race -coverprofile=coverage.txt -covermode=atomic ${TEST_ARGS} ./... | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
_ "net/http/pprof" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/packethost/pkg/env" | ||
"github.com/packethost/pkg/log" | ||
grpcserver "github.com/tinkerbell/hegel/grpc-server" | ||
httpserver "github.com/tinkerbell/hegel/http-server" | ||
"github.com/tinkerbell/hegel/metrics" | ||
) | ||
|
||
var ( | ||
GitRev string | ||
logger log.Logger | ||
customEndpoints string | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
// setup structured logging | ||
l, err := log.Init("github.com/tinkerbell/hegel") | ||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
logger = l.Package("main") | ||
defer l.Close() | ||
metrics.Init(l) | ||
|
||
metrics.State.Set(metrics.Initializing) | ||
|
||
hegelServer, err := grpcserver.NewServer(l, nil) | ||
if err != nil { | ||
logger.Fatal(err, "failed to create hegel server") | ||
} | ||
|
||
c := make(chan os.Signal) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
var ret error | ||
ctx, cancel := context.WithCancel(context.TODO()) | ||
var once sync.Once | ||
var wg sync.WaitGroup | ||
customEndpoints = env.Get("CUSTOM_ENDPOINTS", `{"/metadata":".metadata.instance"}`) | ||
|
||
runGoroutine(&ret, cancel, &once, &wg, func() error { | ||
return httpserver.Serve(ctx, logger, hegelServer, GitRev, time.Now(), customEndpoints) | ||
}) | ||
|
||
runGoroutine(&ret, cancel, &once, &wg, func() error { | ||
return grpcserver.Serve(ctx, logger, hegelServer) | ||
}) | ||
runGoroutine(&ret, cancel, &once, &wg, func() error { | ||
select { | ||
case sig, ok := <-c: | ||
if ok { | ||
l.With("signal", sig).Info("received stop signal, gracefully shutting down") | ||
cancel() | ||
} | ||
case <-ctx.Done(): | ||
} | ||
return nil | ||
}) | ||
l.Info("waiting") | ||
wg.Wait() | ||
if ret != nil { | ||
logger.Fatal(ret) | ||
} | ||
} | ||
|
||
func runGoroutine(ret *error, cancel context.CancelFunc, once *sync.Once, wg *sync.WaitGroup, fn func() error) { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := fn() | ||
if err != nil { | ||
// we only care about the first error | ||
once.Do(func() { | ||
*ret = err | ||
cancel() | ||
}) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/packethost/hegel | ||
module github.com/tinkerbell/hegel | ||
|
||
go 1.13 | ||
|
||
|
Oops, something went wrong.