(k8sClient kubernetes.Interface, cfg *rest.Config, nodeName string, ptyHandler PtyHandler)
| 333 | } |
| 334 | } |
| 335 | func startNodeShellProcess(k8sClient kubernetes.Interface, cfg *rest.Config, nodeName string, ptyHandler PtyHandler) error { |
| 336 | |
| 337 | /*创建特权容器*/ |
| 338 | containerName := "node-shell" |
| 339 | podName :="node-"+nodeName+"-shell" |
| 340 | namespace :="default" |
| 341 | image := "alpine:latest" |
| 342 | ctx :=context.Background() |
| 343 | |
| 344 | getpod ,err :=k8sClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ |
| 345 | FieldSelector :"metadata.name="+podName+",metadata.namespace="+namespace, |
| 346 | }) |
| 347 | if err != nil { |
| 348 | return err |
| 349 | } |
| 350 | //没有pod则创建 |
| 351 | if len(getpod.Items) == 0 { |
| 352 | container := &v1.Container{ |
| 353 | Name: containerName, |
| 354 | Image: image, |
| 355 | ImagePullPolicy: v1.PullIfNotPresent, |
| 356 | Stdin: true, |
| 357 | StdinOnce: true, |
| 358 | TTY: true, |
| 359 | Command: []string{"/bin/sh"}, |
| 360 | SecurityContext: &v1.SecurityContext{ |
| 361 | Privileged : ptr.To(true), |
| 362 | }, |
| 363 | //挂载hostPath到容器中 |
| 364 | VolumeMounts: []v1.VolumeMount{ |
| 365 | { |
| 366 | Name: "host-root", |
| 367 | MountPath: "/host", |
| 368 | }, |
| 369 | }, |
| 370 | } |
| 371 | |
| 372 | |
| 373 | pod := &v1.Pod{ |
| 374 | ObjectMeta: metav1.ObjectMeta{ |
| 375 | Name: podName, |
| 376 | Namespace: namespace, |
| 377 | }, |
| 378 | Spec: v1.PodSpec{ |
| 379 | Containers: []v1.Container{*container}, |
| 380 | NodeName: nodeName, |
| 381 | RestartPolicy: v1.RestartPolicyNever, |
| 382 | HostNetwork: true, |
| 383 | HostPID: true, |
| 384 | Tolerations: []v1.Toleration{ |
| 385 | |
| 386 | { |
| 387 | Key: "CriticalAddonsOnly", |
| 388 | Operator: v1.TolerationOpExists, |
| 389 | }, |
| 390 | { |
| 391 | Key: "NoExecute", |
| 392 | Operator: v1.TolerationOpExists, |
no test coverage detected