mnubo / kubernetes-py

A python module for Kubernetes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Environment Variables lost on update

morissette opened this issue · comments

Surely, we should be using secrets.

But we are not. But environment configurations should not be lost during updates

Code:

def update_kube(image, namespace, deployment_name, receipt):
    """
    Set image for deployment in namespace
    """
    # Force unicode to string
    image = str(image)
    namespace = str(namespace)
    deployment_name = str(deployment_name)
    print "starting deployment of {} on {}".format(deployment_name, namespace)

    cfg_token = K8sConfig(
        kubeconfig=None,
        api_host=os.getenv("K8S_API"),
        token=os.getenv("K8S_KEY"),
        namespace=namespace
    )

    deployment = K8sDeployment(config=cfg_token, name=deployment_name)
    container = K8sContainer(name=deployment_name, image=image)
    deployment.add_container(container)
    deployment.desired_replicas = 1

    try:
        deployment.create()
        print "created deployment {} on {}".format(deployment_name, namespace)
    except K8sExceptions.AlreadyExistsException:
        pass

    deployment.container_image = (deployment_name, image)

    try:
        deployment.update()
        print "updated deployment {} on {}".format(deployment_name, namespace)
    except K8sExceptions.TimedOutException:
        print "check resource limits for namespace {}".format(namespace)
        return False, receipt
    return True, receipt

Hi,

Can you give us a bit more details please?

  • Kubernetes version
  • kubernetes-py version
  • Python version

I wrote a test to try and reproduce your issue and it seems to work well on Kubernetes 1.5.4 and kubernetes-py version 1.4.7.14.

The test code is here.

Could you confirm I understood your snippet correctly in the test?

Thanks,

Sebastien

Hi,

Could you provide the information I need?

Thanks,

Sebastien

Sorry for the delay; unfortunately had to move to hacky solution. We are running 1.3.5.

#!/usr/bin/env python
import ast
import json
import os
import simplejson
import subprocess
import time
import concurrent.futures

from boto3.session import Session

def create_aws_session():
    """
    Create AWS Session
    """
    session = Session(
        aws_access_key_id=os.getenv("AWS_ACCESS_KEY"),
        aws_secret_access_key=os.getenv("AWS_SECRET_KEY"),
        region_name=os.getenv("AWS_REGION")
    )
    c = session.client('sqs')
    return c

def get_queue_url(c):
    """
    Get Queue URL
    """
    queue_name = "DeploymentEvents"
    response = c.get_queue_url(
            QueueName=queue_name
    )
    url = response['QueueUrl']
    return url

def check_sqs(c, url):
    """
    Pull SQS queue and look for new messages
    """
    messages = c.receive_message(
        QueueUrl=url,
        AttributeNames=['All'],
        MaxNumberOfMessages=1,
        VisibilityTimeout=120,
        WaitTimeSeconds=5
    )

    if messages.get('Messages'):
        message = messages.get('Messages')[0]
        body = message['Body']
        receipt = message['ReceiptHandle']
        status = deploy_service(body)
        if status:
            delete_msg(c, url, receipt)



def delete_msg(c, url, receipt_handle):
    """
    Delete message from SQS
    """
    response = c.delete_message(
        QueueUrl=url,
        ReceiptHandle=receipt_handle
    )

def deploy_service(message):
    """
    Deploy service to kubernetes
    """
    data = json.loads(message)
    image = data.get("image", None)
    namespace = data.get("namespace", None)
    deployment = data.get("deployment", None)
    if image is not None:
        if namespace is not None:
            if deployment is not None:
                return update_kube(image, namespace, deployment)
    return False


def update_kube(image, namespace, deployment):
    """
    Set image for deployment in namespace
    """
    # Force unicode to string
    image = str(image)
    namespace = str(namespace)
    deployment = str(deployment)
    print "starting deployment of {} on {}".format(deployment, namespace)

    # Because none of the py modules are mature enough
    # Using direct kubectl command
    switch_namespace_cmd = "kubectl config set-context aws_k8s --namespace={namespace}".format(namespace=namespace)
    set_image_cmd = "kubectl set image deployment/{deployment} {deployment}={image}".format(
        deployment=deployment,
        image=image
    )
    status = subprocess.call(switch_namespace_cmd, shell=True)
    if status == 0:
        status = subprocess.call(set_image_cmd, shell=True)
        if status == 0:
            return True
    return False


if __name__ == "__main__":
    client = create_aws_session()
    url = get_queue_url(client)

    while True:
        check_sqs(client, url)
        time.sleep(5)

There are no repro steps here, just some chunks of Python code specific to your use case. It's unclear what the problem is, as well as how to reliably reproduce it. Unless you can contribute more information, I'll be closing this as non-constructive.