Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Task] Add namespace crud #227

Merged
merged 10 commits into from
Aug 14, 2018
97 changes: 97 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
- [List Services](#list-services)
- [Get Service](#get-service)
- [Delete Service](#delete-service)
- [Namespace](#namespace)
- [Create Namespace](#create-namespace)
- [List Namespaces](#list-namespaces)
- [Get Namespace](#get-namespace)
- [Delete Namespace](#delete-namespace)



Expand Down Expand Up @@ -1361,3 +1366,95 @@ Response Data:
"message": "Delete success"
}
```

## Namespace
### Create Namespace

**POST /v1/namespaces**

Example:

```
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"awesome"}' \
http://localhost:7890/v1/namespaces
```

Request Data:

```json
{
"name": "awesome",
}
```

Response Data:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response data should be the namespace entity not create success message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied from service, sorry that I forgot to change the response data.
Updated it.


```json
{
"id": "5b4edcbc4807c557d9feb69e",
"name": "awesome",
"createdAt": "2018-07-18T06:22:52.403Z"
}
```

### List Namespaces

**GET /v1/namespaces/**

Example:

```
curl http://localhost:7890/v1/namespaces/
```

Response Data:

```json
[
{
"id": "5b4edcbc4807c557d9feb69e",
"name": "awesome",
"createdAt": "2018-07-18T06:22:52.403Z"
}
]
```

### Get Namespace

**GET /v1/namespaces/[id]**

Example:

```
curl http://localhost:7890/v1/namespaces/5b4edcbc4807c557d9feb69e
```

Response Data:

```json
{
"id": "5b4edcbc4807c557d9feb69e",
"name": "awesome",
"createdAt": "2018-07-18T06:22:52.403Z"
}
```

### Delete Namespace

**DELETE /v1/namespaces/[id]**

Example:

```
curl -X DELETE http://localhost:7890/v1/namespaces/5b4edcbc4807c557d9feb69e
```

Response Data:

```json
{
"error": false,
"message": "Delete success"
}
```
24 changes: 24 additions & 0 deletions src/entity/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package entity

import (
"time"

"gopkg.in/mgo.v2/bson"
)

// the const for NamespaceCollectionName
const (
NamespaceCollectionName string = "namespaces"
)

// Namespace is the structure for namespace info
type Namespace struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
}

// GetCollection - get model mongo collection name.
func (m Namespace) GetCollection() string {
return NamespaceCollectionName
}
35 changes: 35 additions & 0 deletions src/kubernetes/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kubernetes

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// GetNamespace will get the namespace object by the namespace name
func (kc *KubeCtl) GetNamespace(name string) (*corev1.Namespace, error) {
return kc.Clientset.CoreV1().Namespaces().Get(name, metav1.GetOptions{})
}

// GetNamespaces will get all namespaces from the k8s cluster
func (kc *KubeCtl) GetNamespaces() ([]*corev1.Namespace, error) {
namespaces := []*corev1.Namespace{}
namespacesList, err := kc.Clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return namespaces, err
}
for _, n := range namespacesList.Items {
namespaces = append(namespaces, &n)
}
return namespaces, nil
}

// CreateNamespace will create the namespace by the namespace object
func (kc *KubeCtl) CreateNamespace(namespace *corev1.Namespace) (*corev1.Namespace, error) {
return kc.Clientset.CoreV1().Namespaces().Create(namespace)
}

// DeleteNamespace will delete the namespace by the namespace name
func (kc *KubeCtl) DeleteNamespace(name string) error {
foreground := metav1.DeletePropagationForeground
return kc.Clientset.CoreV1().Namespaces().Delete(name, &metav1.DeleteOptions{PropagationPolicy: &foreground})
}
81 changes: 81 additions & 0 deletions src/kubernetes/namespaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package kubernetes

import (
"testing"

"github.com/stretchr/testify/suite"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakeclientset "k8s.io/client-go/kubernetes/fake"
)

type KubeCtlNamespaceTestSuite struct {
suite.Suite
kubectl *KubeCtl
fakeclient *fakeclientset.Clientset
}

func (suite *KubeCtlNamespaceTestSuite) SetupSuite() {
suite.fakeclient = fakeclientset.NewSimpleClientset()
suite.kubectl = New(suite.fakeclient)
}

func (suite *KubeCtlNamespaceTestSuite) TestGetNamespace() {
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "K8S-Namespace-1",
},
}
_, err := suite.fakeclient.CoreV1().Namespaces().Create(&namespace)
suite.NoError(err)

result, err := suite.kubectl.GetNamespace("K8S-Namespace-1")
suite.NoError(err)
suite.Equal(namespace.GetName(), result.GetName())
}

func (suite *KubeCtlNamespaceTestSuite) TestGetNamespaceFail() {
_, err := suite.kubectl.GetNamespace("Unknown_Name")
suite.Error(err)
}

func (suite *KubeCtlNamespaceTestSuite) TestGetNamespaces() {
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "K8S-Namespace-2",
},
}
_, err := suite.fakeclient.CoreV1().Namespaces().Create(&namespace)
suite.NoError(err)

namespace = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "K8S-Namespace-3",
},
}
_, err = suite.fakeclient.CoreV1().Namespaces().Create(&namespace)
suite.NoError(err)

namespaces, err := suite.kubectl.GetNamespaces()
suite.NoError(err)
suite.NotEqual(0, len(namespaces))
}

func (suite *KubeCtlNamespaceTestSuite) TestCreateDeleteNamespace() {
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "K8S-Namespace-4",
},
}
_, err := suite.kubectl.CreateNamespace(&namespace)
suite.NoError(err)
err = suite.kubectl.DeleteNamespace("K8S-Namespace-4")
suite.NoError(err)
}

func (suite *KubeCtlNamespaceTestSuite) TearDownSuite() {}

func TestKubeNamespaceTestSuite(t *testing.T) {
suite.Run(t, new(KubeCtlNamespaceTestSuite))
}
25 changes: 25 additions & 0 deletions src/namespace/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package namespace

import (
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CreateNamespace will create namespace by serviceprovider container
func CreateNamespace(sp *serviceprovider.Container, namespace *entity.Namespace) error {
n := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace.Name,
},
}
_, err := sp.KubeCtl.CreateNamespace(&n)
return err
}

// DeleteNamespace will delete namespace
func DeleteNamespace(sp *serviceprovider.Container, namespace *entity.Namespace) error {
return sp.KubeCtl.DeleteNamespace(namespace.Name)
}
48 changes: 48 additions & 0 deletions src/namespace/namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package namespace

import (
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"
"gopkg.in/mgo.v2/bson"
"math/rand"
"testing"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

type NamespaceTestSuite struct {
suite.Suite
sp *serviceprovider.Container
}

func (suite *NamespaceTestSuite) SetupSuite() {
cf := config.MustRead("../../config/testing.json")
suite.sp = serviceprovider.NewForTesting(cf)
}

func (suite *NamespaceTestSuite) TearDownSuite() {
}

func TestNamespaceSuite(t *testing.T) {
suite.Run(t, new(NamespaceTestSuite))
}

func (suite *NamespaceTestSuite) TestCreateDeleteNamespace() {
namespaceName := namesgenerator.GetRandomName(0)
namespace := &entity.Namespace{
ID: bson.NewObjectId(),
Name: namespaceName,
}

err := CreateNamespace(suite.sp, namespace)
suite.NoError(err)

err = DeleteNamespace(suite.sp, namespace)
suite.NoError(err)
}
Loading