[GLBC] Set Description field for backend services (#681)
Set svc namespace/name/port in description field of backend services
This commit is contained in:
parent
188c64aaac
commit
4601775c18
4 changed files with 38 additions and 10 deletions
|
@ -26,6 +26,8 @@ import (
|
|||
"github.com/golang/glog"
|
||||
|
||||
compute "google.golang.org/api/compute/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
api_v1 "k8s.io/client-go/pkg/api/v1"
|
||||
|
||||
|
@ -92,6 +94,16 @@ func portKey(port int64) string {
|
|||
type ServicePort struct {
|
||||
Port int64
|
||||
Protocol utils.AppProtocol
|
||||
SvcName types.NamespacedName
|
||||
SvcPort intstr.IntOrString
|
||||
}
|
||||
|
||||
// Description returns a string describing the ServicePort.
|
||||
func (sp ServicePort) Description() string {
|
||||
if sp.SvcName.String() == "" || sp.SvcPort.String() == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(`{"kubernetes.io/service-name":"%s","kubernetes.io/service-port":"%s"}`, sp.SvcName.String(), sp.SvcPort.String())
|
||||
}
|
||||
|
||||
// NewBackendPool returns a new backend pool.
|
||||
|
@ -173,10 +185,11 @@ func (b *Backends) ensureHealthCheck(sp ServicePort) (string, error) {
|
|||
return b.healthChecker.Sync(hc)
|
||||
}
|
||||
|
||||
func (b *Backends) create(namedPort *compute.NamedPort, hcLink string, protocol utils.AppProtocol, name string) (*compute.BackendService, error) {
|
||||
func (b *Backends) create(namedPort *compute.NamedPort, hcLink string, sp ServicePort, name string) (*compute.BackendService, error) {
|
||||
bs := &compute.BackendService{
|
||||
Name: name,
|
||||
Protocol: string(protocol),
|
||||
Description: sp.Description(),
|
||||
Protocol: string(sp.Protocol),
|
||||
HealthChecks: []string{hcLink},
|
||||
Port: namedPort.Port,
|
||||
PortName: namedPort.Name,
|
||||
|
@ -210,7 +223,7 @@ func (b *Backends) Add(p ServicePort) error {
|
|||
be, _ = b.Get(p.Port)
|
||||
if be == nil {
|
||||
glog.V(2).Infof("Creating backend service for port %v named port %v", p.Port, namedPort)
|
||||
be, err = b.create(namedPort, hcLink, p.Protocol, pName)
|
||||
be, err = b.create(namedPort, hcLink, p, pName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -225,6 +238,7 @@ func (b *Backends) Add(p ServicePort) error {
|
|||
glog.V(2).Infof("Updating backend protocol %v (%v) for change in protocol (%v) or health check", pName, be.Protocol, string(p.Protocol))
|
||||
be.Protocol = string(p.Protocol)
|
||||
be.HealthChecks = []string{hcLink}
|
||||
be.Description = p.Description()
|
||||
if err = b.cloud.UpdateBackendService(be); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -68,8 +68,8 @@ func TestBackendPoolAdd(t *testing.T) {
|
|||
namer := utils.Namer{}
|
||||
|
||||
testCases := []ServicePort{
|
||||
{80, utils.ProtocolHTTP},
|
||||
{443, utils.ProtocolHTTPS},
|
||||
{Port: 80, Protocol: utils.ProtocolHTTP},
|
||||
{Port: 443, Protocol: utils.ProtocolHTTPS},
|
||||
}
|
||||
|
||||
for _, nodePort := range testCases {
|
||||
|
|
|
@ -29,6 +29,7 @@ import (
|
|||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
|
@ -461,7 +462,12 @@ PortLoop:
|
|||
proto = utils.AppProtocol(protoStr)
|
||||
}
|
||||
|
||||
p := backends.ServicePort{Port: int64(port.NodePort), Protocol: proto}
|
||||
p := backends.ServicePort{
|
||||
Port: int64(port.NodePort),
|
||||
Protocol: proto,
|
||||
SvcName: types.NamespacedName{Namespace: namespace, Name: be.ServiceName},
|
||||
SvcPort: be.ServicePort,
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ import (
|
|||
flag "github.com/spf13/pflag"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/pkg/api"
|
||||
|
@ -227,13 +229,18 @@ func main() {
|
|||
glog.Fatalf("Default backend should take the form namespace/name: %v",
|
||||
*defaultSvc)
|
||||
}
|
||||
nodePort, err := getNodePort(kubeClient, parts[0], parts[1])
|
||||
port, nodePort, err := getNodePort(kubeClient, parts[0], parts[1])
|
||||
if err != nil {
|
||||
glog.Fatalf("Could not configure default backend %v: %v",
|
||||
*defaultSvc, err)
|
||||
}
|
||||
// The default backend is known to be HTTP
|
||||
defaultBackendNodePort := backends.ServicePort{Port: nodePort, Protocol: utils.ProtocolHTTP}
|
||||
defaultBackendNodePort := backends.ServicePort{
|
||||
Port: int64(nodePort),
|
||||
Protocol: utils.ProtocolHTTP,
|
||||
SvcName: types.NamespacedName{Namespace: parts[0], Name: parts[1]},
|
||||
SvcPort: intstr.FromInt(int(port)),
|
||||
}
|
||||
|
||||
if *inCluster || *useRealCloud {
|
||||
// Create cluster manager
|
||||
|
@ -411,7 +418,7 @@ func getClusterUID(kubeClient kubernetes.Interface, name string) (string, error)
|
|||
}
|
||||
|
||||
// getNodePort waits for the Service, and returns it's first node port.
|
||||
func getNodePort(client kubernetes.Interface, ns, name string) (nodePort int64, err error) {
|
||||
func getNodePort(client kubernetes.Interface, ns, name string) (port, nodePort int32, err error) {
|
||||
var svc *api_v1.Service
|
||||
glog.V(3).Infof("Waiting for %v/%v", ns, name)
|
||||
wait.Poll(1*time.Second, 5*time.Minute, func() (bool, error) {
|
||||
|
@ -421,7 +428,8 @@ func getNodePort(client kubernetes.Interface, ns, name string) (nodePort int64,
|
|||
}
|
||||
for _, p := range svc.Spec.Ports {
|
||||
if p.NodePort != 0 {
|
||||
nodePort = int64(p.NodePort)
|
||||
port = p.Port
|
||||
nodePort = p.NodePort
|
||||
glog.V(3).Infof("Node port %v", nodePort)
|
||||
break
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue