Skip to content

Commit

Permalink
refactor: aligning to kubernetes-sigs/controller-runtime@v0.1.50
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Tranchitella <dario@tranchitella.eu>
  • Loading branch information
prometherion committed May 29, 2023
1 parent 4685180 commit 933a0a5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
14 changes: 10 additions & 4 deletions cmd/adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import (
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
basecmd "sigs.k8s.io/custom-metrics-apiserver/pkg/cmd"
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"

Expand Down Expand Up @@ -111,10 +113,14 @@ func (a *Adapter) makeProvider(ctx context.Context, globalHTTPTimeout time.Durat
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
MetricsBindAddress: metricsBindAddress,
Scheme: scheme,
Namespace: namespace,
LeaseDuration: leaseDuration,
RenewDeadline: renewDeadline,
RetryPeriod: retryPeriod,
NewCache: func(config *rest.Config, opts ctrlcache.Options) (ctrlcache.Cache, error) {
opts.Namespaces = []string{namespace}

return ctrlcache.New(config, opts)
},
LeaseDuration: leaseDuration,
RenewDeadline: renewDeadline,
RetryPeriod: retryPeriod,
})
if err != nil {
logger.Error(err, "failed to setup manager")
Expand Down
28 changes: 17 additions & 11 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import (
"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/client-go/tools/cache"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/config"
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
kedacontrollers "github.com/kedacore/keda/v2/controllers/keda"
Expand Down Expand Up @@ -136,16 +138,22 @@ func main() {
cfg.DisableCompression = disableCompression

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Scheme: scheme,
MetricsBindAddress: metricsAddr,
WebhookServer: webhook.NewServer(webhook.Options{
Port: 9443,
}),
NewCache: func(config *rest.Config, opts ctrlcache.Options) (ctrlcache.Cache, error) {
opts.Namespaces = []string{namespace}

return ctrlcache.New(config, opts)
},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "operator.keda.sh",
LeaseDuration: leaseDuration,
RenewDeadline: renewDeadline,
RetryPeriod: retryPeriod,
Namespace: namespace,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -204,9 +212,8 @@ func main() {
ScaleClient: scaleClient,
ScaleHandler: scaledHandler,
}).SetupWithManager(mgr, controller.Options{
Controller: config.Controller{
MaxConcurrentReconciles: scaledObjectMaxReconciles,
}}); err != nil {
MaxConcurrentReconciles: scaledObjectMaxReconciles,
}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ScaledObject")
os.Exit(1)
}
Expand All @@ -218,9 +225,8 @@ func main() {
SecretsLister: secretInformer.Lister(),
SecretsSynced: secretInformer.Informer().HasSynced,
}).SetupWithManager(mgr, controller.Options{
Controller: config.Controller{
MaxConcurrentReconciles: scaledJobMaxReconciles,
}}); err != nil {
MaxConcurrentReconciles: scaledJobMaxReconciles,
}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ScaledJob")
os.Exit(1)
}
Expand Down
44 changes: 33 additions & 11 deletions cmd/webhooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package main

import (
"crypto/tls"
"flag"
"fmt"
"os"

"github.com/spf13/pflag"
Expand All @@ -29,6 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"

kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/k8s"
Expand Down Expand Up @@ -74,14 +77,37 @@ func main() {
cfg := ctrl.GetConfigOrDie()
cfg.QPS = webhooksClientRequestQPS
cfg.Burst = webhooksClientRequestBurst
// Configuring minimum TLS version for the webhook server
var minTLSVersion uint16

switch tlsMinVersion {
case "1.0":
minTLSVersion = tls.VersionTLS10
case "1.1":
minTLSVersion = tls.VersionTLS11
case "1.2":
minTLSVersion = tls.VersionTLS11
case "1.3":
minTLSVersion = tls.VersionTLS13
default:
setupLog.Error(fmt.Errorf("unsupported minimum TLS version"), fmt.Sprintf("option %s non recognized", tlsMinVersion))
os.Exit(1)
}

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
LeaderElection: false,
MetricsBindAddress: metricsAddr,
Port: 9443,
Scheme: scheme,
LeaderElection: false,
MetricsBindAddress: metricsAddr,
WebhookServer: webhook.NewServer(webhook.Options{
Port: 9443,
CertDir: certDir,
TLSOpts: []func(tlsConfig *tls.Config){
func(tlsConfig *tls.Config) {
tlsConfig.MinVersion = minTLSVersion
},
},
}),
HealthProbeBindAddress: probeAddr,
CertDir: certDir,
})
if err != nil {
setupLog.Error(err, "unable to start admission webhooks")
Expand All @@ -98,7 +124,7 @@ func main() {

kedautil.PrintWelcome(setupLog, kubeVersion, "admission webhooks")

setupWebhook(mgr, tlsMinVersion)
setupWebhook(mgr)

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
Expand All @@ -115,14 +141,10 @@ func main() {
}
}

func setupWebhook(mgr manager.Manager, tlsMinVersion string) {
func setupWebhook(mgr manager.Manager) {
// setup webhooks
if err := (&kedav1alpha1.ScaledObject{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ScaledObject")
os.Exit(1)
}

setupLog.V(1).Info("setting up webhook server")
hookServer := mgr.GetWebhookServer()
hookServer.TLSMinVersion = tlsMinVersion
}

0 comments on commit 933a0a5

Please sign in to comment.