Thread
#prefect-community
    m

    Marwan Sarieddine

    2 years ago
    Question regarding the kubernetes agent - is their a programmatic way to “remove”/“de-register” an agent from the cloud ? I would have expected to do so using the
    prefect agent
    command in the cli …
    Kyle Moon-Wright

    Kyle Moon-Wright

    2 years ago
    Hello @Marwan Sarieddine! You can delete an agent programmatically using the GraqhQL API call with a mutation like this:
    mutation {
      delete_agent(input: <agent_id>) {
        success,
        error
      }
    }
    m

    Marwan Sarieddine

    2 years ago
    Thanks @Kyle Moon-Wright! - was hoping not to write a graphql call but I guess if that’s the only way ….
    Will Milner

    Will Milner

    2 years ago
    you can call kubectl delete pod <POD_ID> as well
    Kyle Moon-Wright

    Kyle Moon-Wright

    2 years ago
    Thanks @Will Milner - there’s definitely the K8s side of the equation as well, though I think deleting the pod will just stop the Agent from polling Cloud and not fully de-register it.
    Will Milner

    Will Milner

    2 years ago
    good point
    m

    Marwan Sarieddine

    2 years ago
    @Kyle Moon-Wright - sorry completely unfamiliar with graphql - to create the query - I will have to spin up a graphql server ?
    nicholas

    nicholas

    2 years ago
    @Marwan Sarieddine nope not at all! There are a few ways you can execute a graphql query, let me pull up some code rq
    m

    Marwan Sarieddine

    2 years ago
    Sorry scratch that - I guess I should send a request to`https://api.prefect.io/graphql` somehow ?
    @nicholas - thank you - would really appreciate a guide on this - I guess the docs assume a familiarity with graphql
    nicholas

    nicholas

    2 years ago
    Yup, that's correct, there's a method in
    prefect.Client
    that I think will make that pretty easy, one sec
    Kyle Moon-Wright

    Kyle Moon-Wright

    2 years ago
    @nicholas something like this?:
    import prefect
    client = prefect.Client()
    
    client.graphql(
        mutation {
          delete_agent(input:<agent_id>) {
            success,
            error
          }
        }
    )
    nicholas

    nicholas

    2 years ago
    You'll want to pass in the token and query args to that, but yeah! So like this:
    client.graphql(
        query="mutation { delete_agent(input: agent_id) { success } }", token=api_token
    )
    (the token is assuming
    client.access_token
    doesn't exist)
    m

    Marwan Sarieddine

    2 years ago
    I see - gotcha ! - thank you @nicholas @Kyle Moon-Wright
    sorry last question @nicholas - I am currently replacing the
    agent_id
    with a string - but I am getting an error message -
    Expected type delete_agent_input!, found \"dxxxxxxx"
    (x’s for hashing out the actual id … )
    nicholas

    nicholas

    2 years ago
    No worries at all, we're here to help!
    Sorry, there was an error in the mutation above, it should be this:
    mutation {
      delete_agent(input: { agent_id: <your agent_id> }) {
        success
      }
    }
    m

    Marwan Sarieddine

    2 years ago
    ah gotcha - success !
    thanks again
    nicholas

    nicholas

    2 years ago
    Happy to help! 😄
    m

    Marwan Sarieddine

    2 years ago
    @nicholas - as a follow up So I run
    prefect agent install kubernetes
    to generate the manifests - but how can I tell the agent id of the agent I just deployed ? I can query the agents to get a list of agent ids - but is there a way to tell which is the id I just created ? the only way I see is to do a diff on the list of agents before and after I deploy - but that’s quite the workaround …
    Kyle Moon-Wright

    Kyle Moon-Wright

    2 years ago
    I believe you can give your agent a name with the
    --name <agent_name>
    tag, so when you query your agents you can discern the latest one a bit easier. Without specifying, the name defaults to
    agent
    , if you’re seeing that in your queries at all.
    m

    Marwan Sarieddine

    2 years ago
    Ah nice - thanks for the tip
    You are right about the name defaulting to
    agent
    - but looks like
    prefect agent install kubernetes
    doesn’t accept a
    --name
    option (at least not in prefect v 0.11.2)
    Usage: prefect agent install [OPTIONS] NAME
    Try 'prefect agent install -h' for help.
    
    Error: no such option: --name  Did you mean --namespace?
    nicholas

    nicholas

    2 years ago
    Ah sorry @Marwan Sarieddine - the
    install
    command doesn't accept the
    --name
    flag, the
    prefect agent start
    command does.
    m

    Marwan Sarieddine

    2 years ago
    ah @nicholas - I see thanks for the clarification
    nicholas

    nicholas

    2 years ago
    Yup sorry about that, we can continue in the newer thread