Skip to content

Commit

Permalink
[GLBC] Set Description field for backend services (#681)
Browse files Browse the repository at this point in the history
Set svc namespace/name/port in description field of backend services
  • Loading branch information
nicksardo authored May 5, 2017
1 parent 188c64a commit 4601775
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
20 changes: 17 additions & 3 deletions controllers/gce/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/gce/backends/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion controllers/gce/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 12 additions & 4 deletions controllers/gce/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit 4601775

Please sign in to comment.