ConnectDaemons connect cri plugin and containerd, and initialize the clients.
()
| 87 | |
| 88 | // ConnectDaemons connect cri plugin and containerd, and initialize the clients. |
| 89 | func ConnectDaemons() error { |
| 90 | fail := func(format string, args ...any) error { |
| 91 | err := fmt.Errorf(format, args...) |
| 92 | if cleanupErr := DisconnectDaemons(); cleanupErr != nil { |
| 93 | return errors.Join(err, cleanupErr) |
| 94 | } |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | var err error |
| 99 | runtimeService, err = remote.NewRuntimeService(*criEndpoint, timeout) |
| 100 | if err != nil { |
| 101 | return fail("failed to create runtime service: %w", err) |
| 102 | } |
| 103 | runtimeService2, err = remote.NewRuntimeService(*criEndpoint, timeout) |
| 104 | if err != nil { |
| 105 | return fail("failed to create runtime service: %w", err) |
| 106 | } |
| 107 | imageService, err = remote.NewImageService(*criEndpoint, timeout) |
| 108 | if err != nil { |
| 109 | return fail("failed to create image service: %w", err) |
| 110 | } |
| 111 | // Since CRI grpc client doesn't have `WithBlock` specified, we |
| 112 | // need to check whether it is actually connected. |
| 113 | // TODO(#6069) Use grpc options to block on connect and remove for this list containers request. |
| 114 | _, err = runtimeService.ListContainers(&runtime.ContainerFilter{}) |
| 115 | if err != nil { |
| 116 | return fail("failed to list containers: %w", err) |
| 117 | } |
| 118 | _, err = runtimeService2.ListContainers(&runtime.ContainerFilter{}) |
| 119 | if err != nil { |
| 120 | return fail("failed to list containers: %w", err) |
| 121 | } |
| 122 | _, err = imageService.ListImages(&runtime.ImageFilter{}) |
| 123 | if err != nil { |
| 124 | return fail("failed to list images: %w", err) |
| 125 | } |
| 126 | // containerdEndpoint is the same with criEndpoint now |
| 127 | containerdEndpoint = strings.TrimPrefix(*criEndpoint, "unix://") |
| 128 | containerdEndpoint = strings.TrimPrefix(containerdEndpoint, "npipe:") |
| 129 | containerdClient, err = containerd.New(containerdEndpoint, containerd.WithDefaultNamespace(k8sNamespace)) |
| 130 | if err != nil { |
| 131 | return fail("failed to connect containerd: %w", err) |
| 132 | } |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | // DisconnectDaemons tears down the shared CRI and containerd clients. |
| 137 | func DisconnectDaemons() error { |
no test coverage detected
searching dependent graphs…