-
Notifications
You must be signed in to change notification settings - Fork 8
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
+746
−0
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2c23149
Add newNamespaceService in server/route
sufuf3 2d2a80a
Add namespace entity for server handler
sufuf3 33829fc
Add src/kubernetes/namespaces for kubeCtl
sufuf3 d82acfc
Add src/namespace/ for server handler package
sufuf3 3b59ace
Add namespace server handler
sufuf3 020e3f3
Remove labels of namespace
sufuf3 143d1f8
Update DeletionPropagation from background to foreground
sufuf3 760aee7
Fix CI test fail
sufuf3 76d3196
Add Namespace CRUD in API.md
sufuf3 6936c7d
Update namespace.go
John-Lin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.