fake_kubelet

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2022 License: MIT Imports: 25 Imported by: 0

README

fake-kubelet

Build Go Report Card GitHub license

This is a fake kubelet. that can simulate any number of nodes and maintain pods on those nodes. It is useful for test control plane.

What's the difference with Kind

Kind is run Kubernetes in Docker that is a real cluster.

fake-kubelet is simulation an nodes of Kubernetes.

There is a fake-k8s here, that can start a cluster using the fake-kubelet simulation nodes.

It can be used as an alternative to Kind in some scenarios where you don’t need to actually run the Pod.

What's the difference with Kubemark

Kubemark is directly implemented with the code of kubelet, replacing the runtime part, except that it does not actually start the container, other behaviors are exactly the same as kubelet, mainly used for Kubernetes own e2e test, simulating a large number of nodes and pods will occupy the same memory as the real scene.

fake-kubelet that only does the minimum work of maintaining nodes and pods, and is very suitable for simulating a large number of nodes and pods for pressure testing on the control plane.

Usage

Deploying the fake-kubelet.

kubectl apply -f https://raw.githubusercontent.com/wzshiming/fake-kubelet/master/deploy.yaml

kubectl get node You will find an fake nodes.

> kubectl get node -o wide
NAME         STATUS   ROLES   AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE    KERNEL-VERSION   CONTAINER-RUNTIME
fake-0       Ready    agent   10s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-1       Ready    agent   10s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-2       Ready    agent   10s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-3       Ready    agent   10s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-4       Ready    agent   10s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>

Deploying an application.

> kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fake-pod
  namespace: default
spec:
  replicas: 10
  selector:
    matchLabels:
      app: fake-pod
  template:
    metadata:
      labels:
        app: fake-pod
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: type
                    operator: In
                    values:
                      - fake-kubelet
      tolerations: # A taints was added to an automatically created Node. You can remove taints of Node or add this tolerations
        - key: "fake-kubelet/provider"
          operator: "Exists"
          effect: "NoSchedule"
      # nodeName: fake-0 # Or direct scheduling to a fake node
      containers:
        - name: fake-pod
          image: fake
EOF

kubectl get pod You will find that it has been started, although the image does not exist.

> kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
fake-pod-78884479b7-52qcx   1/1     Running   0          6s    10.0.0.23   fake-4   <none>           <none>
fake-pod-78884479b7-bd6nk   1/1     Running   0          6s    10.0.0.13   fake-2   <none>           <none>
fake-pod-78884479b7-dqjtn   1/1     Running   0          6s    10.0.0.15   fake-2   <none>           <none>
fake-pod-78884479b7-h2fv6   1/1     Running   0          6s    10.0.0.31   fake-0   <none>           <none>
fake-pod-78884479b7-hc9kd   1/1     Running   0          6s    10.0.0.29   fake-4   <none>           <none>
fake-pod-78884479b7-m4rb8   1/1     Running   0          6s    10.0.0.19   fake-1   <none>           <none>
fake-pod-78884479b7-p9zmn   1/1     Running   0          6s    10.0.0.27   fake-0   <none>           <none>
fake-pod-78884479b7-pmgmf   1/1     Running   0          6s    10.0.0.21   fake-0   <none>           <none>
fake-pod-78884479b7-rzbs2   1/1     Running   0          6s    10.0.0.17   fake-0   <none>           <none>
fake-pod-78884479b7-scsjb   1/1     Running   0          6s    10.0.0.25   fake-1   <none>           <none>

By adding the specified Annotation, the status of Pods can be modified independently.

Modify a container of pod as unready, and you will see the pod is 0/1 of ready.

> kubectl annotate pod fake-pod-78884479b7-52qcx --overwrite fake=custom
pod/fake-pod-78884479b7-52qcx annotated

> kubectl edit pod fake-pod-78884479b7-52qcx --subresource=status

> kubectl get pod fake-pod-78884479b7-52qcx -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
fake-pod-78884479b7-52qcx   0/1     Running   0          6s    10.0.0.23   fake-4   <none>           <none>

Create a custom node, that content can be fully customized. using Kubectl to modify node status must be added --subresource=status.

> kubectl apply -f - <<EOF
apiVersion: v1
kind: Node
metadata:
  annotations:
    node.alpha.kubernetes.io/ttl: "0"
  labels:
    app: fake-kubelet
    beta.kubernetes.io/arch: arm64
    beta.kubernetes.io/os: linux
    kubernetes.io/arch: arm64
    kubernetes.io/hostname: fake-arm-0
    kubernetes.io/os: linux
    kubernetes.io/role: agent
    node-role.kubernetes.io/agent: ""
    type: fake-kubelet # Matches to fake-kubelet's environment variable TAKE_OVER_LABELS_SELECTOR, this node will be taken over by fake-kubelet
  name: fake-arm-0
spec:
  taints: # Avoid scheduling actual running pods to fake Node
    - effect: NoSchedule
      key: fake-kubelet/provider
      value: fake
status:
  allocatable:
    cpu: 32
    memory: 256Gi
    pods: 110
  capacity:
    cpu: 32
    memory: 256Gi
    pods: 110
  nodeInfo:
    architecture: arm64
    bootID: ""
    containerRuntimeVersion: ""
    kernelVersion: ""
    kubeProxyVersion: fake
    kubeletVersion: fake
    machineID: ""
    operatingSystem: linux
    osImage: ""
    systemUUID: ""
  phase: Running
EOF

kubectl get node You will find the newly created fake-arm-0 node, and that it is ready.

> kubectl get node -o wide
NAME         STATUS   ROLES   AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE    KERNEL-VERSION   CONTAINER-RUNTIME
fake-0       Ready    agent   12s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-1       Ready    agent   12s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-2       Ready    agent   12s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-3       Ready    agent   12s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-4       Ready    agent   12s   fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>
fake-arm-0   Ready    agent   2s    fake      10.88.0.136   <none>        <unknown>   <unknown>        <unknown>

License

Licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateSerialNumber added in v0.7.3

func GenerateSerialNumber(n int, minLen int, fun func(string) bool)

Types

type Config added in v0.4.0

type Config struct {
	ClientSet                         kubernetes.Interface
	TakeOverAll                       bool
	TakeOverLabelsSelector            string
	PodCustomStatusAnnotationSelector string
	CIDR                              string
	NodeIP                            string
	Logger                            Logger
	PodStatusTemplate                 string
	NodeTemplate                      string
	NodeInitializationTemplate        string
	NodeHeartbeatTemplate             string
}

type Controller

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

Controller is a fake kubelet implementation that can be used to test

func NewController

func NewController(conf Config) (*Controller, error)

NewController creates a new fake kubelet controller

func (*Controller) CreateNode added in v0.7.3

func (c *Controller) CreateNode(ctx context.Context, nodeName string) error

func (*Controller) Start added in v0.3.4

func (c *Controller) Start(ctx context.Context) error

type Logger added in v0.4.0

type Logger interface {
	Printf(format string, v ...interface{})
}

type NodeController added in v0.5.0

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

NodeController is a fake nodes implementation that can be used to test

func NewNodeController added in v0.5.0

func NewNodeController(conf NodeControllerConfig) (*NodeController, error)

NewNodeController creates a new fake nodes controller

func (*NodeController) CreateNode added in v0.7.3

func (c *NodeController) CreateNode(ctx context.Context, nodeName string) error

CreateNode create a node use node template

func (*NodeController) Has added in v0.5.0

func (c *NodeController) Has(nodeName string) bool

func (*NodeController) KeepNodeHeartbeat added in v0.5.0

func (c *NodeController) KeepNodeHeartbeat(ctx context.Context)

KeepNodeHeartbeat keep node heartbeat

func (*NodeController) ListNodes added in v0.5.0

func (c *NodeController) ListNodes(ctx context.Context, ch chan<- string, opt metav1.ListOptions) error

ListNodes list nodes put into the channel

func (*NodeController) LockNode added in v0.5.0

func (c *NodeController) LockNode(ctx context.Context, nodeName string) (*corev1.Node, error)

LockNode locks a given node

func (*NodeController) LockNodes added in v0.5.0

func (c *NodeController) LockNodes(ctx context.Context, nodes <-chan string)

LockNodes locks a nodes from the channel if they don't exist we create them and then take over them if they exist we take over them

func (*NodeController) Size added in v0.7.3

func (c *NodeController) Size() int

func (*NodeController) Start added in v0.5.0

func (c *NodeController) Start(ctx context.Context) error

Start starts the fake nodes controller It will create and take over the nodes and keep them alive if nodeSelectorFunc is not nil, it will use it to determine if the node should be taken over

func (*NodeController) WatchNodes added in v0.5.0

func (c *NodeController) WatchNodes(ctx context.Context, ch chan<- string, opt metav1.ListOptions) error

WatchNodes watch nodes put into the channel

type NodeControllerConfig added in v0.5.0

type NodeControllerConfig struct {
	ClientSet                  kubernetes.Interface
	NodeSelectorFunc           func(node *corev1.Node) bool
	NodeLabelSelector          string
	LockPodsOnNodeFunc         func(nodeName string) error
	NodeIP                     string
	NodeTemplate               string
	NodeInitializationTemplate string
	NodeHeartbeatTemplate      string
	Logger                     Logger
	NodeHeartbeatInterval      time.Duration
	NodeHeartbeatParallelism   int
	LockNodeParallelism        int
	FuncMap                    template.FuncMap
}

NodeControllerConfig is the configuration for the NodeController

type PodController added in v0.5.0

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

PodController is a fake pods implementation that can be used to test

func NewPodController added in v0.5.0

func NewPodController(conf PodControllerConfig) (*PodController, error)

NewPodController creates a new fake pods controller

func (*PodController) DeletePod added in v0.5.0

func (c *PodController) DeletePod(ctx context.Context, pod *corev1.Pod) error

DeletePod deletes a pod

func (*PodController) DeletePods added in v0.5.0

func (c *PodController) DeletePods(ctx context.Context, pods <-chan *corev1.Pod)

DeletePods deletes pods from the channel

func (*PodController) ListPods added in v0.5.0

func (c *PodController) ListPods(ctx context.Context, ch chan<- *corev1.Pod, opt metav1.ListOptions) error

ListPods list pods put into the channel

func (*PodController) LockPod added in v0.5.0

func (c *PodController) LockPod(ctx context.Context, pod *corev1.Pod) error

LockPod locks a given pod

func (*PodController) LockPods added in v0.5.0

func (c *PodController) LockPods(ctx context.Context, pods <-chan *corev1.Pod)

LockPods locks a pods from the channel

func (*PodController) LockPodsOnNode added in v0.5.0

func (c *PodController) LockPodsOnNode(ctx context.Context, nodeName string) error

LockPodsOnNode locks pods on the node

func (*PodController) Start added in v0.5.0

func (c *PodController) Start(ctx context.Context) error

Start starts the fake pod controller It will modify the pods status to we want

func (*PodController) WatchPods added in v0.5.0

func (c *PodController) WatchPods(ctx context.Context, lockChan, deleteChan chan<- *corev1.Pod, opt metav1.ListOptions) error

WatchPods watch pods put into the channel

type PodControllerConfig added in v0.5.0

type PodControllerConfig struct {
	ClientSet                         kubernetes.Interface
	PodCustomStatusAnnotationSelector string
	NodeIP                            string
	CIDR                              string
	NodeHasFunc                       func(nodeName string) bool
	PodStatusTemplate                 string
	Logger                            Logger
	LockPodParallelism                int
	DeletePodParallelism              int
	FuncMap                           template.FuncMap
}

PodControllerConfig is the configuration for the PodController

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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