Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement webhook warnings for the MXJob #2058

Merged
Merged
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
21 changes: 21 additions & 0 deletions manifests/base/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-kubeflow-org-v1-mxjob
failurePolicy: Fail
name: validator.mxjob.training-operator.kubeflow.org
rules:
- apiGroups:
- kubeflow.org
apiVersions:
- v1
operations:
- CREATE
- UPDATE
- DELETE
resources:
- mxjobs
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
Expand Down
3 changes: 3 additions & 0 deletions manifests/base/webhook/patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
- op: replace
path: /webhooks/3/clientConfig/service/name
value: training-operator
- op: replace
path: /webhooks/4/clientConfig/service/name
value: training-operator
- op: replace
path: /metadata/name
value: validator.training-operator.kubeflow.org
24 changes: 24 additions & 0 deletions pkg/controller.v1/mxnet/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ package mxnet

import (
"context"
"crypto/tls"
"fmt"
"net"
"path/filepath"
"testing"
"time"

kubeflowv1 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/controller.v1/common"
mxnetwebhook "github.com/kubeflow/training-operator/pkg/webhooks/mxnet"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -31,6 +36,7 @@ import (
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"
"volcano.sh/apis/pkg/apis/scheduling/v1beta1"
//+kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -60,6 +66,9 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "manifests", "base", "crds")},
ErrorIfCRDPathMissing: true,
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "..", "manifests", "base", "webhook", "manifests.yaml")},
},
}

cfg, err := testEnv.Start()
Expand All @@ -81,19 +90,34 @@ var _ = BeforeSuite(func() {
Metrics: metricsserver.Options{
BindAddress: "0",
},
WebhookServer: webhook.NewServer(
webhook.Options{
Host: testEnv.WebhookInstallOptions.LocalServingHost,
Port: testEnv.WebhookInstallOptions.LocalServingPort,
CertDir: testEnv.WebhookInstallOptions.LocalServingCertDir,
}),
})
Expect(err).NotTo(HaveOccurred())

gangSchedulingSetupFunc := common.GenNonGangSchedulerSetupFunc()
r := NewReconciler(mgr, gangSchedulingSetupFunc)

Expect(r.SetupWithManager(mgr, 1)).NotTo(HaveOccurred())
Expect(mxnetwebhook.SetupWebhook(mgr)).NotTo(HaveOccurred())

go func() {
defer GinkgoRecover()
err = mgr.Start(testCtx)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()

dialer := &net.Dialer{Timeout: time.Second}
addrPort := fmt.Sprintf("%s:%d", testEnv.WebhookInstallOptions.LocalServingHost, testEnv.WebhookInstallOptions.LocalServingPort)
Eventually(func(g Gomega) {
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(conn.Close()).NotTo(HaveOccurred())
}).Should(Succeed())
})

var _ = AfterSuite(func() {
Expand Down
71 changes: 71 additions & 0 deletions pkg/webhooks/mxnet/mxnet_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2024 The Kubeflow Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.
*/

package mxnet

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
)

type Webhook struct{}

func SetupWebhook(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(&trainingoperator.MXJob{}).
WithValidator(&Webhook{}).
Complete()
}

// +kubebuilder:webhook:path=/validate-kubeflow-org-v1-mxjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=mxjobs,verbs=create;update;delete,versions=v1,name=validator.mxjob.training-operator.kubeflow.org,admissionReviewVersions=v1

var _ webhook.CustomValidator = &Webhook{}

func (w *Webhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
job := obj.(*trainingoperator.MXJob)
log := ctrl.LoggerFrom(ctx).WithName("mxjob-webhook")
log.V(5).Info("Validating create", "mxJob", klog.KObj(job))
return deprecatedWarning(), nil
}

func (w *Webhook) ValidateUpdate(ctx context.Context, _, newObj runtime.Object) (admission.Warnings, error) {
job := newObj.(*trainingoperator.MXJob)
log := ctrl.LoggerFrom(ctx).WithName("mxjob-webhook")
log.V(5).Info("Validating update", "mxJob", klog.KObj(job))
return deprecatedWarning(), nil
}

func (w *Webhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
job := obj.(*trainingoperator.MXJob)
log := ctrl.LoggerFrom(ctx).WithName("mxjob-webhook")
log.V(5).Info("Validating delete", "mxJob", klog.KObj(job))
return deprecatedWarning(), nil
}

func deprecatedWarning() admission.Warnings {
return admission.Warnings{
fmt.Sprintf("MXJob is deprecated, and the MXJob will be removed in the release v1.9.0.\n" +
"Please see https://github.com/kubeflow/training-operator/issues/1996 for more details"),
}
}
3 changes: 2 additions & 1 deletion pkg/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/webhooks/mxnet"
"github.com/kubeflow/training-operator/pkg/webhooks/paddlepaddle"
"github.com/kubeflow/training-operator/pkg/webhooks/pytorch"
"github.com/kubeflow/training-operator/pkg/webhooks/tensorflow"
Expand All @@ -32,7 +33,7 @@ var (
SupportedSchemeWebhook = map[string]WebhookSetupFunc{
trainingoperator.PyTorchJobKind: pytorch.SetupWebhook,
trainingoperator.TFJobKind: tensorflow.SetupWebhook,
trainingoperator.MXJobKind: scaffold,
trainingoperator.MXJobKind: mxnet.SetupWebhook,
trainingoperator.XGBoostJobKind: xgboost.SetupWebhook,
trainingoperator.MPIJobKind: scaffold,
trainingoperator.PaddleJobKind: paddlepaddle.SetupWebhook,
Expand Down
Loading