-
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.
- Loading branch information
1 parent
4d35a92
commit d6fcabe
Showing
2 changed files
with
112 additions
and
0 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
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) | ||
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) | ||
} |
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,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)) | ||
} |