Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Build the manager binary
FROM m.daocloud.io/docker.io/golang:1.16 AS builder
FROM registry.erda.cloud/erda-x/golang:1.24 AS builder

ARG APP
WORKDIR /workspace
ENV APP=${APP}
ENV GOPROXY=https://goproxy.cn,direct

COPY . /workspace
# Build
Expand Down Expand Up @@ -34,4 +32,4 @@ RUN if [ "$APP" = "probe-agent" ] ; then \

COPY --from=builder /workspace/${APP} .

CMD [ "sh", "-c", "/${APP}"]
CMD [ "sh", "-c", "/${APP}"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.16.4)

KUSTOMIZE = $(shell pwd)/bin/kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5@v5.5.0)

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
Expand Down
56 changes: 41 additions & 15 deletions apis/v1/cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
package v1

import (
"context"

"k8s.io/apimachinery/pkg/runtime"
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"
)

var clusterlog = logf.Log.WithName("cluster-resource")
Expand All @@ -30,33 +33,56 @@ func (c *Cluster) SetupWebhookWithManager(mgr ctrl.Manager) error {

//+kubebuilder:webhook:path=/mutate-kubeprober-erda-cloud-v1-cluster,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubeprober.erda.cloud,resources=clusters,verbs=create;update,versions=v1,name=cluster.probe.kubeprober.erda.cloud,admissionReviewVersions={v1,v1beta1}

var _ webhook.Defaulter = &Cluster{}
var _ webhook.CustomDefaulter = &Cluster{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (c *Cluster) Default() {
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
func (c *Cluster) Default(_ context.Context, obj runtime.Object) error {
cluster, ok := obj.(*Cluster)
if ok {
clusterlog.Info("default", "name", cluster.Name)
} else {
clusterlog.Info("default", "unexpectedObject", obj)
}
// TODO(user): fill in your defaulting logic.
return nil
}

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-kubeprober-erda-cloud-v1-cluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubeprober.erda.cloud,resources=clusters,versions=v1,name=cluster.probe.kubeprober.erda.cloud,admissionReviewVersions={v1,v1beta1}

var _ webhook.Validator = &Cluster{}
var _ webhook.CustomValidator = &Cluster{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (c *Cluster) ValidateCreate() error {
clusterlog.Info("validate create", "name", c.Name)
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (c *Cluster) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
cluster, ok := obj.(*Cluster)
if ok {
clusterlog.Info("validate create", "name", cluster.Name)
} else {
clusterlog.Info("validate create", "unexpectedObject", obj)
}
// TODO(user): fill in your validation logic upon object creation.
return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (c *Cluster) ValidateUpdate(old runtime.Object) error {
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (c *Cluster) ValidateUpdate(_ context.Context, _ runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
cluster, ok := newObj.(*Cluster)
if ok {
clusterlog.Info("validate update", "name", cluster.Name)
} else {
clusterlog.Info("validate update", "unexpectedObject", newObj)
}
// TODO(user): fill in your validation logic upon object update.
return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (c *Cluster) ValidateDelete() error {
clusterlog.Info("validate delete", "name", c.Name)
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (c *Cluster) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
cluster, ok := obj.(*Cluster)
if ok {
clusterlog.Info("validate delete", "name", cluster.Name)
} else {
clusterlog.Info("validate delete", "unexpectedObject", obj)
}
// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
66 changes: 44 additions & 22 deletions apis/v1/probe_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ 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"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -69,55 +70,76 @@ func (p *Probe) SetupWebhookWithManager(mgr ctrl.Manager) error {

//+kubebuilder:webhook:path=/mutate-kubeprober-erda-cloud-v1-probe,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubeprober.erda.cloud,resources=probes,verbs=create;update,versions=v1,name=probe.kubeprober.erda.cloud,admissionReviewVersions={v1beta1,v1}

var _ webhook.Defaulter = &Probe{}
var _ webhook.CustomDefaulter = &Probe{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (p *Probe) Default() {
probelog.Info("default", "name", p.Name)
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
func (p *Probe) Default(_ context.Context, obj runtime.Object) error {
probe, ok := obj.(*Probe)
if ok {
probelog.Info("default", "name", probe.Name)
} else {
probelog.Info("default", "unexpectedObject", obj)
}

// TODO(user): fill in your defaulting logic.
return nil
}

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-kubeprober-erda-cloud-v1-probe,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubeprober.erda.cloud,resources=probes,versions=v1,name=probe.kubeprober.erda.cloud,admissionReviewVersions={v1beta1,v1}

var _ webhook.Validator = &Probe{}
var _ webhook.CustomValidator = &Probe{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (p *Probe) ValidateCreate() error {
probelog.Info("validate create", "name", p.Name)
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
func (p *Probe) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
probe, ok := obj.(*Probe)
if ok {
probelog.Info("validate create", "name", probe.Name)
} else {
probelog.Info("validate create", "unexpectedObject", obj)
}
// TODO(user): fill in your validation logic upon object creation.
return nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (p *Probe) ValidateUpdate(old runtime.Object) error {
probelog.Info("validate update", "name", p.Name)
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
func (p *Probe) ValidateUpdate(_ context.Context, _ runtime.Object, newObj runtime.Object) (admission.Warnings, error) {
probe, ok := newObj.(*Probe)
if ok {
probelog.Info("validate update", "name", probe.Name)
} else {
probelog.Info("validate update", "unexpectedObject", newObj)
}
// TODO(user): fill in your validation logic upon object update.
return nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (p *Probe) ValidateDelete() error {
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
func (p *Probe) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
var err error
var attachedCluster []string
probelog.Info("validate delete", "name", p.Name)
probe, ok := obj.(*Probe)
if ok {
probelog.Info("validate delete", "name", probe.Name)
} else {
probelog.Info("validate delete", "unexpectedObject", obj)
}
clusters := &ClusterList{}
if err = clusterRestClient.List(context.Background(), clusters); err != nil {
return nil
if err = clusterRestClient.List(ctx, clusters); err != nil {
return nil, nil
}

for i := range clusters.Items {
cluster := clusters.Items[i]
for k, v := range cluster.GetLabels() {
if v == "true" && strings.Split(k, "/")[0] == "probe" && strings.Split(k, "/")[1] == p.Name {
if v == "true" && strings.Split(k, "/")[0] == "probe" && strings.Split(k, "/")[1] == probe.Name {
attachedCluster = append(attachedCluster, cluster.Name)
}
}
}

if len(attachedCluster) > 0 {
errstr := fmt.Sprintf("There are cluster %s attached this probe, you need detached cluster first", attachedCluster)
return errors.New(errstr)
return nil, errors.New(errstr)
}
return nil
return nil, nil
}
2 changes: 1 addition & 1 deletion cli/probe/app/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down
4 changes: 2 additions & 2 deletions cli/probe/app/tunnelclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog"
"k8s.io/klog/v2"

"sigs.k8s.io/controller-runtime/pkg/client"
)

//Generate k8sclient of cluster
// Generate k8sclient of cluster
func GenerateProbeClient(cluster *kubeproberv1.Cluster) (client.Client, error) {
var clusterToken []byte
var err error
Expand Down
2 changes: 1 addition & 1 deletion cli/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/erda-project/kubeprober/cli/probe/app"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
"k8s.io/klog/v2"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cli/probe/tunnel-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/klog"
"k8s.io/klog/v2"

kubeproberv1 "github.com/erda-project/kubeprober/apis/v1"
"github.com/erda-project/kubeprober/cli/probe/tunnel-client/clusterdialer"
Expand Down
23 changes: 14 additions & 9 deletions cmd/probe-agent/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap/zapcore"
batchv1beta1 "k8s.io/api/batch/v1beta1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -36,9 +36,12 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"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"

kubeproberv1 "github.com/erda-project/kubeprober/apis/v1"
"github.com/erda-project/kubeprober/cmd/probe-agent/options"
Expand Down Expand Up @@ -158,26 +161,28 @@ func startOperator(ctx context.Context) error {

// listwatch pod for failed probe pod, listwatch cronjob for reconcile
// TODO: add list label selector in related controller & merge them here
newCacheFunc := cache.BuilderWithOptions(cache.Options{
SelectorsByObject: cache.SelectorsByObject{
cacheOptions := cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Pod{}: {
Label: labels.SelectorFromSet(labels.Set{kubeproberv1.LabelKeyApp: kubeproberv1.LabelValueApp}),
},
&batchv1beta1.CronJob{}: {
&batchv1.CronJob{}: {
Label: labels.SelectorFromSet(labels.Set{kubeproberv1.LabelKeyApp: kubeproberv1.LabelValueApp}),
},
},
})
}
if ns := opts.GetNamespace(); ns != "" {
cacheOptions.DefaultNamespaces = map[string]cache.Config{ns: {}}
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: opts.MetricsAddr,
Port: 9443,
Metrics: metricsserver.Options{BindAddress: opts.MetricsAddr},
HealthProbeBindAddress: opts.HealthProbeAddr,
LeaderElection: opts.EnableLeaderElection,
LeaderElectionID: "probe-agent",
Namespace: opts.GetNamespace(),
NewCache: newCacheFunc,
Cache: cacheOptions,
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
2 changes: 1 addition & 1 deletion cmd/probe-agent/probe-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/erda-project/kubeprober/cmd/probe-agent/app"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
"k8s.io/klog/v2"
)

func main() {
Expand Down
8 changes: 5 additions & 3 deletions cmd/probe-master/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

kubeproberv1 "github.com/erda-project/kubeprober/apis/v1"
"github.com/erda-project/kubeprober/apistructs"
Expand Down Expand Up @@ -101,11 +103,11 @@ func Run(opts *options.ProbeMasterOptions) {

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: opts.MetricsAddr,
Port: 9443,
Metrics: metricsserver.Options{BindAddress: opts.MetricsAddr},
HealthProbeBindAddress: opts.HealthProbeAddr,
LeaderElection: opts.EnableLeaderElection,
LeaderElectionID: "probe-master",
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
//CertDir: "config/cert/", //used to develop in local
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/probe-master/probe-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/erda-project/kubeprober/cmd/probe-master/app"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
"k8s.io/klog/v2"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/probe-tunnel/probe-tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
"k8s.io/klog/v2"

"github.com/erda-project/kubeprober/cmd/probe-tunnel/app"
)
Expand Down
Loading
Loading