Skip to content

Commit

Permalink
put monitoring plugin determination in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
feichashao committed Jan 29, 2024
1 parent fab2f65 commit bf6d824
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
43 changes: 32 additions & 11 deletions cmd/ocm-backplane/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type consoleOptions struct {
url string
openBrowser bool
enablePlugins bool
needMonitorPlugin bool
monitorPluginPort string
monitorPluginImage string
}
Expand Down Expand Up @@ -217,6 +218,12 @@ func (o *consoleOptions) run(cmd *cobra.Command, argv []string) error {
if err != nil {
return err
}

err = o.determineNeedMonitorPlugin()
if err != nil {
return err
}

err = o.determineMonitorPluginPort()
if err != nil {
return err
Expand Down Expand Up @@ -375,9 +382,20 @@ func (o *consoleOptions) pullConsoleImage(ce containerEngineInterface) error {
return ce.pullImage(o.image)
}

func (o *consoleOptions) determineNeedMonitorPlugin() error {
if isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
logger.Debugln("monitoring plugin is needed")
o.needMonitorPlugin = true
return nil
} else {
logger.Debugln("monitoring plugin is not needed")
o.needMonitorPlugin = false
return nil
}
}

func (o *consoleOptions) determineMonitorPluginPort() error {
// we don't need this for lower than 4.14
if !isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
if !o.needMonitorPlugin {
logger.Debugln("monitoring plugin is not needed, not to assign monitoring plugin port")
return nil
}
Expand All @@ -392,7 +410,7 @@ func (o *consoleOptions) determineMonitorPluginPort() error {

func (o *consoleOptions) determineMonitorPluginImage(config *rest.Config) error {
// we don't need this for lower than 4.14
if !isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
if !o.needMonitorPlugin {
logger.Debugln("monitoring plugin is not needed, not to get monitoring plugin image")
return nil
}
Expand All @@ -410,8 +428,7 @@ func (o *consoleOptions) determineMonitorPluginImage(config *rest.Config) error
}

func (o *consoleOptions) pullMonitorPluginImage(ce containerEngineInterface) error {
// we don't need this for lower than 4.14
if !isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
if !o.needMonitorPlugin {
logger.Debugln("monitoring plugin is not needed, not to pull monitoring plugin image")
return nil
}
Expand Down Expand Up @@ -446,7 +463,7 @@ func (o *consoleOptions) getPlugins() (string, error) {
plugins = append(plugins, consolePlugins...)
}
// monitoring plugin
if isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
if o.needMonitorPlugin {
logger.Debugln("monitoring plugin is needed, adding the monitoring plugin parameter to console container")
plugins = append(plugins, fmt.Sprintf("monitoring-plugin=http://127.0.0.1:%s", o.monitorPluginPort))
}
Expand Down Expand Up @@ -544,8 +561,7 @@ func (o *consoleOptions) runConsoleContainer(ce containerEngineInterface) error
}

func (o *consoleOptions) runMonitorPlugin(ce containerEngineInterface) error {
// we don't need this for lower than 4.14
if !isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
if !o.needMonitorPlugin {
logger.Debugln("monitoring plugin is not needed, not to run monitoring plugin")
return nil
}
Expand Down Expand Up @@ -604,8 +620,8 @@ func (o *consoleOptions) cleanUp(ce containerEngineInterface) error {
containersToCleanUp := []string{}

// forcing order of removal as the order is not deterministic between container engines
if isRunningHigherOrEqualTo(versionForMonitoringPlugin) {
logger.Debugln("monitoring plugin is not needed, no need to clean up monitoring plugin")
if o.needMonitorPlugin {
logger.Debugln("monitoring plugin is needed, need to clean up monitoring plugin first")
containersToCleanUp = append(containersToCleanUp, fmt.Sprintf("monitoring-plugin-%s", clusterID))
}
containersToCleanUp = append(containersToCleanUp, fmt.Sprintf("console-%s", clusterID))
Expand Down Expand Up @@ -823,7 +839,6 @@ func GetConfigDirectory() (string, error) {
return pullSecretConfigDirectory, nil
}

// FIXME: this function are called many times, we should avoid calling OCM api every time.
// isRunningHigherOrEqualTo check if the cluster is running higher or equal to target version
func isRunningHigherOrEqualTo(targetVersionStr string) bool {
var (
Expand Down Expand Up @@ -1166,18 +1181,21 @@ func (ce *podmanLinux) putFileToMount(filename string, content []byte) error {

// Check if file already exists, if it does remove it
if _, err = os.Stat(dstFileName); !os.IsNotExist(err) {
logger.Debugf("remove existing file %s", dstFileName)
err = os.Remove(dstFileName)
if err != nil {
return err
}
}

if err = os.WriteFile(dstFileName, content, 0600); err != nil {
logger.Debugf("wrote file %s", dstFileName)
return err
}

// change permission as a work around to gosec
if err = os.Chmod(dstFileName, 0640); err != nil {
logger.Debugf("change permission to 0640 for %s", dstFileName)
return err
}

Expand Down Expand Up @@ -1213,18 +1231,21 @@ func dockerPutFileToMount(filename string, content []byte) error {

// Check if file already exists, if it does remove it
if _, err = os.Stat(dstFileName); !os.IsNotExist(err) {
logger.Debugf("remove existing file %s", dstFileName)
err = os.Remove(dstFileName)
if err != nil {
return err
}
}

if err = os.WriteFile(dstFileName, content, 0600); err != nil {
logger.Debugf("wrote file %s", dstFileName)
return err
}

// change permission as a work around to gosec
if err = os.Chmod(dstFileName, 0644); err != nil {
logger.Debugf("change permission to 0644 for %s", dstFileName)
return err
}

Expand Down
20 changes: 16 additions & 4 deletions cmd/ocm-backplane/console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ var _ = Describe("console command", func() {
setupConfig()
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
o := consoleOptions{}
err := o.determineMonitorPluginPort()
err := o.determineNeedMonitorPlugin()
Expect(err).To(BeNil())
err = o.determineMonitorPluginPort()
Expect(err).To(BeNil())
Expect(len(o.monitorPluginPort)).To(Equal(0))
})
Expand All @@ -185,7 +187,9 @@ var _ = Describe("console command", func() {
setupConfig()
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
o := consoleOptions{}
err := o.determineMonitorPluginImage(nil)
err := o.determineNeedMonitorPlugin()
Expect(err).To(BeNil())
err = o.determineMonitorPluginImage(nil)
Expect(err).To(BeNil())
Expect(len(o.monitorPluginImage)).To(Equal(0))
})
Expand All @@ -194,6 +198,8 @@ var _ = Describe("console command", func() {
setupConfig()
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
o := consoleOptions{}
err := o.determineNeedMonitorPlugin()
Expect(err).To(BeNil())
plugins, err := o.getPlugins()
Expect(err).To(BeNil())
Expect(plugins).ToNot(ContainSubstring("monitoring-plugin"))
Expand All @@ -212,7 +218,9 @@ var _ = Describe("console command", func() {
setupConfig()
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
o := consoleOptions{}
err := o.determineMonitorPluginPort()
err := o.determineNeedMonitorPlugin()
Expect(err).To(BeNil())
err = o.determineMonitorPluginPort()
Expect(err).To(BeNil())
Expect(len(o.monitorPluginPort)).ToNot(Equal(0))
})
Expand All @@ -239,7 +247,9 @@ var _ = Describe("console command", func() {
setupConfig()
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
o := consoleOptions{}
err := o.determineMonitorPluginImage(nil)
err := o.determineNeedMonitorPlugin()
Expect(err).To(BeNil())
err = o.determineMonitorPluginImage(nil)
Expect(err).To(BeNil())
Expect(o.monitorPluginImage).To(Equal("testrepo.com/test/monitorplugin:latest"))
})
Expand All @@ -248,6 +258,8 @@ var _ = Describe("console command", func() {
setupConfig()
mockOcmInterface.EXPECT().GetClusterInfoByID(clusterID).Return(clusterInfo, nil).AnyTimes()
o := consoleOptions{}
err := o.determineNeedMonitorPlugin()
Expect(err).To(BeNil())
plugins, err := o.getPlugins()
Expect(err).To(BeNil())
Expect(plugins).To(ContainSubstring("monitoring-plugin"))
Expand Down

0 comments on commit bf6d824

Please sign in to comment.