Skip to content

Commit fd3a957

Browse files
Merge pull request #584 from diakovnec/cleanup_way
adding beforeStartCleanUp logic to cleanup console pods
2 parents c090c68 + 63ec59a commit fd3a957

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

cmd/ocm-backplane/console/console.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ func (o *consoleOptions) run(cmd *cobra.Command, argv []string) error {
249249
if err != nil {
250250
return err
251251
}
252-
252+
//Perform a cleanup before starting a new console
253+
err = o.beforeStartCleanUp(ce)
254+
if err != nil {
255+
return err
256+
}
253257
done := make(chan bool)
254258
errs := make(chan error)
255259

@@ -644,6 +648,35 @@ func (o *consoleOptions) printURL() error {
644648
return nil
645649
}
646650

651+
func (o *consoleOptions) beforeStartCleanUp(ce containerEngineInterface) error {
652+
clusterID, err := getClusterID()
653+
if err != nil {
654+
return fmt.Errorf("error getting cluster ID: %v", err)
655+
}
656+
containersToCleanUp := []string{
657+
fmt.Sprintf("monitoring-plugin-%s", clusterID),
658+
fmt.Sprintf("console-%s", clusterID),
659+
}
660+
661+
logger.Infoln("Starting initial cleanup of containers")
662+
663+
for _, c := range containersToCleanUp {
664+
exist, err := ce.containerIsExist(c)
665+
if err != nil {
666+
return fmt.Errorf("failed to check if container %s exists: %v", c, err)
667+
}
668+
if exist {
669+
err := ce.stopContainer(c)
670+
if err != nil {
671+
return fmt.Errorf("failed to stop container %s during the cleanup process: %v", c, err)
672+
}
673+
} else {
674+
logger.Infof("Container %s does not exist, no need to clean up", c)
675+
}
676+
}
677+
return nil
678+
}
679+
647680
func (o *consoleOptions) cleanUp(ce containerEngineInterface) error {
648681
clusterID, err := getClusterID()
649682
if err != nil {

cmd/ocm-backplane/console/console_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,36 @@ var _ = Describe("console command", func() {
113113
err = utils.CreateTempKubeConfig(&testKubeCfg)
114114
Expect(err).To(BeNil())
115115
}
116+
Context("Should perform existing pods cleanup before starting the console", func() {
117+
It("Should not return an error if no pods are found", func() {
118+
setupConfig()
119+
createPathPodman()
120+
o := consoleOptions{}
121+
ce, err := o.getContainerEngineImpl()
122+
Expect(err).To(BeNil())
123+
err = o.beforeStartCleanUp(ce)
124+
Expect(err).To(BeNil())
125+
})
126+
127+
It("Should perform verification if console and monitoring plugin containers are present", func() {
128+
setupConfig()
129+
createPathPodman()
130+
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(nil, nil).AnyTimes()
131+
o := consoleOptions{}
132+
ce, err := o.getContainerEngineImpl()
133+
Expect(err).To(BeNil())
134+
capturedCommands = nil
135+
136+
err = o.beforeStartCleanUp(ce)
137+
Expect(err).To(BeNil())
138+
139+
Expect(len(capturedCommands)).To(Equal(2))
140+
Expect(capturedCommands[0][0]).To(Equal("podman"))
141+
Expect(capturedCommands[0][1]).To(Equal("ps"))
142+
Expect(capturedCommands[1][0]).To(Equal("podman"))
143+
Expect(capturedCommands[1][1]).To(Equal("ps"))
144+
})
145+
})
116146

117147
Context("when console command executes", func() {
118148
It("should read the openbrowser variable from environment variables and it is true", func() {

0 commit comments

Comments
 (0)