Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small aesthetic fixes to code base. #266

Merged
merged 1 commit into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/controller/cluster_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *ClusterManager) EnsureLoadBalancer(lb *loadbalancers.L7RuntimeInfo, lbS
return err
}

return c.l7Pool.Sync([]*loadbalancers.L7RuntimeInfo{lb})
return c.l7Pool.Sync(lb)
}

func (c *ClusterManager) EnsureInstanceGroupsAndPorts(nodeNames []string, servicePorts []utils.ServicePort) ([]*compute.InstanceGroup, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type LoadBalancerController struct {
// TODO: Watch secrets
CloudClusterManager *ClusterManager
ingQueue utils.TaskQueue
Translator *translator.GCE
Translator *translator.Translator
stopCh chan struct{}
// stopLock is used to enforce only a single call to Stop is active.
// Needed because we allow stopping through an http endpoint and
Expand Down Expand Up @@ -170,7 +170,7 @@ func NewLoadBalancerController(kubeClient kubernetes.Interface, stopCh chan stru
if ctx.EndpointInformer != nil {
endpointIndexer = ctx.EndpointInformer.GetIndexer()
}
lbc.Translator = translator.New(lbc.ctx, lbc.CloudClusterManager.ClusterNamer,
lbc.Translator = translator.NewTranslator(lbc.ctx, lbc.CloudClusterManager.ClusterNamer,
ctx.ServiceInformer.GetIndexer(),
ctx.NodeInformer.GetIndexer(),
ctx.PodInformer.GetIndexer(),
Expand Down
25 changes: 13 additions & 12 deletions pkg/controller/translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ type recorderSource interface {
Recorder(ns string) record.EventRecorder
}

// New returns a new ControllerContext.
func New(recorders recorderSource, namer *utils.Namer, svcLister cache.Indexer, nodeLister cache.Indexer, podLister cache.Indexer, endpointLister cache.Indexer, negEnabled bool) *GCE {
return &GCE{
// NewTranslator returns a new Translator.
func NewTranslator(recorders recorderSource,
namer *utils.Namer, svcLister cache.Indexer, nodeLister cache.Indexer, podLister cache.Indexer, endpointLister cache.Indexer, negEnabled bool) *Translator {
return &Translator{
recorders,
namer,
svcLister,
Expand All @@ -58,8 +59,8 @@ func New(recorders recorderSource, namer *utils.Namer, svcLister cache.Indexer,
}
}

// GCE helps with kubernetes -> gce api conversion.
type GCE struct {
// Translator helps with kubernetes -> gce api conversion.
type Translator struct {
recorders recorderSource

namer *utils.Namer
Expand All @@ -72,7 +73,7 @@ type GCE struct {

// getServiceNodePort looks in the svc store for a matching service:port,
// and returns the nodeport.
func (t *GCE) getServiceNodePort(be extensions.IngressBackend, namespace string) (utils.ServicePort, error) {
func (t *Translator) getServiceNodePort(be extensions.IngressBackend, namespace string) (utils.ServicePort, error) {
obj, exists, err := t.svcLister.Get(
&api_v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
Expand Down Expand Up @@ -137,7 +138,7 @@ PortLoop:
}

// TranslateIngress converts an Ingress into our internal UrlMap representation.
func (t *GCE) TranslateIngress(ing *extensions.Ingress, glbcDefaultBackend utils.ServicePort) *utils.GCEURLMap {
func (t *Translator) TranslateIngress(ing *extensions.Ingress, glbcDefaultBackend utils.ServicePort) *utils.GCEURLMap {
urlMap := utils.NewGCEURLMap()
for _, rule := range ing.Spec.Rules {
if rule.HTTP == nil {
Expand Down Expand Up @@ -194,7 +195,7 @@ func getZone(n *api_v1.Node) string {
}

// GetZoneForNode returns the zone for a given node by looking up its zone label.
func (t *GCE) GetZoneForNode(name string) (string, error) {
func (t *Translator) GetZoneForNode(name string) (string, error) {
nodes, err := listers.NewNodeLister(t.nodeLister).ListWithPredicate(utils.NodeIsReady)
if err != nil {
return "", err
Expand All @@ -210,7 +211,7 @@ func (t *GCE) GetZoneForNode(name string) (string, error) {
}

// ListZones returns a list of zones this Kubernetes cluster spans.
func (t *GCE) ListZones() ([]string, error) {
func (t *Translator) ListZones() ([]string, error) {
zones := sets.String{}
readyNodes, err := listers.NewNodeLister(t.nodeLister).ListWithPredicate(utils.NodeIsReady)
if err != nil {
Expand All @@ -224,7 +225,7 @@ func (t *GCE) ListZones() ([]string, error) {

// geHTTPProbe returns the http readiness probe from the first container
// that matches targetPort, from the set of pods matching the given labels.
func (t *GCE) getHTTPProbe(svc api_v1.Service, targetPort intstr.IntOrString, protocol annotations.AppProtocol) (*api_v1.Probe, error) {
func (t *Translator) getHTTPProbe(svc api_v1.Service, targetPort intstr.IntOrString, protocol annotations.AppProtocol) (*api_v1.Probe, error) {
l := svc.Spec.Selector

// Lookup any container with a matching targetPort from the set of pods
Expand Down Expand Up @@ -274,7 +275,7 @@ func (t *GCE) getHTTPProbe(svc api_v1.Service, targetPort intstr.IntOrString, pr
}

// GatherEndpointPorts returns all ports needed to open NEG endpoints.
func (t *GCE) GatherEndpointPorts(svcPorts []utils.ServicePort) []string {
func (t *Translator) GatherEndpointPorts(svcPorts []utils.ServicePort) []string {
portMap := map[int64]bool{}
for _, p := range svcPorts {
if t.negEnabled && p.NEGEnabled {
Expand Down Expand Up @@ -304,7 +305,7 @@ func isSimpleHTTPProbe(probe *api_v1.Probe) bool {
}

// GetProbe returns a probe that's used for the given nodeport
func (t *GCE) GetProbe(port utils.ServicePort) (*api_v1.Probe, error) {
func (t *Translator) GetProbe(port utils.ServicePort) (*api_v1.Probe, error) {
sl := t.svcLister.List()

// Find the label and target port of the one service with the given nodePort
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
firstPodCreationTime = time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC)
)

func gceForTest(negEnabled bool) *GCE {
func fakeTranslator(negEnabled bool) *Translator {
client := fake.NewSimpleClientset()
broadcaster := record.NewBroadcaster()
broadcaster.StartLogging(glog.Infof)
Expand All @@ -52,7 +52,7 @@ func gceForTest(negEnabled bool) *GCE {
namer := utils.NewNamer("uid1", "fw1")

ctx := context.NewControllerContext(client, apiv1.NamespaceAll, 1*time.Second, negEnabled)
gce := &GCE{
gce := &Translator{
recorders: ctx,
namer: namer,
svcLister: ctx.ServiceInformer.GetIndexer(),
Expand All @@ -67,7 +67,7 @@ func gceForTest(negEnabled bool) *GCE {
}

func TestGetProbe(t *testing.T) {
translator := gceForTest(false)
translator := fakeTranslator(false)
nodePortToHealthCheck := map[utils.ServicePort]string{
{NodePort: 3001, Protocol: annotations.ProtocolHTTP}: "/healthz",
{NodePort: 3002, Protocol: annotations.ProtocolHTTPS}: "/foo",
Expand All @@ -90,7 +90,7 @@ func TestGetProbe(t *testing.T) {
}

func TestGetProbeNamedPort(t *testing.T) {
translator := gceForTest(false)
translator := fakeTranslator(false)
nodePortToHealthCheck := map[utils.ServicePort]string{
{NodePort: 3001, Protocol: annotations.ProtocolHTTP}: "/healthz",
}
Expand All @@ -113,7 +113,7 @@ func TestGetProbeNamedPort(t *testing.T) {
}

func TestGetProbeCrossNamespace(t *testing.T) {
translator := gceForTest(false)
translator := fakeTranslator(false)

firstPod := &apiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -237,7 +237,7 @@ func getProbePath(p *apiv1.Probe) string {
}

func TestGatherEndpointPorts(t *testing.T) {
translator := gceForTest(true)
translator := fakeTranslator(true)

ep1 := "ep1"
ep2 := "ep2"
Expand Down
3 changes: 1 addition & 2 deletions pkg/loadbalancers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ type LoadBalancers interface {
// with a gce loadbalancer.
type LoadBalancerPool interface {
Get(name string) (*L7, error)
Add(ri *L7RuntimeInfo) error
Delete(name string) error
Sync(ri []*L7RuntimeInfo) error
Sync(ri *L7RuntimeInfo) error
GC(names []string) error
Shutdown() error
}
39 changes: 16 additions & 23 deletions pkg/loadbalancers/l7s.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ func NewLoadBalancerPool(cloud LoadBalancers, namer *utils.Namer) LoadBalancerPo
return &L7s{cloud, storage.NewInMemoryPool(), namer}
}

func (l *L7s) create(ri *L7RuntimeInfo) (*L7, error) {
return &L7{
runtimeInfo: ri,
Name: l.namer.LoadBalancer(ri.Name),
cloud: l.cloud,
namer: l.namer,
}, nil
}

// Get returns the loadbalancer by name.
func (l *L7s) Get(name string) (*L7, error) {
name = l.namer.LoadBalancer(name)
Expand All @@ -66,15 +57,20 @@ func (l *L7s) Get(name string) (*L7, error) {
return lb.(*L7), nil
}

// Add gets or creates a loadbalancer.
// If the loadbalancer already exists, it checks that its edges are valid.
func (l *L7s) Add(ri *L7RuntimeInfo) (err error) {
// addLB gets or creates a loadbalancer. If the loadbalancer already exists,
// it checks that its edges are valid.
func (l *L7s) addLB(ri *L7RuntimeInfo) (err error) {
name := l.namer.LoadBalancer(ri.Name)

lb, _ := l.Get(name)
if lb == nil {
glog.V(3).Infof("Creating l7 %v", name)
lb, err = l.create(ri)
lb = &L7{
runtimeInfo: ri,
Name: l.namer.LoadBalancer(ri.Name),
cloud: l.cloud,
namer: l.namer,
}
if err != nil {
return err
}
Expand All @@ -100,7 +96,7 @@ func (l *L7s) Add(ri *L7RuntimeInfo) (err error) {
return nil
}

// Delete deletes a loadbalancer by name.
// Delete deletes a load balancer by name.
func (l *L7s) Delete(name string) error {
name = l.namer.LoadBalancer(name)
lb, err := l.Get(name)
Expand All @@ -115,15 +111,12 @@ func (l *L7s) Delete(name string) error {
return nil
}

// Sync loadbalancers with the given runtime info from the controller.
func (l *L7s) Sync(lbs []*L7RuntimeInfo) error {
glog.V(3).Infof("Syncing loadbalancers %v", lbs)

// create new loadbalancers, validate existing
for _, ri := range lbs {
if err := l.Add(ri); err != nil {
return err
}
// Sync a load balancer with the given runtime info from the controller.
func (l *L7s) Sync(ri *L7RuntimeInfo) error {
glog.V(3).Infof("Syncing load balancer %v", ri)
// Create new load balancer and validate existing
if err := l.addLB(ri); err != nil {
return err
}
return nil
}
Expand Down
Loading