github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/infra/tunnel/pods.go (about) 1 package tunnel 2 3 import ( 4 "context" 5 6 "github.com/containers/podman/v2/libpod/define" 7 "github.com/containers/podman/v2/pkg/bindings/pods" 8 "github.com/containers/podman/v2/pkg/domain/entities" 9 "github.com/containers/podman/v2/pkg/specgen" 10 "github.com/containers/podman/v2/pkg/util" 11 "github.com/pkg/errors" 12 ) 13 14 func (ic *ContainerEngine) PodExists(ctx context.Context, nameOrID string) (*entities.BoolReport, error) { 15 exists, err := pods.Exists(ic.ClientCxt, nameOrID) 16 return &entities.BoolReport{Value: exists}, err 17 } 18 19 func (ic *ContainerEngine) PodKill(ctx context.Context, namesOrIds []string, options entities.PodKillOptions) ([]*entities.PodKillReport, error) { 20 _, err := util.ParseSignal(options.Signal) 21 if err != nil { 22 return nil, err 23 } 24 25 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 26 if err != nil { 27 return nil, err 28 } 29 reports := make([]*entities.PodKillReport, 0, len(foundPods)) 30 for _, p := range foundPods { 31 response, err := pods.Kill(ic.ClientCxt, p.Id, &options.Signal) 32 if err != nil { 33 report := entities.PodKillReport{ 34 Errs: []error{err}, 35 Id: p.Id, 36 } 37 reports = append(reports, &report) 38 continue 39 } 40 reports = append(reports, response) 41 } 42 return reports, nil 43 } 44 45 func (ic *ContainerEngine) PodPause(ctx context.Context, namesOrIds []string, options entities.PodPauseOptions) ([]*entities.PodPauseReport, error) { 46 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 47 if err != nil { 48 return nil, err 49 } 50 reports := make([]*entities.PodPauseReport, 0, len(foundPods)) 51 for _, p := range foundPods { 52 response, err := pods.Pause(ic.ClientCxt, p.Id) 53 if err != nil { 54 report := entities.PodPauseReport{ 55 Errs: []error{err}, 56 Id: p.Id, 57 } 58 reports = append(reports, &report) 59 continue 60 } 61 reports = append(reports, response) 62 } 63 return reports, nil 64 } 65 66 func (ic *ContainerEngine) PodUnpause(ctx context.Context, namesOrIds []string, options entities.PodunpauseOptions) ([]*entities.PodUnpauseReport, error) { 67 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 68 if err != nil { 69 return nil, err 70 } 71 reports := make([]*entities.PodUnpauseReport, 0, len(foundPods)) 72 for _, p := range foundPods { 73 response, err := pods.Unpause(ic.ClientCxt, p.Id) 74 if err != nil { 75 report := entities.PodUnpauseReport{ 76 Errs: []error{err}, 77 Id: p.Id, 78 } 79 reports = append(reports, &report) 80 continue 81 } 82 reports = append(reports, response) 83 } 84 return reports, nil 85 } 86 87 func (ic *ContainerEngine) PodStop(ctx context.Context, namesOrIds []string, options entities.PodStopOptions) ([]*entities.PodStopReport, error) { 88 timeout := -1 89 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 90 if err != nil && !(options.Ignore && errors.Cause(err) == define.ErrNoSuchPod) { 91 return nil, err 92 } 93 if options.Timeout != -1 { 94 timeout = options.Timeout 95 } 96 reports := make([]*entities.PodStopReport, 0, len(foundPods)) 97 for _, p := range foundPods { 98 response, err := pods.Stop(ic.ClientCxt, p.Id, &timeout) 99 if err != nil { 100 report := entities.PodStopReport{ 101 Errs: []error{err}, 102 Id: p.Id, 103 } 104 reports = append(reports, &report) 105 continue 106 } 107 reports = append(reports, response) 108 } 109 return reports, nil 110 } 111 112 func (ic *ContainerEngine) PodRestart(ctx context.Context, namesOrIds []string, options entities.PodRestartOptions) ([]*entities.PodRestartReport, error) { 113 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 114 if err != nil { 115 return nil, err 116 } 117 reports := make([]*entities.PodRestartReport, 0, len(foundPods)) 118 for _, p := range foundPods { 119 response, err := pods.Restart(ic.ClientCxt, p.Id) 120 if err != nil { 121 report := entities.PodRestartReport{ 122 Errs: []error{err}, 123 Id: p.Id, 124 } 125 reports = append(reports, &report) 126 continue 127 } 128 reports = append(reports, response) 129 } 130 return reports, nil 131 } 132 133 func (ic *ContainerEngine) PodStart(ctx context.Context, namesOrIds []string, options entities.PodStartOptions) ([]*entities.PodStartReport, error) { 134 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 135 if err != nil { 136 return nil, err 137 } 138 reports := make([]*entities.PodStartReport, 0, len(foundPods)) 139 for _, p := range foundPods { 140 response, err := pods.Start(ic.ClientCxt, p.Id) 141 if err != nil { 142 report := entities.PodStartReport{ 143 Errs: []error{err}, 144 Id: p.Id, 145 } 146 reports = append(reports, &report) 147 continue 148 } 149 reports = append(reports, response) 150 } 151 return reports, nil 152 } 153 154 func (ic *ContainerEngine) PodRm(ctx context.Context, namesOrIds []string, options entities.PodRmOptions) ([]*entities.PodRmReport, error) { 155 foundPods, err := getPodsByContext(ic.ClientCxt, options.All, namesOrIds) 156 if err != nil && !(options.Ignore && errors.Cause(err) == define.ErrNoSuchPod) { 157 return nil, err 158 } 159 reports := make([]*entities.PodRmReport, 0, len(foundPods)) 160 for _, p := range foundPods { 161 response, err := pods.Remove(ic.ClientCxt, p.Id, &options.Force) 162 if err != nil { 163 report := entities.PodRmReport{ 164 Err: err, 165 Id: p.Id, 166 } 167 reports = append(reports, &report) 168 continue 169 } 170 reports = append(reports, response) 171 } 172 return reports, nil 173 } 174 175 func (ic *ContainerEngine) PodPrune(ctx context.Context, opts entities.PodPruneOptions) ([]*entities.PodPruneReport, error) { 176 return pods.Prune(ic.ClientCxt) 177 } 178 179 func (ic *ContainerEngine) PodCreate(ctx context.Context, opts entities.PodCreateOptions) (*entities.PodCreateReport, error) { 180 podSpec := specgen.NewPodSpecGenerator() 181 opts.ToPodSpecGen(podSpec) 182 return pods.CreatePodFromSpec(ic.ClientCxt, podSpec) 183 } 184 185 func (ic *ContainerEngine) PodTop(ctx context.Context, options entities.PodTopOptions) (*entities.StringSliceReport, error) { 186 switch { 187 case options.Latest: 188 return nil, errors.New("latest is not supported") 189 case options.NameOrID == "": 190 return nil, errors.New("NameOrID must be specified") 191 } 192 193 topOutput, err := pods.Top(ic.ClientCxt, options.NameOrID, options.Descriptors) 194 if err != nil { 195 return nil, err 196 } 197 return &entities.StringSliceReport{Value: topOutput}, nil 198 } 199 200 func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOptions) ([]*entities.ListPodsReport, error) { 201 return pods.List(ic.ClientCxt, options.Filters) 202 } 203 204 func (ic *ContainerEngine) PodInspect(ctx context.Context, options entities.PodInspectOptions) (*entities.PodInspectReport, error) { 205 switch { 206 case options.Latest: 207 return nil, errors.New("latest is not supported") 208 case options.NameOrID == "": 209 return nil, errors.New("NameOrID must be specified") 210 } 211 return pods.Inspect(ic.ClientCxt, options.NameOrID) 212 } 213 214 func (ic *ContainerEngine) PodStats(ctx context.Context, namesOrIds []string, options entities.PodStatsOptions) ([]*entities.PodStatsReport, error) { 215 return pods.Stats(ic.ClientCxt, namesOrIds, options) 216 }