Skip to content

Commit

Permalink
KEP-2170: Initial Implementations for v2 Manager
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Iwai <[email protected]>
  • Loading branch information
tenzen-y committed Sep 6, 2024
1 parent 6ddeb2b commit 56cb9cd
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/controller.v2/clustertrainingruntime_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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 controllerv2

import (
"context"

"github.com/go-logr/logr"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

kubeflowv2 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v2alpha1"
)

type ClusterTrainingRuntimeReconciler struct {
log logr.Logger
client client.Client
recorder record.EventRecorder
}

func NewClusterTrainingRuntimeReconciler(client client.Client, recorder record.EventRecorder) *ClusterTrainingRuntimeReconciler {
return &ClusterTrainingRuntimeReconciler{
log: ctrl.Log.WithName("clustertrainingruntime-controller"),
client: client,
recorder: recorder,
}
}

func (r *ClusterTrainingRuntimeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var clRuntime kubeflowv2.ClusterTrainingRuntime
if err := r.client.Get(ctx, req.NamespacedName, &clRuntime); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log := ctrl.LoggerFrom(ctx).WithValues("clusterTrainingRuntime", klog.KObj(&clRuntime))
ctrl.LoggerInto(ctx, log)
log.V(2).Info("Reconciling ClusterTrainingRuntime")
return ctrl.Result{}, nil
}

func (r *ClusterTrainingRuntimeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&kubeflowv2.ClusterTrainingRuntime{}).
Complete(r)
}
29 changes: 29 additions & 0 deletions pkg/controller.v2/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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 controllerv2

import ctrl "sigs.k8s.io/controller-runtime"

func SetupControllers(mgr ctrl.Manager) (string, error) {
if err := NewTrainJobReconciler(
mgr.GetClient(),
mgr.GetEventRecorderFor("training-operator-trainjob-controller"),
).SetupWithManager(mgr); err != nil {
return "TrainJob", err
}
return "", nil
}
59 changes: 59 additions & 0 deletions pkg/controller.v2/traininigruntime_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 controllerv2

import (
"context"

"github.com/go-logr/logr"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

kubeflowv2 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v2alpha1"
)

type TrainingRuntimeReconciler struct {
log logr.Logger
client client.Client
recorder record.EventRecorder
}

func NewTrainingRuntimeReconciler(client client.Client, recorder record.EventRecorder) *TrainingRuntimeReconciler {
return &TrainingRuntimeReconciler{
log: ctrl.Log.WithName("trainingruntime-controller"),
client: client,
recorder: recorder,
}
}

func (r *TrainingRuntimeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var runtime kubeflowv2.TrainingRuntime
if err := r.client.Get(ctx, req.NamespacedName, &runtime); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log := ctrl.LoggerFrom(ctx).WithValues("trainingRuntime", runtime)
ctrl.LoggerInto(ctx, log)
log.V(2).Info("Reconciling TrainingRuntime")
return ctrl.Result{}, nil
}

func (r *TrainingRuntimeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&kubeflowv2.TrainingRuntime{}).
Complete(r)
}
72 changes: 72 additions & 0 deletions test/controller.v2/clustertrainingruntime_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
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 controllerv2

import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

kubeflowv2 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v2alpha1"
"github.com/kubeflow/training-operator/test/framework"
)

var _ = ginkgo.Describe("ClusterTrainingRuntime controller", ginkgo.Ordered, func() {
var ns *corev1.Namespace

ginkgo.BeforeAll(func() {
fwk = &framework.Framework{}
cfg = fwk.Init()
ctx, k8sClient = fwk.RunManager(cfg)
})
ginkgo.AfterAll(func() {
fwk.Teardown()
})

ginkgo.BeforeEach(func() {
ns = &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "clustertrainingruntime-",
},
}
gomega.Expect(k8sClient.Create(ctx, ns)).To(gomega.Succeed())
})

ginkgo.When("Reconciling ClusterTrainingRuntime", func() {
ginkgo.AfterEach(func() {
gomega.Expect(k8sClient.DeleteAllOf(ctx, &kubeflowv2.ClusterTrainingRuntime{})).Should(gomega.Succeed())
})

ginkgo.It("Should succeed to create ClusterTrainingRuntime", func() {
clRuntime := &kubeflowv2.ClusterTrainingRuntime{
TypeMeta: metav1.TypeMeta{
APIVersion: kubeflowv2.SchemeGroupVersion.String(),
Kind: "ClusterTrainingRuntime",
},
ObjectMeta: metav1.ObjectMeta{
Name: "alpha",
},
}
gomega.Expect(k8sClient.Create(ctx, clRuntime)).Should(gomega.Succeed())
})
})
})
42 changes: 42 additions & 0 deletions test/controller.v2/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
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 controllerv2

import (
"context"
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubeflow/training-operator/test/framework"
)

var (
cfg *rest.Config
k8sClient client.Client
ctx context.Context
fwk *framework.Framework
)

func TestAPIs(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)

ginkgo.RunSpecs(t, "v2 Controllers Suite")
}
74 changes: 74 additions & 0 deletions test/controller.v2/trainingruntime_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
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 controllerv2

import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

kubeflowv2 "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v2alpha1"
"github.com/kubeflow/training-operator/test/framework"
)

var _ = ginkgo.Describe("TrainingRuntime controller", ginkgo.Ordered, func() {
var ns *corev1.Namespace

ginkgo.BeforeAll(func() {
fwk = &framework.Framework{}
cfg = fwk.Init()
ctx, k8sClient = fwk.RunManager(cfg)
})
ginkgo.AfterAll(func() {
fwk.Teardown()
})

ginkgo.BeforeEach(func() {
ns = &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "trainingruntime-",
},
}
gomega.Expect(k8sClient.Create(ctx, ns)).To(gomega.Succeed())
})

ginkgo.When("Reconciling TrainingRuntime", func() {
ginkgo.AfterEach(func() {
gomega.Expect(k8sClient.DeleteAllOf(ctx, &kubeflowv2.TrainingRuntime{}, client.InNamespace(ns.Name)))
})

ginkgo.It("Should succeed to create TrainingRuntime", func() {
runtime := &kubeflowv2.TrainingRuntime{
TypeMeta: metav1.TypeMeta{
APIVersion: kubeflowv2.SchemeGroupVersion.String(),
Kind: "TrainingRuntime",
},
ObjectMeta: metav1.ObjectMeta{
Name: "alpha",
Namespace: ns.Name,
},
}
gomega.Expect(k8sClient.Create(ctx, runtime)).Should(gomega.Succeed())
})
})
})
Loading

0 comments on commit 56cb9cd

Please sign in to comment.