k8s

package
v2.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 11 Imported by: 0

README

goutils/v2/k8s

The k8s package is a collection of utility functions designed to simplify common k8s tasks.


Table of contents


Functions

DefaultJobPodNameGetter.GetJobPodName(context.Context, string)
GetJobPodName(context.Context, string) string, error

GetJobPodName retrieves the name of the first pod associated with a specific Kubernetes job within a given namespace. It uses a label selector to find pods that are labeled with the job's name. This method is typically used in scenarios where jobs create a single pod or when only the first pod is of interest.

Parameters:

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to find pods for. namespace: Namespace where the job and its pods are located.

Returns:

string: The name of the first pod found that is associated with the job error: An error if no pods are found or if an error occurs during the pod retrieval


JobsClient.ApplyKubernetesJob(string, func(string) ([]byte, error))
ApplyKubernetesJob(string, func(string) ([]byte, error)) error

ApplyKubernetesJob applies a Kubernetes job manifest to a Kubernetes cluster using the provided kubeconfig file. The job is applied to the specified namespace.

Parameters:

jobFilePath: Path to the job manifest file to apply. namespace: Namespace where the job should be applied.

Returns:

error: An error if the job could not be applied.


JobsClient.DeleteKubernetesJob(context.Context, string)
DeleteKubernetesJob(context.Context, string) error

DeleteKubernetesJob deletes a specified Kubernetes job within a given namespace. It sets the deletion propagation policy to 'Foreground' to ensure that the delete operation waits until the cascading delete has completed.

Parameters:

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to delete. namespace: Namespace where the job is located.

Returns:

error: An error if the job could not be deleted.


JobsClient.GetJobPodName(context.Context, string)
GetJobPodName(context.Context, string) string, error

GetJobPodName retrieves the name of the first pod associated with a specific Kubernetes job within a given namespace. It uses a label selector to find pods that are labeled with the job's name. This method is typically used in scenarios where jobs create a single pod or when only the first pod is of interest.

Parameters:

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to find pods for. namespace: Namespace where the job and its pods are located.

Returns:

string: The name of the first pod found that is associated with the job error: An error if no pods are found or if an error occurs during the pod retrieval


JobsClient.JobExists(context.Context, string)
JobExists(context.Context, string) bool, error

JobExists checks if a Kubernetes job with the specified name exists within a given namespace.

Parameters:

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to check for existence. namespace: Namespace where the job is located.

Returns:

bool: true if the job exists, false otherwise. error: An error if the job existence check fails.


JobsClient.ListKubernetesJobs(context.Context, string)
ListKubernetesJobs(context.Context, string) []batchv1.Job, error

ListKubernetesJobs lists Kubernetes jobs from a specified namespace, or all namespaces if no namespace is specified. This method allows for either targeted or broad job retrieval.

Parameters:

ctx: Context for managing control flow of the request. namespace: Optional; specifies the namespace from which to list jobs. If empty, jobs will be listed from all namespaces.

Returns:

[]batchv1.Job: A slice of batchv1.Job objects containing the jobs found. error: An error if the API call to fetch the jobs fails.


JobsClient.StreamJobLogs(string)
StreamJobLogs(string) error

StreamJobLogs monitors a Kubernetes job by waiting for it to reach the 'Ready' state and then streams logs from the associated pod.

Parameters:

jobsClient: A JobsClient for managing Kubernetes jobs. workloadName: Name of the Kubernetes job to monitor. namespace: Namespace where the job is located.

Returns:

error: An error if the job monitoring fails.


Installation

To use the goutils/v2/k8s package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/k8s

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/k8s"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/k8s:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultJobPodNameGetter added in v2.2.7

type DefaultJobPodNameGetter struct {
	JC *JobsClient
}

DefaultJobPodNameGetter implements the default behavior for getting job pod names by using the JobsClient to fetch the pod name.

**Attributes:**

JC: A JobsClient for managing Kubernetes jobs.

func (*DefaultJobPodNameGetter) GetJobPodName added in v2.2.7

func (d *DefaultJobPodNameGetter) GetJobPodName(ctx context.Context, jobName, namespace string) (string, error)

GetJobPodName retrieves the name of the first pod associated with a specific Kubernetes job within a given namespace. It uses a label selector to find pods that are labeled with the job's name. This method is typically used in scenarios where jobs create a single pod or when only the first pod is of interest.

**Parameters:**

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to find pods for. namespace: Namespace where the job and its pods are located.

**Returns:**

string: The name of the first pod found that is associated with the job error: An error if no pods are found or if an error occurs during the pod retrieval

type DynK8sInterface added in v2.2.7

type DynK8sInterface interface {
	WaitForResourceState(ctx context.Context, resourceName, namespace, resourceType, desiredState string, checkStatusFunc func(name, namespace string) (bool, error)) error
	GetResourceStatus(ctx context.Context, kc *client.KubernetesClient, resourceName, namespace string, gvr schema.GroupVersionResource) (bool, error)
}

DynK8sInterface defines the methods used from dynK8s to manage Kubernetes resources.

**Methods:**

WaitForResourceState: Waits for a Kubernetes resource to reach a desired state. GetResourceStatus: Retrieves the status of a Kubernetes resource.

type JobPodNameGetter added in v2.2.7

type JobPodNameGetter interface {
	GetJobPodName(ctx context.Context, jobName, namespace string) (string, error)
}

JobPodNameGetter defines the method to get job pod name by job name and namespace.

**Methods:**

GetJobPodName: Retrieves the name of the first pod associated with a specific

type JobsClient

type JobsClient struct {
	Client        *client.KubernetesClient
	DynK8s        DynK8sInterface
	K8sLogger     K8sLoggerInterface
	StreamLogsFn  func(clientset *kubernetes.Clientset, namespace, resourceType, resourceName string) error
	PodNameGetter JobPodNameGetter
}

JobsClient represents a client for managing Kubernetes jobs through the Kubernetes API.

**Attributes:**

Client: A pointer to KubernetesClient for accessing Kubernetes API. DynK8s: A DynK8sInterface for managing Kubernetes resources. K8sLogger: A K8sLoggerInterface for streaming logs from Kubernetes pods. StreamLogsFn: A function for streaming logs from a Kubernetes pod. PodNameGetter: A JobPodNameGetter for getting job pod names.

func (*JobsClient) ApplyKubernetesJob added in v2.2.7

func (jc *JobsClient) ApplyKubernetesJob(jobFilePath, namespace string, readFile func(string) ([]byte, error)) error

ApplyKubernetesJob applies a Kubernetes job manifest to a Kubernetes cluster using the provided kubeconfig file. The job is applied to the specified namespace.

**Parameters:**

jobFilePath: Path to the job manifest file to apply. namespace: Namespace where the job should be applied.

**Returns:**

error: An error if the job could not be applied.

func (*JobsClient) DeleteKubernetesJob

func (jc *JobsClient) DeleteKubernetesJob(ctx context.Context, jobName, namespace string) error

DeleteKubernetesJob deletes a specified Kubernetes job within a given namespace. It sets the deletion propagation policy to 'Foreground' to ensure that the delete operation waits until the cascading delete has completed.

**Parameters:**

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to delete. namespace: Namespace where the job is located.

**Returns:**

error: An error if the job could not be deleted.

func (*JobsClient) GetJobPodName

func (jc *JobsClient) GetJobPodName(ctx context.Context, jobName, namespace string) (string, error)

GetJobPodName retrieves the name of the first pod associated with a specific Kubernetes job within a given namespace. It uses a label selector to find pods that are labeled with the job's name. This method is typically used in scenarios where jobs create a single pod or when only the first pod is of interest.

**Parameters:**

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to find pods for. namespace: Namespace where the job and its pods are located.

**Returns:**

string: The name of the first pod found that is associated with the job error: An error if no pods are found or if an error occurs during the pod retrieval

func (*JobsClient) JobExists

func (jc *JobsClient) JobExists(ctx context.Context, jobName, namespace string) (bool, error)

JobExists checks if a Kubernetes job with the specified name exists within a given namespace.

**Parameters:**

ctx: Context for managing control flow of the request. jobName: Name of the Kubernetes job to check for existence. namespace: Namespace where the job is located.

**Returns:**

bool: true if the job exists, false otherwise. error: An error if the job existence check fails.

func (*JobsClient) ListKubernetesJobs

func (jc *JobsClient) ListKubernetesJobs(ctx context.Context, namespace string) ([]batchv1.Job, error)

ListKubernetesJobs lists Kubernetes jobs from a specified namespace, or all namespaces if no namespace is specified. This method allows for either targeted or broad job retrieval.

**Parameters:**

ctx: Context for managing control flow of the request. namespace: Optional; specifies the namespace from which to list jobs. If empty, jobs will be listed from all namespaces.

**Returns:**

[]batchv1.Job: A slice of batchv1.Job objects containing the jobs found. error: An error if the API call to fetch the jobs fails.

func (*JobsClient) StreamJobLogs added in v2.2.7

func (jc *JobsClient) StreamJobLogs(workloadName, namespace string) error

StreamJobLogs monitors a Kubernetes job by waiting for it to reach the 'Ready' state and then streams logs from the associated pod.

**Parameters:**

jobsClient: A JobsClient for managing Kubernetes jobs. workloadName: Name of the Kubernetes job to monitor. namespace: Namespace where the job is located.

**Returns:**

error: An error if the job monitoring fails.

type K8sLoggerInterface added in v2.2.7

type K8sLoggerInterface interface {
	StreamLogs(clientset kubernetes.Interface, namespace, resourceType, podName string) error
}

K8sLoggerInterface defines the methods used from k8sLogger to stream logs from Kubernetes pods.

**Methods:**

StreamLogs: Streams logs from a Kubernetes pod.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL