@@ -30,9 +30,10 @@ const podConfigFilename = "pod.json"
3030const cntrConfigFilename = "container.json"
3131
3232type crictlInfo struct {
33- testArgs containerTestArgs
34- podID string
35- containerID string
33+ testArgs containerTestArgs
34+ podID string
35+ containerID string
36+ sideContainerIDs []string
3637}
3738
3839func newCrictlTool (args containerTestArgs ) * crictlInfo {
@@ -133,6 +134,39 @@ func crictlNewContainerConfig(path string, a containerTestArgs) (string, error)
133134 return absContConf , nil
134135}
135136
137+ // crictlNewSideContainerConfig writes a container config for a side container,
138+ // named after it so multiple side containers don't collide on disk.
139+ func crictlNewSideContainerConfig (path string , sc sideContainer ) (string , error ) {
140+ var mounts []* criruntimeapi.Mount
141+ for _ , vol := range sc .Volumes {
142+ mounts = append (mounts , & criruntimeapi.Mount {
143+ ContainerPath : vol .Dest ,
144+ HostPath : vol .Source ,
145+ Readonly : false ,
146+ })
147+ }
148+ containerConfig := criruntimeapi.ContainerConfig {
149+ Metadata : & criruntimeapi.ContainerMetadata {
150+ Name : sc .Name ,
151+ },
152+ Image : & criruntimeapi.ImageSpec {
153+ Image : sc .Image ,
154+ },
155+ Command : strings .Fields (sc .Cli ),
156+ Mounts : mounts ,
157+ }
158+ cc , err := json .MarshalIndent (& containerConfig , "" , " " )
159+ if err != nil {
160+ return "" , fmt .Errorf ("Failed to marshal side container config: %v" , err )
161+ }
162+ absConf := filepath .Join (path , sc .Name + ".json" )
163+ err = writeToFile (absConf , string (cc ))
164+ if err != nil {
165+ return "" , fmt .Errorf ("Failed to write side container config: %v" , err )
166+ }
167+ return absConf , nil
168+ }
169+
136170func (i * crictlInfo ) Name () string {
137171 return crictlName
138172}
@@ -201,14 +235,56 @@ func (i *crictlInfo) createContainer() (string, error) {
201235 return commonCmdExec (cmdBase )
202236}
203237
238+ // startSideContainers starts every side container declared on the test
239+ // case as an extra container in the SAME pod as the primary container.
240+ func (i * crictlInfo ) startSideContainers () error {
241+ if len (i .testArgs .SideContainers ) == 0 {
242+ return nil
243+ }
244+
245+ cwd , err := os .Getwd ()
246+ if err != nil {
247+ return fmt .Errorf ("Failed to get CWD to write side container configs: %v" , err )
248+ }
249+ absPodConf := filepath .Join (cwd , podConfigFilename )
250+
251+ for _ , sc := range i .testArgs .SideContainers {
252+ if sc .NetMode != sideContainerNetModeShared {
253+ return fmt .Errorf ("crictl does not support side container network mode %q: crictl has no named/user-defined network, only pod-shared networking (sideContainerNetModeShared)" , sc .NetMode )
254+ }
255+
256+ absSideConf , err := crictlNewSideContainerConfig (cwd , sc )
257+ if err != nil {
258+ return err
259+ }
260+
261+ cmdBase := crictlName + " create " + i .podID + " " + absSideConf + " " + absPodConf
262+ cID , err := commonCmdExec (cmdBase )
263+ if err != nil {
264+ return fmt .Errorf ("failed to create side container %s: %s -- %v" , sc .Name , cID , err )
265+ }
266+ if output , err := commonCmdExec (crictlName + " start " + cID ); err != nil {
267+ return fmt .Errorf ("failed to start side container %s: %s -- %v" , sc .Name , output , err )
268+ }
269+ i .sideContainerIDs = append (i .sideContainerIDs , cID )
270+ }
271+ return nil
272+ }
273+
204274func (i * crictlInfo ) startContainer (bool ) (string , error ) {
275+ if err := i .startSideContainers (); err != nil {
276+ return "" , err
277+ }
205278 cmdBase := crictlName
206279 cmdBase += " start "
207280 cmdBase += i .containerID
208281 return commonCmdExec (cmdBase )
209282}
210283
211284func (i * crictlInfo ) runContainer (bool ) (string , error ) {
285+ if err := i .startSideContainers (); err != nil {
286+ return "" , err
287+ }
212288 cwd , err := os .Getwd ()
213289 if err != nil {
214290 return "" , fmt .Errorf ("Failed to get CWD to write Container/Pod config: %v" , err )
@@ -234,6 +310,10 @@ func (i *crictlInfo) runContainer(bool) (string, error) {
234310}
235311
236312func (i * crictlInfo ) stopContainer () error {
313+ if err := commonStopSideContainers (crictlName , i .sideContainerIDs ); err != nil {
314+ return err
315+ }
316+
237317 output , err := commonStopContainer (crictlName , i .containerID )
238318 err = checkExpectedOut (i .containerID , output , err )
239319 if err != nil {
@@ -257,6 +337,10 @@ func (i *crictlInfo) stopPod() error {
257337}
258338
259339func (i * crictlInfo ) rmContainer () error {
340+ if err := commonRmSideContainers (crictlName , i .sideContainerIDs ); err != nil {
341+ return err
342+ }
343+
260344 output , err := commonRmContainer (crictlName , i .containerID )
261345 err = checkExpectedOut (i .containerID , output , err )
262346 if err != nil {
@@ -272,6 +356,13 @@ func (i *crictlInfo) rmContainer() error {
272356 if err != nil {
273357 return fmt .Errorf ("Could not remove container config file: %v" , err )
274358 }
359+
360+ for _ , sc := range i .testArgs .SideContainers {
361+ absSideConf := filepath .Join (cwd , sc .Name + ".json" )
362+ if err := os .Remove (absSideConf ); err != nil {
363+ return fmt .Errorf ("Could not remove side container %s config file: %v" , sc .Name , err )
364+ }
365+ }
275366 return nil
276367}
277368
0 commit comments