Skip to content

Commit

Permalink
inject-connect command tests pass locally
Browse files Browse the repository at this point in the history
* they needed the auth plugin import: _ "k8s.io/client-go/plugin/pkg/client/auth"
* kubernetes/client-go#242
  • Loading branch information
ndhanushkodi committed Mar 17, 2021
1 parent ab10724 commit b93dcb7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
1 change: 1 addition & 0 deletions connect-inject/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type EndpointsController struct {
DenyK8sNamespacesSet mapset.Set
Log logr.Logger
Scheme *runtime.Scheme
Context context.Context
}

// TODO: get consul installation namespace and release name passed in for querying agents (for more efficient lookup of agent pods)
Expand Down
87 changes: 47 additions & 40 deletions subcommand/inject-connect/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -443,52 +444,58 @@ func (c *Command) Run(args []string) int {
}
}()

// Start the cleanup controller that cleans up Consul service instances
// still registered after the pod has been deleted (usually due to a force delete).
// Create a channel for all controllers' exits.
ctrlExitCh := make(chan error)

// Create a manager for endpoints controller and the mutating webhook.
// Note: the webhook refactor PR will use this manager for the mutating webhook.
zapLogger := zap.New(zap.UseDevMode(true), zap.Level(zapcore.InfoLevel))
ctrl.SetLogger(zapLogger)
klog.SetLogger(zapLogger)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
LeaderElection: false,
Logger: zapLogger,
MetricsBindAddress: "0.0.0.0:9444",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
return 1
}

// Start the endpoints controller
{
zapLogger := zap.New(zap.UseDevMode(true), zap.Level(zapcore.InfoLevel))
ctrl.SetLogger(zapLogger)
klog.SetLogger(zapLogger)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
LeaderElection: false,
Logger: zapLogger,
MetricsBindAddress: "0.0.0.0:9444",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
return 1
}
if err = (&connectinject.EndpointsController{
Client: mgr.GetClient(),
ConsulClient: c.consulClient,
ConsulScheme: consulURL.Scheme,
ConsulPort: consulURL.Port(),
AllowK8sNamespacesSet: allowK8sNamespaces,
DenyK8sNamespacesSet: denyK8sNamespaces,
Log: ctrl.Log.WithName("controller").WithName("endpoints-controller"),
Scheme: mgr.GetScheme(),
Context: ctx,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", connectinject.EndpointsController{})
return 1
}

if err = (&connectinject.EndpointsController{
ConsulClient: c.consulClient,
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controller").WithName("endpoints-controller"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", connectinject.EndpointsController{})
return 1
// todo: Add tests in case it's not refactored to not have any signal handling
// (In the future, we plan to only have the manager and rely on it to do signal handling for us).
go func() {
// Pass existing context's done channel so that the controller
// will stop when this context is canceled.
// This could be due to an interrupt signal or if any other component did not start
// successfully. In those cases, we want to make sure that this controller is no longer
// running.
if err := mgr.Start(ctx.Done()); err != nil {
setupLog.Error(err, "problem running manager")
// Use an existing channel for ctrl exists in case manager failed to start properly.
ctrlExitCh <- fmt.Errorf("endpoints controller exited unexpectedly")
}
}()

// todo: Add tests in case it's not refactored to not have any signal handling
// (In the future, we plan to only have the manager and rely on it to do signal handling for us).
go func() {
// Pass existing context's done channel so that the controller
// will stop when this context is canceled.
// This could be due to an interrupt signal or if any other component did not start
// successfully. In those cases, we want to make sure that this controller is no longer
// running.
if err := mgr.Start(ctx.Done()); err != nil {
setupLog.Error(err, "problem running manager")
// Use an existing channel for ctrl exists in case manager failed to start properly.
ctrlExitCh <- fmt.Errorf("endpoints controller exited unexpectedly")
}
}()
}

// Start the cleanup controller that cleans up Consul service instances
// still registered after the pod has been deleted (usually due to a force delete).
if c.flagEnableCleanupController {
cleanupResource := connectinject.CleanupResource{
Log: logger.Named("cleanupResource"),
Expand Down

0 comments on commit b93dcb7

Please sign in to comment.