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

How do I run kubectl apply-f using the API #1168

Closed
moluzhui opened this issue May 19, 2020 · 21 comments
Closed

How do I run kubectl apply-f using the API #1168

moluzhui opened this issue May 19, 2020 · 21 comments
Labels
kind/documentation Categorizes issue or PR as related to documentation. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@moluzhui
Copy link

I tried using the Kubernetes API python to execute the kubect apply command to deploy and update resources

the kubectl command is as follows:

kubectl apply -f export

export is a folder containing multiple yaml files, which is generated by kompose convert -o export

I only found an alternative API for kubectl create, but I didn't find apply

How do I implement kubectl apply to create resources and possible subsequent updates

If you know, please let me know. thank you in advance!

@moluzhui moluzhui added the kind/documentation Categorizes issue or PR as related to documentation. label May 19, 2020
@Sunnykaby
Copy link

maybe you can use replace_XXX api

@moshevayner
Copy link
Member

@vishnu667
Copy link
Contributor

vishnu667 commented May 21, 2020

@MoShitrit create_from_yaml works when we have to create a resource.
Let's say I want to update an annotation or the image create_from_yaml fails with a FailToCreateError

@vishnu667
Copy link
Contributor

Trying to write a utility replace_from_yaml
But am hitting the error.

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "the name of the object (scheduler-deployment) does not match the name on the URL (airflow)",
  "reason": "BadRequest",
  "code": 400
}

The same payload works with kubectl replace -f not sure what am missing.

@moshevayner
Copy link
Member

Trying to write a utility replace_from_yaml
But am hitting the error.

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "the name of the object (scheduler-deployment) does not match the name on the URL (airflow)",
  "reason": "BadRequest",
  "code": 400
}

The same payload works with kubectl replace -f not sure what am missing.

What's the value of kind from this line, in the error you're getting?
https://github.com/vishnu667/python/blob/replace_from_yaml/kubernetes/utils/replace_from_yaml.py#L156

@vishnu667
Copy link
Contributor

This is the payload Am passing

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment_time: '2020-05-22 12:39:38.324272'
  labels:
    app: scheduler
  name: scheduler-deployment
  namespace: airflow
spec:
  replicas: 1
  selector:
    matchLabels:
      app: scheduler
  template:
    metadata:
      annotations:
        deployment_time: '2020-05-22 12:39:38.324286'
      labels:
        app: scheduler
    spec:
      containers:
      - args:
        - scheduler
        env:
        - name: AIRFLOW__KUBERNETES__NAMESPACE
          value: airflow
        image: airflow_base:local
        imagePullPolicy: IfNotPresent
        name: scheduler
      serviceAccountName: airflow-scheduler-serviceaccount

If you can point me in the right direction I might be able to write down both these functionalities replace and apply.

@moshevayner
Copy link
Member

moshevayner commented May 24, 2020

The payload seems fine. But looking back at your code, I think I see what's missing.
You'll need to add a namespace argument to the call you make here https://github.com/vishnu667/python/blob/replace_from_yaml/kubernetes/utils/replace_from_yaml.py#L157
and in order to make sure you always have a value (which would align with how kubectl does it), you'll need to add an else block here like this:

else:
    namespace = "default"

namespace is a required parameter, and by the error message it seems like the function is trying to use airflow as the name of the deployment, rather than the namespace.
So I would try to be more explicit and see if it helps.

@roycaihw
Copy link
Member

roycaihw commented Jun 8, 2020

kubectl implements a three-way diff logic to perform a client-side apply. We should add example/documentation about how to use the new server-side apply feature instead of re-implementing client-side apply in this client.

@vishnu667
Copy link
Contributor

@roycaihw Can you point me to the kubectl implementation Let me see if I can code the same logic in python.

@vishnu667
Copy link
Contributor

@MoShitrit agree but for the yaml am providing am passing the namespace value.
Will add the else statement also.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 18, 2020
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Nov 3, 2020
@solarmosaic-kflorence
Copy link

solarmosaic-kflorence commented Nov 14, 2020

I'm not sure how people are using the python client without this feature... I'm trying to move our deployment process from command line tools (like kubectl) into a python lambda function and I'm surprised to find there is no easy way to replace a kubectl apply -f with a client call.

@solarmosaic-kflorence
Copy link

@tvkit i'm sure it is, but it doesn't have a lot to do with the apply functionality that kubectl provides. And I'd rather not use python just to call a CLI command... kind of defeats the purpose of using python.

@solarmosaic-kflorence
Copy link

This ticket may be a duplicate of #1093

@fejta-bot
Copy link

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@k8s-ci-robot
Copy link
Contributor

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@adammrozik
Copy link

Hey! Is there any chance this would be implemented in the future?

Not sure how to effectively mimic kubectl apply behaviour with current API

@robcxyz
Copy link

robcxyz commented Apr 4, 2022

@adammrozik - Looks like they just merged an example https://github.com/kubernetes-client/python/pull/1752/files

@Sispheor
Copy link

@adammrozik - Looks like they just merged an example https://github.com/kubernetes-client/python/pull/1752/files

This example actually uses the create_from_directory method. Which does not perform an "apply" and return "Error from server (Conflict)" if objects exists already...

@Pithikos
Copy link

Pithikos commented Jan 7, 2023

I assume this requires a rewrite of kubectl's apply in Python.

The workaround is to pipe input to kubectl

subprocess.run(["kubectl", "apply", "-f", "-"], input=substituted, encoding='utf-8')

Where input is just the parsed YAML as a string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/documentation Categorizes issue or PR as related to documentation. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests