diff options
Diffstat (limited to 'arthur')
| -rw-r--r-- | arthur/exts/error_handler/error_handler.py | 6 | ||||
| -rw-r--r-- | arthur/exts/kubernetes/certificates.py | 42 | ||||
| -rw-r--r-- | arthur/exts/kubernetes/deployments.py | 53 | ||||
| -rw-r--r-- | arthur/exts/kubernetes/nodes.py | 49 | ||||
| -rw-r--r-- | arthur/utils.py | 16 | 
5 files changed, 82 insertions, 84 deletions
| diff --git a/arthur/exts/error_handler/error_handler.py b/arthur/exts/error_handler/error_handler.py index 09653f8..2a61f40 100644 --- a/arthur/exts/error_handler/error_handler.py +++ b/arthur/exts/error_handler/error_handler.py @@ -4,7 +4,7 @@ from discord.ext import commands  from discord.ext.commands import Cog  from arthur.bot import KingArthur -from arthur.utils import generate_error_embed +from arthur.utils import generate_error_message  class ErrorHandler(Cog): @@ -38,7 +38,7 @@ class ErrorHandler(Cog):          elif isinstance(error, commands.CommandInvokeError):              await self._add_error_reaction(ctx.message)              await ctx.send( -                embed=generate_error_embed( +                generate_error_message(                      description=(                          f"Command raised an error: `{error.original.__class__.__name__}:"                          f" {error.original}`" @@ -47,7 +47,7 @@ class ErrorHandler(Cog):              )          else:              await ctx.send( -                embed=generate_error_embed( +                generate_error_message(                      description=(                          f"Unknown exception occurred: `{error.__class__.__name__}:"                          f" {error}`" diff --git a/arthur/exts/kubernetes/certificates.py b/arthur/exts/kubernetes/certificates.py index bb08655..951a968 100644 --- a/arthur/exts/kubernetes/certificates.py +++ b/arthur/exts/kubernetes/certificates.py @@ -1,13 +1,11 @@  """The Certificates cog helps with managing TLS certificates.""" -from datetime import datetime  from textwrap import dedent -from discord import Embed  from discord.ext import commands +from tabulate import tabulate  from arthur.apis.kubernetes import certificates  from arthur.bot import KingArthur -from arthur.utils import datetime_to_discord  class Certificates(commands.Cog): @@ -26,28 +24,32 @@ class Certificates(commands.Cog):          """List TLS certificates in the selected namespace (defaults to default)."""          certs = await certificates.list_certificates(namespace) -        return_embed = Embed(title=f"Certificates in namespace {namespace}") +        table_data = []          for certificate in certs["items"]: -            expiry = datetime.fromisoformat( -                certificate["status"]["notAfter"].rstrip("Z") + "+00:00" -            ) -            renews = datetime.fromisoformat( -                certificate["status"]["renewalTime"].rstrip("Z") + "+00:00" -            ) -            body = dedent( -                f""" -                **Subjects:** {", ".join(certificate["spec"]["dnsNames"])} -                **Issuer:** {certificate["spec"]["issuerRef"]["name"]} -                **Status:** {certificate["status"]["conditions"][0]["message"]} -                **Expires:** {datetime_to_discord(expiry)} ({datetime_to_discord(expiry, "R")}) -                **Renews:** {datetime_to_discord(renews)} ({datetime_to_discord(renews, "R")}) -                """ +            table_data.append( +                [ +                    certificate["metadata"]["name"], +                    ", ".join(certificate["spec"]["dnsNames"]), +                    certificate["spec"]["issuerRef"]["name"], +                    certificate["status"]["conditions"][0]["message"], +                ]              ) -            return_embed.add_field(name=certificate["metadata"]["name"], value=body.strip()) +        table = tabulate( +            table_data, headers=["Name", "DNS Names", "Issuer", "Status"], tablefmt="psql" +        ) + +        return_message = dedent( +            f""" +            **Certificates in namespace `{namespace}`** +            ``` +            {table} +            ``` +            """ +        ) -        await ctx.send(embed=return_embed) +        await ctx.send(return_message)  def setup(bot: KingArthur) -> None: diff --git a/arthur/exts/kubernetes/deployments.py b/arthur/exts/kubernetes/deployments.py index 92deacc..a12442a 100644 --- a/arthur/exts/kubernetes/deployments.py +++ b/arthur/exts/kubernetes/deployments.py @@ -1,7 +1,7 @@  """The Deployments cog helps with managing Kubernetes deployments."""  import asyncio +from textwrap import dedent -from discord import Colour, Embed  from discord.ext import commands  from discord_components.component import ActionRow, Button, ButtonStyle  from kubernetes_asyncio.client.models import V1Deployment @@ -10,7 +10,7 @@ from tabulate import tabulate  from arthur.apis.kubernetes import deployments  from arthur.bot import KingArthur -from arthur.utils import generate_error_embed +from arthur.utils import generate_error_message  def deployment_to_emote(deployment: V1Deployment) -> str: @@ -67,23 +67,22 @@ class Deployments(commands.Cog):              colalign=("center", "left", "center"),          ) -        return_embed = Embed( -            title=f"Deployments in namespace {namespace}", description=f"```\n{table}\n```" +        return_message = dedent( +            f""" +            **Deployments in namespace `{namespace}`** +            ``` +            {table} +            ``` +            """          ) -        await ctx.send(embed=return_embed) +        await ctx.send(return_message)      @deployments.command(name="restart", aliases=["redeploy"])      async def deployments_restart(          self, ctx: commands.Context, deployment: str, namespace: str = "default"      ) -> None:          """Restart the specified deployment in the selected namespace (defaults to default).""" -        confirm_embed = Embed( -            title="Confirm redeployment", -            description=f"Confirm you want to redeploy `{deployment}` in namespace `{namespace}`.", -            colour=Colour.orange(), -        ) -          components = ActionRow(              Button(                  label="Redeploy", @@ -98,7 +97,7 @@ class Deployments(commands.Cog):          )          msg = await ctx.send( -            embed=confirm_embed, +            f":warning: Please confirm you want to restart `deploy/{deployment}` in `{namespace}`",              components=[components],          ) @@ -111,25 +110,20 @@ class Deployments(commands.Cog):              )          except asyncio.TimeoutError:              return await msg.edit( -                embed=Embed( +                generate_error_message(                      title="What is the airspeed velocity of an unladen swallow?",                      description=(                          "Whatever the answer may be, it's certainly "                          "faster than you could select a confirmation option."                      ), -                    colour=Colour.greyple(),                  ),                  components=[],              )          if interaction.component.custom_id == f"{ctx.message.id}-abort":              await interaction.respond( +                ":x: Redeployment aborted",                  ephemeral=False, -                embed=Embed( -                    title="Deployment aborted", -                    description="The deployment was aborted.", -                    colour=Colour.red(), -                ),              )          else:              try: @@ -137,33 +131,32 @@ class Deployments(commands.Cog):              except ApiException as e:                  if e.status == 404:                      return await interaction.respond( -                        ephemeral=False, -                        embed=generate_error_embed( +                        generate_error_message(                              description="Could not find deployment, check the namespace.",                          ), +                        ephemeral=False                      )                  return await interaction.respond( -                    ephemeral=False, -                    embed=generate_error_embed( +                    generate_error_message(                          description=f"Unexpected error occurred, error code {e.status}"                      ), +                    ephemeral=False                  )              else: -                description = f"Restarted deployment `{deployment}` in namespace `{namespace}`." +                description = ( +                    f":white_check_mark: Restarted deployment " +                    f"`{deployment}` in namespace `{namespace}`." +                )                  await interaction.respond( +                    description,                      ephemeral=False, -                    embed=Embed( -                        title="Redeployed", -                        description=description, -                        colour=Colour.blurple(), -                    ),                  )          for component in components.components:              component.disabled = True -        await msg.edit(embed=confirm_embed, components=[components]) +        await msg.edit(components=[components])  def setup(bot: KingArthur) -> None: diff --git a/arthur/exts/kubernetes/nodes.py b/arthur/exts/kubernetes/nodes.py index b5cfc29..f7ac42e 100644 --- a/arthur/exts/kubernetes/nodes.py +++ b/arthur/exts/kubernetes/nodes.py @@ -1,12 +1,11 @@  """The Nodes cog helps with managing Kubernetes nodes.""" -import textwrap +from textwrap import dedent -from discord import Embed  from discord.ext import commands +from tabulate import tabulate  from arthur.apis.kubernetes import nodes  from arthur.bot import KingArthur -from arthur.utils import datetime_to_discord  class Nodes(commands.Cog): @@ -25,7 +24,7 @@ class Nodes(commands.Cog):          """List Kubernetes nodes in the cluster."""          cluster_nodes = await nodes.list_nodes() -        return_embed = Embed(title="Cluster nodes") +        table_data = []          for node in cluster_nodes.items:              statuses = [] @@ -50,19 +49,27 @@ class Nodes(commands.Cog):              node_creation = node.metadata.creation_timestamp -            return_embed.add_field( -                name=node.metadata.name, -                value=textwrap.dedent( -                    f""" -                    **Status:** {", ".join(statuses)} -                    **Kubernetes version:** {node.status.node_info.kubelet_version} -                    **Created**: {datetime_to_discord(node_creation, "R")} -                    """ -                ), -                inline=False, +            table_data.append( +                [ +                    node.metadata.name, +                    ", ".join(statuses), +                    node.status.node_info.kubelet_version, +                    node_creation, +                ]              ) -        await ctx.send(embed=return_embed) +        table = tabulate(table_data, headers=["Name", "Status", "Kubernetes Version", "Created"]) + +        return_message = dedent( +            f""" +            **Cluster nodes** +            ``` +            {table} +            ``` +            """ +        ) + +        await ctx.send(return_message)      @nodes.command(name="cordon")      async def nodes_cordon(self, ctx: commands.Context, *, node: str) -> None: @@ -74,10 +81,8 @@ class Nodes(commands.Cog):          await nodes.cordon_node(node)          await ctx.send( -            embed=Embed( -                title=f"Cordoned {node}", -                description=f"`{node}` is now cordoned and no pods will be scheduled to it.", -            ) +            f":construction: **Cordoned {node}** `{node}` is now " +            "cordoned and no pods will be scheduled to it."          )      @nodes.command(name="uncordon") @@ -90,10 +95,8 @@ class Nodes(commands.Cog):          await nodes.uncordon_node(node)          await ctx.send( -            embed=Embed( -                title=f"Uncordoned {node}", -                description=f"`{node}` is now uncordoned, future pods may be scheduled to it.", -            ) +            f":construction: **Uncordoned {node}** `{node}` is now " +            "uncordoned, future pods may be scheduled to it."          ) diff --git a/arthur/utils.py b/arthur/utils.py index c81a47a..090abc5 100644 --- a/arthur/utils.py +++ b/arthur/utils.py @@ -2,15 +2,15 @@  from datetime import datetime -from discord import Embed -from discord.colour import Colour - -def generate_error_embed( -    *, title: str = "'Tis but a scratch!", description: str = "An error occurred" -) -> Embed: -    """Generate an error embed to return to Discord.""" -    return Embed(title=title, description=description, colour=Colour.red()) +def generate_error_message( +    *, +    title: str = "'Tis but a scratch!", +    description: str = "An error occurred", +    emote: str = ":no_entry_sign:", +) -> str: +    """Generate an error message to return to Discord.""" +    return f"{emote} **{description}** {description}"  def datetime_to_discord(time: datetime, format: str = "f") -> str: | 
