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

Can i implemen apply func like this? #1474

Closed
woshihaoren opened this issue May 19, 2021 · 3 comments
Closed

Can i implemen apply func like this? #1474

woshihaoren opened this issue May 19, 2021 · 3 comments
Labels
kind/documentation Categorizes issue or PR as related to documentation.

Comments

@woshihaoren
Copy link

woshihaoren commented May 19, 2021

Can i implemen apply func like this?
I think no problem, use replace on CoreV1Api or AppsV1Api

class K8sServiceClient(object):

    def __init__(self, k8sclient):
        self.core_v1 = k8sclient.CoreV1Api()

    def create_service(self, data, namespace="default"):
        api_response = self.core_v1.create_namespaced_service(
            body=data,
            namespace=namespace)
        return api_response

    def _replace_update(self, name, data, namespace="default"):
        api_response = self.core_v1.replace_namespaced_service(
            name=name,
            body=data,
            namespace=namespace)
        return api_response

    def apply(self, name, data, namespace="default"):
        try:
            exist_service = self.get_service(name, namespace)
        except ApiException as err:
            if err.status == '404':
                exist_service = None
            else:
                raise err
        if exist_service:
            data.metadata.resource_version = exist_service.metadata.resource_version
            # data.spec.cluster_ip = exist_service.spec.cluster_ip
            return self._replace_update(name, data, namespace)
        else:
            return self.create_service(data, namespace)

    def get_service(self, name, namespace):
        return self.core_v1.read_namespaced_service(name=name, namespace=namespace)
@woshihaoren woshihaoren added the kind/documentation Categorizes issue or PR as related to documentation. label May 19, 2021
@roycaihw
Copy link
Member

It would work, but you need to watch out for situation like a concurrent update happened after get_service and before _replace_update. In that case replace_namespaced_service will get a 409. A brute force solution is to retry when you get a 409.

This python client doesn't support client-side apply as kubectl does. The long term plan is to support server-side apply: #1430. Please +1 that issue if you need the feature.

/close

@k8s-ci-robot
Copy link
Contributor

@roycaihw: Closing this issue.

In response to this:

It would work, but you need to watch out for situation like a concurrent update happened after get_service and before _replace_update. In that case replace_namespaced_service will get a 409. A brute force solution is to retry when you get a 409.

This python client doesn't support client-side apply as kubectl does. The long term plan is to support server-side apply: #1430. Please +1 that issue if you need the feature.

/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.

@woshihaoren
Copy link
Author

thank your answer. I will +1.

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.
Projects
None yet
Development

No branches or pull requests

3 participants