workload

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 37 Imported by: 0

README

应用负载

Workloads 说明

k8s job样例

关于临时容器

临时容器 使用 client-go 实现 kubectl debug 命令 Kubectl Debug源码

关于Hold Pod

要让Kubernetes Pod保持运行状态而不退出,可以采取以下几种方法:

  • 使用无限循环:在容器中运行一个无限循环的进程,例如使用while true命令。这样,Pod将一直运行,直到手动终止。
  • 使用sleep命令:在容器中运行一个sleep命令,例如sleep infinity。这将使容器进入休眠状态,但Pod仍然处于运行状态。
  • 使用sidecar容器:在Pod中添加一个额外的sidecar容器,该容器可以保持运行状态并监控主容器。如果主容器退出,sidecar容器可以重新启动它。
  • 使用init容器:在Pod中添加一个init容器,该容器可以在主容器启动之前运行,并保持运行状态。这样,即使主容器退出,Pod仍然可以保持运行状态。

需要注意的是,Kubernetes会自动重新启动失败的Pod,因此如果Pod退出,Kubernetes将尝试重新启动它。如果您希望Pod保持运行状态而不被重新启动,可以使用上述方法之一。

Documentation

Index

Constants

View Source
const (
	ANNOTATION_SECRET_MOUNT    = "secret.mpaas.infraboard.io/mountpath"
	ANNOTATION_CONFIGMAP_MOUNT = "configmap.mpaas.infraboard.io/mountpath"
)

Variables

View Source
var (
	WORKLOAD_KIND_NAME = map[int32]string{
		0: "Deployment",
		1: "StatefulSet",
		2: "DaemonSet",
		3: "Job",
		4: "CronJob",
	}
	WORKLOAD_KIND_VALUE = map[string]int32{
		"Deployment":  0,
		"StatefulSet": 1,
		"DaemonSet":   2,
		"Job":         3,
		"CronJob":     4,
	}
)

Enum value maps for WORKLOAD_KIND.

Functions

func FormatContainerState

func FormatContainerState(stage corev1.ContainerState) string

func GetContainerFromPodSpec

func GetContainerFromPodSpec(spec v1.PodSpec, name string) *v1.Container

func GetContainerFromPodTemplate

func GetContainerFromPodTemplate(temp v1.PodTemplateSpec, name string) *v1.Container

func GetPodContainerStatusByName

func GetPodContainerStatusByName(pod *corev1.Pod, containerName string) *corev1.ContainerStatus

func GetPrimaryContainerName

func GetPrimaryContainerName(spec v1.PodSpec) string

func HoldContaienrCmd

func HoldContaienrCmd(d time.Duration) []string

func InjectContainerEnvVars

func InjectContainerEnvVars(c *v1.Container, envs []v1.EnvVar)

func InjectJobAnnotations

func InjectJobAnnotations(pod *v1.Job, annotations map[string]string)

注入Job注解

func InjectJobLabels

func InjectJobLabels(pod *v1.Job, labels map[string]string)

注入Job标签

func InjectPodConfigMapVolume

func InjectPodConfigMapVolume(pod *v1.PodSpec, cs ...*v1.ConfigMap)

把configmap注入到Pod中 挂载成卷使用

func InjectPodEnvVars

func InjectPodEnvVars(pod *v1.PodSpec, envs []v1.EnvVar)

func InjectPodSecretVolume

func InjectPodSecretVolume(pod *v1.PodSpec, ss ...*v1.Secret)

把secret注入到Pod中 挂载成卷使用

func InjectPodTemplateSpecAnnotations

func InjectPodTemplateSpecAnnotations(pod *v1.PodTemplateSpec, key, value string)

func InjectPodTemplateSpecLabel

func InjectPodTemplateSpecLabel(pod *v1.PodTemplateSpec, key, value string)

func NewConfigMapVolume

func NewConfigMapVolume(cm *v1.ConfigMap) v1.Volume

func NewSecretVolume

func NewSecretVolume(secret *v1.Secret) v1.Volume

func NewVolumeMount

func NewVolumeMount(readonly bool, vs []MountVolume) []v1.VolumeMount

func WaitForContainerRunning

func WaitForContainerRunning(containerName string, printer terminal.Logger) watchtools.ConditionFunc

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewWorkload

func NewWorkload(cs *kubernetes.Clientset, restconf *rest.Config) *Client

func (*Client) CopyPodRun

func (c *Client) CopyPodRun(ctx context.Context, req *CopyPodRunRequest) (*v1.Pod, error)

func (*Client) CreateCronJob

func (b *Client) CreateCronJob(ctx context.Context, job *v1.CronJob) (*v1.CronJob, error)

func (*Client) CreateDaemonSet

func (c *Client) CreateDaemonSet(ctx context.Context, obj *appsv1.DaemonSet) (*appsv1.DaemonSet, error)

func (*Client) CreateDeployment

func (c *Client) CreateDeployment(ctx context.Context, req *appsv1.Deployment) (*appsv1.Deployment, error)

func (*Client) CreateJob

func (b *Client) CreateJob(ctx context.Context, job *v1.Job) (*v1.Job, error)

func (*Client) CreatePod

func (c *Client) CreatePod(ctx context.Context, pod *v1.Pod, req *meta.CreateRequest) (*v1.Pod, error)

func (*Client) CreateStatefulSet

func (c *Client) CreateStatefulSet(ctx context.Context, req *appsv1.StatefulSet) (*appsv1.StatefulSet, error)

func (*Client) DebugPod

func (c *Client) DebugPod(ctx context.Context, req *DebugPodRequest) error

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, wl *WorkLoad) (*WorkLoad, error)

func (*Client) DeleteCronJob

func (b *Client) DeleteCronJob(ctx context.Context, req *meta.DeleteRequest) error

func (*Client) DeleteDaemonSet

func (c *Client) DeleteDaemonSet(ctx context.Context, req *meta.DeleteRequest) error

func (*Client) DeleteDeployment

func (c *Client) DeleteDeployment(ctx context.Context, req *meta.DeleteRequest) error

func (*Client) DeleteJob

func (c *Client) DeleteJob(ctx context.Context, req *meta.DeleteRequest) error

func (*Client) DeletePod

func (c *Client) DeletePod(ctx context.Context, req *meta.DeleteRequest) error

func (*Client) DeleteStatefulSet

func (c *Client) DeleteStatefulSet(ctx context.Context, req *meta.DeleteRequest) error

func (*Client) Get

func (c *Client) Get(ctx context.Context, wl *WorkLoad, req *meta.GetRequest) error

func (*Client) GetCronJob

func (b *Client) GetCronJob(ctx context.Context, req *meta.GetRequest) (*v1.CronJob, error)

func (*Client) GetDaemonSet

func (c *Client) GetDaemonSet(ctx context.Context, req *meta.GetRequest) (*appsv1.DaemonSet, error)

func (*Client) GetDeployment

func (c *Client) GetDeployment(ctx context.Context, req *meta.GetRequest) (*appsv1.Deployment, error)

func (*Client) GetJob

func (b *Client) GetJob(ctx context.Context, req *meta.GetRequest) (*v1.Job, error)

func (*Client) GetPod

func (c *Client) GetPod(ctx context.Context, req *meta.GetRequest) (*v1.Pod, error)

func (*Client) GetStatefulSet

func (c *Client) GetStatefulSet(ctx context.Context, req *meta.GetRequest) (*appsv1.StatefulSet, error)

func (*Client) ListCronJob

func (b *Client) ListCronJob(ctx context.Context, req *meta.ListRequest) (*v1.CronJobList, error)

func (*Client) ListDaemonSet

func (c *Client) ListDaemonSet(ctx context.Context, req *meta.ListRequest) (*appsv1.DaemonSetList, error)

func (*Client) ListDeployment

func (c *Client) ListDeployment(ctx context.Context, req *meta.ListRequest) (*appsv1.DeploymentList, error)

func (*Client) ListJob

func (b *Client) ListJob(ctx context.Context, req *meta.ListRequest) (*v1.JobList, error)

func (*Client) ListPod

func (c *Client) ListPod(ctx context.Context, req *meta.ListRequest) (*v1.PodList, error)

func (*Client) ListStatefulSet

func (c *Client) ListStatefulSet(ctx context.Context, req *meta.ListRequest) (*appsv1.StatefulSetList, error)

func (*Client) LoginContainer

func (c *Client) LoginContainer(ctx context.Context, req *LoginContainerRequest) error

登录容器

func (*Client) ReDeploy

func (c *Client) ReDeploy(ctx context.Context, req *meta.GetRequest) (*appsv1.Deployment, error)

原生并没有重新部署的功能, 通过变更注解时间来触发重新部署 dpObj.Spec.Template.Annotations["cattle.io/timestamp"] = time.Now().Format(time.RFC3339)

func (*Client) Run

func (c *Client) Run(ctx context.Context, wl *WorkLoad) (*WorkLoad, error)

func (*Client) ScaleDeployment

func (c *Client) ScaleDeployment(ctx context.Context, req *meta.ScaleRequest) (*v1.Scale, error)

func (*Client) Update

func (c *Client) Update(ctx context.Context, wl *WorkLoad) (*WorkLoad, error)

func (*Client) UpdateCronJob

func (b *Client) UpdateCronJob(ctx context.Context, job *v1.CronJob) (*v1.CronJob, error)

func (*Client) UpdateDaemonSet

func (c *Client) UpdateDaemonSet(ctx context.Context, obj *appsv1.DaemonSet) (*appsv1.DaemonSet, error)

func (*Client) UpdateDeployment

func (c *Client) UpdateDeployment(ctx context.Context, req *appsv1.Deployment) (*appsv1.Deployment, error)

func (*Client) UpdateJob

func (b *Client) UpdateJob(ctx context.Context, job *v1.Job) (*v1.Job, error)

func (*Client) UpdateStatefulSet

func (c *Client) UpdateStatefulSet(ctx context.Context, req *appsv1.StatefulSet) (*appsv1.StatefulSet, error)

func (*Client) WaitForPodCondition

func (c *Client) WaitForPodCondition(ctx context.Context, req *WaitForContainerRequest) (*corev1.Pod, error)

WaitForPodCondition watches the given pod until the container is running

func (*Client) WatchContainerLog

func (c *Client) WatchContainerLog(ctx context.Context, req *WatchContainerLogRequest) (io.ReadCloser, error)

查看容器日志

func (*Client) WatchDeployment

func (c *Client) WatchDeployment(ctx context.Context, req *appsv1.Deployment) (watch.Interface, error)

type ContainerTerminal

type ContainerTerminal interface {
	io.Reader
	io.Writer
	remotecommand.TerminalSizeQueue
}

type CopyPodRunRequest

type CopyPodRunRequest struct {
	// 需要被Copy的容器
	SourcePod *meta.GetRequest
	// 克隆后的目标容器运行时的其他参数
	TargetPodMeta metav1.ObjectMeta
	// 目标创建容器的创建选项
	TargetPodOpts *meta.CreateRequest
	// 登录目标容器的名称
	ExecContainer string `json:"exec_container"`
	// 用于Hold主Container的命令, 默认 sleep infinity
	ExecHoldCmd []string `json:"exec_hold_cmd"`
	// 登录目标容器的命令
	ExecRunCmd []string `json:"exec_run_cmd"`
	// 是否登录目录容器
	Attach bool `json:"attach"`
	// 当登录终端后,退出终端是否删除Pod
	Remove bool `json:"remove"`
	// 目标容器的优雅关闭时间, 默认30秒
	TerminationGracePeriodSeconds int64
	// 登录终端
	Terminal *terminal.WebSocketTerminal `json:"-"`
}

func NewCopyPodRunRequest

func NewCopyPodRunRequest() *CopyPodRunRequest

func (*CopyPodRunRequest) SetAttachTerminal

func (r *CopyPodRunRequest) SetAttachTerminal(term *terminal.WebSocketTerminal)

func (*CopyPodRunRequest) SetDefaultExecContainer

func (r *CopyPodRunRequest) SetDefaultExecContainer(containerName string)

type DebugPodRequest

type DebugPodRequest struct {
	*meta.GetRequest
	EphemeralContainer corev1.EphemeralContainer   `json:""`
	Excutor            *terminal.WebSocketTerminal `json:"-"`
}

type EnvVarSet

type EnvVarSet struct {
	Items []*v1.EnvVar
}

func NewEnvVarSet

func NewEnvVarSet(envs []v1.EnvVar) *EnvVarSet

func (*EnvVarSet) Add

func (s *EnvVarSet) Add(item *v1.EnvVar)

func (*EnvVarSet) EnvVars

func (s *EnvVarSet) EnvVars() (envs []v1.EnvVar)

func (*EnvVarSet) GetOrNewEnv

func (s *EnvVarSet) GetOrNewEnv(name string) *v1.EnvVar

如果有就返回已有的Env, 如果没有则创建新的Env

func (*EnvVarSet) String

func (s *EnvVarSet) String() string

type EventNotifier

type EventNotifier func(textMsg string)

type LoginContainerRequest

type LoginContainerRequest struct {
	Namespace     string            `json:"namespace" validate:"required"`
	PodName       string            `json:"pod_name" validate:"required"`
	ContainerName string            `json:"container_name"`
	Command       []string          `json:"command"`
	Executor      ContainerTerminal `json:"-"`
}

func NewLoginContainerRequest

func NewLoginContainerRequest(ce ContainerTerminal) *LoginContainerRequest

func (*LoginContainerRequest) String

func (req *LoginContainerRequest) String() string

func (*LoginContainerRequest) Validate

func (req *LoginContainerRequest) Validate() error

type MountVolume

type MountVolume struct {
	Path   string
	Volume v1.Volume
}

func NewMountVolume

func NewMountVolume(v v1.Volume, path string) MountVolume

type POD_STATUS

type POD_STATUS int
const (
	POD_STATUS_PENDDING POD_STATUS = iota
	POD_STATUS_SCHEDULED
	POD_STATUS_INITIALIZED
	POD_STATUS_CONTAINER_READY
	POD_STATUS_POD_READY
)

func GetPodStatus

func GetPodStatus(p *v1.Pod) POD_STATUS

func (POD_STATUS) String

func (s POD_STATUS) String() string

type SystemVaraible

type SystemVaraible struct {
	WorkloadName string `json:"workload_name"`
	Image        string `json:"image"`
}

func NewSystemVaraible

func NewSystemVaraible() *SystemVaraible

func (*SystemVaraible) ImageDetail

func (v *SystemVaraible) ImageDetail() (addr, version string)

type WORKLOAD_KIND

type WORKLOAD_KIND int32
const (
	// Deployment无状态部署
	WORKLOAD_KIND_DEPLOYMENT WORKLOAD_KIND = 0
	// StatefulSet
	WORKLOAD_KIND_STATEFULSET WORKLOAD_KIND = 1
	// DaemonSet
	WORKLOAD_KIND_DAEMONSET WORKLOAD_KIND = 2
	// Job
	WORKLOAD_KIND_JOB WORKLOAD_KIND = 3
	// CronJob
	WORKLOAD_KIND_CRONJOB WORKLOAD_KIND = 4
)

func ParseWorkloadKindFromString

func ParseWorkloadKindFromString(str string) (WORKLOAD_KIND, error)

func (WORKLOAD_KIND) Equal

func (t WORKLOAD_KIND) Equal(target WORKLOAD_KIND) bool

Equal type compare

func (WORKLOAD_KIND) IsIn

func (t WORKLOAD_KIND) IsIn(targets ...WORKLOAD_KIND) bool

IsIn todo

func (WORKLOAD_KIND) MarshalJSON

func (t WORKLOAD_KIND) MarshalJSON() ([]byte, error)

MarshalJSON todo

func (WORKLOAD_KIND) String

func (t WORKLOAD_KIND) String() string

func (*WORKLOAD_KIND) UnmarshalJSON

func (t *WORKLOAD_KIND) UnmarshalJSON(b []byte) error

UnmarshalJSON todo

type WORKLOAD_STAGE

type WORKLOAD_STAGE int32
const (
	// 未处理
	WORKLOAD_STAGE_PENDDING WORKLOAD_STAGE = iota
	// 处理中
	WORKLOAD_STAGE_PROGERESS
	// 正常
	WORKLOAD_STAGE_ACTIVE
	// 异常
	WORKLOAD_STAGE_ERROR
)

type WaitForContainerRequest

type WaitForContainerRequest struct {
	Namespace     string `json:"namespace" validate:"required"`
	PodName       string `json:"pod_name" validate:"required"`
	ContainerName string `json:"container_name"`
	TimoutSecond  int    `json:"timeout_second"`
	ExitCondition watchtools.ConditionFunc
}

type WatchContainerLogRequest

type WatchContainerLogRequest struct {
	Namespace string `json:"namespace" validate:"required"`
	PodName   string `json:"pod_name" validate:"required"`
	*v1.PodLogOptions
}

func NewWatchContainerLogRequest

func NewWatchContainerLogRequest() *WatchContainerLogRequest

func (*WatchContainerLogRequest) Validate

func (req *WatchContainerLogRequest) Validate() error

type WorkLoad

type WorkLoad struct {
	WorkloadKind WORKLOAD_KIND
	Deployment   *appsv1.Deployment
	StatefulSet  *appsv1.StatefulSet
	DaemonSet    *appsv1.DaemonSet
	CronJob      *batchv1.CronJob
	Job          *batchv1.Job
}

func NewWorkLoad

func NewWorkLoad() *WorkLoad

func ParseWorkloadFromYaml

func ParseWorkloadFromYaml(kindStr string, workload string) (w *WorkLoad, err error)

func (*WorkLoad) GetMainContainer

func (w *WorkLoad) GetMainContainer() *v1.Container

约定第一个容器为主容器, 其他容器为辅助容器

func (*WorkLoad) GetMainContainerVersion

func (w *WorkLoad) GetMainContainerVersion() string

获取主容器的镜像版本

func (*WorkLoad) GetObjectMeta

func (w *WorkLoad) GetObjectMeta() *metav1.ObjectMeta

func (*WorkLoad) GetPodTemplateSpec

func (w *WorkLoad) GetPodTemplateSpec() (podSpec *v1.PodTemplateSpec)

func (*WorkLoad) GetServiceContainer

func (w *WorkLoad) GetServiceContainer(serviceName string) *v1.Container

func (*WorkLoad) MustToYaml

func (w *WorkLoad) MustToYaml() string

func (*WorkLoad) SetAnnotations

func (w *WorkLoad) SetAnnotations(key, value string)

func (*WorkLoad) SetDefaultNamespace

func (w *WorkLoad) SetDefaultNamespace(ns string)

func (*WorkLoad) SetMatchLabel

func (w *WorkLoad) SetMatchLabel(k, v string)

func (*WorkLoad) Status

func (w *WorkLoad) Status() *WorkloadStatus

获取负载当前状态

func (*WorkLoad) SystemVaraible

func (w *WorkLoad) SystemVaraible(serviceName string) *SystemVaraible

type WorkloadStatus

type WorkloadStatus struct {
	Stage   WORKLOAD_STAGE
	Reason  string
	Message string
}

func GetCronJobStatus

func GetCronJobStatus(*v1.CronJob) *WorkloadStatus

func GetDaemonSetStatus

func GetDaemonSetStatus(*appsv1.DaemonSet) *WorkloadStatus

func GetDeploymentStatus

func GetDeploymentStatus(obj *appsv1.Deployment) *WorkloadStatus

func GetJobStatus

func GetJobStatus(*v1.Job) *WorkloadStatus

func GetStatefulSetStatus

func GetStatefulSetStatus(obj *appsv1.StatefulSet) *WorkloadStatus

func NewWorklaodStatus

func NewWorklaodStatus() *WorkloadStatus

func (*WorkloadStatus) UpdateDeploymentStatus

func (w *WorkloadStatus) UpdateDeploymentStatus(cond appsv1.DeploymentCondition)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳