Ejemplo n.º 1
0
def apply(from_file: str):
    # Load the deployments from file and get the current count of replicas in
    # the target cluster for each of the deployments. Then update the replicas
    # to match the target cluster. Save the file and pass on to kubectl apply.
    with open(from_file) as fd:
        content = fd.read()
    kube_list = yaml.load(content)

    kubectl = Kubectl()
    kubectl.update_replicas(kube_list)

    with open(from_file, mode='w') as fd:
        fd.write(yaml.dump(kube_list, default_flow_style=False))

    try:
        lines = kubectl.apply(from_file)
        for line in lines.split('\n'):
            prompt(line)
    except KubectlCallFailed as e:
        error_prompt(str(e))
Ejemplo n.º 2
0
    def test_update_replicas_no_remote(self, mock_call):
        kube_list = json.loads('''
{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "extensions/v1beta1",
            "kind": "Deployment",
            "metadata": {
                "labels": {
                    "app": "test-service-one",
                    "servicegroup": "twyla"
                },
                "name": "test-service-one",
                "namespace": "twyla"
            },
            "spec": {
                "replicas": 2,
                "selector": {
                    "matchLabels": {
                        "app": "test-service-one",
                        "name": "test-service-one"
                    }
                },
                "strategy": {
                    "rollingUpdate": {
                        "maxSurge": "0%",
                        "maxUnavailable": "100%"
                    },
                    "type": "RollingUpdate"
                },
                "template": {
                    "metadata": {
                        "creationTimestamp": null,
                        "labels": {
                            "app": "test-service-one",
                            "name": "test-service-one"
                        }
                    },
                    "spec": {
                        "containers": [
                            {
                                "env": [
                                    {
                                        "name": "TWYLA_CLUSTER_NAME",
                                        "valueFrom": {
                                            "configMapKeyRef": {
                                                "key": "cluster-name",
                                                "name": "cluster-vars"
                                            }
                                        }
                                    }
                                ],
                                "image": "twyla.azurecr.io/test-service-one:cc0cd960",
                                "imagePullPolicy": "Always",
                                "name": "test-service-one",
                                "resources": {},
                                "terminationMessagePath": "/dev/termination-log",
                                "terminationMessagePolicy": "File"
                            }
                        ],
                        "dnsPolicy": "ClusterFirst",
                        "imagePullSecrets": [
                            {
                                "name": "twyla-registry-login"
                            }
                        ],
                        "restartPolicy": "Always",
                        "schedulerName": "default-scheduler",
                        "securityContext": {},
                        "terminationGracePeriodSeconds": 30
                    }
                }
            }
        },
        {
            "apiVersion": "extensions/v1beta1",
            "kind": "Deployment",
            "metadata": {
                "labels": {
                    "app": "test-service-two",
                    "servicegroup": "twyla"
                },
                "name": "test-service-two",
                "namespace": "twyla"
            },
            "spec": {
                "progressDeadlineSeconds": 600,
                "replicas": 1,
                "revisionHistoryLimit": 2,
                "selector": {
                    "matchLabels": {
                        "app": "test-service-two"
                    }
                },
                "strategy": {
                    "rollingUpdate": {
                        "maxSurge": 1,
                        "maxUnavailable": 1
                    },
                    "type": "RollingUpdate"
                },
                "template": {
                    "metadata": {
                        "creationTimestamp": null,
                        "labels": {
                            "app": "test-service-two",
                            "name": "test-service-two"
                        }
                    },
                    "spec": {
                        "containers": [
                            {
                                "env": [
                                    {
                                        "name": "TWYLA_CLUSTER_NAME",
                                        "valueFrom": {
                                            "configMapKeyRef": {
                                                "key": "cluster-name",
                                                "name": "cluster-vars"
                                            }
                                        }
                                    }
                                ],
                                "image": "twyla.azurecr.io/test-service-two:c42d0ebe",
                                "imagePullPolicy": "Always",
                                "name": "test-service-two",
                                "ports": [
                                    {
                                        "containerPort": 5000,
                                        "protocol": "TCP"
                                    }
                                ],
                                "resources": {},
                                "terminationMessagePath": "/dev/termination-log",
                                "terminationMessagePolicy": "File"
                            }
                        ],
                        "dnsPolicy": "ClusterFirst",
                        "imagePullSecrets": [
                            {
                                "name": "twyla-registry-login"
                            }
                        ],
                        "restartPolicy": "Always",
                        "schedulerName": "default-scheduler",
                        "securityContext": {},
                        "terminationGracePeriodSeconds": 30
                    }
                }
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}
        ''')
        dep1 = json.loads('''{
   "apiVersion":"extensions/v1beta1",
   "kind":"Deployment",
   "metadata":{
      "annotations":{
         "deployment.kubernetes.io/revision":"89",
         "kubectl.kubernetes.io/last-applied-configuration":"{\\"apiVersion\\":\\"extensions/v1beta1\\",\\"kind\\":\\"Deployment\\",\\"metadata\\":{\\"annotations\\":{},\\"labels\\":{\\"app\\":\\"test-service-one\\",\\"servicegroup\\":\\"twyla\\"},\\"name\\":\\"test-service-one\\",\\"namespace\\":\\"twyla\\"},\\"spec\\":{\\"progressDeadlineSeconds\\":600,\\"replicas\\":1,\\"revisionHistoryLimit\\":2,\\"selector\\":{\\"matchLabels\\":{\\"app\\":\\"test-service-one\\"}},\\"strategy\\":{\\"rollingUpdate\\":{\\"maxSurge\\":\\"50%\\",\\"maxUnavailable\\":\\"50%\\"},\\"type\\":\\"RollingUpdate\\"},\\"template\\":{\\"metadata\\":{\\"creationTimestamp\\":null,\\"labels\\":{\\"app\\":\\"test-service-one\\",\\"name\\":\\"test-service-one\\"}},\\"spec\\":{\\"containers\\":[{\\"env\\":[{\\"name\\":\\"TWYLA_CLUSTER_NAME\\",\\"valueFrom\\":{\\"configMapKeyRef\\":{\\"key\\":\\"cluster-name\\",\\"name\\":\\"cluster-vars\\"}}},{\\"name\\":\\"TWYLA_DOCUMENT_STORE_URI\\",\\"valueFrom\\":{\\"secretKeyRef\\":{\\"key\\":\\"twyla_document_store_string\\",\\"name\\":\\"document-store-secrets\\"}}}],\\"image\\":\\"twyla.azurecr.io/test-service-one:356dcef4\\",\\"imagePullPolicy\\":\\"Always\\",\\"name\\":\\"test-service-one\\",\\"resources\\":{},\\"terminationMessagePath\\":\\"/dev/termination-log\\",\\"terminationMessagePolicy\\":\\"File\\"}],\\"dnsPolicy\\":\\"ClusterFirst\\",\\"imagePullSecrets\\":[{\\"name\\":\\"twyla-registry-login\\"}],\\"restartPolicy\\":\\"Always\\",\\"schedulerName\\":\\"default-scheduler\\",\\"securityContext\\":{},\\"terminationGracePeriodSeconds\\":30}}}}\\n"
      },
      "creationTimestamp":"2017-10-16T14:55:37Z",
      "generation":95,
      "labels":{
         "app":"test-service-one",
         "servicegroup":"twyla"
      },
      "name":"test-service-one",
      "namespace":"twyla",
      "resourceVersion":"16810663",
      "selfLink":"/apis/extensions/v1beta1/namespaces/twyla/deployments/test-service-one",
      "uid":"120abf54-b282-11e7-b58f-000d3a2bee3e"
   },
   "spec":{
      "progressDeadlineSeconds":600,
      "replicas":1,
      "revisionHistoryLimit":2,
      "selector":{
         "matchLabels":{
            "app":"test-service-one"
         }
      },
      "strategy":{
         "rollingUpdate":{
            "maxSurge":"50%",
            "maxUnavailable":"50%"
         },
         "type":"RollingUpdate"
      },
      "template":{
         "metadata":{
            "creationTimestamp":null,
            "labels":{
               "app":"test-service-one",
               "name":"test-service-one"
            }
         },
         "spec":{
            "containers":[
               {
                  "env":[
                     {
                        "name":"TWYLA_CLUSTER_NAME",
                        "valueFrom":{
                           "configMapKeyRef":{
                              "key":"cluster-name",
                              "name":"cluster-vars"
                           }
                        }
                     },
                     {
                        "name":"TWYLA_DOCUMENT_STORE_URI",
                        "valueFrom":{
                           "secretKeyRef":{
                              "key":"twyla_document_store_string",
                              "name":"document-store-secrets"
                           }
                        }
                     }
                  ],
                  "image":"twyla.azurecr.io/test-service-one:356dcef4",
                  "imagePullPolicy":"Always",
                  "name":"test-service-one",
                  "resources":{

                  },
                  "terminationMessagePath":"/dev/termination-log",
                  "terminationMessagePolicy":"File"
               }
            ],
            "dnsPolicy":"ClusterFirst",
            "imagePullSecrets":[
               {
                  "name":"twyla-registry-login"
               }
            ],
            "restartPolicy":"Always",
            "schedulerName":"default-scheduler",
            "securityContext":{

            },
            "terminationGracePeriodSeconds":30
         }
      }
   },
   "status":{
      "availableReplicas":1,
      "conditions":[
         {
            "lastTransitionTime":"2018-01-08T16:38:21Z",
            "lastUpdateTime":"2018-02-13T18:47:02Z",
            "message":"ReplicaSet \\"test-service-one-3114667387\\" has successfully progressed.",
            "reason":"NewReplicaSetAvailable",
            "status":"True",
            "type":"Progressing"
         },
         {
            "lastTransitionTime":"2018-02-14T16:36:34Z",
            "lastUpdateTime":"2018-02-14T16:36:34Z",
            "message":"Deployment has minimum availability.",
            "reason":"MinimumReplicasAvailable",
            "status":"True",
            "type":"Available"
         }
      ],
      "observedGeneration":95,
      "readyReplicas":1,
      "replicas":1,
      "updatedReplicas":1
   }
}''')
        mock_call.side_effect = [
            dep1,
            KubectlCallFailed
        ]

        # before (this makes the test more obvious)
        assert kube_list['items'][0]['spec']['replicas'] == 2
        assert kube_list['items'][1]['spec']['replicas'] == 1

        kubectl = Kubectl()
        kubectl.update_replicas(kube_list)

        # after
        assert kube_list['items'][0]['spec']['replicas'] == 1
        assert kube_list['items'][1]['spec']['replicas'] == 1