|
| 1 | +package cri |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + criv1alpha1 "github.com/alibaba/pouch/cri/v1alpha1" |
| 7 | + servicev1alpha1 "github.com/alibaba/pouch/cri/v1alpha1/service" |
| 8 | + criv1alpha2 "github.com/alibaba/pouch/cri/v1alpha2" |
| 9 | + servicev1alpha2 "github.com/alibaba/pouch/cri/v1alpha2/service" |
| 10 | + "github.com/alibaba/pouch/daemon/config" |
| 11 | + "github.com/alibaba/pouch/daemon/mgr" |
| 12 | + |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +// RunCriService start cri service if pouchd is specified with --enable-cri. |
| 17 | +func RunCriService(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, stopCh chan error) { |
| 18 | + var err error |
| 19 | + |
| 20 | + defer func() { |
| 21 | + stopCh <- err |
| 22 | + close(stopCh) |
| 23 | + }() |
| 24 | + if !daemonconfig.IsCriEnabled { |
| 25 | + return |
| 26 | + } |
| 27 | + switch daemonconfig.CriConfig.CriVersion { |
| 28 | + case "v1alpha1": |
| 29 | + err = runv1alpha1(daemonconfig, containerMgr, imageMgr) |
| 30 | + case "v1alpha2": |
| 31 | + err = runv1alpha2(daemonconfig, containerMgr, imageMgr) |
| 32 | + default: |
| 33 | + err = fmt.Errorf("invalid CRI version,failed to start CRI service") |
| 34 | + } |
| 35 | + return |
| 36 | +} |
| 37 | + |
| 38 | +// Start CRI service with CRI version: v1alpha1 |
| 39 | +func runv1alpha1(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr) error { |
| 40 | + logrus.Infof("Start CRI service with CRI version: v1alpha1") |
| 41 | + criMgr, err := criv1alpha1.NewCriManager(daemonconfig, containerMgr, imageMgr) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to get CriManager with error: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + service, err := servicev1alpha1.NewService(daemonconfig, criMgr) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to start CRI service with error: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // TODO: Stop the whole CRI service if any of the critical service exits |
| 52 | + grpcServerCloseCh := make(chan struct{}) |
| 53 | + go func() { |
| 54 | + if err := service.Serve(); err != nil { |
| 55 | + logrus.Errorf("failed to start grpc server: %v", err) |
| 56 | + } |
| 57 | + close(grpcServerCloseCh) |
| 58 | + }() |
| 59 | + |
| 60 | + streamServerCloseCh := make(chan struct{}) |
| 61 | + go func() { |
| 62 | + if err := criMgr.StreamServerStart(); err != nil { |
| 63 | + logrus.Errorf("failed to start stream server: %v", err) |
| 64 | + } |
| 65 | + close(streamServerCloseCh) |
| 66 | + }() |
| 67 | + |
| 68 | + // TODO: refactor it with select |
| 69 | + <-streamServerCloseCh |
| 70 | + logrus.Infof("CRI Stream server stopped") |
| 71 | + <-grpcServerCloseCh |
| 72 | + logrus.Infof("CRI GRPC server stopped") |
| 73 | + |
| 74 | + logrus.Infof("CRI service stopped") |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// Start CRI service with CRI version: v1alpha2 |
| 79 | +func runv1alpha2(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr) error { |
| 80 | + logrus.Infof("Start CRI service with CRI version: v1alpha2") |
| 81 | + criMgr, err := criv1alpha2.NewCriManager(daemonconfig, containerMgr, imageMgr) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to get CriManager with error: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + service, err := servicev1alpha2.NewService(daemonconfig, criMgr) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to start CRI service with error: %v", err) |
| 89 | + } |
| 90 | + // TODO: Stop the whole CRI service if any of the critical service exits |
| 91 | + grpcServerCloseCh := make(chan struct{}) |
| 92 | + go func() { |
| 93 | + if err := service.Serve(); err != nil { |
| 94 | + logrus.Errorf("failed to start grpc server: %v", err) |
| 95 | + } |
| 96 | + close(grpcServerCloseCh) |
| 97 | + }() |
| 98 | + |
| 99 | + streamServerCloseCh := make(chan struct{}) |
| 100 | + go func() { |
| 101 | + if err := criMgr.StreamServerStart(); err != nil { |
| 102 | + logrus.Errorf("failed to start stream server: %v", err) |
| 103 | + } |
| 104 | + close(streamServerCloseCh) |
| 105 | + }() |
| 106 | + // TODO: refactor it with select |
| 107 | + <-streamServerCloseCh |
| 108 | + logrus.Infof("CRI Stream server stopped") |
| 109 | + <-grpcServerCloseCh |
| 110 | + logrus.Infof("CRI GRPC server stopped") |
| 111 | + |
| 112 | + logrus.Infof("CRI service stopped") |
| 113 | + return nil |
| 114 | +} |
0 commit comments