Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controls are not shown in service panel #3107

Closed
Thierry5612 opened this issue Mar 12, 2018 · 5 comments
Closed

Controls are not shown in service panel #3107

Thierry5612 opened this issue Mar 12, 2018 · 5 comments
Labels
bug Broken end user or developer functionality; not working as the developers intended it component/ui Predominantly a front-end issue; most/all of the work can be completed by a f/e developer

Comments

@Thierry5612
Copy link

Thierry5612 commented Mar 12, 2018

Hi,

I'm working with plugins and I'm trying to add custom controls to the panel of services. I don't have any problem to do it on container but the "node-details-controls" web section is not shown for services and I can't see my control icon. (Adding value to monitor in the INFO section does not give any problem.)
Hereafter a sample code to test it, derived and adapted from sample plugins.
I'm not sure if it is a bug or if something is wrong with my code.

(Just need to run kubectl proxy --port=8080 to allow requests.get(url="http://localhost:8080/api/v1/namespaces/default/services") )

#!/usr/bin/env python

from docker import Client
import BaseHTTPServer
import SocketServer
import datetime
import errno
import json
import os
import signal
import socket
import threading
import time
import urllib2
import requests

PLUGIN_ID="service-control"
PLUGIN_UNIX_SOCK="/var/run/scope/plugins/" + PLUGIN_ID + ".sock"
DOCKER_SOCK="unix://var/run/docker.sock"

nodes = {}

def update_loop():
    global nodes
    next_call = time.time()
    while True:
        # Get current timestamp in RFC3339
        timestamp = datetime.datetime.utcnow()
        timestamp = timestamp.isoformat('T') + 'Z'

        # Fetch and convert data to scope data model
        new = {}
        for service in list_services():
            new["%s;<service>" % (service['metadata']['uid'])] = {
                'latestControls': {
                  "ctrl-one" :{
                      "timestamp":timestamp,
                      "value":{
                          "dead":False
                      }
                  }
                },
                'latest': {
                    'test': {
                        'timestamp': timestamp,
                        'value': str(98),
                    }
                }
            }
        print(str(new))
        nodes = new
        next_call += 1
        time.sleep(next_call - time.time())

def start_update_loop():
    updateThread = threading.Thread(target=update_loop)
    updateThread.daemon = True
    updateThread.start()

def list_services():
    services = {}
    response = requests.get(url="http://localhost:8080/api/v1/namespaces/default/services")
    if response.status_code != 200:
        return []
    else:
        if 'items' in response.json():
            return response.json()['items']
        else:
            return []

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        # The logger requires a client_address, but unix sockets don't have
        # one, so we fake it.
        self.client_address = "-"

        # Generate our json body
        body = json.dumps({
            'Plugins': [
                {
                    'id': PLUGIN_ID,
                    'label': 'Service test control',
                    'description': 'Test to add custom controls to service',
                    'interfaces': ['reporter','controller'],
                    'api_version': '1',
                }
            ],
            'Service': {
                'controls':
                {
                    "ctrl-one": {
                        "id": "ctrl-one",
                            "human": "Ctrl One",
                            "icon": "fa-minus",
                            "rank": 10
                    },
                },
                'nodes': nodes,
                # Templates tell the UI how to render this field.
                'metadata_templates': {
                    'test_value': {
                        'id': "test",
                        'label': "## Test",
                        'from': "latest",
                        'priority': 8.1,
                    },
                },
            },
        })
        #print(str(body))
        # Send the headers
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.send_header('Content-Length', len(body))
        self.end_headers()

        # Send the body
        self.wfile.write(body)

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise

def delete_socket_file():
    if os.path.exists(PLUGIN_UNIX_SOCK):
        os.remove(PLUGIN_UNIX_SOCK)

def sig_handler(b, a):
    delete_socket_file()
    exit(0)

def main():
    signal.signal(signal.SIGTERM, sig_handler)
    signal.signal(signal.SIGINT, sig_handler)

    start_update_loop()

    # Ensure the socket directory exists
    mkdir_p(os.path.dirname(PLUGIN_UNIX_SOCK))
    # Remove existing socket in case it was left behind
    delete_socket_file()
    # Listen for connections on the unix socket
    server = SocketServer.UnixStreamServer(PLUGIN_UNIX_SOCK, Handler)
    try:
        server.serve_forever()
    except:
        delete_socket_file()
        raise

main()
@Thierry5612 Thierry5612 changed the title Controls are not shown in services panel Controls are not shown in service panel Mar 12, 2018
@squaremo squaremo added bug Broken end user or developer functionality; not working as the developers intended it component/ui Predominantly a front-end issue; most/all of the work can be completed by a f/e developer labels Mar 14, 2018
@squaremo
Copy link

Thanks for this report! I don't really know how plugins work; but, I wonder if this:

the "node-details-controls" web section is not shown for services

.. is the key bit -- as in the controls fail to be inserted because the element in question just isn't present.

@Thierry5612
Copy link
Author

Hi,

Yes, it is not present by inspecting the html code of the page. But I don"t know if it is supposed to always be there or dynamically added only if extra controls are configured.

@rade
Copy link
Member

rade commented Mar 15, 2018

@foot any idea what's going wrong here?

@rndstr
Copy link
Contributor

rndstr commented Mar 15, 2018

It is not displaying for services due to missing control_probe_id in latest. The app doesn't pass on the control found in the report because it cannot find the probeID. I'll come up with a PR.

@Thierry5612
Copy link
Author

Great, thanks for that quick update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Broken end user or developer functionality; not working as the developers intended it component/ui Predominantly a front-end issue; most/all of the work can be completed by a f/e developer
Projects
None yet
Development

No branches or pull requests

4 participants