Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
2f4c4846
Unverified
Commit
2f4c4846
authored
Aug 05, 2021
by
Mark Tyneway
Committed by
GitHub
Aug 05, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1286 from ethereum-optimism/bwilson/op_exporter-k8s-api
Adding k8s api client to op_exporter
parents
a8c9cd67
0572c242
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
260 additions
and
10 deletions
+260
-10
go.mod
go/op_exporter/go.mod
+2
-0
go.sum
go/op_exporter/go.sum
+176
-0
main.go
go/op_exporter/k8sClient/main.go
+20
-0
main.go
go/op_exporter/main.go
+62
-10
No files found.
go/op_exporter/go.mod
View file @
2f4c4846
...
...
@@ -8,4 +8,6 @@ require (
github.com/sirupsen/logrus v1.4.2
github.com/ybbus/jsonrpc v2.1.2+incompatible
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/apimachinery v0.21.2 // indirect
k8s.io/client-go v0.21.2
)
go/op_exporter/go.sum
View file @
2f4c4846
This diff is collapsed.
Click to expand it.
go/op_exporter/k8sClient/main.go
0 → 100644
View file @
2f4c4846
package
k8sClient
import
(
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func
Newk8sClient
()
(
client
*
kubernetes
.
Clientset
,
err
error
)
{
// creates the in-cluster config
config
,
err
:=
rest
.
InClusterConfig
()
if
err
!=
nil
{
panic
(
err
.
Error
())
}
// creates the clientset
client
,
err
=
kubernetes
.
NewForConfig
(
config
)
if
err
!=
nil
{
panic
(
err
.
Error
())
}
return
client
,
nil
}
go/op_exporter/main.go
View file @
2f4c4846
package
main
import
(
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
"github.com/ethereum-optimism/optimism/go/op_exporter/k8sClient"
"github.com/ethereum-optimism/optimism/go/op_exporter/version"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prometheus/client_golang/prometheus/promhttp"
log
"github.com/sirupsen/logrus"
"github.com/ybbus/jsonrpc"
"gopkg.in/alecthomas/kingpin.v2"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var
(
...
...
@@ -36,7 +41,10 @@ var (
"wait.minutes"
,
"Number of minutes to wait for the next block before marking provider unhealthy."
,
)
.
Default
(
"10"
)
.
Int
()
//unhealthyTimePeriod = time.Minute * 10
enableK8sQuery
=
kingpin
.
Flag
(
"k8s.enable"
,
"Enable kubernetes info lookup."
,
)
.
Default
(
"true"
)
.
Bool
()
)
type
healthCheck
struct
{
...
...
@@ -44,13 +52,15 @@ type healthCheck struct {
height
uint64
healthy
bool
updateTime
time
.
Time
allowedMethods
[]
string
version
*
string
}
func
healthHandler
(
health
*
healthCheck
)
http
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
health
.
mu
.
RLock
()
defer
health
.
mu
.
RUnlock
()
w
.
Write
([]
byte
(
fmt
.
Sprintf
(
`{ "healthy": "%t"
}`
,
health
.
healthy
)))
w
.
Write
([]
byte
(
fmt
.
Sprintf
(
`{ "healthy": "%t"
, "version": "%s" }`
,
health
.
healthy
,
*
health
.
version
)))
}
}
...
...
@@ -71,6 +81,8 @@ func main() {
height
:
0
,
healthy
:
false
,
updateTime
:
time
.
Now
(),
allowedMethods
:
nil
,
version
:
nil
,
}
http
.
Handle
(
"/metrics"
,
promhttp
.
Handler
())
http
.
Handle
(
"/health"
,
healthHandler
(
&
health
))
...
...
@@ -86,6 +98,13 @@ func main() {
})
go
getRollupGasPrices
()
go
getBlockNumber
(
&
health
)
if
*
enableK8sQuery
{
client
,
err
:=
k8sClient
.
Newk8sClient
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
go
getSequencerVersion
(
&
health
,
client
)
}
log
.
Infoln
(
"Listening on"
,
*
listenAddress
)
if
err
:=
http
.
ListenAndServe
(
*
listenAddress
,
nil
);
err
!=
nil
{
log
.
Fatal
(
err
)
...
...
@@ -93,6 +112,39 @@ func main() {
}
func
getSequencerVersion
(
health
*
healthCheck
,
client
*
kubernetes
.
Clientset
)
{
ns
,
err
:=
ioutil
.
ReadFile
(
"/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
if
err
!=
nil
{
log
.
Fatalf
(
"Unable to read namespace file: %s"
,
err
)
}
ticker
:=
time
.
NewTicker
(
30
*
time
.
Second
)
for
{
<-
ticker
.
C
getOpts
:=
metav1
.
GetOptions
{
TypeMeta
:
metav1
.
TypeMeta
{},
ResourceVersion
:
""
,
}
sequencerStatefulSet
,
err
:=
client
.
AppsV1
()
.
StatefulSets
(
string
(
ns
))
.
Get
(
context
.
TODO
(),
"sequencer"
,
getOpts
)
if
err
!=
nil
{
unknownStatus
:=
"UNKNOWN"
health
.
version
=
&
unknownStatus
log
.
Errorf
(
"Unable to retrieve a sequencer StatefulSet: %s"
,
err
)
continue
}
for
_
,
c
:=
range
sequencerStatefulSet
.
Spec
.
Template
.
Spec
.
Containers
{
log
.
Infof
(
"Checking container %s"
,
c
.
Name
)
switch
{
case
c
.
Name
==
"sequencer"
:
log
.
Infof
(
"The sequencer version is: %s"
,
c
.
Image
)
health
.
version
=
&
c
.
Image
default
:
log
.
Infof
(
"Unable to find the sequencer container in the statefulset?!?"
)
}
}
}
}
func
getBlockNumber
(
health
*
healthCheck
)
{
rpcClient
:=
jsonrpc
.
NewClientWithOpts
(
*
rpcProvider
,
&
jsonrpc
.
RPCClientOpts
{})
var
blockNumberResponse
*
string
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment