-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from linkernetworks/alex/addk8sfunction
VX-149 VX-150 Add k8s pod, service, pvc: get list create delete function Former-commit-id: 2e93f942b209e675723145e557b7ffb0bc8994d4 [formerly e206b23] Former-commit-id: 77129bca7098229b4145cc2be5b9b3fdddea2d8d
- Loading branch information
Showing
8 changed files
with
366 additions
and
3 deletions.
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
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" | ||
) | ||
|
||
//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{}) | ||
} | ||
|
||
//Get all pods from the k8s cluster | ||
func (kc *KubeCtl) GetPods(namespace string) ([]*corev1.Pod, error) { | ||
pods := []*corev1.Pod{} | ||
podsList, err := kc.Clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{}) | ||
if err != nil { | ||
return pods, err | ||
} | ||
for _, p := range podsList.Items { | ||
pods = append(pods, &p) | ||
} | ||
return pods, nil | ||
} | ||
|
||
//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) | ||
} | ||
|
||
//Delete the pod by the pod name | ||
func (kc *KubeCtl) DeletePod(name string, namespace string) error { | ||
options := metav1.DeleteOptions{} | ||
return kc.Clientset.CoreV1().Pods(namespace).Delete(name, &options) | ||
} |
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,86 @@ | ||
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 KubeCtlPodTestSuite struct { | ||
suite.Suite | ||
kubectl *KubeCtl | ||
fakeclient *fakeclientset.Clientset | ||
} | ||
|
||
func (suite *KubeCtlPodTestSuite) SetupSuite() { | ||
suite.fakeclient = fakeclientset.NewSimpleClientset() | ||
namespace := "default" | ||
suite.kubectl = New(suite.fakeclient, namespace) | ||
} | ||
|
||
func (suite *KubeCtlPodTestSuite) TestGetPod() { | ||
namespace := "default" | ||
pod := corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Pod-1", | ||
}, | ||
} | ||
_, err := suite.fakeclient.CoreV1().Pods(namespace).Create(&pod) | ||
suite.NoError(err) | ||
|
||
result, err := suite.kubectl.GetPod("K8S-Pod-1", namespace) | ||
suite.NoError(err) | ||
suite.Equal(pod.GetName(), result.GetName()) | ||
} | ||
|
||
func (suite *KubeCtlPodTestSuite) TestGetPodFail() { | ||
namespace := "Unknown_Namespace" | ||
_, err := suite.kubectl.GetPod("Unknown_Name", namespace) | ||
suite.Error(err) | ||
} | ||
|
||
func (suite *KubeCtlPodTestSuite) TestGetPods() { | ||
namespace := "default" | ||
pod := corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Pod-2", | ||
}, | ||
} | ||
_, err := suite.fakeclient.CoreV1().Pods(namespace).Create(&pod) | ||
suite.NoError(err) | ||
|
||
pod = corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Pod-3", | ||
}, | ||
} | ||
_, err = suite.fakeclient.CoreV1().Pods(namespace).Create(&pod) | ||
suite.NoError(err) | ||
|
||
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, namespace) | ||
suite.NoError(err) | ||
err = suite.kubectl.DeletePod("K8S-Pod-4", namespace) | ||
suite.NoError(err) | ||
} | ||
|
||
func (suite *KubeCtlPodTestSuite) TearDownSuite() {} | ||
|
||
func TestKubePodTestSuite(t *testing.T) { | ||
suite.Run(t, new(KubeCtlPodTestSuite)) | ||
} |
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" | ||
) | ||
|
||
//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{}) | ||
} | ||
|
||
//Get all PVCs from the k8s cluster | ||
func (kc *KubeCtl) GetPVCs(namespace string) ([]*corev1.PersistentVolumeClaim, error) { | ||
pvcs := []*corev1.PersistentVolumeClaim{} | ||
pvcsList, err := kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).List(metav1.ListOptions{}) | ||
if err != nil { | ||
return pvcs, err | ||
} | ||
for _, p := range pvcsList.Items { | ||
pvcs = append(pvcs, &p) | ||
} | ||
return pvcs, nil | ||
} | ||
|
||
//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) | ||
} | ||
|
||
//Delete the PVC by the PVC name | ||
func (kc *KubeCtl) DeletePVC(name string, namespace string) error { | ||
options := metav1.DeleteOptions{} | ||
return kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(name, &options) | ||
} |
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,86 @@ | ||
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 KubeCtlPVCTestSuite struct { | ||
suite.Suite | ||
kubectl *KubeCtl | ||
fakeclient *fakeclientset.Clientset | ||
} | ||
|
||
func (suite *KubeCtlPVCTestSuite) SetupSuite() { | ||
suite.fakeclient = fakeclientset.NewSimpleClientset() | ||
namespace := "default" | ||
suite.kubectl = New(suite.fakeclient, namespace) | ||
} | ||
|
||
func (suite *KubeCtlPVCTestSuite) TestGetPVC() { | ||
namespace := "default" | ||
pvc := corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-PVC-1", | ||
}, | ||
} | ||
_, err := suite.fakeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc) | ||
suite.NoError(err) | ||
|
||
result, err := suite.kubectl.GetPVC("K8S-PVC-1", namespace) | ||
suite.NoError(err) | ||
suite.Equal(pvc.GetName(), result.GetName()) | ||
} | ||
|
||
func (suite *KubeCtlPVCTestSuite) TestGetPVCFail() { | ||
namespace := "Unknown_Namespace" | ||
_, err := suite.kubectl.GetPVC("Unknown_Name", namespace) | ||
suite.Error(err) | ||
} | ||
|
||
func (suite *KubeCtlPVCTestSuite) TestGetPVCs() { | ||
namespace := "default" | ||
pvc := corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-PVC-2", | ||
}, | ||
} | ||
_, err := suite.fakeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc) | ||
suite.NoError(err) | ||
|
||
pvc = corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-PVC-3", | ||
}, | ||
} | ||
_, err = suite.fakeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc) | ||
suite.NoError(err) | ||
|
||
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, namespace) | ||
suite.NoError(err) | ||
err = suite.kubectl.DeletePVC("K8S-PVC-4", namespace) | ||
suite.NoError(err) | ||
} | ||
|
||
func (suite *KubeCtlPVCTestSuite) TearDownSuite() {} | ||
|
||
func TestKubePVCTestSuite(t *testing.T) { | ||
suite.Run(t, new(KubeCtlPVCTestSuite)) | ||
} |
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" | ||
) | ||
|
||
//Get the service object by the service name | ||
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(namespace string) ([]*corev1.Service, error) { | ||
services := []*corev1.Service{} | ||
servicesList, err := kc.Clientset.CoreV1().Services(namespace).List(metav1.ListOptions{}) | ||
if err != nil { | ||
return services, err | ||
} | ||
for _, s := range servicesList.Items { | ||
services = append(services, &s) | ||
} | ||
return services, nil | ||
} | ||
|
||
//Create the service by the service object | ||
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, namespace string) error { | ||
options := metav1.DeleteOptions{} | ||
return kc.Clientset.CoreV1().Services(namespace).Delete(name, &options) | ||
} |
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,86 @@ | ||
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 KubeCtlServiceTestSuite struct { | ||
suite.Suite | ||
kubectl *KubeCtl | ||
fakeclient *fakeclientset.Clientset | ||
} | ||
|
||
func (suite *KubeCtlServiceTestSuite) SetupSuite() { | ||
suite.fakeclient = fakeclientset.NewSimpleClientset() | ||
namespace := "default" | ||
suite.kubectl = New(suite.fakeclient, namespace) | ||
} | ||
|
||
func (suite *KubeCtlServiceTestSuite) TestGetService() { | ||
namespace := "default" | ||
service := corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Service-1", | ||
}, | ||
} | ||
_, err := suite.fakeclient.CoreV1().Services(namespace).Create(&service) | ||
suite.NoError(err) | ||
|
||
result, err := suite.kubectl.GetService("K8S-Service-1", namespace) | ||
suite.NoError(err) | ||
suite.Equal(service.GetName(), result.GetName()) | ||
} | ||
|
||
func (suite *KubeCtlServiceTestSuite) TestGetServiceFail() { | ||
namespace := "Unknown_Namespace" | ||
_, err := suite.kubectl.GetService("Unknown_Name", namespace) | ||
suite.Error(err) | ||
} | ||
|
||
func (suite *KubeCtlServiceTestSuite) TestGetServices() { | ||
namespace := "default" | ||
service := corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Service-2", | ||
}, | ||
} | ||
_, err := suite.fakeclient.CoreV1().Services(namespace).Create(&service) | ||
suite.NoError(err) | ||
|
||
service = corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Service-3", | ||
}, | ||
} | ||
_, err = suite.fakeclient.CoreV1().Services(namespace).Create(&service) | ||
suite.NoError(err) | ||
|
||
services, err := suite.kubectl.GetServices(namespace) | ||
suite.NoError(err) | ||
suite.NotEqual(0, len(services)) | ||
} | ||
|
||
func (suite *KubeCtlServiceTestSuite) TestCreateDeleteService() { | ||
namespace := "default" | ||
service := corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Service-4", | ||
}, | ||
} | ||
_, err := suite.kubectl.CreateService(&service, namespace) | ||
suite.NoError(err) | ||
err = suite.kubectl.DeleteService("K8S-Service-4", namespace) | ||
suite.NoError(err) | ||
} | ||
|
||
func (suite *KubeCtlServiceTestSuite) TearDownSuite() {} | ||
|
||
func TestKubeServiceTestSuite(t *testing.T) { | ||
suite.Run(t, new(KubeCtlServiceTestSuite)) | ||
} |