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

Commit

Permalink
Remove usage of crd client for checking CRD existence
Browse files Browse the repository at this point in the history
  • Loading branch information
wackxu committed May 28, 2019
1 parent 91b4201 commit c4c4042
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions cmd/mxnet-operator.v1/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
log "github.com/sirupsen/logrus"

"k8s.io/api/core/v1"
crdclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeinformers "k8s.io/client-go/informers"
kubeclientset "k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -102,6 +102,11 @@ func Run(opt *options.ServerOption) error {
return err
}

if !checkCRDExists(mxJobClientSet, opt.Namespace) {
log.Info("CRD doesn't exist. Exiting")
os.Exit(1)
}

// Create informer factory.
kubeInformerFactory := kubeinformers.NewFilteredSharedInformerFactory(kubeClientSet, resyncPeriod, opt.Namespace, nil)
mxJobInformerFactory := mxjobinformers.NewSharedInformerFactory(mxJobClientSet, resyncPeriod)
Expand Down Expand Up @@ -167,15 +172,6 @@ func Run(opt *options.ServerOption) error {
}

func createClientSets(config *restclientset.Config) (kubeclientset.Interface, kubeclientset.Interface, mxjobclientset.Interface, kubebatchclient.Interface, error) {

crdClient, err := crdclient.NewForConfig(config)

if err != nil {
return nil, nil, nil, nil, err
}

checkCRDExists(crdClient, mxnetv1.MXCRD)

kubeClientSet, err := kubeclientset.NewForConfig(restclientset.AddUserAgent(config, "mxnet-operator"))
if err != nil {
return nil, nil, nil, nil, err
Expand All @@ -199,10 +195,16 @@ func createClientSets(config *restclientset.Config) (kubeclientset.Interface, ku
return kubeClientSet, leaderElectionClientSet, mxJobClientSet, kubeBatchClientSet, nil
}

func checkCRDExists(clientset crdclient.Interface, crdName string) {
_, err := clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Get(crdName, metav1.GetOptions{})
func checkCRDExists(clientset mxjobclientset.Interface, namespace string) bool {
_, err := clientset.KubeflowV1().MXJobs(namespace).List(metav1.ListOptions{})

if err != nil {
log.Error(err)
os.Exit(1)
if _, ok := err.(*errors.StatusError); ok {
if errors.IsNotFound(err) {
return false
}
}
}
return true
}
4 changes: 2 additions & 2 deletions pkg/common/util/v1/unstructured/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func newFilteredUnstructuredInformer(resource schema.GroupVersionResource, clien
return cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return client.Resource(resource).List(options)
return client.Resource(resource).Namespace(namespace).List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return client.Resource(resource).Watch(options)
return client.Resource(resource).Namespace(namespace).Watch(options)
},
},
&unstructured.Unstructured{},
Expand Down

0 comments on commit c4c4042

Please sign in to comment.