Узел выполняет команду оболочки, используя переменное содержимое - PullRequest
1 голос
/ 16 апреля 2020

Я пытаюсь запустить команду оболочки, используя kubectl для запуска развертывания. Конфигурация развертывания сохраняется в переменной deploymentConfiguration.

Когда я запускаю одну из следующих команд, я получаю ошибку.

const shelljs = require('shelljs');
const commandOutput = shelljs.exec(`echo ${deploymentConfiguration} | kubectl apply -f -`);

Возвращает ошибку:

error: error validating "STDIN": error validating data: invalid object to validate; if you choose to ignore these errors, turn validation off with --validate=false

const shelljs = require('shelljs');
const commandOutput = shelljs.exec(`echo ${JSON.stringify(deploymentConfiguration,null,1)} | kubectl apply -f -`);

Возвращает ошибку:

/bin/sh: apiVersion:: not found /bin/sh: kind:: not found

Я проверил это локально с помощью терминала, повторил конфигурацию и канал с помощью команды kubectl и смог развернуть.

Содержимое deploymentConfiguration:

{
"apiVersion": "extensions/v1beta1",
"kind": "Deployment",
"metadata": {
    "annotations": {
        "deployment.kubernetes.io/revision": "51",
        "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"sandbox-service-name\"},\"name\":\"sandbox-service-name-deployment\",\"namespace\":\"apps\"},\"spec\":{\"replicas\":1,\"selector\":{\"matchLabels\":{\"app\":\"sandbox-service-name\"}},\"strategy\":{\"rollingUpdate\":{\"maxSurge\":1,\"maxUnavailable\":\"0%\"},\"type\":\"RollingUpdate\"},\"template\":{\"metadata\":{\"annotations\":{\"prometheus.io/scrape\":\"true\"},\"labels\":{\"SECRET_MANAGER\":\"enable\",\"SECRET_PREFIX\":\"sandbox\",\"app\":\"sandbox-service-name\",\"deployed_from_master\":\"true\",\"deployment_tool_version\":\"k8s-deployment-9.0.61\"}},\"spec\":{\"affinity\":{\"podAntiAffinity\":{\"preferredDuringSchedulingIgnoredDuringExecution\":[{\"podAffinityTerm\":{\"labelSelector\":{\"matchExpressions\":[{\"key\":\"app\",\"operator\":\"In\",\"values\":[\"sandbox-service-name\"]}]},\"topologyKey\":\"failure-domain.beta.kubernetes.io/zone\"},\"weight\":100}],\"requiredDuringSchedulingIgnoredDuringExecution\":[{\"labelSelector\":{\"matchExpressions\":[{\"key\":\"app\",\"operator\":\"In\",\"values\":[\"sandbox-service-name\"]}]},\"topologyKey\":\"kubernetes.io/hostname\"}]}},\"containers\":[{\"env\":[{\"name\":\"VAULT_ADDRESS\",\"value\":\"http://vault-service-name-sandbox-service.delivery:8200\"},{\"name\":\"BIN_DETAILS_ADDRESS\",\"value\":\"http://bin-lookup-service\"},{\"name\":\"CASSANDRA_URL\",\"value\":\"someurl\"},{\"name\":\"CASSANDRA_KEYSPACE\",\"value\":\"service-name_sandbox\"},{\"name\":\"INTEGRATION_TIMEOUT\",\"value\":\"\"},{\"name\":\"KAFKA_ADDRESS\",\"value\":\"master.mesos:2181/dcos-service-kafka\"},{\"name\":\"KAFKA_INTERVAL\",\"value\":\"60\"},{\"name\":\"KAFKA_TOPIC\",\"value\":\"FundSourceService_Metrics_Sandbox\"},{\"name\":\"KAFKA_BUFFER_LIMIT\",\"value\":\"10\"},{\"name\":\"IS_VALIDATE_RESPONSE\",\"value\":\"false\"},{\"name\":\"RETRY_TIMEOUT_SECONDS\",\"value\":\"\"},{\"name\":\"VAULT_REQUEST_RETRY_ATTEMPTS\",\"value\":\"\"},{\"name\":\"VAULT_REQUEST_RETRY_DELAY_IN_MS\",\"value\":\"\"},{\"name\":\"RETRY_RENEW_CREDS_SEC\",\"value\":\"\"},{\"name\":\"TOKEN_STATE_MACHINE_ADDRESS\",\"value\":\"http://someurl\"},{\"name\":\"IP_VALIDATION_LEVEL\",\"value\":\"ONLY_LOG\"},{\"name\":\"IP_WHITELIST\",\"value\":\"35.156.1.128,35.156.80.222,52.57.133.225,52.57.167.185,52.29.43.63,52.57.32.148\"},{\"name\":\"IAM_URL\",\"value\":\"http://someurl\"},{\"name\":\"LOG_BODY_ON_SUCCESS\",\"value\":\"true\"},{\"name\":\"ACTIVITY_TOKEN_TOPIC\",\"value\":\"Payment_Method_Service_Tokens_Activity_Sandbox\"},{\"name\":\"ZOOKEEPER_URL\",\"value\":\"someurl\"},{\"name\":\"S3_BUCKET_ACTIVITY\",\"value\":\"someurl\"},{\"name\":\"PAYMENT_METHOD_TTL_SECONDS\",\"value\":\"31556926\"},{\"name\":\"ACTIVITY_TOPIC_PARTITIONS\",\"value\":\"10\"},{\"name\":\"service-name_OPERATIONAL_ROLE_ID\",\"value\":\"1e89d9ca-a428-ba2c-b452-f66f38fc969b\"},{\"name\":\"SECRET_MANAGER_PROJECT_NAMES\",\"value\":\"svcservice-nameoperational_sandbox\"},{\"name\":\"OPERATIONAL_TOKEN_RENEWAL_FIRST_BACK_OFF_IN_MS\",\"value\":\"5000\"},{\"name\":\"OPERATIONAL_TOKEN_RENEWAL_FIRST_RETRIES\",\"value\":\"3\"},{\"name\":\"OPERATIONAL_TOKEN_RENEWAL_SECOND_BACK_OFF_IN_MS\",\"value\":\"60000\"},{\"name\":\"LOG_LEVEL\",\"value\":\"info\"},{\"name\":\"PORT\",\"value\":\"3000\"},{\"name\":\"SERVER_KEEP_ALIVE_TIMEOUT_IN_MILLIS\",\"value\":\"120000\"},{\"name\":\"timestamp\",\"value\":\"'1586339281105'\"}],\"image\":\"docker-registry.blabla.co:4567/smartrouting-service//service-name/master:master-645009834fdafbe83c6f509c4852473c85e52864\",\"imagePullPolicy\":\"Always\",\"livenessProbe\":{\"httpGet\":{\"path\":\"/health\",\"port\":3000},\"initialDelaySeconds\":10,\"periodSeconds\":10,\"successThreshold\":1,\"timeoutSeconds\":3},\"name\":\"sandbox-service-name\",\"ports\":[{\"containerPort\":3000,\"name\":\"client\",\"protocol\":\"TCP\"}],\"readinessProbe\":{\"failureThreshold\":3,\"httpGet\":{\"path\":\"/health\",\"port\":3000,\"scheme\":\"HTTP\"},\"initialDelaySeconds\":10,\"periodSeconds\":2,\"successThreshold\":1,\"timeoutSeconds\":2},\"resources\":{\"requests\":{\"cpu\":0.2,\"memory\":512}}}],\"imagePullSecrets\":[{\"name\":\"regcred\"}]}}}}\n"
    },
    "creationTimestamp": "2019-11-11T11:27:53Z",
    "generation": 51,
    "labels": {
        "app": "sandbox-service-name"
    },
    "name": "sandbox-service-name-deployment",
    "namespace": "apps",
    "resourceVersion": "59769082",
    "selfLink": "/apis/extensions/v1beta1/namespaces/apps/deployments/sandbox-service-name-deployment",
    "uid": "4ce4f6c7-0476-11ea-9797-0a1cf276607c"
},
"spec": {
    "progressDeadlineSeconds": 600,
    "replicas": 1,
    "revisionHistoryLimit": 10,
    "selector": {
        "matchLabels": {
            "app": "sandbox-service-name"
        }
    },
    "strategy": {
        "rollingUpdate": {
            "maxSurge": 1,
            "maxUnavailable": "0%"
        },
        "type": "RollingUpdate"
    },
    "template": {
        "metadata": {
            "annotations": {
                "kubectl.kubernetes.io/restartedAt": "2020-04-13T20:44:49+03:00",
                "prometheus.io/scrape": "true"
            },
            "creationTimestamp": null,
            "labels": {
                "SECRET_MANAGER": "enable",
                "SECRET_PREFIX": "sandbox",
                "app": "sandbox-service-name",
                "deployed_from_master": "true",
                "deployment_tool_version": "k8s-deployment-9.0.61"
            }
        },
        "spec": {
            "affinity": {
                "podAntiAffinity": {
                    "preferredDuringSchedulingIgnoredDuringExecution": [
                        {
                            "podAffinityTerm": {
                                "labelSelector": {
                                    "matchExpressions": [
                                        {
                                            "key": "app",
                                            "operator": "In",
                                            "values": [
                                                "sandbox-service-name"
                                            ]
                                        }
                                    ]
                                },
                                "topologyKey": "failure-domain.beta.kubernetes.io/zone"
                            },
                            "weight": 100
                        }
                    ],
                    "requiredDuringSchedulingIgnoredDuringExecution": [
                        {
                            "labelSelector": {
                                "matchExpressions": [
                                    {
                                        "key": "app",
                                        "operator": "In",
                                        "values": [
                                            "sandbox-service-name"
                                        ]
                                    }
                                ]
                            },
                            "topologyKey": "kubernetes.io/hostname"
                        }
                    ]
                }
            },
            "containers": [
                {
                    "env": [
                    ],
                    "image": "docker-registry.blabla.co:4567/master:master-645009834fdafbe83c6f509c4852473c85e52864",
                    "imagePullPolicy": "Always",
                    "livenessProbe": {
                        "failureThreshold": 3,
                        "httpGet": {
                            "path": "/health",
                            "port": 3000,
                            "scheme": "HTTP"
                        },
                        "initialDelaySeconds": 10,
                        "periodSeconds": 10,
                        "successThreshold": 1,
                        "timeoutSeconds": 3
                    },
                    "name": "sandbox-service-name",
                    "ports": [
                        {
                            "containerPort": 3000,
                            "name": "client",
                            "protocol": "TCP"
                        }
                    ],
                    "readinessProbe": {
                        "failureThreshold": 3,
                        "httpGet": {
                            "path": "/health",
                            "port": 3000,
                            "scheme": "HTTP"
                        },
                        "initialDelaySeconds": 10,
                        "periodSeconds": 2,
                        "successThreshold": 1,
                        "timeoutSeconds": 2
                    },
                    "resources": {
                        "requests": {
                            "cpu": "200m",
                            "memory": "512"
                        }
                    },
                    "terminationMessagePath": "/dev/termination-log",
                    "terminationMessagePolicy": "File",
                    "volumeMounts": [
                        {
                            "mountPath": "/var/share/secrets/vault_secrets",
                            "name": "empty-dir"
                        }
                    ]
                }
            ],
            "dnsPolicy": "ClusterFirst",
            "imagePullSecrets": [
                {
                    "name": "regcred"
                }
            ],
            "initContainers": [
                {
                    "env": [
                    ],
                    "image": "docker-registry.blabla.co:4567/cd/k8s-secrets-init/release:1.0.2",
                    "imagePullPolicy": "Always",
                    "name": "init-secret-manger",
                    "resources": {},
                    "terminationMessagePath": "/dev/termination-log",
                    "terminationMessagePolicy": "File",
                    "volumeMounts": [
                        {
                            "mountPath": "/var/share/secrets/vault_secrets",
                            "name": "empty-dir"
                        },
                        {
                            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                            "name": "sandbox--smartrouting-service-service-name5ppkb",
                            "readOnly": true
                        },
                        {
                            "mountPath": "/var/run/secrets/istio.io/ssl/creds",
                            "name": "istio-secret",
                            "readOnly": true
                        }
                    ]
                }
            ],
            "restartPolicy": "Always",
            "schedulerName": "default-scheduler",
            "securityContext": {},
            "serviceAccount": "sandbox--smartrouting-service-service-name--master",
            "serviceAccountName": "sandbox--smartrouting-service-service-name--master",
            "terminationGracePeriodSeconds": 30,
            "volumes": [
                {
                    "emptyDir": {
                        "medium": "Memory"
                    },
                    "name": "empty-dir"
                },
                {
                    "name": "sandbox--smartrouting-service-service-namefh7sz",
                    "secret": {
                        "defaultMode": 420,
                        "secretName": "sandbox--smartrouting-service-service-namefh7sz"
                    }
                },
                {
                    "name": "sandbox--smartrouting-service-service-nameljjxs",
                    "secret": {
                        "defaultMode": 420,
                        "secretName": "sandbox--smartrouting-service-service-nameljjxs"
                    }
                },
                {
                    "name": "sandbox--smartrouting-service-service-name5ppkb",
                    "secret": {
                        "defaultMode": 420,
                        "secretName": "sandbox--smartrouting-service-service-name5ppkb"
                    }
                },
                {
                    "name": "istio-secret",
                    "secret": {
                        "defaultMode": 400,
                        "secretName": "istio.sandbox--smartrouting-service-service-name--master"
                    }
                }
            ]
        }
    }
},
"status": {
    "availableReplicas": 1,
    "conditions": [
        {
            "lastTransitionTime": "2020-04-13T18:28:09Z",
            "lastUpdateTime": "2020-04-13T18:28:09Z",
            "message": "Deployment has minimum availability.",
            "reason": "MinimumReplicasAvailable",
            "status": "True",
            "type": "Available"
        },
        {
            "lastTransitionTime": "2020-04-13T18:28:09Z",
            "lastUpdateTime": "2020-04-13T18:28:09Z",
            "message": "ReplicaSet \"sandbox-service-name-deployment-86bd84665b\" has successfully progressed.",
            "reason": "NewReplicaSetAvailable",
            "status": "True",
            "type": "Progressing"
        }
    ],
    "observedGeneration": 51,
    "readyReplicas": 1,
    "replicas": 1,
    "updatedReplicas": 1
}
};

Это вывод, который я получаю, просто повторяя конфигурацию:

 /bin/sh: apiVersion:: not found
 /bin/sh: kind:: not found
 /bin/sh: metadata:: not found
 /bin/sh: annotations:: not found
 /bin/sh: deployment.kubernetes.io/revision:: not found
 /bin/sh: kubectl.kubernetes.io/last-applied-configuration:: not found
 /bin/sh: },: not found
 /bin/sh: creationTimestamp:: not found
 /bin/sh: generation:: not found
 /bin/sh: labels:: not found
 /bin/sh: app:: not found
 /bin/sh: },: not found
 /bin/sh: name:: not found
 /bin/sh: namespace:: not found
 /bin/sh: resourceVersion:: not found
 /bin/sh: selfLink:: not found
 /bin/sh: uid:: not found
 /bin/sh: },: not found
 /bin/sh: spec:: not found
 /bin/sh: progressDeadlineSeconds:: not found
 /bin/sh: replicas:: not found
 /bin/sh: revisionHistoryLimit:: not found
 /bin/sh: selector:: not found
 /bin/sh: matchLabels:: not found
 /bin/sh: app:: not found
 /bin/sh: syntax error: unexpected "}"
 (node:90) UnhandledPromiseRejectionWarning: /bin/sh: apiVersion:: not found
 /bin/sh: kind:: not found
 /bin/sh: metadata:: not found
 /bin/sh: annotations:: not found
 /bin/sh: deployment.kubernetes.io/revision:: not found
 /bin/sh: kubectl.kubernetes.io/last-applied-configuration:: not found
 /bin/sh: },: not found
 /bin/sh: creationTimestamp:: not found
 /bin/sh: generation:: not found
 /bin/sh: labels:: not found
 /bin/sh: app:: not found
 /bin/sh: },: not found
 /bin/sh: name:: not found
 /bin/sh: namespace:: not found
 /bin/sh: resourceVersion:: not found
 /bin/sh: selfLink:: not found
 /bin/sh: uid:: not found
 /bin/sh: },: not found
 /bin/sh: spec:: not found
 /bin/sh: progressDeadlineSeconds:: not found
 /bin/sh: replicas:: not found
 /bin/sh: revisionHistoryLimit:: not found
 /bin/sh: selector:: not found
 /bin/sh: matchLabels:: not found
 /bin/sh: app:: not found
 /bin/sh: syntax error: unexpected "}"

Я испустил переменные окружения из-за информационной безопасности

Как я могу убедиться, что я правильно передаю содержимое deploymentConfiguration в команду оболочки?

Спасибо.

...