Skip to content

Commit

Permalink
Bootstraper polish: (#654)
Browse files Browse the repository at this point in the history
* Bootstraper polish:
1. Use namespace pass in argument (--namespace=) or defalut namespace ('kubeflow'),
   create it if not exists.
2. Add unit test pkg (github.com/stretchr/testify) to gilde.
3. Edit Makefile for macOS compatibility

* rm debug output

* undo change, will handle in separate PR
  • Loading branch information
kunmingg authored and k8s-ci-robot committed Apr 19, 2018
1 parent 158b74c commit bd21780
Show file tree
Hide file tree
Showing 123 changed files with 20,514 additions and 21 deletions.
2 changes: 2 additions & 0 deletions bootstrap/cmd/bootstrap/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ServerOption struct {
JsonLogFormat bool
AppDir string
KfVersion string
NameSpace string
}

// NewServerOption creates a new CMServer with a default config.
Expand All @@ -39,5 +40,6 @@ func (s *ServerOption) AddFlags(fs *flag.FlagSet) {
fs.BoolVar(&s.JsonLogFormat, "json-log-format", true, "Set true to use json style log format. Set false to use plaintext style log format")
fs.StringVar(&s.AppDir, "app-dir", "", "The directory for the ksonnet application.")
fs.StringVar(&s.KfVersion, "kubeflow-version", "v0.1.0-rc.4", "The Kubeflow version to use.")
fs.StringVar(&s.NameSpace, "namespace", "kubeflow", "The namespace where all resources for kubeflow will be created")
fs.BoolVar(&s.Apply, "apply", true, "Whether or not to apply the configuraiton.")
}
50 changes: 33 additions & 17 deletions bootstrap/cmd/bootstrap/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
core_v1 "k8s.io/api/core/v1"
type_v1 "k8s.io/client-go/kubernetes/typed/core/v1"

"os"
"path"
Expand All @@ -49,8 +51,6 @@ const RecommendedConfigPathEnvVar = "KUBECONFIG"
// whether a storage class is the default.
const DefaultStorageAnnotation = "storageclass.beta.kubernetes.io/is-default-class"

const DefaultNamespace = "kubeflow"

// Assume gcloud is on the path.
const GcloudPath = "gcloud"

Expand Down Expand Up @@ -98,7 +98,7 @@ func getKubeConfigFile() string {

// gGetClusterConfig obtain the config from the Kube configuration used by kubeconfig.
//
func getClusterConfig() (*rest.Config, string, error) {
func getClusterConfig() (*rest.Config, error) {
configFile := getKubeConfigFile()

if len(configFile) > 0 {
Expand All @@ -113,21 +113,15 @@ func getClusterConfig() (*rest.Config, string, error) {
rawConfig, err := clientConfig.RawConfig()

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

if err := modifyGcloudCommand(&rawConfig); err != nil {
return nil, "", err
}

namespace, _, err := clientConfig.Namespace()

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

config, err := clientConfig.ClientConfig()
return config, namespace, err
return config, err
}

// Work around https://github.com/kubernetes/kubernetes/issues/40973
Expand All @@ -138,7 +132,7 @@ func getClusterConfig() (*rest.Config, string, error) {
panic(err)
}
if err := os.Setenv("KUBERNETES_SERVICE_HOST", addrs[0]); err != nil {
return nil, "", err
return nil, err
}
}
if len(os.Getenv("KUBERNETES_SERVICE_PORT")) == 0 {
Expand All @@ -148,7 +142,7 @@ func getClusterConfig() (*rest.Config, string, error) {
}

config, err := rest.InClusterConfig()
return config, DefaultNamespace, err
return config, err
}

func isGke(v *k8sVersion.Info) bool {
Expand Down Expand Up @@ -183,15 +177,32 @@ func hasDefaultStorage(sClasses *v1.StorageClassList) bool {
return false
}

func setupNamespace(namespaces type_v1.NamespaceInterface, name_space string) error {
namespace, err := namespaces.Get(name_space, meta_v1.GetOptions{})
if err == nil {
log.Infof("Using existing namespace: %v", namespace.Name)
} else {
log.Infof("Creating namespace: %v for all kubeflow resources", name_space)
_, err = namespaces.Create(
&core_v1.Namespace{
ObjectMeta: meta_v1.ObjectMeta{
Name: name_space,
},
},
)
return err
}
return err
}

// Run the tool.
func Run(opt *options.ServerOption) error {
// Check if the -version flag was passed and, if so, print the version and exit.
if opt.PrintVersion {
version.PrintVersionAndExit()
}

config, namespace, err := getClusterConfig()
log.Infof("Using namespace: %v", namespace)
config, err := getClusterConfig()
if err != nil {
return err
}
Expand All @@ -201,6 +212,11 @@ func Run(opt *options.ServerOption) error {
return err
}

err = setupNamespace(kubeClient.CoreV1().Namespaces(), opt.NameSpace)
if err != nil {
return err
}

clusterVersion, err := kubeClient.DiscoveryClient.ServerVersion()

if err != nil {
Expand Down Expand Up @@ -237,7 +253,7 @@ func Run(opt *options.ServerOption) error {
// TODO(jlewi): What is the proper version to use? It shouldn't be a version like v1.9.0-gke as that
// will create an error because ksonnet will be unable to fetch a swagger spec.
actions.OptionSpecFlag: "version:v1.7.0",
actions.OptionNamespace: namespace,
actions.OptionNamespace: opt.NameSpace,
actions.OptionSkipDefaultRegistries: true,
}

Expand Down
50 changes: 49 additions & 1 deletion bootstrap/cmd/bootstrap/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

"reflect"

"k8s.io/api/storage/v1"
k8sVersion "k8s.io/apimachinery/pkg/version"
"github.com/stretchr/testify/mock"
core_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
type_v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"errors"
)

func TestModifyGcloudCommand(t *testing.T) {
Expand Down Expand Up @@ -139,3 +143,47 @@ func TestHasDefaultStorageClass(t *testing.T) {
}
}
}

type MockedNamespace struct{
mock.Mock
type_v1.NamespaceInterface
}

func (n *MockedNamespace) Get(name string, options meta_v1.GetOptions) (*core_v1.Namespace, error) {
if name == "existing" {
return &core_v1.Namespace{
ObjectMeta: meta_v1.ObjectMeta{
Name: "existing",
},
}, nil
}
return nil, errors.New("not found")
}

func (n *MockedNamespace) Create(ns *core_v1.Namespace) (*core_v1.Namespace, error) {
n.Called(ns)
// no consumer of return value, so return nil
return nil, nil
}

// Make sure setupNamespace will create namespace if and only if the namespace doesn't exist.
func TestSetupNamespace(t *testing.T) {
// create an instance of our test object
mockedNamespace := new(MockedNamespace)

nsIns := &core_v1.Namespace{
ObjectMeta: meta_v1.ObjectMeta{
Name: "new",
},
}
mockedNamespace.On("Create", nsIns).Return(
nsIns, nil)

// "Create" should be called 0 times when namespace exists already
setupNamespace(mockedNamespace, "existing")
mockedNamespace.AssertNumberOfCalls(t, "Create", 0)

// "Create" should be called 1 times when namespace doesn't exist
setupNamespace(mockedNamespace, "new")
mockedNamespace.AssertNumberOfCalls(t, "Create", 1)
}
18 changes: 15 additions & 3 deletions bootstrap/glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bootstrap/glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ import:
- filename
- package: github.com/sirupsen/logrus
version: ^1.0.5
- package: github.com/stretchr/testify
version: ^1.2.0

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd21780

Please sign in to comment.