From 1b21c142f1c4026a7d299fd2beded9a118bb4df5 Mon Sep 17 00:00:00 2001 From: pb Date: Fri, 25 Apr 2025 15:42:21 +0200 Subject: [PATCH] added a step to connect the monitord container to the 'oc' network --- daemons/execute_monitor_container.go | 28 +++++++++++++++++++++------- main.go | 13 ++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/daemons/execute_monitor_container.go b/daemons/execute_monitor_container.go index 57b5642..c643f33 100644 --- a/daemons/execute_monitor_container.go +++ b/daemons/execute_monitor_container.go @@ -70,11 +70,11 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) { var containerID string imageName := "oc-monitord" - url := "http://" + conf.GetConfig().KubeHost + ":2375" + url := "http://" + cm.KubeHost + ":2375" resp, err := http.Get(url + "/images/" + imageName + "/json") if err != nil { - l.Fatal().Msg("Error when contacting the docker API on " + conf.GetConfig().KubeHost + ": " + err.Error()) + l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error()) } if resp.StatusCode != http.StatusOK { @@ -92,7 +92,7 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) { r.Header.Add("Content-Type","application/json") resp, err = http.DefaultClient.Do(r) if err != nil { - l.Fatal().Msg("Error when contacting the docker API on " + conf.GetConfig().KubeHost + ": " + err.Error()) + l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error()) } if resp.StatusCode == 201 { @@ -111,18 +111,32 @@ func (cm *ContainerMonitor) LaunchMonitor(args []string, l zerolog.Logger) { containerID = d["Id"].(string) } else { d, _ := io.ReadAll(resp.Body) - l.Fatal().Msg("Error when creating the container on " + conf.GetConfig().KubeHost + "\n " + string(d)) + l.Fatal().Msg("Error when creating the container on " + url + "\n " + string(d)) + } + + networkName := "oc" + + dataNetwork, _ := json.Marshal(map[string]string{"Container" : containerID}) + r, _ = http.NewRequest("POST",url + "/networks/" + networkName + "/connect", bytes.NewBuffer(dataNetwork)) + r.Header.Add("Content-Type","application/json") + resp, err = http.DefaultClient.Do(r) + if err != nil { + l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error()) + } + if resp.StatusCode != 200 { + d, _ := io.ReadAll(resp.Body) + l.Error().Msg("Error when adding container to the network : " + string(d)) + return } - resp, err = http.Post( url + "/containers/" + containerID + "/start", "", nil) if err != nil { - l.Fatal().Msg("Error when contacting the docker API on " + conf.GetConfig().KubeHost + ": " + err.Error()) + l.Fatal().Msg("Error when contacting the docker API on " + url + ": " + err.Error()) } if resp.StatusCode >= 300 { d, _ := io.ReadAll(resp.Body) - l.Fatal().Msg("Error when starting the container on " + conf.GetConfig().KubeHost + "\n " + string(d)) + l.Fatal().Msg("Error when starting the container on " + url + "\n " + string(d)) } l.Info().Msg("Started container " + containerID) diff --git a/main.go b/main.go index 9b0a36f..05d956d 100644 --- a/main.go +++ b/main.go @@ -27,12 +27,15 @@ func main() { conf.GetConfig().NatsUrl = c.NATSUrl conf.GetConfig().LokiUrl = c.LokiUrl conf.GetConfig().Mode = o.GetStringDefault("MODE", "") - conf.GetConfig().KubeHost = o.GetStringDefault("KUBERNETES_SERVICE_HOST", "") - conf.GetConfig().KubePort = o.GetStringDefault("KUBERNETES_SERVICE_PORT", "6443") - conf.GetConfig().KubeCA = o.GetStringDefault("KUBE_CA", "") - conf.GetConfig().KubeCert = o.GetStringDefault("KUBE_CERT", "") - conf.GetConfig().KubeData = o.GetStringDefault("KUBE_DATA", "") + if conf.GetConfig().Mode == "container"{ + conf.GetConfig().KubeHost = o.GetStringDefault("KUBERNETES_SERVICE_HOST", "") + conf.GetConfig().KubePort = o.GetStringDefault("KUBERNETES_SERVICE_PORT", "6443") + + conf.GetConfig().KubeCA = o.GetStringDefault("KUBE_CA", "") + conf.GetConfig().KubeCert = o.GetStringDefault("KUBE_CERT", "") + conf.GetConfig().KubeData = o.GetStringDefault("KUBE_DATA", "") + } // Test if oc-monitor binary is reachable // For local executions