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

Move cloud to ControllerContext #316

Merged
merged 1 commit into from
Jun 6, 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
13 changes: 7 additions & 6 deletions cmd/glbc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ func main() {

cloud := app.NewGCEClient()
enableNEG := cloud.AlphaFeatureGate.Enabled(gce.AlphaFeatureNetworkEndpointGroup)
ctx := context.NewControllerContext(kubeClient, backendConfigClient, flags.F.WatchNamespace, flags.F.ResyncPeriod, enableNEG, flags.F.EnableBackendConfig)
ctx := context.NewControllerContext(kubeClient, backendConfigClient, cloud, flags.F.WatchNamespace, flags.F.ResyncPeriod, enableNEG, flags.F.EnableBackendConfig)
go app.RunHTTPServer(ctx.HealthCheck)

if !flags.F.LeaderElection.LeaderElect {
runControllers(ctx, cloud)
runControllers(ctx)
return
}

electionConfig, err := makeLeaderElectionConfig(kubeClient, ctx.Recorder(flags.F.LeaderElection.LockObjectNamespace), func() {
runControllers(ctx, cloud)
runControllers(ctx)
})
if err != nil {
glog.Fatalf("%v", err)
Expand Down Expand Up @@ -158,14 +158,14 @@ func makeLeaderElectionConfig(client clientset.Interface, recorder record.EventR
}, nil
}

func runControllers(ctx *context.ControllerContext, cloud *gce.GCECloud) {
func runControllers(ctx *context.ControllerContext) {
namer, err := app.NewNamer(ctx.KubeClient, flags.F.ClusterName, controller.DefaultFirewallName)
if err != nil {
glog.Fatalf("app.NewNamer(ctx.KubeClient, %q, %q) = %v", flags.F.ClusterName, controller.DefaultFirewallName, err)
}

defaultBackendServicePortID := app.DefaultBackendServicePortID(ctx.KubeClient)
clusterManager, err := controller.NewClusterManager(cloud, namer, defaultBackendServicePortID, flags.F.HealthCheckPath, flags.F.DefaultSvcHealthCheckPath)
clusterManager, err := controller.NewClusterManager(ctx.Cloud, namer, defaultBackendServicePortID, flags.F.HealthCheckPath, flags.F.DefaultSvcHealthCheckPath)
if err != nil {
glog.Fatalf("controller.NewClusterManager(cloud, namer, %+v, %q, %q) = %v", defaultBackendServicePortID, flags.F.HealthCheckPath, flags.F.DefaultSvcHealthCheckPath, err)
}
Expand All @@ -183,7 +183,8 @@ func runControllers(ctx *context.ControllerContext, cloud *gce.GCECloud) {
glog.V(0).Infof("clusterManager initialized")

if ctx.NEGEnabled {
negController, _ := neg.NewController(ctx.KubeClient, cloud, ctx, lbc.Translator, namer, flags.F.ResyncPeriod)
// TODO: Refactor NEG to use cloud mocks so ctx.Cloud can be referenced within NewController.
negController, _ := neg.NewController(ctx.Cloud, ctx, lbc.Translator, namer, flags.F.ResyncPeriod)
go negController.Run(stopCh)
glog.V(0).Infof("negController started")
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ import (
"k8s.io/client-go/tools/record"
backendconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned"
informerbackendconfig "k8s.io/ingress-gce/pkg/backendconfig/client/informers/externalversions/backendconfig/v1beta1"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
)

// ControllerContext holds the state needed for the execution of the controller.
type ControllerContext struct {
KubeClient kubernetes.Interface

Cloud *gce.GCECloud

IngressInformer cache.SharedIndexInformer
ServiceInformer cache.SharedIndexInformer
BackendConfigInformer cache.SharedIndexInformer
Expand All @@ -56,6 +59,7 @@ type ControllerContext struct {
func NewControllerContext(
kubeClient kubernetes.Interface,
backendConfigClient backendconfigclient.Interface,
cloud *gce.GCECloud,
namespace string,
resyncPeriod time.Duration,
enableNEG bool,
Expand All @@ -66,6 +70,7 @@ func NewControllerContext(
}
context := &ControllerContext{
KubeClient: kubeClient,
Cloud: cloud,
IngressInformer: informerv1beta1.NewIngressInformer(kubeClient, namespace, resyncPeriod, newIndexer()),
ServiceInformer: informerv1.NewServiceInformer(kubeClient, namespace, resyncPeriod, newIndexer()),
PodInformer: informerv1.NewPodInformer(kubeClient, namespace, resyncPeriod, newIndexer()),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newLoadBalancerController(t *testing.T, cm *fakeClusterManager) *LoadBalanc
backendConfigClient := backendconfigclient.NewSimpleClientset()

stopCh := make(chan struct{})
ctx := context.NewControllerContext(kubeClient, backendConfigClient, api_v1.NamespaceAll, 1*time.Minute, true, false)
ctx := context.NewControllerContext(kubeClient, backendConfigClient, cm.fakeBackends, api_v1.NamespaceAll, 1*time.Minute, true, false)
lbc, err := NewLoadBalancerController(ctx, cm.ClusterManager, stopCh)
if err != nil {
t.Fatalf("%v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func fakeTranslator(negEnabled bool) *Translator {
backendConfigClient := backendconfigclient.NewSimpleClientset()

namer := utils.NewNamer("uid1", "")
ctx := context.NewControllerContext(client, backendConfigClient, apiv1.NamespaceAll, 1*time.Second, negEnabled, false)
ctx := context.NewControllerContext(client, backendConfigClient, nil, apiv1.NamespaceAll, 1*time.Second, negEnabled, false)
gce := &Translator{
namer: namer,
ctx: ctx,
Expand Down
4 changes: 1 addition & 3 deletions pkg/neg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
unversionedcore "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -62,7 +61,6 @@ type Controller struct {

// NewController returns a network endpoint group controller.
func NewController(
kubeClient kubernetes.Interface,
cloud networkEndpointGroupCloud,
ctx *context.ControllerContext,
zoneGetter zoneGetter,
Expand All @@ -74,7 +72,7 @@ func NewController(
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{
Interface: kubeClient.Core().Events(""),
Interface: ctx.KubeClient.Core().Events(""),
})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme,
apiv1.EventSource{Component: "neg-controller"})
Expand Down
4 changes: 2 additions & 2 deletions pkg/neg/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (

func newTestController(kubeClient kubernetes.Interface) *Controller {
backendConfigClient := backendconfigclient.NewSimpleClientset()
context := context.NewControllerContext(kubeClient, backendConfigClient, apiv1.NamespaceAll, 1*time.Second, true, false)
controller, _ := NewController(kubeClient,
context := context.NewControllerContext(kubeClient, backendConfigClient, nil, apiv1.NamespaceAll, 1*time.Second, true, false)
controller, _ := NewController(
NewFakeNetworkEndpointGroupCloud("test-subnetwork", "test-network"),
context,
NewFakeZoneGetter(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (

func NewTestSyncerManager(kubeClient kubernetes.Interface) *syncerManager {
backendConfigClient := backendconfigclient.NewSimpleClientset()
context := context.NewControllerContext(kubeClient, backendConfigClient, apiv1.NamespaceAll, 1*time.Second, true, false)
context := context.NewControllerContext(kubeClient, backendConfigClient, nil, apiv1.NamespaceAll, 1*time.Second, true, false)
manager := newSyncerManager(
utils.NewNamer(CluseterID, ""),
record.NewFakeRecorder(100),
Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
func NewTestSyncer() *syncer {
kubeClient := fake.NewSimpleClientset()
backendConfigClient := backendconfigclient.NewSimpleClientset()
context := context.NewControllerContext(kubeClient, backendConfigClient, apiv1.NamespaceAll, 1*time.Second, true, false)
context := context.NewControllerContext(kubeClient, backendConfigClient, nil, apiv1.NamespaceAll, 1*time.Second, true, false)
svcPort := servicePort{
namespace: testServiceNamespace,
name: testServiceName,
Expand Down