Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Add XGBoostJob v1 controller and APIs (#82)
Browse files Browse the repository at this point in the history
Signed-off-by: terrytangyuan <terrytangyuan@gmail.com>
  • Loading branch information
terrytangyuan authored May 18, 2020
1 parent 448614c commit 9fbc5ee
Show file tree
Hide file tree
Showing 31 changed files with 1,836 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"os"

"github.com/kubeflow/xgboost-operator/pkg/apis"
"github.com/kubeflow/xgboost-operator/pkg/controller"
controller "github.com/kubeflow/xgboost-operator/pkg/controller/v1"
"github.com/kubeflow/xgboost-operator/pkg/webhook"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ limitations under the License.
package apis

import (
"github.com/kubeflow/xgboost-operator/pkg/apis/xgboostjob/v1"
"github.com/kubeflow/xgboost-operator/pkg/apis/xgboostjob/v1alpha1"
)

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme, v1.SchemeBuilder.AddToScheme)
}
35 changes: 35 additions & 0 deletions pkg/apis/xgboostjob/v1/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
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 v1

const (
// GroupName is the group name use in this package.
GroupName = "kubeflow.org"
// Kind is the kind name.
Kind = "XGBoostJob"
// GroupVersion is the version.
GroupVersion = "v1"
// Plural is the Plural for XGBoostJob.
Plural = "xgboostjobs"
// Singular is the singular for XGBoostJob.
Singular = "xgboostjob"
// XGBOOSTCRD is the CRD name for XGBoostJob.
XGBoostCRD = "xgboostjobs.kubeflow.org"

DefaultContainerName = "xgboostjob"
DefaultContainerPortName = "xgboostjob-port"
DefaultPort = 9999
)
22 changes: 22 additions & 0 deletions pkg/apis/xgboostjob/v1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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 v1 contains API Schema definitions for the xgboostjob v1 API group
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=github.com/kubeflow/xgboost-operator/pkg/apis/xgboostjob
// +k8s:defaulter-gen=TypeMeta
// +groupName=xgboostjob.kubeflow.org
package v1
48 changes: 48 additions & 0 deletions pkg/apis/xgboostjob/v1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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.
*/

// NOTE: Boilerplate only. Ignore this file.

// Package v1 contains API Schema definitions for the xgboostjob v1 API group
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=github.com/kubeflow/xgboost-operator/pkg/apis/xgboostjob
// +k8s:defaulter-gen=TypeMeta
// +groupName=xgboostjob.kubeflow.org
package v1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/runtime/scheme"
)

var (
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: "xgboostjob.kubeflow.org", Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}

// SchemeGroupVersionKind is the GroupVersionKind of the resource.
SchemeGroupVersionKind = SchemeGroupVersion.WithKind(Kind)

// AddToScheme is required by pkg/client/...
AddToScheme = SchemeBuilder.AddToScheme
)

// Resource is required by pkg/client/listers/...
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}
54 changes: 54 additions & 0 deletions pkg/apis/xgboostjob/v1/v1_suite_test.go.back
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*

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 v1

import (
"log"
"os"
"path/filepath"
"testing"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

var cfg *rest.Config
var c client.Client

func TestMain(m *testing.M) {
t := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "config", "crds")},
}

err := SchemeBuilder.AddToScheme(scheme.Scheme)
if err != nil {
log.Fatal(err)
}

if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}

if c, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}); err != nil {
log.Fatal(err)
}

code := m.Run()
t.Stop()
os.Exit(code)
}
77 changes: 77 additions & 0 deletions pkg/apis/xgboostjob/v1/xgboostjob_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
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 v1

import (
common "github.com/kubeflow/common/job_controller/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// XGBoostJobSpec defines the desired state of XGBoostJob
type XGBoostJobSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
RunPolicy common.RunPolicy `json:",inline"`

XGBReplicaSpecs map[common.ReplicaType]*common.ReplicaSpec `json:"xgbReplicaSpecs"`
}

// XGBoostJobStatus defines the observed state of XGBoostJob
type XGBoostJobStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
common.JobStatus `json:",inline"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// XGBoostJob is the Schema for the xgboostjobs API
// +k8s:openapi-gen=true
type XGBoostJob struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec XGBoostJobSpec `json:"spec,omitempty"`
Status XGBoostJobStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// XGBoostJobList contains a list of XGBoostJob
type XGBoostJobList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []XGBoostJob `json:"items"`
}

// XGBoostJobReplicaType is the type for XGBoostJobReplica.
type XGBoostJobReplicaType common.ReplicaType

const (
// XGBoostReplicaTypeMaster is the type of Master of distributed PyTorch
XGBoostReplicaTypeMaster XGBoostJobReplicaType = "Master"

// XGBoostReplicaTypeWorker is the type for workers of distributed PyTorch.
XGBoostReplicaTypeWorker XGBoostJobReplicaType = "Worker"
)

func init() {
SchemeBuilder.Register(&XGBoostJob{}, &XGBoostJobList{})
}
57 changes: 57 additions & 0 deletions pkg/apis/xgboostjob/v1/xgboostjob_types_test.go.back
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*

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 v1

import (
"testing"

"github.com/onsi/gomega"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestStorageXGBoostJob(t *testing.T) {
key := types.NamespacedName{
Name: "foo",
Namespace: "default",
}
created := &XGBoostJob{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
}}
g := gomega.NewGomegaWithT(t)

// Test Create
fetched := &XGBoostJob{}
g.Expect(c.Create(context.TODO(), created)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(created))

// Test Updating the Labels
updated := fetched.DeepCopy()
updated.Labels = map[string]string{"hello": "world"}
g.Expect(c.Update(context.TODO(), updated)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(updated))

// Test Delete
g.Expect(c.Delete(context.TODO(), fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(context.TODO(), key, fetched)).To(gomega.HaveOccurred())
}
Loading

0 comments on commit 9fbc5ee

Please sign in to comment.