kubernetes之namespace资源导出02

从k8s导出资源后,需要删除yaml里面的垃圾数据,需要批量改namespace名称,deploy中的镜像地址,改ingress的域名等等信息
通过如下脚本实现:

#!/bin/bash
# set -x

# 镜像环境变量之项目环境变量标签
itemEnv=dev
# 项目域名
## 项目主域名
mainDoname="xiaojinguang.com"
## 项目前端域名二级域名头
webDoname="side"
## 项目后端域名二级域名头
apiDoname="api"
# 项目二级域名中的环境变量标签(导出时):比如:dev,test,正式为空,同上面变量组合为:apidev.xiaojinguang.com,apitest.xiaojinguang.com
oldName=""
# 项目二级域名中的环境变量标签(需变更后的)
repName="dev"
# namespace
## 变更后的命名空间名称
NSnew=jintest
## change image
### 从k8s集群获取deploy中容器image数据的命名空间名称
NS=jindev
# docker 镜像仓库的项目名称
PROJECK=xiao
# 指定获取deploy中容器image数据时,连接k8s集群用的kubectl认证文件
configFile=~/.kube/config

shareDel=(
  "creationTimestamp:"
  "generation:"
  "resourceVersion:"
  "selfLink:"
  "uid:"
)
deployDel=(
  "annotations:"
  "field.cattle.io\/publicEndpoints:"
  "deployment.kubernetes.io\/revision:"
  "cattle.io\/timestamp:"
  "field.cattle.io\/ports:"
  "creationTimestamp:"
  "environment:"
)
ingressDel=(
  "field.cattle.io\/publicEndpoints:"
)
servicesDel=(
  "clusterIP:"
)
pvcDel=(
  "pv.kubernetes.io\/bind-completed:"
  "pv.kubernetes.io\/bound-by-controller:"
)
secretsDel=(
  "annotations:"
  "field.cattle.io\/projectId:"
  "kubernetes.io\/service-account.name:"
  "istio.io\/service-account.name:"
)
namespacesDel=(
  "annotations:"
  "field.cattle.io\/projectId"
  "lifecycle.cattle.io\/create.namespace-auth"
  "cattle.io\/status"
)

function getDirList(){
  #script dir list
  dirList=$(/bin/ls -lF $scriptDir|grep "/$" |awk '{print $NF}' |cut -d/ -f1)
  # dirList=$(echo ingress)
  for i in $dirList; do
    # echo $(ls $i)
    if [ "$1" == "deleteStatus" ] || [ "$1" == "deleteYamlAges" ];then
      for ii in $(ls $i); do
        deleteStatus(){
          # delete item yaml status ages
          echo "deleteStatus sed $i/$ii start"
          sed -i "" -e '/^status:$/,$d' $i/$ii
        }
        deleteYamlAges(){
          tmpname="${i}Del"
          eval tmpList=\${$tmpname[@]}
          workList="${shareDel[@]} $tmpList"
          echo "workList=$workList"
          # delete item yaml tmp ages
          for iii in ${workList[@]}; do
            # if [ ${iii%#*}x == "x"  ];then
            #   continue
            # fi
            # if [[ $iii =~ "annotations" ]] && [ $i == "ingress" ];then
            #   continue
            # fi
            echo "deleteYamlAges $iii from $i/$ii sed"
            sed -i "" "/$iii/d" $i/$ii
          done
        }
        $1
      done
    elif [ $1 == "changeNamespace" ];then
      changeNamespace(){
        # delete namespce age
        echo "changeNamespace start $i/*.yaml sed"
        sed -i "" "s/namespace:.*/namespace: $NSnew/" $i/*.yaml
      }
      $1
    fi
  done
  if [ $1 == "changeNamespace" ];then
    sed -i "" "s/name:.*/name: $NSnew/" namespaces.yaml
  fi
}

changeItemEnv(){
  # replace item env value
  echo "changeItemEnv start sed"
  sed -i "" "/name: ITEMENV/{n;s/value:.*/value: $itemEnv/;}" deploy/*.yaml
}
changeDoname(){
  echo "changeDoname start sed"
  # replace item web doname
  sed -i "" "s#${webDoname}${oldName}.${mainDoname}#$webDoname$repName.$mainDoname#"  ingress/*.yaml
  # replace item api doname
  sed -i "" "s#${apiDoname}${oldName}.${mainDoname}#$apiDoname$repName.$mainDoname#"  ingress/*.yaml
}

resetimage(){
  resourceList=(
  deploy
  )
  for aa in ${resourceList[@]}; do
    aList=$(kubectl --kubeconfig $configFile -n $NS get $aa |grep -v NAME  |awk '{print $1}')
    if [ ! "${aList[*]}"x == "x" ];then
      #[ -d ./$aa ] || mkdir ./$aa
      for i in $aList;
      do
        itemImage=$(kubectl --kubeconfig $configFile -n $NS get deployments.apps $i -o wide |grep -v IMAGE |awk '{print $7}')
        echo "$aa $i image:$itemImage"
        # replace item image
        sed -i "" "s#image:.*$PROJECK.*#image: $itemImage#" $aa/$i.yaml
      done
    fi
  done
}

deleteOther(){
  oList=$(ls *.yaml)
  for bb in $oList; do
    for cc in ${shareDel[@]}; do
      echo "deleteOther $cc from $bb"
      sed -i "" "/$cc/d" $bb
    done
    if [ "$bb" == "namespaces.yaml" ];then
      for cc in ${namespacesDel[@]}; do
        echo "deleteOther $cc from $bb"
        sed -i "" "/$cc/d" $bb
      done
    fi
    # delete item yaml status ages
    echo "deleteStatus sed $bb start"
    sed -i "" -e '/^status:$/,$d' $bb
  done
}

# 替换deploy文件夹中yaml文件容器 image的值,通过参考一个k8s集群的namespace
resetimage
# 替换deploy 容器中环境变量名称ITEMENV的值
changeItemEnv
# 替换ingress中域名
changeDoname
# 替换yaml文件中命名空间值
getDirList changeNamespace
# 删除yaml资源中status资源数据
getDirList deleteStatus
# 删除yaml垃圾数据,可按不同的资源类型
getDirList deleteYamlAges
deleteOther

你可能感兴趣的:(kubernetes之namespace资源导出02)