Skip to content

Commit

Permalink
WIP (#345)
Browse files Browse the repository at this point in the history
Close #281

Separate CRD and controller
Add CRD support in helm charts

Signed-off-by: Ce Gao <ce.gao@outlook.com>
  • Loading branch information
gaocegege authored and jlewi committed Jan 29, 2018
1 parent 8caa0c9 commit b0ea60e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 64 deletions.
4 changes: 3 additions & 1 deletion developer_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $ tree -d -I 'vendor|bin|.git'
│   ├── charts
│   │   └── tensorboard
│   │   └── templates
│   ├── crd
│   ├── gke
│   │   └── notebook_image
│   ├── tensorflow-models
Expand Down Expand Up @@ -147,10 +148,11 @@ export MY_POD_NAME=my-pod
Now we are ready to run operator locally:

```sh
kubectl create -f examples/crd/crd.yaml
tf_operator --logtostderr
```

The command creates a CRD `tfjobs` and block watching for creation of the resource kind. To verify local
The first command creates a CRD `tfjobs`. And the second command runs the operator locally. To verify local
operator is working, create an example job and you should see jobs created by it.

```sh
Expand Down
11 changes: 11 additions & 0 deletions examples/crd/crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: tfjobs.tensorflow.org
spec:
group: tensorflow.org
version: v1alpha1
names:
kind: TFJob
singular: tfjob
plural: tfjobs
62 changes: 0 additions & 62 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ package controller
import (
"errors"
"fmt"
"reflect"
"time"

"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sErrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -24,7 +21,6 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"

"github.com/tensorflow/k8s/pkg/apis/tensorflow/helper"
tfv1alpha1 "github.com/tensorflow/k8s/pkg/apis/tensorflow/v1alpha1"
tfjobclient "github.com/tensorflow/k8s/pkg/client/clientset/versioned"
kubeflowscheme "github.com/tensorflow/k8s/pkg/client/clientset/versioned/scheme"
Expand Down Expand Up @@ -121,10 +117,6 @@ func New(kubeClient kubernetes.Interface, APIExtclient apiextensionsclient.Inter
controller.TFJobSynced = tfJobInformer.Informer().HasSynced
controller.syncHandler = controller.syncTFJob

if err := controller.createCRD(); err != nil {
return nil, err
}

return controller, nil
}

Expand Down Expand Up @@ -271,57 +263,3 @@ func (c *Controller) handleDelete(obj interface{}) {

c.jobs[tfjob.ObjectMeta.Namespace+"-"+tfjob.ObjectMeta.Name].Delete()
}

func (c *Controller) createCRD() error {
crd := &v1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: helper.CRDName(),
},
Spec: v1beta1.CustomResourceDefinitionSpec{
Group: tfv1alpha1.CRDGroup,
Version: tfv1alpha1.CRDVersion,
Scope: v1beta1.NamespaceScoped,
Names: v1beta1.CustomResourceDefinitionNames{
Plural: tfv1alpha1.CRDKindPlural,
Singular: tfv1alpha1.CRDKind,
// Kind is the serialized kind of the resource. It is normally CamelCase and singular.
Kind: reflect.TypeOf(tfv1alpha1.TFJob{}).Name(),
},
},
}

_, err := c.APIExtclient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}

// wait for CRD being established
err = wait.Poll(500*time.Millisecond, 60*time.Second, func() (bool, error) {
crd, err = c.APIExtclient.ApiextensionsV1beta1().CustomResourceDefinitions().Get(helper.CRDName(), metav1.GetOptions{})
if err != nil {
return false, err
}
for _, cond := range crd.Status.Conditions {
switch cond.Type {
case v1beta1.Established:
if cond.Status == v1beta1.ConditionTrue {
return true, err
}
case v1beta1.NamesAccepted:
if cond.Status == v1beta1.ConditionFalse {
glog.Errorf("Name conflict: %v\n", cond.Reason)
}
}
}
return false, err
})

if err != nil {
deleteErr := c.APIExtclient.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(helper.CRDName(), nil)
if deleteErr != nil {
return k8sErrors.NewAggregate([]error{err, deleteErr})
}
return err
}
return nil
}
2 changes: 1 addition & 1 deletion tf-job-operator-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ description: K8s Custom Resource and Operator For TensorFlow jobs
home: https://github.com/tensorflow/k8s
name: tf-job-operator-chart
sources: ['https://github.com/tensorflow/k8s']
version: 0.2.0
version: 0.2.1
11 changes: 11 additions & 0 deletions tf-job-operator-chart/templates/crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: tfjobs.tensorflow.org
spec:
group: tensorflow.org
version: v1alpha1
names:
kind: TFJob
singular: tfjob
plural: tfjobs

0 comments on commit b0ea60e

Please sign in to comment.