Question regarding the kubernetes agent - is their...
# prefect-community
m
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 …
k
Hello @Marwan Sarieddine! You can delete an agent programmatically using the GraqhQL API call with a mutation like this:
Copy code
mutation {
  delete_agent(input: <agent_id>) {
    success,
    error
  }
}
upvote 2
m
Thanks @Kyle Moon-Wright! - was hoping not to write a graphql call but I guess if that’s the only way ….
w
you can call kubectl delete pod <POD_ID> as well
k
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.
👍 1
w
good point
m
@Kyle Moon-Wright - sorry completely unfamiliar with graphql - to create the query - I will have to spin up a graphql server ?
n
@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
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
n
Yup, that's correct, there's a method in
prefect.Client
that I think will make that pretty easy, one sec
k
@nicholas something like this?:
Copy code
import prefect
client = prefect.Client()

client.graphql(
    mutation {
      delete_agent(input:<agent_id>) {
        success,
        error
      }
    }
)
n
You'll want to pass in the token and query args to that, but yeah! So like this:
Copy code
client.graphql(
    query="mutation { delete_agent(input: agent_id) { success } }", token=api_token
)
(the token is assuming
client.access_token
doesn't exist)
m
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 … )
n
No worries at all, we're here to help!
Sorry, there was an error in the mutation above, it should be this:
Copy code
mutation {
  delete_agent(input: { agent_id: <your agent_id> }) {
    success
  }
}
m
ah gotcha - success !
thanks again
n
Happy to help! 😄
👍 1
m
@nicholas - as a follow up So I run
Copy code
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 …
k
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
Ah nice - thanks for the tip
👍 1
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)
Copy code
Usage: prefect agent install [OPTIONS] NAME
Try 'prefect agent install -h' for help.

Error: no such option: --name  Did you mean --namespace?
n
Ah sorry @Marwan Sarieddine - the
install
command doesn't accept the
--name
flag, the
prefect agent start
command does.
m
ah @nicholas - I see thanks for the clarification
n
Yup sorry about that, we can continue in the newer thread