Skip to content

Commit

Permalink
Upgrade to controller-runtime v0.17.2
Browse files Browse the repository at this point in the history
  • Loading branch information
roivaz committed Mar 21, 2024
1 parent b52c1c5 commit ffd50a5
Show file tree
Hide file tree
Showing 26 changed files with 588 additions and 512 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ KIND ?= $(LOCALBIN)/kind
## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.11.3
GINKGO_VERSION ?= v2.9.1
GINKGO_VERSION ?= v2.14.0
CRD_REFDOCS_VERSION ?= v0.0.8
KIND_VERSION ?= v0.16.0

Expand Down
15 changes: 8 additions & 7 deletions apis/marin3r/v1alpha1/envoyconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -43,25 +44,25 @@ func (r *EnvoyConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &EnvoyConfig{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *EnvoyConfig) ValidateCreate() error {
func (r *EnvoyConfig) ValidateCreate() (admission.Warnings, error) {
validationlog.Info("ValidateCreate", "type", "EnvoyConfig", "resource", util.ObjectKey(r).String())
if err := r.Validate(); err != nil {
return err
return nil, err
}
return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *EnvoyConfig) ValidateUpdate(old runtime.Object) error {
func (r *EnvoyConfig) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
validationlog.Info("validateUpdate", "type", "EnvoyConfig", "resource", util.ObjectKey(r).String())
if err := r.Validate(); err != nil {
return err
return nil, err
}
return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *EnvoyConfig) ValidateDelete() error { return nil }
func (r *EnvoyConfig) ValidateDelete() (admission.Warnings, error) { return nil, nil }

// Validates the EnvoyConfig resource
func (r *EnvoyConfig) Validate() error {
Expand Down
11 changes: 6 additions & 5 deletions apis/operator.marin3r/v1alpha1/envoydeployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -38,21 +39,21 @@ func (r *EnvoyDeployment) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Validator = &EnvoyDeployment{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *EnvoyDeployment) ValidateCreate() error {
func (r *EnvoyDeployment) ValidateCreate() (admission.Warnings, error) {
envoydeploymentlog.V(1).Info("validate create", "name", r.Name)

return r.Validate()
return nil, r.Validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *EnvoyDeployment) ValidateUpdate(old runtime.Object) error {
func (r *EnvoyDeployment) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
envoydeploymentlog.V(1).Info("validate update", "name", r.Name)

return r.Validate()
return nil, r.Validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *EnvoyDeployment) ValidateDelete() error { return nil }
func (r *EnvoyDeployment) ValidateDelete() (admission.Warnings, error) { return nil, nil }

// Validate checks that the spec of the EnvoyDeployment resource is correct
func (r *EnvoyDeployment) Validate() error {
Expand Down
18 changes: 12 additions & 6 deletions apis/operator.marin3r/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -98,12 +100,16 @@ var _ = BeforeSuite(func() {
// start webhook server using Manager
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
CertDir: webhookInstallOptions.LocalServingCertDir,
LeaderElection: false,
MetricsBindAddress: "0",
Scheme: scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Host: webhookInstallOptions.LocalServingHost,
Port: webhookInstallOptions.LocalServingPort,
CertDir: webhookInstallOptions.LocalServingCertDir,
}),
LeaderElection: false,
Metrics: metricsserver.Options{
BindAddress: "0",
},
})
Expect(err).NotTo(HaveOccurred())

Expand Down
14 changes: 11 additions & 3 deletions cmd/discoveryservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ import (
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

const (
Expand Down Expand Up @@ -96,12 +98,18 @@ func runDiscoveryService(cmd *cobra.Command, args []string) {
ctx := signals.SetupSignalHandler()

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: dsScheme,
MetricsBindAddress: metricsAddr,
Scheme: dsScheme,
Metrics: metricsserver.Options{
BindAddress: "0",
},
HealthProbeBindAddress: probeAddr,
LeaderElectionID: "2cfbe7d6.marin3r.3scale.net",
LeaderElectionResourceLock: "leases",
Namespace: os.Getenv("WATCH_NAMESPACE"),
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
os.Getenv("WATCH_NAMESPACE"): {},
},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
24 changes: 15 additions & 9 deletions cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

marin3rv1alpha1 "github.com/3scale-ops/marin3r/apis/marin3r/v1alpha1"
operatorv1alpha1 "github.com/3scale-ops/marin3r/apis/operator.marin3r/v1alpha1"
Expand Down Expand Up @@ -80,24 +81,29 @@ func runOperator(cmd *cobra.Command, args []string) {
}

options := ctrl.Options{
Scheme: operatorScheme,
MetricsBindAddress: metricsAddr,
Scheme: operatorScheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
HealthProbeBindAddress: probeAddr,
LeaderElection: leaderElect,
LeaderElectionID: "2cfbe7d6.operator.marin3r.3scale.net",
LeaderElectionResourceLock: "leases",
Namespace: watchNamespace, // namespaced-scope when the value is not an empty string
}

if strings.Contains(watchNamespace, ",") {
setupLog.Info(fmt.Sprintf("manager in MultiNamespaced mode will be watching namespaces %q", watchNamespace))
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ","))
} else if watchNamespace == "" {
setupLog.Info("manager in Cluster scope mode will be watching all namespaces")
options.Namespace = watchNamespace
} else {
options.Cache = cache.Options{DefaultNamespaces: map[string]cache.Config{}}
for _, ns := range strings.Split(watchNamespace, ",") {
options.Cache.DefaultNamespaces[ns] = cache.Config{}
}
} else if watchNamespace != "" {
setupLog.Info(fmt.Sprintf("manager in Namespaced mode will be watching namespace %q", watchNamespace))
options.Namespace = watchNamespace
options.Cache = cache.Options{DefaultNamespaces: map[string]cache.Config{
watchNamespace: {},
}}
} else {
setupLog.Info("manager in Cluster scope mode will be watching all namespaces")
}

mgr, err := ctrl.NewManager(cfg, options)
Expand Down
47 changes: 30 additions & 17 deletions cmd/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

marin3rv1alpha1 "github.com/3scale-ops/marin3r/apis/marin3r/v1alpha1"
operatorv1alpha1 "github.com/3scale-ops/marin3r/apis/operator.marin3r/v1alpha1"
Expand Down Expand Up @@ -90,21 +92,33 @@ func runWebhook(cmd *cobra.Command, args []string) {

options := ctrl.Options{
Scheme: webhookScheme,
MetricsBindAddress: "0",
HealthProbeBindAddress: probeAddr,
Port: webhookPort,
LeaderElection: false,
WebhookServer: webhook.NewServer(webhook.Options{
// Setup the webhook
Port: webhookPort,
CertDir: webhookTLSCertDir,
CertName: webhookTLSCertName,
KeyName: webhookTLSKeyName,
}),
Metrics: metricsserver.Options{
BindAddress: "0",
},
}

if strings.Contains(watchNamespace, ",") {
setupLog.Info(fmt.Sprintf("webhook in MultiNamespaced mode will be watching namespaces %q", watchNamespace))
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ","))
} else if watchNamespace == "" {
setupLog.Info("webhook in Cluster scope mode will be watching all namespaces")
options.Namespace = watchNamespace
setupLog.Info(fmt.Sprintf("manager in MultiNamespaced mode will be watching namespaces %q", watchNamespace))
options.Cache = cache.Options{DefaultNamespaces: map[string]cache.Config{}}
for _, ns := range strings.Split(watchNamespace, ",") {
options.Cache.DefaultNamespaces[ns] = cache.Config{}
}
} else if watchNamespace != "" {
setupLog.Info(fmt.Sprintf("manager in Namespaced mode will be watching namespace %q", watchNamespace))
options.Cache = cache.Options{DefaultNamespaces: map[string]cache.Config{
watchNamespace: {},
}}
} else {
setupLog.Info(fmt.Sprintf("webhook in Namespaced mode will be watching namespace %q", watchNamespace))
options.Namespace = watchNamespace
setupLog.Info("manager in Cluster scope mode will be watching all namespaces")
}

mgr, err := ctrl.NewManager(cfg, options)
Expand All @@ -113,16 +127,15 @@ func runWebhook(cmd *cobra.Command, args []string) {
os.Exit(1)
}

// Setup the webhook
hookServer := mgr.GetWebhookServer()
hookServer.CertDir = webhookTLSCertDir
hookServer.KeyName = webhookTLSKeyName
hookServer.CertName = webhookTLSCertName
hookServer.Port = webhookPort

// Register the Pod mutating webhook
hookServer := mgr.GetWebhookServer()
ctrl.Log.Info("registering the pod mutating webhook with webhook server")
hookServer.Register(podv1mutator.MutatePath, &webhook.Admission{Handler: &podv1mutator.PodMutator{Client: mgr.GetClient()}})
hookServer.Register(podv1mutator.MutatePath, &webhook.Admission{
Handler: &podv1mutator.PodMutator{
Client: mgr.GetClient(),
Decoder: admission.NewDecoder(mgr.GetScheme()),
},
})

// Register the EnvoyConfig v1alpha1 webhooks
if err = (&marin3rv1alpha1.EnvoyConfig{}).SetupWebhookWithManager(mgr); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ spec:
description: "Claims lists the names of resources, defined in
spec.resourceClaims, that are used by this container. \n This
is an alpha field and requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable."
feature gate. \n This field is immutable. It can only be set
for containers."
items:
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
properties:
Expand Down Expand Up @@ -137,7 +138,8 @@ spec:
description: 'Requests describes the minimum amount of compute
resources required. If Requests is omitted for a container,
it defaults to Limits if that is explicitly specified, otherwise
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
to an implementation-defined value. Requests cannot exceed Limits.
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
serviceConfig:
Expand Down
Loading

0 comments on commit ffd50a5

Please sign in to comment.