Skip to content

Commit

Permalink
Merge pull request #103 from linkernetworks/alex/AddDefaultNamespace
Browse files Browse the repository at this point in the history
VX-173 Add default namespace
  • Loading branch information
Hung-Wei Chiu authored Jul 16, 2018
2 parents b39026f + 07d8db1 commit 9f2ebe4
Show file tree
Hide file tree
Showing 23 changed files with 112 additions and 112 deletions.
16 changes: 8 additions & 8 deletions src/kubernetes/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
)

//Get the external IP address of node
func (kc *KubeCtl) CreateDeployment(deployment *appsv1.Deployment) (*appsv1.Deployment, error) {
return kc.Clientset.AppsV1().Deployments(kc.Namespace).Create(deployment)
func (kc *KubeCtl) CreateDeployment(deployment *appsv1.Deployment, namespace string) (*appsv1.Deployment, error) {
return kc.Clientset.AppsV1().Deployments(namespace).Create(deployment)
}

func (kc *KubeCtl) GetDeployment(name string) (*appsv1.Deployment, error) {
return kc.Clientset.AppsV1().Deployments(kc.Namespace).Get(name, metav1.GetOptions{})
func (kc *KubeCtl) GetDeployment(name string, namespace string) (*appsv1.Deployment, error) {
return kc.Clientset.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
}

func (kc *KubeCtl) GetDeployments() ([]*appsv1.Deployment, error) {
func (kc *KubeCtl) GetDeployments(namespace string) ([]*appsv1.Deployment, error) {
deployments := []*appsv1.Deployment{}
deploymentsList, err := kc.Clientset.AppsV1().Deployments(kc.Namespace).List(metav1.ListOptions{})
deploymentsList, err := kc.Clientset.AppsV1().Deployments(namespace).List(metav1.ListOptions{})
if err != nil {
return deployments, err
}
Expand All @@ -26,6 +26,6 @@ func (kc *KubeCtl) GetDeployments() ([]*appsv1.Deployment, error) {
return deployments, nil
}

func (kc *KubeCtl) DeleteDeployment(name string) error {
return kc.Clientset.AppsV1().Deployments(kc.Namespace).Delete(name, &metav1.DeleteOptions{})
func (kc *KubeCtl) DeleteDeployment(name string, namespace string) error {
return kc.Clientset.AppsV1().Deployments(namespace).Delete(name, &metav1.DeleteOptions{})
}
19 changes: 10 additions & 9 deletions src/kubernetes/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func init() {

func (suite *KubeCtlDeploymentTestSuite) SetupSuite() {
suite.fakeclient = fakeclientset.NewSimpleClientset()
namespace := "default"
suite.kubectl = New(suite.fakeclient, namespace)
suite.kubectl = New(suite.fakeclient)
}

func (suite *KubeCtlDeploymentTestSuite) TearDownSuite() {}
func (suite *KubeCtlDeploymentTestSuite) TestCreateDeployment() {
namespace := "default"
var replicas int32
replicas = 3
name := namesgenerator.GetRandomName(0)
Expand All @@ -43,22 +43,23 @@ func (suite *KubeCtlDeploymentTestSuite) TestCreateDeployment() {
},
Status: appsv1.DeploymentStatus{},
}
ret, err := suite.kubectl.CreateDeployment(&deployment)
ret, err := suite.kubectl.CreateDeployment(&deployment, namespace)
suite.NoError(err)
suite.NotNil(ret)

deploy, err := suite.kubectl.GetDeployment(name)
deploy, err := suite.kubectl.GetDeployment(name, namespace)
suite.NoError(err)
suite.NotNil(deploy)
suite.Equal(replicas, *deploy.Spec.Replicas)

deploys, err := suite.kubectl.GetDeployments()
deploys, err := suite.kubectl.GetDeployments(namespace)
suite.NoError(err)
suite.NotNil(deploys)
suite.Equal(replicas, *deploys[0].Spec.Replicas)
}

func (suite *KubeCtlDeploymentTestSuite) TestDeleteDeployment() {
namespace := "default"
var replicas int32
replicas = 3
name := namesgenerator.GetRandomName(0)
Expand All @@ -71,18 +72,18 @@ func (suite *KubeCtlDeploymentTestSuite) TestDeleteDeployment() {
},
Status: appsv1.DeploymentStatus{},
}
ret, err := suite.kubectl.CreateDeployment(&deployment)
ret, err := suite.kubectl.CreateDeployment(&deployment, namespace)
suite.NoError(err)
suite.NotNil(ret)

deploy, err := suite.kubectl.GetDeployment(name)
deploy, err := suite.kubectl.GetDeployment(name, namespace)
suite.NoError(err)
suite.NotNil(deploy)
suite.Equal(replicas, *deploy.Spec.Replicas)

err = suite.kubectl.DeleteDeployment(name)
err = suite.kubectl.DeleteDeployment(name, namespace)
suite.NoError(err)
deploy, err = suite.kubectl.GetDeployment(name)
deploy, err = suite.kubectl.GetDeployment(name, namespace)
suite.Error(err)
suite.Nil(deploy)
}
Expand Down
8 changes: 1 addition & 7 deletions src/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ import (
*/
type KubeCtl struct {
Clientset kubernetes.Interface
Namespace string
}

/*
The API to New a kubectl object and you need to pass two parameters
1. The kubernetes clientset object from the client-go library. You can also use the fake-client for testing
2. The namespace of the kubernetes you want to manipulate
*/
func New(clientset kubernetes.Interface, namespace string) *KubeCtl {
func New(clientset kubernetes.Interface) *KubeCtl {
return &KubeCtl{
Clientset: clientset,
Namespace: namespace,
}
}

func (kc *KubeCtl) SetNamespace(namespace string) {
kc.Namespace = namespace
}
6 changes: 1 addition & 5 deletions src/kubernetes/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (

func TestNewKubeCtl(t *testing.T) {
clientset := fakeclientset.NewSimpleClientset()
namespace := "default"
kubectl := New(clientset, namespace)
assert.Equal(t, namespace, kubectl.Namespace)
kubectl := New(clientset)
assert.NotNil(t, kubectl)
// Change struct namespace field value
kubectl.SetNamespace("test")
}
3 changes: 1 addition & 2 deletions src/kubernetes/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type KubeCtlNodeTestSuite struct {

func (suite *KubeCtlNodeTestSuite) SetupSuite() {
suite.fakeclient = fakeclientset.NewSimpleClientset()
namespace := "default"
suite.kubectl = New(suite.fakeclient, namespace)
suite.kubectl = New(suite.fakeclient)
}

func (suite *KubeCtlNodeTestSuite) TestGetNode() {
Expand Down
16 changes: 8 additions & 8 deletions src/kubernetes/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

//Get the pod object by the pod name
func (kc *KubeCtl) GetPod(name string) (*corev1.Pod, error) {
return kc.Clientset.CoreV1().Pods(kc.Namespace).Get(name, metav1.GetOptions{})
func (kc *KubeCtl) GetPod(name string, namespace string) (*corev1.Pod, error) {
return kc.Clientset.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
}

//Get all pods from the k8s cluster
func (kc *KubeCtl) GetPods() ([]*corev1.Pod, error) {
func (kc *KubeCtl) GetPods(namespace string) ([]*corev1.Pod, error) {
pods := []*corev1.Pod{}
podsList, err := kc.Clientset.CoreV1().Pods(kc.Namespace).List(metav1.ListOptions{})
podsList, err := kc.Clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
return pods, err
}
Expand All @@ -24,14 +24,14 @@ func (kc *KubeCtl) GetPods() ([]*corev1.Pod, error) {
}

//Create the pod by the pod object
func (kc *KubeCtl) CreatePod(pod *corev1.Pod) (*corev1.Pod, error) {
return kc.Clientset.CoreV1().Pods(kc.Namespace).Create(pod)
func (kc *KubeCtl) CreatePod(pod *corev1.Pod, namespace string) (*corev1.Pod, error) {
return kc.Clientset.CoreV1().Pods(namespace).Create(pod)
}

//Delete the pod by the pod name
func (kc *KubeCtl) DeletePod(name string) error {
func (kc *KubeCtl) DeletePod(name string, namespace string) error {
options := metav1.DeleteOptions{}
return kc.Clientset.CoreV1().Pods(kc.Namespace).Delete(name, &options)
return kc.Clientset.CoreV1().Pods(namespace).Delete(name, &options)
}

func (kc *KubeCtl) IsPodCompleted(pod *corev1.Pod) bool {
Expand Down
18 changes: 10 additions & 8 deletions src/kubernetes/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type KubeCtlPodTestSuite struct {

func (suite *KubeCtlPodTestSuite) SetupSuite() {
suite.fakeclient = fakeclientset.NewSimpleClientset()
namespace := "default"
suite.kubectl = New(suite.fakeclient, namespace)
suite.kubectl = New(suite.fakeclient)
}

func (suite *KubeCtlPodTestSuite) TestGetPod() {
Expand All @@ -39,13 +38,14 @@ func (suite *KubeCtlPodTestSuite) TestGetPod() {
_, err := suite.fakeclient.CoreV1().Pods(namespace).Create(&pod)
suite.NoError(err)

result, err := suite.kubectl.GetPod("K8S-Pod-1")
result, err := suite.kubectl.GetPod("K8S-Pod-1", namespace)
suite.NoError(err)
suite.Equal(pod.GetName(), result.GetName())
}

func (suite *KubeCtlPodTestSuite) TestGetPodFail() {
_, err := suite.kubectl.GetPod("Unknown_Name")
namespace := "default"
_, err := suite.kubectl.GetPod("Unknown_Name", namespace)
suite.Error(err)
}

Expand All @@ -67,24 +67,26 @@ func (suite *KubeCtlPodTestSuite) TestGetPods() {
_, err = suite.fakeclient.CoreV1().Pods(namespace).Create(&pod)
suite.NoError(err)

pods, err := suite.kubectl.GetPods()
pods, err := suite.kubectl.GetPods(namespace)
suite.NoError(err)
suite.NotEqual(0, len(pods))
}

func (suite *KubeCtlPodTestSuite) TestCreateDeletePod() {
namespace := "default"
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "K8S-Pod-4",
},
}
_, err := suite.kubectl.CreatePod(&pod)
_, err := suite.kubectl.CreatePod(&pod, namespace)
suite.NoError(err)
err = suite.kubectl.DeletePod("K8S-Pod-4")
err = suite.kubectl.DeletePod("K8S-Pod-4", namespace)
suite.NoError(err)
}

func (suite *KubeCtlPodTestSuite) TestDoesPodCompleted() {
namespace := "default"
pods := []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -105,7 +107,7 @@ func (suite *KubeCtlPodTestSuite) TestDoesPodCompleted() {
}

for _, pod := range pods {
_, err := suite.kubectl.CreatePod(&pod)
_, err := suite.kubectl.CreatePod(&pod, namespace)
suite.NoError(err)
}

Expand Down
16 changes: 8 additions & 8 deletions src/kubernetes/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

//Get the PVC object by the PVC name
func (kc *KubeCtl) GetPVC(name string) (*corev1.PersistentVolumeClaim, error) {
return kc.Clientset.CoreV1().PersistentVolumeClaims(kc.Namespace).Get(name, metav1.GetOptions{})
func (kc *KubeCtl) GetPVC(name string, namespace string) (*corev1.PersistentVolumeClaim, error) {
return kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Get(name, metav1.GetOptions{})
}

//Get all PVCs from the k8s cluster
func (kc *KubeCtl) GetPVCs() ([]*corev1.PersistentVolumeClaim, error) {
func (kc *KubeCtl) GetPVCs(namespace string) ([]*corev1.PersistentVolumeClaim, error) {
pvcs := []*corev1.PersistentVolumeClaim{}
pvcsList, err := kc.Clientset.CoreV1().PersistentVolumeClaims(kc.Namespace).List(metav1.ListOptions{})
pvcsList, err := kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).List(metav1.ListOptions{})
if err != nil {
return pvcs, err
}
Expand All @@ -24,12 +24,12 @@ func (kc *KubeCtl) GetPVCs() ([]*corev1.PersistentVolumeClaim, error) {
}

//Create the PVC by the PVC object
func (kc *KubeCtl) CreatePVC(pvc *corev1.PersistentVolumeClaim) (*corev1.PersistentVolumeClaim, error) {
return kc.Clientset.CoreV1().PersistentVolumeClaims(kc.Namespace).Create(pvc)
func (kc *KubeCtl) CreatePVC(pvc *corev1.PersistentVolumeClaim, namespace string) (*corev1.PersistentVolumeClaim, error) {
return kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Create(pvc)
}

//Delete the PVC by the PVC name
func (kc *KubeCtl) DeletePVC(name string) error {
func (kc *KubeCtl) DeletePVC(name string, namespace string) error {
options := metav1.DeleteOptions{}
return kc.Clientset.CoreV1().PersistentVolumeClaims(kc.Namespace).Delete(name, &options)
return kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(name, &options)
}
15 changes: 8 additions & 7 deletions src/kubernetes/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type KubeCtlPVCTestSuite struct {

func (suite *KubeCtlPVCTestSuite) SetupSuite() {
suite.fakeclient = fakeclientset.NewSimpleClientset()
namespace := "default"
suite.kubectl = New(suite.fakeclient, namespace)
suite.kubectl = New(suite.fakeclient)
}

func (suite *KubeCtlPVCTestSuite) TestGetPVC() {
Expand All @@ -32,13 +31,14 @@ func (suite *KubeCtlPVCTestSuite) TestGetPVC() {
_, err := suite.fakeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc)
suite.NoError(err)

result, err := suite.kubectl.GetPVC("K8S-PVC-1")
result, err := suite.kubectl.GetPVC("K8S-PVC-1", namespace)
suite.NoError(err)
suite.Equal(pvc.GetName(), result.GetName())
}

func (suite *KubeCtlPVCTestSuite) TestGetPVCFail() {
_, err := suite.kubectl.GetPVC("Unknown_Name")
namespace := "default"
_, err := suite.kubectl.GetPVC("Unknown_Name", namespace)
suite.Error(err)
}

Expand All @@ -60,20 +60,21 @@ func (suite *KubeCtlPVCTestSuite) TestGetPVCs() {
_, err = suite.fakeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc)
suite.NoError(err)

pvcs, err := suite.kubectl.GetPVCs()
pvcs, err := suite.kubectl.GetPVCs(namespace)
suite.NoError(err)
suite.NotEqual(0, len(pvcs))
}

func (suite *KubeCtlPVCTestSuite) TestCreateDeletePVC() {
namespace := "default"
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "K8S-PVC-4",
},
}
_, err := suite.kubectl.CreatePVC(&pvc)
_, err := suite.kubectl.CreatePVC(&pvc, namespace)
suite.NoError(err)
err = suite.kubectl.DeletePVC("K8S-PVC-4")
err = suite.kubectl.DeletePVC("K8S-PVC-4", namespace)
suite.NoError(err)
}

Expand Down
16 changes: 8 additions & 8 deletions src/kubernetes/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

//Get the service object by the service name
func (kc *KubeCtl) GetService(name string) (*corev1.Service, error) {
return kc.Clientset.CoreV1().Services(kc.Namespace).Get(name, metav1.GetOptions{})
func (kc *KubeCtl) GetService(name string, namespace string) (*corev1.Service, error) {
return kc.Clientset.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
}

//Get all services from the k8s cluster
func (kc *KubeCtl) GetServices() ([]*corev1.Service, error) {
func (kc *KubeCtl) GetServices(namespace string) ([]*corev1.Service, error) {
services := []*corev1.Service{}
servicesList, err := kc.Clientset.CoreV1().Services(kc.Namespace).List(metav1.ListOptions{})
servicesList, err := kc.Clientset.CoreV1().Services(namespace).List(metav1.ListOptions{})
if err != nil {
return services, err
}
Expand All @@ -24,12 +24,12 @@ func (kc *KubeCtl) GetServices() ([]*corev1.Service, error) {
}

//Create the service by the service object
func (kc *KubeCtl) CreateService(service *corev1.Service) (*corev1.Service, error) {
return kc.Clientset.CoreV1().Services(kc.Namespace).Create(service)
func (kc *KubeCtl) CreateService(service *corev1.Service, namespace string) (*corev1.Service, error) {
return kc.Clientset.CoreV1().Services(namespace).Create(service)
}

//Delete the service by the service name
func (kc *KubeCtl) DeleteService(name string) error {
func (kc *KubeCtl) DeleteService(name string, namespace string) error {
options := metav1.DeleteOptions{}
return kc.Clientset.CoreV1().Services(kc.Namespace).Delete(name, &options)
return kc.Clientset.CoreV1().Services(namespace).Delete(name, &options)
}
Loading

0 comments on commit 9f2ebe4

Please sign in to comment.