Skip to content

Commit 8b2242a

Browse files
committed
add k8s ops file
Signed-off-by: Ashima-Ashima1 <[email protected]>
1 parent f8c7317 commit 8b2242a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package crutils
2+
3+
import (
4+
"context"
5+
6+
appsv1 "k8s.io/api/apps/v1"
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/types"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
)
11+
12+
type K8sResourceOps struct {
13+
client.Client
14+
Ctx context.Context
15+
Namespace string
16+
}
17+
18+
func (op *K8sResourceOps) GetDeployment(name string) (*appsv1.Deployment, error) {
19+
deployment := &appsv1.Deployment{}
20+
err := op.Get(op.Ctx, types.NamespacedName{Name: name, Namespace: op.Namespace}, deployment)
21+
return deployment, err
22+
}
23+
24+
func (op *K8sResourceOps) ListDeployment() (*appsv1.DeploymentList, error) {
25+
var listOptions = &client.ListOptions{Namespace: op.Namespace}
26+
deploymentList := &appsv1.DeploymentList{}
27+
err := op.List(op.Ctx, deploymentList, listOptions)
28+
return deploymentList, err
29+
}
30+
31+
func (op *K8sResourceOps) DeletePod(pod *corev1.Pod) error {
32+
var zero int64
33+
var deleteOptions = &client.DeleteOptions{GracePeriodSeconds: &zero}
34+
err := op.Delete(op.Ctx, pod, deleteOptions)
35+
return err
36+
}
37+
38+
func (op *K8sResourceOps) ListPod() (*corev1.PodList, error) {
39+
var listOptions = &client.ListOptions{Namespace: op.Namespace}
40+
podList := &corev1.PodList{}
41+
err := op.List(op.Ctx, podList, listOptions)
42+
return podList, err
43+
}
44+
45+
func (op *K8sResourceOps) GetPVC(name string) (*corev1.PersistentVolumeClaim, error) {
46+
pvc := &corev1.PersistentVolumeClaim{}
47+
err := op.Get(op.Ctx, types.NamespacedName{Name: name, Namespace: op.Namespace}, pvc)
48+
return pvc, err
49+
}
50+
51+
func (op *K8sResourceOps) ListPVC() (*corev1.PersistentVolumeClaimList, error) {
52+
var listOptions = &client.ListOptions{Namespace: op.Namespace}
53+
pvcList := &corev1.PersistentVolumeClaimList{}
54+
err := op.List(op.Ctx, pvcList, listOptions)
55+
return pvcList, err
56+
}

0 commit comments

Comments
 (0)