add k8s service ip to metadata (#1367)

* add k8s service ip to metadata

* go fmt

* use same port as container
This commit is contained in:
Asim Aslam 2020-03-18 18:27:29 +00:00 committed by GitHub
parent c91bf7e9e7
commit 1bd340701b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 7 deletions

View File

@ -2,6 +2,7 @@
package kubernetes package kubernetes
import ( import (
"fmt"
"sync" "sync"
"time" "time"
@ -70,8 +71,7 @@ func (k *kubernetes) getService(labels map[string]string) ([]*service, error) {
// version of the service // version of the service
version := kservice.Metadata.Labels["version"] version := kservice.Metadata.Labels["version"]
// save as service srv := &service{
svcMap[name+version] = &service{
Service: &runtime.Service{ Service: &runtime.Service{
Name: name, Name: name,
Version: version, Version: version,
@ -80,10 +80,18 @@ func (k *kubernetes) getService(labels map[string]string) ([]*service, error) {
kservice: &kservice, kservice: &kservice,
} }
// set the address
address := kservice.Spec.ClusterIP
port := kservice.Spec.Ports[0]
srv.Service.Metadata["address"] = fmt.Sprintf("%s:%d", address, port.Port)
// copy annotations metadata into service metadata // copy annotations metadata into service metadata
for k, v := range kservice.Metadata.Annotations { for k, v := range kservice.Metadata.Annotations {
svcMap[name+version].Service.Metadata[k] = v srv.Service.Metadata[k] = v
} }
// save as service
svcMap[name+version] = srv
} }
// collect additional info from kubernetes deployment // collect additional info from kubernetes deployment

View File

@ -217,7 +217,7 @@ func NewService(name, version, typ string) *Service {
Type: "ClusterIP", Type: "ClusterIP",
Selector: Labels, Selector: Labels,
Ports: []ServicePort{{ Ports: []ServicePort{{
"service-port", 9090, "", "service-port", 8080, "",
}}, }},
} }

View File

@ -132,6 +132,7 @@ type ServicePort struct {
// ServiceSpec provides service configuration // ServiceSpec provides service configuration
type ServiceSpec struct { type ServiceSpec struct {
ClusterIP string `json:"clusterIP"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Selector map[string]string `json:"selector,omitempty"` Selector map[string]string `json:"selector,omitempty"`
Ports []ServicePort `json:"ports,omitempty"` Ports []ServicePort `json:"ports,omitempty"`