You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2025/02/22 04:33:26 UTC
(skywalking-swck) branch master updated: Replace deprecated APIs with recommended ones (#140)
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-swck.git
The following commit(s) were added to refs/heads/master by this push:
new 3222a85 Replace deprecated APIs with recommended ones (#140)
3222a85 is described below
commit 3222a852193e8f210a6c1dfa0e7a59be12a05ead
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Sat Feb 22 12:33:21 2025 +0800
Replace deprecated APIs with recommended ones (#140)
---
.../apis/operator/v1alpha1/banyandb_webhook.go | 64 ++++++++++++++------
.../operator/v1alpha1/eventexporter_webhook.go | 69 ++++++++++++++--------
operator/apis/operator/v1alpha1/fetcher_webhook.go | 54 +++++++++++------
.../apis/operator/v1alpha1/javaagent_webhook.go | 63 +++++++++++++-------
.../apis/operator/v1alpha1/oapserver_webhook.go | 63 +++++++++++++-------
.../operator/v1alpha1/oapserverconfig_webhook.go | 55 +++++++++++------
.../v1alpha1/oapserverdynamicconfig_webhook.go | 58 ++++++++++++------
.../apis/operator/v1alpha1/satellite_webhook.go | 65 +++++++++++++-------
operator/apis/operator/v1alpha1/storage_webhook.go | 67 ++++++++++++++-------
operator/apis/operator/v1alpha1/swagent_webhook.go | 49 ++++++++++-----
operator/apis/operator/v1alpha1/ui_webhook.go | 61 ++++++++++++-------
11 files changed, 454 insertions(+), 214 deletions(-)
diff --git a/operator/apis/operator/v1alpha1/banyandb_webhook.go b/operator/apis/operator/v1alpha1/banyandb_webhook.go
index 81c30c1..5fe78ec 100644
--- a/operator/apis/operator/v1alpha1/banyandb_webhook.go
+++ b/operator/apis/operator/v1alpha1/banyandb_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -32,50 +34,74 @@ var banyandbLog = logf.Log.WithName("banyandb-resource")
func (r *BanyanDB) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
//+kubebuilder:webhook:path=/mutate-operator-skywalking-apache-org-v1alpha1-banyandb,mutating=true,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=banyandbs,verbs=create;update,versions=v1alpha1,name=mbanyandb.kb.io,admissionReviewVersions=v1
-var _ webhook.Defaulter = &BanyanDB{}
+var _ webhook.CustomDefaulter = &BanyanDB{}
-func (r *BanyanDB) Default() {
- banyandbLog.Info("default", "name", r.Name)
+func (r *BanyanDB) Default(_ context.Context, o runtime.Object) error {
+ banyandb, ok := o.(*BanyanDB)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a BanyanDB")
+ }
+
+ banyandbLog.Info("default", "name", banyandb.Name)
- if r.Spec.Version == "" {
+ if banyandb.Spec.Version == "" {
// use the latest version by default
- r.Spec.Version = "latest"
+ banyandb.Spec.Version = "latest"
}
- if r.Spec.Image == "" {
- r.Spec.Image = fmt.Sprintf("apache/skywalking-banyandb:%s", r.Spec.Version)
+ if banyandb.Spec.Image == "" {
+ banyandb.Spec.Image = fmt.Sprintf("apache/skywalking-banyandb:%s", banyandb.Spec.Version)
}
- if r.Spec.Counts == 0 {
+ if banyandb.Spec.Counts == 0 {
// currently only support one data copy
- r.Spec.Counts = 1
+ banyandb.Spec.Counts = 1
}
+
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,verbs=create;update,path=/validate-operator-skywalking-apache-org-v1alpha1-banyandb,mutating=false,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=banyandbs,versions=v1alpha1,name=vbanyandb.kb.io
-var _ webhook.Validator = &BanyanDB{}
+var _ webhook.CustomValidator = &BanyanDB{}
-func (r *BanyanDB) ValidateCreate() (admission.Warnings, error) {
- banyandbLog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+func (r *BanyanDB) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ banyandb, ok := o.(*BanyanDB)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a BanyanDB")
+ }
+
+ banyandbLog.Info("validate create", "name", banyandb.Name)
+ return nil, banyandb.validate()
}
-func (r *BanyanDB) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- banyandbLog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+func (r *BanyanDB) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ banyandb, ok := o.(*BanyanDB)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a BanyanDB")
+ }
+
+ banyandbLog.Info("validate update", "name", banyandb.Name)
+ return nil, banyandb.validate()
}
-func (r *BanyanDB) ValidateDelete() (admission.Warnings, error) {
- banyandbLog.Info("validate delete", "name", r.Name)
- return nil, r.validate()
+func (r *BanyanDB) ValidateDelete(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ banyandb, ok := o.(*BanyanDB)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a BanyanDB")
+ }
+
+ banyandbLog.Info("validate delete", "name", banyandb.Name)
+ return nil, banyandb.validate()
}
func (r *BanyanDB) validate() error {
diff --git a/operator/apis/operator/v1alpha1/eventexporter_webhook.go b/operator/apis/operator/v1alpha1/eventexporter_webhook.go
index eac2e55..8235635 100644
--- a/operator/apis/operator/v1alpha1/eventexporter_webhook.go
+++ b/operator/apis/operator/v1alpha1/eventexporter_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -27,8 +29,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
-const latestVersion = "latest"
-const image = "apache/skywalking-kubernetes-event-exporter"
+const (
+ latestVersion = "latest"
+ image = "apache/skywalking-kubernetes-event-exporter"
+)
// log is for logging in this package.
var eventexporterlog = logf.Log.WithName("eventexporter-resource")
@@ -36,52 +40,71 @@ var eventexporterlog = logf.Log.WithName("eventexporter-resource")
func (r *EventExporter) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
//+kubebuilder:webhook:path=/mutate-operator-skywalking-apache-org-v1alpha1-eventexporter,mutating=true,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=eventexporters,verbs=create;update,versions=v1alpha1,name=meventexporter.kb.io,admissionReviewVersions=v1
-var _ webhook.Defaulter = &EventExporter{}
+var _ webhook.CustomDefaulter = &EventExporter{}
+
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *EventExporter) Default(_ context.Context, o runtime.Object) error {
+ eventexporter, ok := o.(*EventExporter)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a EventExporter")
+ }
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *EventExporter) Default() {
- eventexporterlog.Info("default", "name", r.Name)
+ eventexporterlog.Info("default", "name", eventexporter.Name)
- if r.Spec.Version == "" {
- r.Spec.Version = latestVersion
+ if eventexporter.Spec.Version == "" {
+ eventexporter.Spec.Version = latestVersion
}
- if r.Spec.Image == "" {
- r.Spec.Image = fmt.Sprintf("%s:%s", image, r.Spec.Version)
+ if eventexporter.Spec.Image == "" {
+ eventexporter.Spec.Image = fmt.Sprintf("%s:%s", image, eventexporter.Spec.Version)
}
- if r.Spec.Replicas == 0 {
- r.Spec.Replicas = 1
+ if eventexporter.Spec.Replicas == 0 {
+ eventexporter.Spec.Replicas = 1
}
+
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,path=/mutate-operator-skywalking-apache-org-v1alpha1-eventexporter,mutating=true,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=eventexporters,verbs=create;update,versions=v1alpha1,name=meventexporter.kb.io
-var _ webhook.Validator = &EventExporter{}
+var _ webhook.CustomValidator = &EventExporter{}
+
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *EventExporter) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ eventexporter, ok := o.(*EventExporter)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a EventExporter")
+ }
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *EventExporter) ValidateCreate() (admission.Warnings, error) {
- eventexporterlog.Info("validate create", "name", r.Name)
+ eventexporterlog.Info("validate create", "name", eventexporter.Name)
- return nil, r.validate()
+ return nil, eventexporter.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *EventExporter) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- eventexporterlog.Info("validate update", "name", r.Name)
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *EventExporter) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ eventexporter, ok := o.(*EventExporter)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a EventExporter")
+ }
+
+ eventexporterlog.Info("validate update", "name", eventexporter.Name)
- return nil, r.validate()
+ return nil, eventexporter.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *EventExporter) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *EventExporter) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
eventexporterlog.Info("validate delete", "name", r.Name)
return nil, nil
diff --git a/operator/apis/operator/v1alpha1/fetcher_webhook.go b/operator/apis/operator/v1alpha1/fetcher_webhook.go
index cd02aba..6d561f8 100644
--- a/operator/apis/operator/v1alpha1/fetcher_webhook.go
+++ b/operator/apis/operator/v1alpha1/fetcher_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -33,41 +35,59 @@ var fetcherlog = logf.Log.WithName("fetcher-resource")
func (r *Fetcher) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,path=/mutate-operator-skywalking-apache-org-v1alpha1-fetcher,mutating=true,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=fetchers,verbs=create;update,versions=v1alpha1,name=mfetcher.kb.io
-var _ webhook.Defaulter = &Fetcher{}
+var _ webhook.CustomDefaulter = &Fetcher{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *Fetcher) Default() {
- fetcherlog.Info("default", "name", r.Name)
- if r.Spec.ClusterName == "" {
- r.Spec.ClusterName = r.Name
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *Fetcher) Default(_ context.Context, o runtime.Object) error {
+ fetcher, ok := o.(*Fetcher)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a Fetcher")
+ }
+
+ fetcherlog.Info("default", "name", fetcher.Name)
+ if fetcher.Spec.ClusterName == "" {
+ fetcher.Spec.ClusterName = fetcher.Name
}
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,verbs=create;update,path=/validate-operator-skywalking-apache-org-v1alpha1-fetcher,mutating=false,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=fetchers,versions=v1alpha1,name=vfetcher.kb.io
-var _ webhook.Validator = &Fetcher{}
+var _ webhook.CustomValidator = &Fetcher{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *Fetcher) ValidateCreate() (admission.Warnings, error) {
- fetcherlog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Fetcher) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ fetcher, ok := o.(*Fetcher)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Fetcher")
+ }
+
+ fetcherlog.Info("validate create", "name", fetcher.Name)
+ return nil, fetcher.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *Fetcher) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- fetcherlog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Fetcher) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ fetcher, ok := o.(*Fetcher)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Fetcher")
+ }
+
+ fetcherlog.Info("validate update", "name", fetcher.Name)
+ return nil, fetcher.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *Fetcher) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Fetcher) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
fetcherlog.Info("validate delete", "name", r.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/javaagent_webhook.go b/operator/apis/operator/v1alpha1/javaagent_webhook.go
index 2f98087..8768371 100644
--- a/operator/apis/operator/v1alpha1/javaagent_webhook.go
+++ b/operator/apis/operator/v1alpha1/javaagent_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -39,53 +41,72 @@ const (
func (r *JavaAgent) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,path=/mutate-operator-skywalking-apache-org-v1alpha1-javaagent,mutating=true,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=javaagents,verbs=create;update,versions=v1alpha1,name=mjavaagent.kb.io
-var _ webhook.Defaulter = &JavaAgent{}
+var _ webhook.CustomDefaulter = &JavaAgent{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *JavaAgent) Default() {
- javaagentlog.Info("default", "name", r.Name)
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *JavaAgent) Default(_ context.Context, o runtime.Object) error {
+ javaagent, ok := o.(*JavaAgent)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a JavaAgent")
+ }
+
+ javaagentlog.Info("default", "name", javaagent.Name)
- config := r.Spec.AgentConfiguration
+ config := javaagent.Spec.AgentConfiguration
if config == nil {
- return
+ return nil
}
service := GetServiceName(&config)
backend := GetBackendService(&config)
- if r.Spec.ServiceName == "" && service != "" {
- r.Spec.ServiceName = service
+ if javaagent.Spec.ServiceName == "" && service != "" {
+ javaagent.Spec.ServiceName = service
}
- if r.Spec.BackendService == "" && backend != "" {
- r.Spec.BackendService = backend
+ if javaagent.Spec.BackendService == "" && backend != "" {
+ javaagent.Spec.BackendService = backend
}
+
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,verbs=create;update,path=/validate-operator-skywalking-apache-org-v1alpha1-javaagent,mutating=false,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=javaagents,versions=v1alpha1,name=vjavaagent.kb.io
-var _ webhook.Validator = &JavaAgent{}
+var _ webhook.CustomValidator = &JavaAgent{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *JavaAgent) ValidateCreate() (admission.Warnings, error) {
- javaagentlog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *JavaAgent) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ javaagent, ok := o.(*JavaAgent)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a JavaAgent")
+ }
+
+ javaagentlog.Info("validate create", "name", javaagent.Name)
+ return nil, javaagent.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *JavaAgent) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- javaagentlog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *JavaAgent) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ javaagent, ok := o.(*JavaAgent)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a JavaAgent")
+ }
+
+ javaagentlog.Info("validate update", "name", javaagent.Name)
+ return nil, javaagent.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *JavaAgent) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *JavaAgent) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
javaagentlog.Info("validate delete", "name", r.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/oapserver_webhook.go b/operator/apis/operator/v1alpha1/oapserver_webhook.go
index 7e3cb0a..c90a0e9 100644
--- a/operator/apis/operator/v1alpha1/oapserver_webhook.go
+++ b/operator/apis/operator/v1alpha1/oapserver_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -35,51 +37,70 @@ var oapserverlog = logf.Log.WithName("oapserver-resource")
func (r *OAPServer) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,path=/mutate-operator-skywalking-apache-org-v1alpha1-oapserver,mutating=true,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=oapservers,verbs=create;update,versions=v1alpha1,name=moapserver.kb.io
-var _ webhook.Defaulter = &OAPServer{}
+var _ webhook.CustomDefaulter = &OAPServer{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *OAPServer) Default() {
- oapserverlog.Info("default", "name", r.Name)
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *OAPServer) Default(_ context.Context, o runtime.Object) error {
+ oapserver, ok := o.(*OAPServer)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a OAPServer")
+ }
+
+ oapserverlog.Info("default", "name", oapserver.Name)
- image := r.Spec.Image
+ image := oapserver.Spec.Image
if image == "" {
- r.Spec.Image = fmt.Sprintf("apache/skywalking-oap-server:%s", r.Spec.Version)
+ oapserver.Spec.Image = fmt.Sprintf("apache/skywalking-oap-server:%s", oapserver.Spec.Version)
}
- for _, envVar := range r.Spec.Config {
+ for _, envVar := range oapserver.Spec.Config {
if envVar.Name == "SW_ENVOY_METRIC_ALS_HTTP_ANALYSIS" &&
- r.ObjectMeta.Annotations[annotationKeyIstioSetup] == "" {
- r.Annotations[annotationKeyIstioSetup] = fmt.Sprintf("istioctl install --set profile=demo "+
+ oapserver.ObjectMeta.Annotations[annotationKeyIstioSetup] == "" {
+ oapserver.Annotations[annotationKeyIstioSetup] = fmt.Sprintf("istioctl install --set profile=demo "+
"--set meshConfig.defaultConfig.envoyAccessLogService.address=%s.%s:11800 "+
- "--set meshConfig.enableEnvoyAccessLogService=true", r.Name, r.Namespace)
+ "--set meshConfig.enableEnvoyAccessLogService=true", oapserver.Name, oapserver.Namespace)
}
}
+
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,verbs=create;update,path=/validate-operator-skywalking-apache-org-v1alpha1-oapserver,mutating=false,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=oapservers,versions=v1alpha1,name=voapserver.kb.io
-var _ webhook.Validator = &OAPServer{}
+var _ webhook.CustomValidator = &OAPServer{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServer) ValidateCreate() (admission.Warnings, error) {
- oapserverlog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServer) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ oapserver, ok := o.(*OAPServer)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a OAPServer")
+ }
+
+ oapserverlog.Info("validate create", "name", oapserver.Name)
+ return nil, oapserver.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServer) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- oapserverlog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServer) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ oapserver, ok := o.(*OAPServer)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a OAPServer")
+ }
+
+ oapserverlog.Info("validate update", "name", oapserver.Name)
+ return nil, oapserver.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServer) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServer) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
oapserverlog.Info("validate delete", "name", r.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/oapserverconfig_webhook.go b/operator/apis/operator/v1alpha1/oapserverconfig_webhook.go
index ee76e94..090d582 100644
--- a/operator/apis/operator/v1alpha1/oapserverconfig_webhook.go
+++ b/operator/apis/operator/v1alpha1/oapserverconfig_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -33,43 +35,62 @@ var oapserverconfiglog = logf.Log.WithName("oapserverconfig-resource")
func (r *OAPServerConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
//+kubebuilder:webhook:path=/mutate-operator-skywalking-apache-org-v1alpha1-oapserverconfig,mutating=true,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=oapserverconfigs,verbs=create;update,versions=v1alpha1,name=moapserverconfig.kb.io,admissionReviewVersions=v1
-var _ webhook.Defaulter = &OAPServerConfig{}
+var _ webhook.CustomDefaulter = &OAPServerConfig{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *OAPServerConfig) Default() {
- oapserverconfiglog.Info("default", "name", r.Name)
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *OAPServerConfig) Default(_ context.Context, o runtime.Object) error {
+ oapserverconfig, ok := o.(*OAPServerConfig)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a OAPServerConfig")
+ }
+
+ oapserverconfiglog.Info("default", "name", oapserverconfig.Name)
// Default version is "9.5.0"
- if r.Spec.Version == "" {
- r.Spec.Version = "9.5.0"
+ if oapserverconfig.Spec.Version == "" {
+ oapserverconfig.Spec.Version = "9.5.0"
}
+
+ return nil
}
// nolint: lll
//+kubebuilder:webhook:path=/validate-operator-skywalking-apache-org-v1alpha1-oapserverconfig,mutating=false,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=oapserverconfigs,verbs=create;update,versions=v1alpha1,name=voapserverconfig.kb.io,admissionReviewVersions=v1
-var _ webhook.Validator = &OAPServerConfig{}
+var _ webhook.CustomValidator = &OAPServerConfig{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServerConfig) ValidateCreate() (admission.Warnings, error) {
- oapserverconfiglog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServerConfig) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ oapserverconfig, ok := o.(*OAPServerConfig)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a OAPServerConfig")
+ }
+
+ oapserverconfiglog.Info("validate create", "name", oapserverconfig.Name)
+ return nil, oapserverconfig.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServerConfig) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- oapserverconfiglog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServerConfig) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ oapserverconfig, ok := o.(*OAPServerConfig)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a OAPServerConfig")
+ }
+
+ oapserverconfiglog.Info("validate update", "name", oapserverconfig.Name)
+ return nil, oapserverconfig.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServerConfig) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServerConfig) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
oapserverconfiglog.Info("validate delete", "name", r.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/oapserverdynamicconfig_webhook.go b/operator/apis/operator/v1alpha1/oapserverdynamicconfig_webhook.go
index 994f0f2..de16ae4 100644
--- a/operator/apis/operator/v1alpha1/oapserverdynamicconfig_webhook.go
+++ b/operator/apis/operator/v1alpha1/oapserverdynamicconfig_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -33,48 +35,66 @@ var oapserverdynamicconfiglog = logf.Log.WithName("oapserverdynamicconfig-resour
func (r *OAPServerDynamicConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
//+kubebuilder:webhook:path=/mutate-operator-skywalking-apache-org-v1alpha1-oapserverdynamicconfig,mutating=true,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=oapserverdynamicconfigs,verbs=create;update,versions=v1alpha1,name=moapserverdynamicconfig.kb.io,admissionReviewVersions=v1
-var _ webhook.Defaulter = &OAPServerDynamicConfig{}
+var _ webhook.CustomDefaulter = &OAPServerDynamicConfig{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *OAPServerDynamicConfig) Default() {
- oapserverdynamicconfiglog.Info("default", "name", r.Name)
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *OAPServerDynamicConfig) Default(_ context.Context, o runtime.Object) error {
+ oapserverdynamicconfig, ok := o.(*OAPServerDynamicConfig)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a OAPServerDynamicConfig")
+ }
+
+ oapserverdynamicconfiglog.Info("default", "name", oapserverdynamicconfig.Name)
// Default version is "9.5.0"
- if r.Spec.Version == "" {
- r.Spec.Version = "9.5.0"
+ if oapserverdynamicconfig.Spec.Version == "" {
+ oapserverdynamicconfig.Spec.Version = "9.5.0"
}
// Default labelselector is "app=collector,release=skywalking"
- if r.Spec.LabelSelector == "" {
- r.Spec.LabelSelector = "app=collector,release=skywalking"
+ if oapserverdynamicconfig.Spec.LabelSelector == "" {
+ oapserverdynamicconfig.Spec.LabelSelector = "app=collector,release=skywalking"
}
+ return nil
}
// nolint: lll
//+kubebuilder:webhook:path=/validate-operator-skywalking-apache-org-v1alpha1-oapserverdynamicconfig,mutating=false,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=oapserverdynamicconfigs,verbs=create;update,versions=v1alpha1,name=voapserverdynamicconfig.kb.io,admissionReviewVersions=v1
-var _ webhook.Validator = &OAPServerDynamicConfig{}
+var _ webhook.CustomValidator = &OAPServerDynamicConfig{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServerDynamicConfig) ValidateCreate() (admission.Warnings, error) {
- oapserverdynamicconfiglog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServerDynamicConfig) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ oapserverdynamicconfig, ok := o.(*OAPServerDynamicConfig)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a OAPServerDynamicConfig")
+ }
+
+ oapserverdynamicconfiglog.Info("validate create", "name", oapserverdynamicconfig.Name)
+ return nil, oapserverdynamicconfig.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServerDynamicConfig) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- oapserverdynamicconfiglog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServerDynamicConfig) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ oapserverdynamicconfig, ok := o.(*OAPServerDynamicConfig)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a OAPServerDynamicConfig")
+ }
+
+ oapserverdynamicconfiglog.Info("validate update", "name", oapserverdynamicconfig.Name)
+ return nil, oapserverdynamicconfig.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *OAPServerDynamicConfig) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *OAPServerDynamicConfig) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
oapserverdynamicconfiglog.Info("validate delete", "name", r.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/satellite_webhook.go b/operator/apis/operator/v1alpha1/satellite_webhook.go
index 55517ec..d009ff8 100644
--- a/operator/apis/operator/v1alpha1/satellite_webhook.go
+++ b/operator/apis/operator/v1alpha1/satellite_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -33,54 +35,73 @@ var satellitelog = logf.Log.WithName("satellite-resource")
func (r *Satellite) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
//nolint: lll
//+kubebuilder:webhook:path=/mutate-operator-skywalking-apache-org-v1alpha1-satellite,mutating=true,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=satellites,verbs=create;update,versions=v1alpha1,name=msatellite.kb.io,admissionReviewVersions=v1
-var _ webhook.Defaulter = &Satellite{}
+var _ webhook.CustomDefaulter = &Satellite{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *Satellite) Default() {
- satellitelog.Info("default", "name", r.Name)
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *Satellite) Default(_ context.Context, o runtime.Object) error {
+ satellite, ok := o.(*Satellite)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a Satellite")
+ }
+
+ satellitelog.Info("default", "name", satellite.Name)
- image := r.Spec.Image
+ image := satellite.Spec.Image
if image == "" {
- r.Spec.Image = fmt.Sprintf("apache/skywalking-satellite:%s", r.Spec.Version)
+ satellite.Spec.Image = fmt.Sprintf("apache/skywalking-satellite:%s", satellite.Spec.Version)
}
- oapServerName := r.Spec.OAPServerName
+ oapServerName := satellite.Spec.OAPServerName
if oapServerName == "" {
- r.Spec.OAPServerName = r.Name
+ satellite.Spec.OAPServerName = satellite.Name
}
- if r.ObjectMeta.Annotations[annotationKeyIstioSetup] == "" {
- r.Annotations[annotationKeyIstioSetup] = fmt.Sprintf("istioctl install --set profile=demo "+
+ if satellite.ObjectMeta.Annotations[annotationKeyIstioSetup] == "" {
+ satellite.Annotations[annotationKeyIstioSetup] = fmt.Sprintf("istioctl install --set profile=demo "+
"--set meshConfig.defaultConfig.envoyAccessLogService.address=%s.%s:11800 "+
- "--set meshConfig.enableEnvoyAccessLogService=true", r.Name, r.Namespace)
+ "--set meshConfig.enableEnvoyAccessLogService=true", satellite.Name, satellite.Namespace)
}
+
+ return nil
}
//nolint: lll
//+kubebuilder:webhook:path=/validate-operator-skywalking-apache-org-v1alpha1-satellite,mutating=false,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=satellites,verbs=create;update,versions=v1alpha1,name=vsatellite.kb.io,admissionReviewVersions=v1
-var _ webhook.Validator = &Satellite{}
+var _ webhook.CustomValidator = &Satellite{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *Satellite) ValidateCreate() (admission.Warnings, error) {
- satellitelog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Satellite) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ satellite, ok := o.(*Satellite)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Satellite")
+ }
+
+ satellitelog.Info("validate create", "name", satellite.Name)
+ return nil, satellite.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *Satellite) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- satellitelog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Satellite) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ satellite, ok := o.(*Satellite)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Satellite")
+ }
+
+ satellitelog.Info("validate update", "name", satellite.Name)
+ return nil, satellite.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *Satellite) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Satellite) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
satellitelog.Info("validate delete", "name", r.Name)
return nil, r.validate()
}
diff --git a/operator/apis/operator/v1alpha1/storage_webhook.go b/operator/apis/operator/v1alpha1/storage_webhook.go
index 94ca31b..816fbe8 100644
--- a/operator/apis/operator/v1alpha1/storage_webhook.go
+++ b/operator/apis/operator/v1alpha1/storage_webhook.go
@@ -18,6 +18,8 @@
package v1alpha1
import (
+ "context"
+
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -34,47 +36,70 @@ var storagelog = logf.Log.WithName("storage-resource")
func (r *Storage) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,path=/mutate-operator-skywalking-apache-org-v1alpha1-storage,mutating=true,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=storages,verbs=create;update,versions=v1alpha1,name=mstorage.kb.io
-var _ webhook.Defaulter = &Storage{}
+var _ webhook.CustomDefaulter = &Storage{}
+
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *Storage) Default(_ context.Context, o runtime.Object) error {
+ storage, ok := o.(*Storage)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a Storage")
+ }
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *Storage) Default() {
- storagelog.Info("default", "name", r.Name)
- if r.Spec.ConnectType == "internal" {
- if r.Spec.Image == "" {
- r.Spec.Image = "docker.elastic.co/elasticsearch/elasticsearch:7.5.1"
+ storagelog.Info("default", "name", storage.Name)
+ if storage.Spec.ConnectType == "internal" {
+ if storage.Spec.Image == "" {
+ storage.Spec.Image = "docker.elastic.co/elasticsearch/elasticsearch:7.5.1"
}
- if r.Spec.Instances == 0 {
- r.Spec.Instances = 3
+ if storage.Spec.Instances == 0 {
+ storage.Spec.Instances = 3
}
}
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,verbs=create;update,path=/validate-operator-skywalking-apache-org-v1alpha1-storage,mutating=false,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=storages,versions=v1alpha1,name=vstorage.kb.io
-var _ webhook.Validator = &Storage{}
+var _ webhook.CustomValidator = &Storage{}
+
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Storage) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ storage, ok := o.(*Storage)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Storage")
+ }
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *Storage) ValidateCreate() (admission.Warnings, error) {
- storagelog.Info("validate create", "name", r.Name)
- return nil, r.valid()
+ storagelog.Info("validate create", "name", storage.Name)
+ return nil, storage.valid()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *Storage) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- storagelog.Info("validate update", "name", r.Name)
- return nil, r.valid()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Storage) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ storage, ok := o.(*Storage)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Storage")
+ }
+
+ storagelog.Info("validate update", "name", storage.Name)
+ return nil, storage.valid()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *Storage) ValidateDelete() (admission.Warnings, error) {
- storagelog.Info("validate delete", "name", r.Name)
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *Storage) ValidateDelete(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ storage, ok := o.(*Storage)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a Storage")
+ }
+
+ storagelog.Info("validate delete", "name", storage.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/swagent_webhook.go b/operator/apis/operator/v1alpha1/swagent_webhook.go
index aac6bf7..4e06703 100644
--- a/operator/apis/operator/v1alpha1/swagent_webhook.go
+++ b/operator/apis/operator/v1alpha1/swagent_webhook.go
@@ -18,6 +18,9 @@
package v1alpha1
import (
+ "context"
+
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -31,40 +34,58 @@ var swagentlog = logf.Log.WithName("swagent-resource")
func (r *SwAgent) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
//+kubebuilder:webhook:path=/mutate-operator-skywalking-apache-org-v1alpha1-swagent,mutating=true,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=swagents,verbs=create;update,versions=v1alpha1,name=mswagent.kb.io,admissionReviewVersions=v1
-var _ webhook.Defaulter = &SwAgent{}
+var _ webhook.CustomDefaulter = &SwAgent{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *SwAgent) Default() {
- swagentlog.Info("default", "name", r.Name)
- r.setDefault()
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *SwAgent) Default(_ context.Context, o runtime.Object) error {
+ swagent, ok := o.(*SwAgent)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a SwAgent")
+ }
+
+ swagentlog.Info("default", "name", swagent.Name)
+ swagent.setDefault()
+ return nil
}
// nolint: lll
//+kubebuilder:webhook:path=/validate-operator-skywalking-apache-org-v1alpha1-swagent,mutating=false,failurePolicy=fail,sideEffects=None,groups=operator.skywalking.apache.org,resources=swagents,verbs=create;update,versions=v1alpha1,name=vswagent.kb.io,admissionReviewVersions=v1
-var _ webhook.Validator = &SwAgent{}
+var _ webhook.CustomValidator = &SwAgent{}
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *SwAgent) ValidateCreate() (admission.Warnings, error) {
- swagentlog.Info("validate create", "name", r.Name)
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *SwAgent) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ swagent, ok := o.(*SwAgent)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a SwAgent")
+ }
+
+ swagentlog.Info("validate create", "name", swagent.Name)
r.setDefault()
return nil, nil
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *SwAgent) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- swagentlog.Info("validate update", "name", r.Name)
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *SwAgent) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ swagent, ok := o.(*SwAgent)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a SwAgent")
+ }
+
+ swagentlog.Info("validate update", "name", swagent.Name)
return nil, nil
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *SwAgent) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *SwAgent) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
swagentlog.Info("validate delete", "name", r.Name)
return nil, nil
}
diff --git a/operator/apis/operator/v1alpha1/ui_webhook.go b/operator/apis/operator/v1alpha1/ui_webhook.go
index 959318f..4c38656 100644
--- a/operator/apis/operator/v1alpha1/ui_webhook.go
+++ b/operator/apis/operator/v1alpha1/ui_webhook.go
@@ -18,8 +18,10 @@
package v1alpha1
import (
+ "context"
"fmt"
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -33,47 +35,66 @@ var uilog = logf.Log.WithName("ui-resource")
func (r *UI) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
+ WithDefaulter(r).
+ WithValidator(r).
Complete()
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,path=/mutate-operator-skywalking-apache-org-v1alpha1-ui,mutating=true,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=uis,verbs=create;update,versions=v1alpha1,name=mui.kb.io
-var _ webhook.Defaulter = &UI{}
+var _ webhook.CustomDefaulter = &UI{}
-// Default implements webhook.Defaulter so a webhook will be registered for the type
-func (r *UI) Default() {
- uilog.Info("default", "name", r.Name)
+// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
+func (r *UI) Default(_ context.Context, o runtime.Object) error {
+ ui, ok := o.(*UI)
+ if !ok {
+ return apierrors.NewBadRequest("object is not a UI")
+ }
- if r.Spec.Image == "" {
- r.Spec.Image = fmt.Sprintf("apache/skywalking-ui:%s", r.Spec.Version)
+ uilog.Info("default", "name", ui.Name)
+
+ if ui.Spec.Image == "" {
+ ui.Spec.Image = fmt.Sprintf("apache/skywalking-ui:%s", ui.Spec.Version)
}
- r.Spec.Service.Template.Default()
- if r.Spec.OAPServerAddress == "" {
- r.Spec.OAPServerAddress = fmt.Sprintf("http://%s-oap.%s:12800", r.Name, r.Namespace)
+ ui.Spec.Service.Template.Default()
+ if ui.Spec.OAPServerAddress == "" {
+ ui.Spec.OAPServerAddress = fmt.Sprintf("http://%s-oap.%s:12800", ui.Name, ui.Namespace)
}
+
+ return nil
}
// nolint: lll
// +kubebuilder:webhook:admissionReviewVersions=v1,sideEffects=None,verbs=create;update,path=/validate-operator-skywalking-apache-org-v1alpha1-ui,mutating=false,failurePolicy=fail,groups=operator.skywalking.apache.org,resources=uis,versions=v1alpha1,name=vui.kb.io
-var _ webhook.Validator = &UI{}
+var _ webhook.CustomValidator = &UI{}
+
+// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *UI) ValidateCreate(_ context.Context, o runtime.Object) (admission.Warnings, error) {
+ ui, ok := o.(*UI)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a UI")
+ }
-// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
-func (r *UI) ValidateCreate() (admission.Warnings, error) {
- uilog.Info("validate create", "name", r.Name)
- return nil, r.validate()
+ uilog.Info("validate create", "name", ui.Name)
+ return nil, ui.validate()
}
-// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
-func (r *UI) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
- uilog.Info("validate update", "name", r.Name)
- return nil, r.validate()
+// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *UI) ValidateUpdate(_ context.Context, o runtime.Object, _ runtime.Object) (admission.Warnings, error) {
+ ui, ok := o.(*UI)
+ if !ok {
+ return nil, apierrors.NewBadRequest("object is not a UI")
+ }
+
+ uilog.Info("validate update", "name", ui.Name)
+ return nil, ui.validate()
}
-// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
-func (r *UI) ValidateDelete() (admission.Warnings, error) {
+// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
+func (r *UI) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
uilog.Info("validate delete", "name", r.Name)
return nil, nil
}