diff --git a/cmd/watch.go b/cmd/watch.go index a7ed46548..fc1d49644 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -113,6 +113,7 @@ func pollCollectorSetStatus(conn *grpc.ClientConn) (bool, error) { case <-timeout: return false, fmt.Errorf("Timeout waiting for collectors to become available") case <-ticker.C: + log.Debugf("Checking collectors status") ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500) defer cancel() req := &healthpb.HealthCheckRequest{ @@ -126,6 +127,7 @@ func pollCollectorSetStatus(conn *grpc.ClientConn) (bool, error) { if healthCheckResponse.GetStatus() == healthpb.HealthCheckResponse_SERVING { return true, nil } + log.Debugf("The collectors are not ready: %d", healthCheckResponse.GetStatus()) } } } diff --git a/pkg/device/device.go b/pkg/device/device.go index b3f1a5704..1dc85cd0a 100644 --- a/pkg/device/device.go +++ b/pkg/device/device.go @@ -40,9 +40,9 @@ func buildDevice(c *config.Config, client api.CollectorSetControllerClient, opti reply, err := client.CollectorID(context.Background(), &api.CollectorIDRequest{}) if err != nil { - log.Printf("Failed to get collector ID: %v", err) + log.Errorf("Failed to get collector ID: %v", err) } else { - log.Printf("Using collector ID %d for %q", reply.Id, device.DisplayName) + log.Infof("Using collector ID %d for %q", reply.Id, device.DisplayName) device.PreferredCollectorId = reply.Id } @@ -100,7 +100,7 @@ func (m *Manager) UpdateAndReplaceByDisplayName(name string, options ...types.De } if d == nil { - log.Printf("Could not find device %q", name) + log.Warnf("Could not find device %q", name) return nil, nil } diff --git a/pkg/sync/initsyncer.go b/pkg/sync/initsyncer.go index e7fc0dbd7..043849110 100644 --- a/pkg/sync/initsyncer.go +++ b/pkg/sync/initsyncer.go @@ -66,8 +66,8 @@ func (i *InitSyncer) InitSync() { // wait the init sync processes finishing wg.Wait() - log.Infof("Finish syncing the resource devices") } + log.Infof("Finished syncing the resource devices") } func (i *InitSyncer) intSyncNodes(parentGroupID int32) { diff --git a/pkg/utilities/utilities.go b/pkg/utilities/utilities.go index 383da4b4f..05f2efb40 100644 --- a/pkg/utilities/utilities.go +++ b/pkg/utilities/utilities.go @@ -34,6 +34,15 @@ func GetLabelByPrefix(prefix string, labels map[string]string) (string, string) // CheckAllErrors is a helper function to deal with the number of possible places that an API call can fail. func CheckAllErrors(restResponse interface{}, apiResponse *logicmonitor.APIResponse, err error) error { + if err != nil { + return fmt.Errorf("[ERROR] %v", err) + } + + if apiResponse.Response != nil && apiResponse.StatusCode != http.StatusOK { + metrics.APIError() + return fmt.Errorf("[API] [%d] %s", apiResponse.StatusCode, apiResponse.Message) + } + var restResponseMessage string var restResponseStatus int64 @@ -62,14 +71,5 @@ func CheckAllErrors(restResponse interface{}, apiResponse *logicmonitor.APIRespo return fmt.Errorf("[REST] [%d] %s", restResponseStatus, restResponseMessage) } - if apiResponse.StatusCode != http.StatusOK { - metrics.APIError() - return fmt.Errorf("[API] [%d] %s", apiResponse.StatusCode, restResponseMessage) - } - - if err != nil { - return fmt.Errorf("[ERROR] %v", err) - } - return nil } diff --git a/pkg/watch/namespace/namespace.go b/pkg/watch/namespace/namespace.go index eed8b8e05..565bf90f2 100644 --- a/pkg/watch/namespace/namespace.go +++ b/pkg/watch/namespace/namespace.go @@ -34,6 +34,7 @@ func (w Watcher) ObjType() runtime.Object { func (w Watcher) AddFunc() func(obj interface{}) { return func(obj interface{}) { namespace := obj.(*v1.Namespace) + log.Debugf("Handling add namespace event: %s", namespace.Name) for name, parentID := range w.DeviceGroups { var appliesTo devicegroup.AppliesToBuilder // Ensure that we are creating namespaces for namespaced resources. @@ -62,7 +63,7 @@ func (w Watcher) AddFunc() func(obj interface{}) { return } - log.Printf("Added namespace %q to %q", namespace.Name, name) + log.Infof("Added namespace %q to %q", namespace.Name, name) } } } @@ -70,6 +71,7 @@ func (w Watcher) AddFunc() func(obj interface{}) { // UpdateFunc is a function that implements the Watcher interface. func (w Watcher) UpdateFunc() func(oldObj, newObj interface{}) { return func(oldObj, newObj interface{}) { + log.Debugf("Ignoring update namespace event") // oldNamespace := oldObj.(*v1.Namespace) // newNamespace := newObj.(*v1.Namespace) } @@ -79,10 +81,12 @@ func (w Watcher) UpdateFunc() func(oldObj, newObj interface{}) { func (w Watcher) DeleteFunc() func(obj interface{}) { return func(obj interface{}) { namespace := obj.(*v1.Namespace) + log.Debugf("Handle deleting namespace event: %s", namespace.Name) + for name, parentID := range w.DeviceGroups { deviceGroup, err := devicegroup.Find(parentID, name, w.LMClient) if err != nil { - log.Printf("Failed to find namespace %s: %v", name, err) + log.Warnf("Failed to find namespace %s: %v", name, err) return } // We should only be returned a device group if it is namespaced. diff --git a/pkg/watch/node/node.go b/pkg/watch/node/node.go index e407c3a78..ca640da78 100644 --- a/pkg/watch/node/node.go +++ b/pkg/watch/node/node.go @@ -43,7 +43,7 @@ func (w *Watcher) AddFunc() func(obj interface{}) { return func(obj interface{}) { node := obj.(*v1.Node) - log.Debugf("received ADD event: %s", node.Name) + log.Debugf("Handling add node event: %s", node.Name) // Require an IP address. if getInternalAddress(node.Status.Addresses) == nil { @@ -59,7 +59,7 @@ func (w *Watcher) UpdateFunc() func(oldObj, newObj interface{}) { old := oldObj.(*v1.Node) new := newObj.(*v1.Node) - log.Debugf("received UPDATE event: %s", old.Name) + log.Debugf("Handling update node event: %s", old.Name) // If the old node does not have an IP, then there is no way we could // have added it to LogicMonitor. Therefore, it must be a new device. @@ -83,7 +83,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) { return func(obj interface{}) { node := obj.(*v1.Node) - log.Debugf("received DELETE event: %s", node.Name) + log.Debugf("Handling delete node event: %s", node.Name) // Delete the node. if w.Config().DeleteDevices { @@ -190,7 +190,21 @@ func (w *Watcher) createRoleDeviceGroup(labels map[string]string) { return } - log.Printf("Added device group for node role %q", role) + log.Infof("Added device group for node role %q", role) +} + +// GetNodesMap implements the getting nodes map info from k8s +func GetNodesMap(k8sClient *kubernetes.Clientset) (map[string]string, error) { + nodesMap := make(map[string]string) + nodeList, err := k8sClient.CoreV1().Nodes().List(metav1.ListOptions{}) + if err != nil || nodeList == nil { + return nil, err + } + for _, nodeInfo := range nodeList.Items { + nodesMap[nodeInfo.Name] = getInternalAddress(nodeInfo.Status.Addresses).Address + } + + return nodesMap, nil } // GetNodesMap implements the getting nodes map info from k8s diff --git a/pkg/watch/pod/pod.go b/pkg/watch/pod/pod.go index 81a7ae6e8..857fbbd08 100644 --- a/pkg/watch/pod/pod.go +++ b/pkg/watch/pod/pod.go @@ -37,7 +37,7 @@ func (w *Watcher) AddFunc() func(obj interface{}) { return func(obj interface{}) { pod := obj.(*v1.Pod) - log.Debugf("received ADD event: %s", pod.Name) + log.Debugf("Handling add pod event: %s", pod.Name) // Require an IP address. if pod.Status.PodIP == "" { @@ -53,7 +53,7 @@ func (w *Watcher) UpdateFunc() func(oldObj, newObj interface{}) { old := oldObj.(*v1.Pod) new := newObj.(*v1.Pod) - log.Debugf("received UPDATE event: %s", old.Name) + log.Debugf("Handling update pod event: %s", old.Name) // If the old pod does not have an IP, then there is no way we could // have added it to LogicMonitor. Therefore, it must be a new w. @@ -83,7 +83,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) { return func(obj interface{}) { pod := obj.(*v1.Pod) - log.Debugf("received DELETE event: %s", pod.Name) + log.Debugf("Handling delete pod event: %s", pod.Name) // Delete the pod. if w.Config().DeleteDevices {