Skip to content

Commit

Permalink
Merge pull request #657 from brotherlogic/change_cli
Browse files Browse the repository at this point in the history
Supports kube lookup for ingress. This closes #656
  • Loading branch information
brotherlogic committed Feb 21, 2023
2 parents aa34485 + 9723114 commit 5885ca1
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 176 deletions.
2 changes: 2 additions & 0 deletions discovery.go
Expand Up @@ -100,6 +100,7 @@ type Server struct {
config *pb.Config
internalState *pb.InternalState
zone string
kube bool
}

type httpGetter interface {
Expand Down Expand Up @@ -546,6 +547,7 @@ func main() {

server.RegisterServerV2(false)
server.SendTrace = false
server.kube = true

server.readZone()

Expand Down
67 changes: 67 additions & 0 deletions discoveryv2api.go
Expand Up @@ -3,14 +3,20 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

pb "github.com/brotherlogic/discovery/proto"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -37,6 +43,44 @@ var (
}, []string{"service", "origin"})
)

func (s *Server) getFromKube(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) {
// use the current context in kubeconfig
kubeconfig := flag.String("kubeconfig", filepath.Join("/home/simon/", ".kube", "config"), "(optional) absolute path to the kubeconfig file")
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
s.RaiseIssue("Missing kubeconfig", fmt.Sprintf("%v is missing the kube config", s.Registry.Identifier))
}

// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

pods, err := clientset.CoreV1().Services("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
var services []*pb.RegistryEntry

for _, pod := range pods.Items {
if pod.Name == req.GetJob() || req.GetJob() == "" {
for _, port := range pod.Spec.Ports {
if port.Name == "grpc" && port.NodePort > 0 {
services = append(services, &pb.RegistryEntry{
Name: pod.Name,
Identifier: "kclust1",
Port: port.NodePort,
Zone: "cluster",
})
}
}
}
}

return &pb.GetResponse{Services: services}, nil
}

func (s *Server) addIP(ip string) {
for _, lip := range s.iplist {
if lip == ip {
Expand Down Expand Up @@ -195,6 +239,17 @@ func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse,
return &pb.GetResponse{Services: jobs, State: s.state}, nil
}

if s.kube {
maybe, err := s.getFromKube(ctx, req)
if err != nil {
return nil, err
}

if len(maybe.Services) > 0 {
return maybe, err
}
}

return nil, status.Errorf(codes.Unavailable, "%v has not found on %v (via %v)", req.Job, req.Server, s.Registry.GetIdentifier())
}

Expand All @@ -205,6 +260,18 @@ func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse,
resp.Services = append(resp.Services, job)
}
}

if s.kube {
maybe, err := s.getFromKube(ctx, req)
if err != nil {
return nil, err
}

if len(maybe.Services) > 0 {
resp.Services = append(resp.Services, maybe.Services...)
}
}

return resp, nil
}

Expand Down
59 changes: 46 additions & 13 deletions go.mod
@@ -1,6 +1,6 @@
module github.com/brotherlogic/discovery

go 1.18
go 1.19

require (
github.com/brotherlogic/goserver v0.0.0-20230208171911-c95aab060a3f
Expand All @@ -9,34 +9,67 @@ require (
golang.org/x/net v0.7.0
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/brotherlogic/buildserver v0.0.0-20230218192153-72cd682a0baa // indirect
github.com/brotherlogic/buildserver v0.0.0-20230127230943-7858efe83402 // indirect
github.com/brotherlogic/datastore v0.0.0-20230118231105-ee668be05973 // indirect
github.com/brotherlogic/dstore v0.0.0-20230214030217-c6f0f5bb942c // indirect
github.com/brotherlogic/githubcard v0.0.0-20230220025615-94a8b7a71a1b // indirect
github.com/brotherlogic/gobuildslave v0.0.0-20230220010314-a70cf0a785d7 // indirect
github.com/brotherlogic/keystore v0.0.0-20230217042242-6e6e50134c19 // indirect
github.com/brotherlogic/lock v0.0.0-20230219162829-bea74ada57a2 // indirect
github.com/brotherlogic/logging v0.0.0-20230219195205-f4540716720b // indirect
github.com/brotherlogic/dstore v0.0.0-20230130175634-8589e150cbab // indirect
github.com/brotherlogic/githubcard v0.0.0-20230201203006-0668c3fa233b // indirect
github.com/brotherlogic/gobuildslave v0.0.0-20230129203430-42b71a044131 // indirect
github.com/brotherlogic/keystore v0.0.0-20230127024524-a987822bb7b0 // indirect
github.com/brotherlogic/lock v0.0.0-20230129012225-5ba9e01895e1 // indirect
github.com/brotherlogic/logging v0.0.0-20230201005903-054ed96ad352 // indirect
github.com/brotherlogic/monitor v0.0.0-20221025152653-c10877c5f9e6 // indirect
github.com/brotherlogic/versionserver v0.0.0-20221025154054-c9bcd41be2f2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/struCoder/pidusage v0.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.39.0 // indirect
go.opentelemetry.io/otel v1.13.0 // indirect
go.opentelemetry.io/otel/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.38.0 // indirect
go.opentelemetry.io/otel v1.12.0 // indirect
go.opentelemetry.io/otel/metric v0.35.0 // indirect
go.opentelemetry.io/otel/trace v1.12.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect
golang.org/x/time v0.1.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230131230820-1c016267d619 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

0 comments on commit 5885ca1

Please sign in to comment.