Exemple #1
0
def create_fn(body, **kwargs):

    # The all-purpose function for the vent creation.
    kopf.event(body,
               type="SomeType",
               reason="SomeReason",
               message="Some message")

    # The shortcuts for the conventional events and common cases.
    kopf.info(body, reason="SomeReason", message="Some message")
    kopf.warn(body, reason="SomeReason", message="Some message")
    try:
        raise RuntimeError("Exception text.")
    except:
        kopf.exception(body, reason="SomeReason", message="Some exception:")
Exemple #2
0
def handler(spec, name, **kwargs):
    logger.debug("Event: %s",kwargs['event'])
    action=kwargs["event"]
    try:
        if action == 'create':
            logger.info("Creating new Grafana dashboard: %s", name)
            kopf.info(
                spec, reason="CreatingDashboard", message="Creating new grafana-dashboard."
            )
            logger.debug("Got the following keyword args for creating the object: %s", kwargs)

            resp = create_or_update_handler(spec, name, **kwargs)

            kopf.info(
                spec,reason="CreatedDashboard",message=("Finished creating dashboard " "at %s." % resp["grafana_url"])
            )
            logger.info("Finished creating Grafana dashboard: %s", name)            
        else:
            logger.info("Updating existing Grafana dashboard object: %s", name)
            kopf.info(
                spec, reason="UpdatingDashboard", message="Updating Grafana dashboard."
            )
            logger.debug("Got the following keyword args for updating the object: %s", kwargs) 

            resp = create_or_update_handler(spec, name, **kwargs)

            kopf.info(
                spec,reason="UpdatedDashboard",message="Finished updating Grafana dashboard: %s." % name
            )
            logger.info("Finished updating Grafana dashboard: %s", name)
            
        return resp
        
    except Exception as e:
        logger.error(
            (
                "Failed to %s Grafana dashboard due to the "
                "following exception: %s"
            ),
            action,
            e,
        )
        kopf.exception(
            spec,
            reason="APIError",
            message=("Failed to %s dashboard due to API " "error: %s",action, e),
        )
        raise kopf.PermanentError("Failed creating the dashboard")
Exemple #3
0
def delete_handler(spec, name, body, **kwargs):
    logger.info("Deleting Grafana dashboard: %s", name)
    kopf.info(spec, reason="DeletingDashboard", message="Deleting grafana dashboard.")
    logger.debug("Got the following keyword args for deleting the object: %s", kwargs)

    # Fetch the uid / try deleting the dashboard only if the object creation
    # was successful earlier
    if "status" in body:
        status = body["status"]
        if status.get("handler"):
            uid = body["status"]["handler"]["uid"]

            try:
                legend_config = load_legend_config()
                status = delete_dashboard(legend_config, uid)
                kopf.info(
                    spec,
                    reason="DeletedDashboard",
                    message="Finished deleting dashboard:  %s." % name,
                )
                logger.info("Finished deleting Grafana dashboard: %s", name)
                return {"status": status}
            except Exception as e:
                logger.error(
                    (
                        "Failed to delete dashboard due to the following "
                        "exception: %s"
                    ),
                    e,
                )
                kopf.exception(
                    spec,
                    reason="APIError",
                    message=("Failed to delete dashboard due to API " "error: %s" % e),
                )
                raise kopf.PermanentError("Failed deleting the dashboard")
    else:
        kopf.info(
            spec,
            reason="DeletedDashboard",
            message="Finished deleting dashboard:  %s." % name,
        )
        logger.info("Finished deleting Grafana dashboard: %s", name)
        return {"status": "Deleted"}
Exemple #4
0
def update_handler(spec, name, **kwargs):
    logger.info("Updating existing Grafana dashboard object: %s", name)
    kopf.info(spec,
              reason="UpdatingDashboard",
              message="Updating Grafana dashboard.")
    logger.debug("Got the following keyword args for udpating the object: %s",
                 kwargs)

    try:
        create_or_update_handler(spec, name, **kwargs)
        kopf.info(spec,
                  reason="UpdatedDashboard",
                  message="Finished updating Grafana dashboard: %s." % name)
        logger.info("Finished updating Grafana dashboard: %s", name)
    except Exception as e:
        logger.error(("Failed to update Grafana dashboard due to the "
                      "following exception: %s"), e)
        kopf.exception(spec,
                       reason="APIError",
                       message=("Failed to update dashboard due to API "
                                "error: %s" % e))
        raise kopf.PermanentError("Failed creating the dashboard")
def patch_namespaced_secret(name, namespace, patch):
    try:
        client = kubernetes.client.CoreV1Api()
        client.patch_namespaced_secret(
            name=name,
            namespace=namespace,
            body=patch
        )

        kopf.info(
            patch,
            reason="UPDATED",
            message=f"Updated secret {name} in namespace {namespace}"
        )

    except ApiException as e:
        message = f"Failed to patch secret {name}: {e.reason} | {e.status}"
        kopf.exception(
            patch,
            reason=e.reason,
            message=message
        )
        raise kopf.PermanentError(message)
def create_namespaced_secret(instrumentation_key, name, namespace):
    try:
        client = kubernetes.client.CoreV1Api()
        body = parse_secret_template(name, instrumentation_key)
        client.create_namespaced_secret(
            namespace=namespace,
            body=body
        )

        kopf.info(
            body,
            reason="CREATED",
            message=f"Created secret {name} in namespace {namespace}")

    except ApiException as e:
        message = f"Failed to create secret {name}: {e.reason} | {e.status}"
        kopf.exception(
            body,
            reason=e.reason,
            message=message
        )

        raise kopf.PermanentError(message)
Exemple #7
0
def create_handler(spec, name, **kwargs):
    logger.info("Creating new Grafana dashboard: %s", name)
    kopf.info(spec,
              reason="CreatingDashboard",
              message="Creating new grafana-dashboard.")
    logger.debug("Got the following keyword args for creating the object: %s",
                 kwargs)

    try:
        resp = create_or_update_handler(spec, name, **kwargs)
        kopf.info(spec,
                  reason="CreatedDashboard",
                  message=("Finished creating dashboard "
                           "at %s." % resp["grafana_url"]))
        logger.info("Finished creating Grafana dashboard: %s", name)
        return resp
    except Exception as e:
        logger.error(("Failed to create Grafana dashboard due to the "
                      "following exception: %s"), e)
        kopf.exception(spec,
                       reason="APIError",
                       message=("Failed to create dashboard due to API "
                                "error: %s" % e))
        raise kopf.PermanentError("Failed creating the dashboard")