Skip to content

Commit 4601775

Browse files
authored
[GLBC] Set Description field for backend services (#681)
Set svc namespace/name/port in description field of backend services
1 parent 188c64a commit 4601775

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

controllers/gce/backends/backends.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/golang/glog"
2727

2828
compute "google.golang.org/api/compute/v1"
29+
"k8s.io/apimachinery/pkg/types"
30+
"k8s.io/apimachinery/pkg/util/intstr"
2931
"k8s.io/apimachinery/pkg/util/sets"
3032
api_v1 "k8s.io/client-go/pkg/api/v1"
3133

@@ -92,6 +94,16 @@ func portKey(port int64) string {
9294
type ServicePort struct {
9395
Port int64
9496
Protocol utils.AppProtocol
97+
SvcName types.NamespacedName
98+
SvcPort intstr.IntOrString
99+
}
100+
101+
// Description returns a string describing the ServicePort.
102+
func (sp ServicePort) Description() string {
103+
if sp.SvcName.String() == "" || sp.SvcPort.String() == "" {
104+
return ""
105+
}
106+
return fmt.Sprintf(`{"kubernetes.io/service-name":"%s","kubernetes.io/service-port":"%s"}`, sp.SvcName.String(), sp.SvcPort.String())
95107
}
96108

97109
// NewBackendPool returns a new backend pool.
@@ -173,10 +185,11 @@ func (b *Backends) ensureHealthCheck(sp ServicePort) (string, error) {
173185
return b.healthChecker.Sync(hc)
174186
}
175187

176-
func (b *Backends) create(namedPort *compute.NamedPort, hcLink string, protocol utils.AppProtocol, name string) (*compute.BackendService, error) {
188+
func (b *Backends) create(namedPort *compute.NamedPort, hcLink string, sp ServicePort, name string) (*compute.BackendService, error) {
177189
bs := &compute.BackendService{
178190
Name: name,
179-
Protocol: string(protocol),
191+
Description: sp.Description(),
192+
Protocol: string(sp.Protocol),
180193
HealthChecks: []string{hcLink},
181194
Port: namedPort.Port,
182195
PortName: namedPort.Name,
@@ -210,7 +223,7 @@ func (b *Backends) Add(p ServicePort) error {
210223
be, _ = b.Get(p.Port)
211224
if be == nil {
212225
glog.V(2).Infof("Creating backend service for port %v named port %v", p.Port, namedPort)
213-
be, err = b.create(namedPort, hcLink, p.Protocol, pName)
226+
be, err = b.create(namedPort, hcLink, p, pName)
214227
if err != nil {
215228
return err
216229
}
@@ -225,6 +238,7 @@ func (b *Backends) Add(p ServicePort) error {
225238
glog.V(2).Infof("Updating backend protocol %v (%v) for change in protocol (%v) or health check", pName, be.Protocol, string(p.Protocol))
226239
be.Protocol = string(p.Protocol)
227240
be.HealthChecks = []string{hcLink}
241+
be.Description = p.Description()
228242
if err = b.cloud.UpdateBackendService(be); err != nil {
229243
return err
230244
}

controllers/gce/backends/backends_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func TestBackendPoolAdd(t *testing.T) {
6868
namer := utils.Namer{}
6969

7070
testCases := []ServicePort{
71-
{80, utils.ProtocolHTTP},
72-
{443, utils.ProtocolHTTPS},
71+
{Port: 80, Protocol: utils.ProtocolHTTP},
72+
{Port: 443, Protocol: utils.ProtocolHTTPS},
7373
}
7474

7575
for _, nodePort := range testCases {

controllers/gce/controller/utils.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/api/meta"
3030
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/labels"
32+
"k8s.io/apimachinery/pkg/types"
3233
"k8s.io/apimachinery/pkg/util/intstr"
3334
"k8s.io/apimachinery/pkg/util/sets"
3435
"k8s.io/apimachinery/pkg/util/wait"
@@ -461,7 +462,12 @@ PortLoop:
461462
proto = utils.AppProtocol(protoStr)
462463
}
463464

464-
p := backends.ServicePort{Port: int64(port.NodePort), Protocol: proto}
465+
p := backends.ServicePort{
466+
Port: int64(port.NodePort),
467+
Protocol: proto,
468+
SvcName: types.NamespacedName{Namespace: namespace, Name: be.ServiceName},
469+
SvcPort: be.ServicePort,
470+
}
465471
return p, nil
466472
}
467473

controllers/gce/main.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
flag "github.com/spf13/pflag"
3333
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3434
"k8s.io/apimachinery/pkg/labels"
35+
"k8s.io/apimachinery/pkg/types"
36+
"k8s.io/apimachinery/pkg/util/intstr"
3537
"k8s.io/apimachinery/pkg/util/wait"
3638
"k8s.io/client-go/kubernetes"
3739
"k8s.io/client-go/pkg/api"
@@ -227,13 +229,18 @@ func main() {
227229
glog.Fatalf("Default backend should take the form namespace/name: %v",
228230
*defaultSvc)
229231
}
230-
nodePort, err := getNodePort(kubeClient, parts[0], parts[1])
232+
port, nodePort, err := getNodePort(kubeClient, parts[0], parts[1])
231233
if err != nil {
232234
glog.Fatalf("Could not configure default backend %v: %v",
233235
*defaultSvc, err)
234236
}
235237
// The default backend is known to be HTTP
236-
defaultBackendNodePort := backends.ServicePort{Port: nodePort, Protocol: utils.ProtocolHTTP}
238+
defaultBackendNodePort := backends.ServicePort{
239+
Port: int64(nodePort),
240+
Protocol: utils.ProtocolHTTP,
241+
SvcName: types.NamespacedName{Namespace: parts[0], Name: parts[1]},
242+
SvcPort: intstr.FromInt(int(port)),
243+
}
237244

238245
if *inCluster || *useRealCloud {
239246
// Create cluster manager
@@ -411,7 +418,7 @@ func getClusterUID(kubeClient kubernetes.Interface, name string) (string, error)
411418
}
412419

413420
// getNodePort waits for the Service, and returns it's first node port.
414-
func getNodePort(client kubernetes.Interface, ns, name string) (nodePort int64, err error) {
421+
func getNodePort(client kubernetes.Interface, ns, name string) (port, nodePort int32, err error) {
415422
var svc *api_v1.Service
416423
glog.V(3).Infof("Waiting for %v/%v", ns, name)
417424
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,
421428
}
422429
for _, p := range svc.Spec.Ports {
423430
if p.NodePort != 0 {
424-
nodePort = int64(p.NodePort)
431+
port = p.Port
432+
nodePort = p.NodePort
425433
glog.V(3).Infof("Node port %v", nodePort)
426434
break
427435
}

0 commit comments

Comments
 (0)