Skip to content

Commit

Permalink
feat: Combine webhooks and controllers into a single binary (kubernet…
Browse files Browse the repository at this point in the history
  • Loading branch information
ellistarn authored Oct 24, 2022
1 parent c2d6e50 commit 5471d36
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 338 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ linters-settings:
locale: US
ignore-words: []
goimports:
local-prefixes: github.com/aws/karpenter
local-prefixes: github.com/aws/karpenter-core
goheader:
template: |-
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 1 addition & 0 deletions hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

8 changes: 4 additions & 4 deletions pkg/cloudprovider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
type Context struct {
context.Context

ClientSet *kubernetes.Clientset
KubeClient client.Client
EventRecorder record.EventRecorder
Clock clock.Clock
KubernetesInterface kubernetes.Interface
KubeClient client.Client
EventRecorder record.EventRecorder
Clock clock.Clock
// StartAsync is a channel that is closed when leader election has been won. This is a signal to start any async
// processing that should only occur while the cloud provider is the leader.
StartAsync <-chan struct{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/consolidation/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var controller *consolidation.Controller
var provisioningController *provisioning.Controller
var provisioner *provisioning.Provisioner
var cloudProvider *fake.CloudProvider
var clientSet *kubernetes.Clientset
var kubernetesInterface kubernetes.Interface
var recorder *test.EventRecorder
var nodeStateController *state.NodeController
var fakeClock *clock.FakeClock
Expand All @@ -80,9 +80,9 @@ var _ = BeforeSuite(func() {
fakeClock = clock.NewFakeClock(time.Now())
cluster = state.NewCluster(ctx, fakeClock, env.Client, cloudProvider)
nodeStateController = state.NewNodeController(env.Client, cluster)
clientSet = kubernetes.NewForConfigOrDie(e.Config)
kubernetesInterface = kubernetes.NewForConfigOrDie(e.Config)
recorder = test.NewEventRecorder()
provisioner = provisioning.NewProvisioner(ctx, env.Client, clientSet.CoreV1(), recorder, cloudProvider, cluster, test.SettingsStore{})
provisioner = provisioning.NewProvisioner(ctx, env.Client, kubernetesInterface.CoreV1(), recorder, cloudProvider, cluster, test.SettingsStore{})
provisioningController = provisioning.NewController(env.Client, provisioner, recorder)
})
Expect(env.Start()).To(Succeed(), "Failed to start environment")
Expand Down
42 changes: 29 additions & 13 deletions pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ limitations under the License.
package controllers

import (
"context"

"k8s.io/client-go/kubernetes"
"k8s.io/utils/clock"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/cloudprovider"
"github.com/aws/karpenter-core/pkg/controllers/consolidation"
"github.com/aws/karpenter-core/pkg/controllers/counter"
Expand All @@ -25,8 +31,8 @@ import (
"github.com/aws/karpenter-core/pkg/controllers/provisioning"
"github.com/aws/karpenter-core/pkg/controllers/state"
"github.com/aws/karpenter-core/pkg/controllers/termination"
"github.com/aws/karpenter-core/pkg/events"
"github.com/aws/karpenter-core/pkg/metrics"
"github.com/aws/karpenter-core/pkg/operator"
"github.com/aws/karpenter-core/pkg/operator/controller"
"github.com/aws/karpenter-core/pkg/operator/settingsstore"
)
Expand All @@ -35,20 +41,30 @@ func init() {
metrics.MustRegister() // Registers cross-controller metrics
}

func GetControllers(ctx operator.Context, cluster *state.Cluster, settingsStore settingsstore.Store, cloudProvider cloudprovider.CloudProvider) []controller.Controller {
provisioner := provisioning.NewProvisioner(ctx, ctx.KubeClient, ctx.Clientset.CoreV1(), ctx.EventRecorder, cloudProvider, cluster, settingsStore)
func NewControllers(
ctx context.Context,
clock clock.Clock,
kubeClient client.Client,
kubernetesInterface kubernetes.Interface,
cluster *state.Cluster,
eventRecorder events.Recorder,
settingsStore settingsstore.Store,
cloudProvider cloudprovider.CloudProvider,
) []controller.Controller {
provisioner := provisioning.NewProvisioner(ctx, kubeClient, kubernetesInterface.CoreV1(), eventRecorder, cloudProvider, cluster, settingsStore)

metricsstate.StartMetricScraper(ctx, cluster)

return []controller.Controller{
provisioning.NewController(ctx.KubeClient, provisioner, ctx.EventRecorder),
state.NewNodeController(ctx.KubeClient, cluster),
state.NewPodController(ctx.KubeClient, cluster),
state.NewProvisionerController(ctx.KubeClient, cluster),
node.NewController(ctx.Clock, ctx.KubeClient, cloudProvider, cluster),
termination.NewController(ctx, ctx.Clock, ctx.KubeClient, ctx.Clientset.CoreV1(), ctx.EventRecorder, cloudProvider),
metricspod.NewController(ctx.KubeClient),
metricsprovisioner.NewController(ctx.KubeClient),
counter.NewController(ctx.KubeClient, cluster),
consolidation.NewController(ctx.Clock, ctx.KubeClient, provisioner, cloudProvider, ctx.EventRecorder, cluster),
provisioning.NewController(kubeClient, provisioner, eventRecorder),
state.NewNodeController(kubeClient, cluster),
state.NewPodController(kubeClient, cluster),
state.NewProvisionerController(kubeClient, cluster),
node.NewController(clock, kubeClient, cloudProvider, cluster),
termination.NewController(ctx, clock, kubeClient, kubernetesInterface.CoreV1(), eventRecorder, cloudProvider),
metricspod.NewController(kubeClient),
metricsprovisioner.NewController(kubeClient),
counter.NewController(kubeClient, cluster),
consolidation.NewController(clock, kubeClient, provisioner, cloudProvider, eventRecorder, cluster),
}
}
96 changes: 0 additions & 96 deletions pkg/operator/context.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/operator/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func TestAPIs(t *testing.T) {

var _ = BeforeEach(func() {
env = test.NewEnvironment(ctx, func(e *test.Environment) {
clientSet := kubernetes.NewForConfigOrDie(e.Config)
cmw = informer.NewInformedWatcher(clientSet, system.Namespace())
ss = settingsstore.WatchSettingsOrDie(e.Ctx, clientSet, cmw, settings.Registration)
kubernetesInterface := kubernetes.NewForConfigOrDie(e.Config)
cmw = informer.NewInformedWatcher(kubernetesInterface, system.Namespace())
ss = settingsstore.WatchSettingsOrDie(e.Ctx, kubernetesInterface, cmw, settings.Registration)

defaultConfigMap = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
17 changes: 0 additions & 17 deletions pkg/operator/injection/injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,12 @@ import (

"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"knative.dev/pkg/configmap/informer"
knativeinjection "knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"

"github.com/aws/karpenter-core/pkg/operator/options"
)

type resourceKey struct{}

// LoggingContextOrDie injects a logger into the returned context. The logger is
// configured by the ConfigMap `config-logging` and live updates the level.
func LoggingContextOrDie(componentName string, config *rest.Config, cmw *informer.InformedWatcher) context.Context {
ctx, startinformers := knativeinjection.EnableInjectionOrDie(signals.NewContext(), config)
logger, atomicLevel := sharedmain.SetupLoggerOrDie(ctx, componentName)
ctx = logging.WithLogger(ctx, logger)
rest.SetDefaultWarningHandler(&logging.WarningHandler{Logger: logger})
sharedmain.WatchLoggingConfigOrDie(ctx, cmw, logger, atomicLevel, componentName)
startinformers()
return ctx
}

func WithNamespacedName(ctx context.Context, namespacedname types.NamespacedName) context.Context {
return context.WithValue(ctx, resourceKey{}, namespacedname)
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/operator/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ limitations under the License.

package operator

import "github.com/go-logr/logr"
import (
"context"

"github.com/go-logr/logr"
"go.uber.org/zap"
"k8s.io/client-go/rest"
"knative.dev/pkg/configmap/informer"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
)

// LoggingContextOrDie injects a logger into the returned context. The logger is
// configured by the ConfigMap `config-logging` and live updates the level.
func NewLogger(ctx context.Context, componentName string, config *rest.Config, cmw *informer.InformedWatcher) *zap.SugaredLogger {
ctx, startinformers := injection.EnableInjectionOrDie(ctx, config)
logger, atomicLevel := sharedmain.SetupLoggerOrDie(ctx, componentName)
rest.SetDefaultWarningHandler(&logging.WarningHandler{Logger: logger})
sharedmain.WatchLoggingConfigOrDie(ctx, cmw, logger, atomicLevel, componentName)
startinformers()
return logger
}

type ignoreDebugEventsSink struct {
name string
Expand Down
Loading

0 comments on commit 5471d36

Please sign in to comment.