Skip to content

Commit

Permalink
Merge pull request #128 from idsulik/issue-123
Browse files Browse the repository at this point in the history
feat: filter pods by labels
  • Loading branch information
omrikiei committed Jun 17, 2024
2 parents 86046b8 + 930e28f commit f6d0eb6
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pkg/k8s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ import (
)

const (
Image = "docker.io/omrieival/ktunnel"
kubeConfigEnvVar = "KUBECONFIG"
Image = "docker.io/omrieival/ktunnel"
kubeConfigEnvVar = "KUBECONFIG"
deploymentNameLabel = "app.kubernetes.io/name"
deploymentInstanceLabel = "app.kubernetes.io/instance"
)

type ByCreationTime []apiv1.Pod
Expand Down Expand Up @@ -93,10 +95,13 @@ func getClients(namespace *string, kubeCtx *string) {
})
}

func getAllPods(namespace, kubeCtx *string) (*apiv1.PodList, error) {
func getPodsFilteredByLabel(namespace, kubeCtx, labelSelector *string) (*apiv1.PodList, error) {
getClients(namespace, kubeCtx)
// TODO: filter pod list
pods, err := podsClient.List(context.Background(), metav1.ListOptions{})
pods, err := podsClient.List(
context.Background(), metav1.ListOptions{
LabelSelector: *labelSelector,
},
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -154,8 +159,8 @@ func newContainer(port int, image string, containerPorts []apiv1.ContainerPort,

func newDeployment(namespace, name string, port int, image string, ports []apiv1.ContainerPort, selector map[string]string, deploymentLabels map[string]string, deploymentAnnotations map[string]string, cert, key string, cpuReq, cpuLimit, memReq, memLimit int64) *appsv1.Deployment {
replicas := int32(1)
deploymentLabels["app.kubernetes.io/name"] = name
deploymentLabels["app.kubernetes.io/instance"] = name
deploymentLabels[deploymentNameLabel] = name
deploymentLabels[deploymentInstanceLabel] = name
co := newContainer(port, image, ports, cert, key, cpuReq, cpuLimit, memReq, memLimit)
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{},
Expand Down Expand Up @@ -211,19 +216,20 @@ func newService(namespace, name string, ports []apiv1.ServicePort, serviceType a
}

func getPodNames(namespace, deploymentName *string, podsPtr *[]string, kubeCtx *string) error {
allPods, err := getAllPods(namespace, kubeCtx)
labelSelector := deploymentNameLabel + "=" + *deploymentName + "," + deploymentInstanceLabel + "=" + *deploymentName
filteredPods, err := getPodsFilteredByLabel(namespace, kubeCtx, &labelSelector)
if err != nil {
return err
}
pods := *podsPtr
matchingPods := ByCreationTime{}
pIndex := 0
for _, p := range allPods.Items {
for _, p := range filteredPods.Items {
if pIndex >= len(pods) {
log.Info("All pods located for port-forwarding")
break
}
if strings.HasPrefix(p.Name, *deploymentName) && p.Status.Phase == apiv1.PodRunning {
if p.Status.Phase == apiv1.PodRunning {
matchingPods = append(matchingPods, p)
}
}
Expand Down

0 comments on commit f6d0eb6

Please sign in to comment.