Skip to content

Commit

Permalink
Merge pull request #1785 from brotherlogic/fix_get
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Aug 5, 2023
2 parents 008f195 + 4f9fde0 commit 77a812e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 185 deletions.
68 changes: 1 addition & 67 deletions discoveryv2api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"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 @@ -41,45 +37,6 @@ var (
}, []string{"service", "origin"})
)

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

// 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",
Ip: "192.168.86.26",
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 @@ -238,18 +195,7 @@ 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 {
s.CtxLog(ctx, fmt.Sprintf("KUBE: %v", err))
} else {
if len(maybe.Services) > 0 {
return maybe, err
}
}
}

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

resp := &pb.GetResponse{Services: []*pb.RegistryEntry{}, State: s.state}
Expand All @@ -260,18 +206,6 @@ func (s *Server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse,
}
}

if s.kube {
maybe, err := s.getFromKube(ctx, req)
if err != nil {
s.CtxLog(ctx, fmt.Sprintf("unable to get from kube: %v", err))
} else {

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

return resp, nil
}

Expand Down
64 changes: 12 additions & 52 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
module github.com/brotherlogic/discovery

go 1.19
go 1.18

require (
github.com/brotherlogic/goserver v0.0.0-20230330215506-65bad61eb180
github.com/brotherlogic/goserver v0.0.0-20230726200050-779ae6811580
github.com/golang/protobuf v1.5.3
github.com/prometheus/client_golang v1.15.1
golang.org/x/net v0.10.0
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
github.com/prometheus/client_golang v1.16.0
golang.org/x/net v0.14.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
)

require (
Expand All @@ -19,58 +17,20 @@ require (
github.com/brotherlogic/datastore v0.0.0-20230118231105-ee668be05973 // indirect
github.com/brotherlogic/dstore v0.0.0-20230527173950-04e3ae8f02ec // indirect
github.com/brotherlogic/githubcard v0.0.0-20230524015807-3bafe4a97e26 // indirect
github.com/brotherlogic/gobuildslave v0.0.0-20230521011524-ecb7b664c7b0 // indirect
github.com/brotherlogic/keystore v0.0.0-20230522040346-409b74da0718 // indirect
github.com/brotherlogic/lock v0.0.0-20230522041011-f7c6edf0ff46 // indirect
github.com/brotherlogic/gobuildslave v0.0.0-20230528035915-9de608647d34 // indirect
github.com/brotherlogic/keystore v0.0.0-20230529052121-9d261c934fc9 // indirect
github.com/brotherlogic/lock v0.0.0-20230529052859-d7f9e8276725 // indirect
github.com/brotherlogic/logging v0.0.0-20230523181650-b961f3b83607 // 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.10.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // 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.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/struCoder/pidusage v0.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.42.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e // 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.27.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 77a812e

Please sign in to comment.