Skip to content

Commit

Permalink
Merge pull request #68 from linkernetworks/hwchiu/remove-unused-nanes…
Browse files Browse the repository at this point in the history
…pace

Use the namespace in the kubectl object
  • Loading branch information
Hung-Wei Chiu authored Jul 6, 2018
2 parents e934329 + e93f6bc commit 21d3569
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
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, namespace string) (*corev1.Pod, error) {
return kc.Clientset.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
func (kc *KubeCtl) GetPod(name string) (*corev1.Pod, error) {
return kc.Clientset.CoreV1().Pods(kc.Namespace).Get(name, metav1.GetOptions{})
}

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

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

//Delete the pod by the pod name
func (kc *KubeCtl) DeletePod(name string, namespace string) error {
func (kc *KubeCtl) DeletePod(name string) error {
options := metav1.DeleteOptions{}
return kc.Clientset.CoreV1().Pods(namespace).Delete(name, &options)
return kc.Clientset.CoreV1().Pods(kc.Namespace).Delete(name, &options)
}
12 changes: 5 additions & 7 deletions src/kubernetes/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ func (suite *KubeCtlPodTestSuite) TestGetPod() {
_, err := suite.fakeclient.CoreV1().Pods(namespace).Create(&pod)
suite.NoError(err)

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

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

Expand All @@ -61,21 +60,20 @@ func (suite *KubeCtlPodTestSuite) TestGetPods() {
_, err = suite.fakeclient.CoreV1().Pods(namespace).Create(&pod)
suite.NoError(err)

pods, err := suite.kubectl.GetPods(namespace)
pods, err := suite.kubectl.GetPods()
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, namespace)
_, err := suite.kubectl.CreatePod(&pod)
suite.NoError(err)
err = suite.kubectl.DeletePod("K8S-Pod-4", namespace)
err = suite.kubectl.DeletePod("K8S-Pod-4")
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, namespace string) (*corev1.PersistentVolumeClaim, error) {
return kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Get(name, metav1.GetOptions{})
func (kc *KubeCtl) GetPVC(name string) (*corev1.PersistentVolumeClaim, error) {
return kc.Clientset.CoreV1().PersistentVolumeClaims(kc.Namespace).Get(name, metav1.GetOptions{})
}

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

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

//Delete the PVC by the PVC name
func (kc *KubeCtl) DeletePVC(name string, namespace string) error {
func (kc *KubeCtl) DeletePVC(name string) error {
options := metav1.DeleteOptions{}
return kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(name, &options)
return kc.Clientset.CoreV1().PersistentVolumeClaims(kc.Namespace).Delete(name, &options)
}
12 changes: 5 additions & 7 deletions src/kubernetes/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ func (suite *KubeCtlPVCTestSuite) TestGetPVC() {
_, err := suite.fakeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc)
suite.NoError(err)

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

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

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

pvcs, err := suite.kubectl.GetPVCs(namespace)
pvcs, err := suite.kubectl.GetPVCs()
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, namespace)
_, err := suite.kubectl.CreatePVC(&pvc)
suite.NoError(err)
err = suite.kubectl.DeletePVC("K8S-PVC-4", namespace)
err = suite.kubectl.DeletePVC("K8S-PVC-4")
suite.NoError(err)
}

Expand Down

0 comments on commit 21d3569

Please sign in to comment.