Skip to content
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

[POA-2609] Add k8s and CRI sub-modules and function interfaces #66

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

mudit-postman
Copy link
Contributor

@mudit-postman mudit-postman commented Jan 28, 2025

In this PR, we created basic interfaces and functions in K8s and CRI modules.

K8s Functions:

  • NewKubeClient: Initalize the kube client, create a clientset object and open a event watcher for events.
  • TearDown: Function to be called on program shutdown to close event watcher.
  • GetPodContainerImages: Will be used to check if agent is running as sidecar
  • GetMainContainerUUID: Fetches container UUID, used by CRI module to inspect container
  • GetPodsInNode: Get the list of pods running in the node agent is running. Will be used to get list of existing pods on agent start up
  • GetPodStatus: Get the health status, used by the go routine which will track health of pods where apidump is running.

CRI Functions:

  • NewCRIClient: Initialize the runtimeService client, opens a GRPC connection to the CRI endpoint
  • GetNetworkNamespace: Fetch network namespace for given container ID
  • GetEnvVars: Fetch env vars for given container ID

Note: This PR have some other functions which were created initially but later removed in implementation PRs.

@mudit-postman mudit-postman marked this pull request as ready for review February 3, 2025 09:14
@mudit-postman
Copy link
Contributor Author

mudit-postman commented Feb 3, 2025

For testing I have created a project which calls all the functions in a flow.
Output

root@mj-test:~/test# go run main.go postman-insights-agent-kxx85
[INFO] Pod Container Images: [public.ecr.aws/postman/postman-insights-agent:preview]
[INFO] Main Container UUID: <uuid>
[INFO] Pods in Node: [<nodes>]
[INFO] Pod Status: Running
[INFO] Stopping event watcher
[INFO] No CRI endpoint provided, trying default endpoints
[INFO] Network Namespace: <string>
[INFO] Environment Variables: <map[string]string>
root@mj-test:~/test#
Test code `main.go`
package main

import (
	"fmt"
	"os"

	"test-proj/cri_apis"
	"test-proj/kube_apis"

	"github.com/postmanlabs/postman-insights-agent/printer"
)

func k8s_funcs(podName string) (string, error) {
	// Initialize KubeClient
	kubeClient, err := kube_apis.NewKubeClient()
	if err != nil {
		return "", fmt.Errorf("failed to create KubeClient: %v", err)
	}
	defer kubeClient.TearDown()

	// GetPodContainerImages
	podImages, err := kubeClient.GetPodContainerImages(podName)
	if err != nil {
		return "", fmt.Errorf("failed to get pod container images: %v", err)
	}
	printer.Infof("Pod Container Images: %v\n", podImages)

	// GetMainContainerUUID
	containerUUID, err := kubeClient.GetMainContainerUUID(podName)
	if err != nil {
		return "", fmt.Errorf("failed to get main container UUID: %v", err)
	}
	printer.Infof("Main Container UUID: %s\n", containerUUID)

	// GetPodsInNode
	podNames, err := kubeClient.GetPodsInNode(kubeClient.AgentNode)
	if err != nil {
		return containerUUID, fmt.Errorf("failed to get pods in node: %v", err)
	}
	printer.Infof("Pods in Node: %v\n", podNames)

	// GetPodStatus
	podStatus, err := kubeClient.GetPodStatus(podName)
	if err != nil {
		return containerUUID, fmt.Errorf("failed to get pod status: %v", err)
	}
	printer.Infof("Pod Status: %s\n", podStatus)

	return containerUUID, nil
}

func cri_funcs(containerUUID string) error {
	// Initialize CriClient
	criClient, err := cri_apis.NewCRIClient("")
	if err != nil {
		return fmt.Errorf("failed to create CriClient: %v", err)
	}

	// GetNetworkNamespace
	networkNamespace, err := criClient.GetNetworkNamespace(containerUUID)
	if err != nil {
		return fmt.Errorf("failed to get network namespace: %v", err)
	}
	printer.Infof("Network Namespace: %s\n", networkNamespace)

	// GetEnvVars
	envVars, err := criClient.GetEnvVars(containerUUID)
	if err != nil {
		return fmt.Errorf("failed to get environment variables: %v", err)
	}
	printer.Infof("Environment Variables: %v\n", envVars)

	return nil
}

func main() {
	// Check if args are provided
	if len(os.Args) < 2 {
		printer.Errorln("Pod name not provided")
		return
	}
	podName := os.Args[1]

	// Call k8s_funcs
	containerUUID, err := k8s_funcs(podName)
	if err != nil {
		printer.Errorf("Error from k8s_funcs: %v\n", err)
	}

	// Call cri_funcs
	if containerUUID == "" {
		printer.Infoln("Container UUID not found, exiting...")
		return
	}
	err = cri_funcs(containerUUID)
	if err != nil {
		printer.Errorf("Error from cri_funcs: %v\n", err)
	}
}

Copy link
Contributor

@mgritter mgritter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good start, I would consider putting these in a subdirectory.

cri_apis/cri_apis.go Outdated Show resolved Hide resolved
kube_apis/kube_apis.go Outdated Show resolved Hide resolved
mudit-postman and others added 5 commits February 5, 2025 10:45
In this PR, we have implemented K8s API functions for interaction with
K8s from inside the container

Functions implemented:
* NewKubeClient: Initalize the kube client, create a clientset object
and open a event watcher for events.
* TearDown: Function to be called on program shutdown to close event
watcher.
* GetPodContainerImages: Will be used to check if agent is running as
sidecar
* GetMainContainerUUID: Fetches container UUID, used by CRI module to
inspect container
* GetPodsInNode: Get the list of pods running in the node agent is
running. Will be used to get list of existing pods on agent start up
* GetPodStatus: Get the health status, used by the go routine to track
the health of pods where apidump is running.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants