From 5afef0c706dc51d2c64d317dce76e13d983f5650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Ollivier?= Date: Wed, 17 Apr 2024 12:51:51 +0200 Subject: [PATCH] Remove -ti in docker and kubectl calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It avoids passing stdin to the container when useless. Please note it removes a couple of issues highlighted by [1] in airgap specs. It also removes all useless blank chars in the files modified by this change. [1] https://github.com/cnti-testcatalog/testsuite/pull/1949 Signed-off-by: Cédric Ollivier --- spec/airgap_task_spec.cr | 6 +-- src/tasks/platform/resilience.cr | 2 +- src/tasks/utils/image_prepull.cr | 2 +- src/tasks/utils/k8s_instrumentation.cr | 2 +- tools/ephemeral_env/ephemeral_env.cr | 6 +-- utils/airgap/airgap.cr | 70 +++++++++++++------------- utils/airgap/spec/airgap_spec.cr | 20 ++++---- 7 files changed, 54 insertions(+), 54 deletions(-) diff --git a/spec/airgap_task_spec.cr b/spec/airgap_task_spec.cr index 3f6c724e3..8025a7f41 100644 --- a/spec/airgap_task_spec.cr +++ b/spec/airgap_task_spec.cr @@ -42,11 +42,11 @@ describe "AirGap" do pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list) pods = KubectlClient::Get.pods_by_label(pods, "name", "cri-tools") # Get the generated name of the cri-tools per node - pods.map do |pod| + pods.map do |pod| pod_name = pod.dig?("metadata", "name") - sh = KubectlClient.exec("-ti #{pod_name} -- cat /usr/local/bin/crictl > /dev/null") + sh = KubectlClient.exec("#{pod_name} -- cat /usr/local/bin/crictl > /dev/null") sh[:status].success? - sh = KubectlClient.exec("-ti #{pod_name} -- cat /usr/local/bin/ctr > /dev/null") + sh = KubectlClient.exec("#{pod_name} -- cat /usr/local/bin/ctr > /dev/null") sh[:status].success? end (/All prerequisites found./ =~ response_s).should_not be_nil diff --git a/src/tasks/platform/resilience.cr b/src/tasks/platform/resilience.cr index 3b318ac0e..b470ccb97 100644 --- a/src/tasks/platform/resilience.cr +++ b/src/tasks/platform/resilience.cr @@ -50,7 +50,7 @@ namespace "platform" do # Find Reboot Daemon name reboot_daemon_pod = KubectlClient::Get.pod_status("reboot", "--field-selector spec.nodeName=#{worker_node}").split(",")[0] - start_reboot = KubectlClient.exec("-ti #{reboot_daemon_pod} touch /tmp/reboot") + start_reboot = KubectlClient.exec("#{reboot_daemon_pod} touch /tmp/reboot") #Watch for Node Failure. pod_ready = "" diff --git a/src/tasks/utils/image_prepull.cr b/src/tasks/utils/image_prepull.cr index cf49ff5a0..1134caa37 100644 --- a/src/tasks/utils/image_prepull.cr +++ b/src/tasks/utils/image_prepull.cr @@ -30,7 +30,7 @@ def self.image_pull(yml, offline) pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list) pods = KubectlClient::Get.pods_by_label(pods, "name", "cri-tools") pods.map do |pod| - KubectlClient.exec("-ti #{pod.dig?("metadata", "name")} -- crictl pull #{image}") + KubectlClient.exec("#{pod.dig?("metadata", "name")} -- crictl pull #{image}") end end end diff --git a/src/tasks/utils/k8s_instrumentation.cr b/src/tasks/utils/k8s_instrumentation.cr index 14ddaada2..d6e46df42 100644 --- a/src/tasks/utils/k8s_instrumentation.cr +++ b/src/tasks/utils/k8s_instrumentation.cr @@ -10,7 +10,7 @@ module K8sInstrumentation cluster_tools = pod_status.split(",")[0] Log.info { "k8s_instrumentation_debug Pod status: #{pod_status.inspect}" } result = ShellCmd.run("kubectl get all -A", "k8s_instrumentation_debug", force_output: true) - resp = KubectlClient.exec("#{cluster_tools} -ti -- /bin/bash -c 'sysbench fileio prepare && sysbench fileio --file-test-mode=rndrw run'", namespace: TESTSUITE_NAMESPACE) + resp = KubectlClient.exec("#{cluster_tools} -- /bin/bash -c 'sysbench fileio prepare && sysbench fileio --file-test-mode=rndrw run'", namespace: TESTSUITE_NAMESPACE) parse_sysbench(resp[:output].to_s) end diff --git a/tools/ephemeral_env/ephemeral_env.cr b/tools/ephemeral_env/ephemeral_env.cr index 635cbeef2..b83f28569 100644 --- a/tools/ephemeral_env/ephemeral_env.cr +++ b/tools/ephemeral_env/ephemeral_env.cr @@ -123,7 +123,7 @@ elsif ARGV[0]? && ARGV[0] == "create_env" puts "Required argument missing [-n, --name]" else puts "Creating ENV For: \n Name: #{env_name} \n Kubeconfig: #{kubeconfig}" - `docker run --name #{env_name} --network host -d -e GITHUB_USER=$GITHUB_USER -e GITHUB_TOKEN=$GITHUB_TOKEN -e DOCKERHUB_USERNAME=$DOCKERHUB_USERNAME -e DOCKERHUB_PASSWORD=$DOCKERHUB_PASSWORD -v $(pwd):/cnf-testsuite -v #{kubeconfig}:/root/.kube/config -ti cnf-test /bin/sleep infinity` + `docker run --name #{env_name} --network host -d -e GITHUB_USER=$GITHUB_USER -e GITHUB_TOKEN=$GITHUB_TOKEN -e DOCKERHUB_USERNAME=$DOCKERHUB_USERNAME -e DOCKERHUB_PASSWORD=$DOCKERHUB_PASSWORD -v $(pwd):/cnf-testsuite -v #{kubeconfig}:/root/.kube/config cnf-test /bin/sleep infinity` puts `docker ps -f name=#{env_name}` end @@ -204,10 +204,10 @@ elsif ARGV[0]? && ARGV[0] == "command" # cyrstal src/cnf-testsuite.cr all # cyrstal ./cnf-testsuite all if binary == true - system "docker exec -ti #{ENV["CRYSTAL_DEV_ENV"]} ./cnf-testsuite #{execute_command}" + system "docker exec #{ENV["CRYSTAL_DEV_ENV"]} ./cnf-testsuite #{execute_command}" exit $?.exit_code else - system "docker exec -ti #{ENV["CRYSTAL_DEV_ENV"]} crystal #{execute_command}" + system "docker exec #{ENV["CRYSTAL_DEV_ENV"]} crystal #{execute_command}" exit $?.exit_code end else diff --git a/utils/airgap/airgap.cr b/utils/airgap/airgap.cr index e981cf3ae..c41339128 100644 --- a/utils/airgap/airgap.cr +++ b/utils/airgap/airgap.cr @@ -39,7 +39,7 @@ module AirGap tar_dir = info[:tar_dir] tar_name = info[:tar_name] - TarClient.modify_tar!(tar_name) do |directory| + TarClient.modify_tar!(tar_name) do |directory| template_files = Find.find(directory, "*.yaml*", "100") template_files.map{|x| AirGap.image_pull_policy(x)} end @@ -47,20 +47,20 @@ module AirGap ensure FileUtils.rm_rf("/tmp/#{repo_path}") end - + def self.tar_manifest(url, output_file : String = "./airgapped.tar.gz", prefix="") - manifest_path = "manifests/" + manifest_path = "manifests/" FileUtils.rm_rf("/tmp/#{manifest_path}") FileUtils.mkdir_p("/tmp/" + manifest_path) - manifest_name = prefix + url.split("/").last - manifest_full_path = manifest_path + manifest_name + manifest_name = prefix + url.split("/").last + manifest_full_path = manifest_path + manifest_name Log.info { "manifest_name: #{manifest_name}" } Log.info { "manifest_full_path: #{manifest_full_path}" } download_path = "/tmp/" + manifest_full_path HttpHelper.download("#{url}", "#{download_path}") - # Halite.get("#{url}") do |response| - # File.open("/tmp/" + manifest_full_path, "w") do |file| + # Halite.get("#{url}") do |response| + # File.open("/tmp/" + manifest_full_path, "w") do |file| # IO.copy(response.body_io, file) # end # end @@ -92,10 +92,10 @@ module AirGap unless kind_name AirGap.bootstrap_cluster() end - #TODO Potentially remove this. + #TODO Potentially remove this. if ENV["CRYSTAL_ENV"]? == "TEST" # todo change chaos-mesh tar to something more generic - image_files = ["#{TAR_BOOTSTRAP_IMAGES_DIR}/kubectl.tar", + image_files = ["#{TAR_BOOTSTRAP_IMAGES_DIR}/kubectl.tar", "#{TAR_BOOTSTRAP_IMAGES_DIR}/chaos-mesh.tar"] tar_image_files = Find.find("#{TarClient::TAR_IMAGES_DIR}", "*.tar*") image_files = image_files + tar_image_files + Find.find("#{TarClient::TAR_IMAGES_DIR}", "*.tgz*") @@ -120,7 +120,7 @@ module AirGap Log.info { "TAR POD: #{pods}" } tar_pod_name = pods[0].dig?("metadata", "name") if pods[0]? Log.info { "TAR POD NAME: #{tar_pod_name}" } - unless tar_pod_name + unless tar_pod_name Log.info { "NO TAR POD, CHECKING FOR PODS WITH SHELL" } pods = AirGap.pods_with_sh() no_tar = true @@ -140,9 +140,9 @@ module AirGap if no_tar Log.info { "NO TAR POD, COPYING TAR FROM HOST" } tar_path = AirGap.check_tar(cri_tools_pod_name, namespace: "default", pod: false) - pods.map do |pod| - KubectlClient.exec("#{pod.dig?("metadata", "name")} -ti -- cp #{tar_path} /usr/local/bin/") - status = KubectlClient.exec("#{pod.dig?("metadata", "name")} -ti -- /usr/local/bin/tar --version") + pods.map do |pod| + KubectlClient.exec("#{pod.dig?("metadata", "name")} -- cp #{tar_path} /usr/local/bin/") + status = KubectlClient.exec("#{pod.dig?("metadata", "name")} -- /usr/local/bin/tar --version") unless status[:status].success? raise "No images with Tar or Shell found. Please deploy a Pod with Tar or Shell to your cluster." end @@ -155,20 +155,20 @@ module AirGap unless kind_name pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list) pods = KubectlClient::Get.pods_by_label(pods, "name", "cri-tools") - pods.map do |pod| + pods.map do |pod| pod_name = pod.dig?("metadata", "name") KubectlClient.cp("#{tarball} #{pod_name}:/tmp/#{tarball.split("/")[-1]}") end - pods.map do |pod| + pods.map do |pod| pod_name = pod.dig?("metadata", "name") - resp = KubectlClient.exec("-ti #{pod_name} -- ctr -n=k8s.io image import /tmp/#{tarball.split("/")[-1]}") + resp = KubectlClient.exec("#{pod_name} -- ctr -n=k8s.io image import /tmp/#{tarball.split("/")[-1]}") Log.debug { "Resp: #{resp}" } resp end else DockerClient.cp("#{tarball} #{kind_name}:/#{tarball.split("/")[-1]}") - #DockerClient.exec("-ti #{kind_name} ctr -n=k8s.io image import /#{tarball.split("/")[-1]}") - `docker exec -ti #{kind_name} ctr -n=k8s.io image import /#{tarball.split("/")[-1]}` + #DockerClient.exec("#{kind_name} ctr -n=k8s.io image import /#{tarball.split("/")[-1]}") + `docker exec #{kind_name} ctr -n=k8s.io image import /#{tarball.split("/")[-1]}` end end @@ -190,7 +190,7 @@ module AirGap end def self.pod_images(pods) - # todo change into a reduce, loop through all containers and append image + # todo change into a reduce, loop through all containers and append image # into final array of images pods.map do |pod| containers = pod.dig("spec","containers").as_a @@ -211,19 +211,19 @@ module AirGap def self.check_sh(pod_name, namespace="default") # --namespace=${POD[1]} - sh = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /bin/sh > /dev/null") + sh = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /bin/sh > /dev/null") sh[:status].success? end def self.check_tar(pod_name, namespace="default", pod=true) if pod - bin_tar = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /bin/tar > /dev/null") - usr_bin_tar = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /usr/bin/tar > /dev/null") - usr_local_bin_tar = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /usr/local/bin/tar > /dev/null") + bin_tar = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /bin/tar > /dev/null") + usr_bin_tar = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /usr/bin/tar > /dev/null") + usr_local_bin_tar = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /usr/local/bin/tar > /dev/null") else - bin_tar = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /tmp/bin/tar > /dev/null") - usr_bin_tar = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /tmp/usr/bin/tar > /dev/null") - usr_local_bin_tar = KubectlClient.exec("--namespace=#{namespace} -ti #{pod_name} -- cat /tmp/usr/local/bin/tar > /dev/null") + bin_tar = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /tmp/bin/tar > /dev/null") + usr_bin_tar = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /tmp/usr/bin/tar > /dev/null") + usr_local_bin_tar = KubectlClient.exec("--namespace=#{namespace} #{pod_name} -- cat /tmp/usr/local/bin/tar > /dev/null") end if pod (bin_tar[:status].success? && "/bin/tar") || (usr_bin_tar.[:status].success? && "/usr/bin/tar") || (usr_local_bin_tar[:status].success? && "/usr/local/bin/tar") @@ -263,7 +263,7 @@ module AirGap pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list).select do |pod| pod_name = pod.dig?("metadata", "name") namespace = pod.dig?("metadata", "namespace") - if check_sh(pod_name, namespace) + if check_sh(pod_name, namespace) Log.debug { "Found sh Pod: #{pod_name}" } true else @@ -281,7 +281,7 @@ module AirGap file_list = Helm::Manifest.manifest_file_list(config_src, silent=false) yml = Helm::Manifest.manifest_ymls_from_file_list(file_list) when Helm::InstallMethod::HelmChart, Helm::InstallMethod::HelmDirectory - Helm.template(release_name, config_src, output_file="cnfs/temp_template.yml") + Helm.template(release_name, config_src, output_file="cnfs/temp_template.yml") yml = Helm::Manifest.parse_manifest_as_ymls(template_file_name="cnfs/temp_template.yml") else raise "config source error: #{install_method}" @@ -296,7 +296,7 @@ module AirGap mc.as_a? if mc }.flatten.compact Log.debug { "containers : #{containers}" } - found_all = true + found_all = true containers.flatten.map do |x| Log.debug { "container x: #{x}" } ipp = x.dig?("imagePullPolicy") @@ -310,7 +310,7 @@ module AirGap if ipp == nil && (parsed_image && parsed_image["tag"] == "latest") Log.info { "ipp or tag not found with ipp: #{ipp} and parsed_image: #{parsed_image}" } found_all = false - end + end end Log.info { "found_all: #{found_all}" } found_all @@ -318,15 +318,15 @@ module AirGap def self.image_pull_policy(file, output_file="") - input_content = File.read(file) + input_content = File.read(file) output_content = input_content.gsub(/(.*imagePullPolicy:)(.*.)/,"\\1 Never") # LOGGING.debug "pull policy found?: #{input_content =~ /(.*imagePullPolicy:)(.*)/}" # LOGGING.debug "output_content: #{output_content}" if output_file.empty? - input_content = File.write(file, output_content) + input_content = File.write(file, output_content) else - input_content = File.write(output_file, output_content) + input_content = File.write(output_file, output_content) end # #TODO find out why this doesn't work @@ -352,7 +352,7 @@ module AirGap repo = config_src.split(" ")[0] repo_dir = repo.gsub("/", "_") chart_name = repo.split("/")[-1] - repo_path = "repositories/#{repo_dir}" + repo_path = "repositories/#{repo_dir}" tar_dir = "/tmp/#{repo_path}" tar_info = {repo: repo, repo_dir: repo_dir, chart_name: chart_name, repo_path: repo_path, tar_dir: tar_dir, tar_name: tar_name_by_helm_chart(config_src)} @@ -367,7 +367,7 @@ module AirGap repo = config_src.split(" ")[0] repo_dir = repo.gsub("/", "_") chart_name = repo.split("/")[-1] - repo_path = "repositories/#{repo_dir}" + repo_path = "repositories/#{repo_dir}" tar_dir = "/tmp/#{repo_path}" Log.info { "helm_tar_dir: #{tar_dir}" } tar_dir diff --git a/utils/airgap/spec/airgap_spec.cr b/utils/airgap/spec/airgap_spec.cr index d886745d6..f220c9762 100644 --- a/utils/airgap/spec/airgap_spec.cr +++ b/utils/airgap/spec/airgap_spec.cr @@ -9,7 +9,7 @@ describe "AirGap" do AirGap.image_pull_policy("./spec/fixtures/litmus-operator-v1.13.2.yaml", "/tmp/imagetest.yml") (File.exists?("/tmp/imagetest.yml")).should be_true - resp = File.read("/tmp/imagetest.yml") + resp = File.read("/tmp/imagetest.yml") (resp).match(/imagePullPolicy: Always/).should be_nil (resp).match(/imagePullPolicy: Never/).should_not be_nil ensure @@ -61,13 +61,13 @@ describe "AirGap" do resp = AirGap.check_tar(pods.dig?("metadata", "name")) resp.should be_false end - + it "'#AirGap.check_tar' should determine if the host has the tar binary on it", tags: ["airgap"] do pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list) pods = KubectlClient::Get.pods_by_label(pods, "name", "cri-tools") resp = AirGap.check_tar(pods[0].dig?("metadata", "name"), pod=false) Log.debug { "Path to tar on the host filesystem: #{resp}" } - resp.should_not be_nil + resp.should_not be_nil end it "'#AirGap.check_sh' should determine if a pod has a shell on it", tags: ["airgap"] do @@ -137,14 +137,14 @@ describe "AirGap" do pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list) pods = KubectlClient::Get.pods_by_label(pods, "name", "cri-tools") # Get the generated name of the cri-tools per node - pods.map do |pod| + pods.map do |pod| pod_name = pod.dig?("metadata", "name") containers = pod.dig("spec","containers").as_a image = containers[0]? && containers[0].dig("image") Log.info { "CRI Pod Image: #{image}" } - sh = KubectlClient.exec("-ti #{pod_name} -- cat /usr/local/bin/crictl > /dev/null") + sh = KubectlClient.exec("#{pod_name} -- cat /usr/local/bin/crictl > /dev/null") sh[:status].success? - sh = KubectlClient.exec("-ti #{pod_name} -- cat /usr/local/bin/ctr > /dev/null") + sh = KubectlClient.exec("#{pod_name} -- cat /usr/local/bin/ctr > /dev/null") sh[:status].success? end ensure @@ -156,17 +156,17 @@ describe "AirGap" do it "'#AirGap.bootstrap_cluster' should install the cri tools in the cluster that does not have tar in the images", tags: ["airgap-tools"] do KubectlClient::Delete.command("daemonset cri-tools") pods = AirGap.pods_with_tar() - # Skip the test if tar is available outside of the cri tools + # Skip the test if tar is available outside of the cri tools if pods.empty? AirGap.bootstrap_cluster() pods = KubectlClient::Get.pods_by_nodes(KubectlClient::Get.schedulable_nodes_list) pods = KubectlClient::Get.pods_by_label(pods, "name", "cri-tools") # Get the generated name of the cri-tools per node - pods.map do |pod| + pods.map do |pod| pod_name = pod.dig?("metadata", "name") - sh = KubectlClient.exec("-ti #{pod_name} -- cat /usr/local/bin/crictl > /dev/null") + sh = KubectlClient.exec("#{pod_name} -- cat /usr/local/bin/crictl > /dev/null") sh[:status].success? - sh = KubectlClient.exec("-ti #{pod_name} -- cat /usr/local/bin/ctr > /dev/null") + sh = KubectlClient.exec("#{pod_name} -- cat /usr/local/bin/ctr > /dev/null") sh[:status].success? end end