#!/bin/bash

export name_space=uavcloud-uat
export new_tag=5.0.0
export image_prefix=harbor.cdcyy.com.cn/cmii
export version_file="/root/version.txt"
echo ""
echo ""
ims=$(kubectl -n ${name_space} get deployment |  grep 'cmii-uav\|cmii-admin\|cmii-open' | awk '{print$1}' | tr "\n" " ")
for image in $ims; do
  image_prefixs=${image_prefix}/${image}
  echo "image is => $image"
  echo ""

  new_image=$(grep "$image" <$version_file)
  echo "new_image_version is $new_image"
  new_image_version=$(echo $new_image | cut -d":" -f2)
  echo "new_image is $new_image_version"
  echo ""
  echo "image_prefixs is $image_prefixs"

  if [ "$new_image_version" != "" ] ; then
    echo  "[update] - namespace [$name_space] start to update [$image] to [$new_image_version]"
#    kubectl set env deployment/${image} IMAGE_VERSION=${new_tag} -n ${name_space}
#    kubectl -n ${name_space} patch deployment ${image} -p "{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"${image}\",\"image\": \"${image_prefixs}:${new_image_version}\"}]}}}}"
  fi

  echo ""

done


#for image in ${ims}; do
#    image_version=$(kubectl -n ${name_space} get deployment "${image}" -o=jsonpath='{.spec.template.spec.containers[*].image}' | cut -d":" -f2)
#
#    if [ "$image_version" == "" ];then
#        image_prefix=$(kubectl -n ${name_space} get deployment "${image}" -o=jsonpath='{.spec.template.spec.containers[*].image}' | cut -d":" -f1)
#        echo  "[update] - namespace [$name_space] start to update [$image_prefix] to [$new_tag]"
#        kubectl set env deployment/${image} IMAGE_VERSION=${new_tag} -n ${name_space}
#        kubectl -n ${name_space} patch deployment ${image} -p "{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"${image}\",\"image\": \"${image_prefix}:${new_tag}\"}]}}}}"
#    fi
#done




