-
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
VX-149 VX-150 Add k8s pod, service, pvc: get list create delete function #58
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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) { | ||
pod, err := kc.Clientset.CoreV1().Pods(namespace).Create(pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pod, nil | ||
} | ||
|
||
//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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 := "default" | ||
_, 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) TearDownSuite() {} | ||
|
||
func TestKubePodTestSuite(t *testing.T) { | ||
suite.Run(t, new(KubeCtlPodTestSuite)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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) { | ||
pvc, err := kc.Clientset.CoreV1().PersistentVolumeClaims(namespace).Create(pvc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can just use |
||
if err != nil { | ||
return nil, err | ||
} | ||
return pvc, nil | ||
} | ||
|
||
//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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 := "default" | ||
_, 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) TearDownSuite() {} | ||
|
||
func TestKubePVCTestSuite(t *testing.T) { | ||
suite.Run(t, new(KubeCtlPVCTestSuite)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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) { | ||
service, err := kc.Clientset.CoreV1().Services(namespace).Create(service) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return kc.Clientset.CoreV1().Services(namespace).Create(service) |
||
if err != nil { | ||
return nil, err | ||
} | ||
return service, nil | ||
} | ||
|
||
//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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 := "default" | ||
_, 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) TearDownSuite() {} | ||
|
||
func TestKubeServiceTestSuite(t *testing.T) { | ||
suite.Run(t, new(KubeCtlServiceTestSuite)) | ||
} |
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.
return kc.Clientset.CoreV1().Pods(namespace).Create(pod)