#!/usr/bin/env sh
set -x
unalias cp rm mv

# 最好多列一些，保证脚本运行
yum install -y yum-utils pigz tar wget tree unzip
# crictl
VERSION="v1.22.0"
wget -c https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz && tar zxvf crictl-$VERSION-linux-amd64.tar.gz && rm -f crictl-$VERSION-linux-amd64.tar.gz && mv crictl /usr/bin/
# Containerd相关组件和配置文件
mkdir -p /root/containerd && cd /root/containerd
wget -c https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
wget -c https://github.com/containerd/containerd/releases/download/v1.5.5/containerd-1.5.5-linux-amd64.tar.gz && tar zxvf containerd-1.5.5-linux-amd64.tar.gz && rm -rf containerd-1.5.5-linux-amd64.tar.gz && mv bin/* . && rm -rf bin/
cp -f /root/containerd/containerd /usr/local/bin/
cp -f /root/containerd/containerd-* /usr/local/bin/
cp -f /root/containerd/ctr /usr/local/bin/
cp -f /root/containerd/containerd.service /etc/systemd/system/containerd.service
containerd config default > config.toml
mkdir -p /etc/containerd/
cp -f /root/containerd/config.toml /etc/containerd/
systemctl daemon-reload
systemctl enable --now containerd

crictl config runtime-endpoint unix:///run/containerd/containerd.sock
crictl config image-endpoint unix:///run/containerd/containerd.sock

# 构建安装包的目录结构
rm -rf /root/dok-release
mkdir -p /root/dok-release
mkdir /root/dok-release/app
mkdir /root/dok-release/docs
mkdir /root/dok-release/network
mkdir /root/dok-release/bin
mkdir /root/dok-release/bin/{cni,k8s,runc,containerd,tools,kernel}
mkdir /root/dok-release/conf
mkdir /root/dok-release/image

cd /root/dok-release/conf || exit
wget -c https://grafana.com/api/dashboards/13502/revisions/1/download -O minio-dashboard_rev18.json
wget -c https://grafana.com/api/dashboards/9614/revisions/1/download -O nginx-ingress-controller_rev1.json
wget -c https://grafana.com/api/dashboards/13032/revisions/1/download -O longhorn-example-v1-1-0_rev6.json
wget -c https://grafana.com/api/dashboards/14075/revisions/1/download -O harbor_rev2.json

# k8s相关组件和配置文件
cd /root/dok-release/bin/k8s/ || exit
# 三个二进制
wget -c https://dl.k8s.io/v1.21.7/bin/linux/amd64/kubelet
# kubeadm如果有证书更改的需求，需要将这个二进制替换掉
wget -c https://dl.k8s.io/v1.21.7/bin/linux/amd64/kubeadm
wget -c https://dl.k8s.io/v1.21.7/bin/linux/amd64/kubectl
# 两个配置文件
wget -c https://raw.githubusercontent.com/kubernetes/release/master/cmd/kubepkg/templates/latest/deb/kubelet/lib/systemd/system/kubelet.service
wget -c https://raw.githubusercontent.com/kubernetes/release/master/cmd/kubepkg/templates/latest/deb/kubeadm/10-kubeadm.conf

# dok的初始配置，谨慎修改
cat <<EOF | sudo tee /root/dok-release/bin/k8s/dok.yaml
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
nodeRegistration:
  criSocket: /run/containerd/containerd.sock
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: 1.21.7
imageRepository: k8s.gcr.io
clusterName: private-kubernetes
controlPlaneEndpoint: private-kubernetes.oa.com
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16
etcd:
  local:
    extraArgs:
      listen-metrics-urls: "http://0.0.0.0:2381"
apiServer:
  timeoutForControlPlane: 4m0s
  extraArgs:
    feature-gates: TTLAfterFinished=true,EphemeralContainers=true
  extraVolumes:
    - name: localtime
      hostPath: /etc/localtime
      mountPath: /etc/localtime
      readOnly: true
      pathType: File
  certSANs:
    - "127.0.0.1"
    - "localhost"
controllerManager:
  extraArgs:
    bind-address: 0.0.0.0
    feature-gates: TTLAfterFinished=true,EphemeralContainers=true
    experimental-cluster-signing-duration: 876000h
  extraVolumes:
    - hostPath: /etc/localtime
      mountPath: /etc/localtime
      name: localtime
      readOnly: true
      pathType: File
scheduler:
  extraArgs:
    bind-address: 0.0.0.0
    feature-gates: TTLAfterFinished=true,EphemeralContainers=true
  extraVolumes:
    - hostPath: /etc/localtime
      mountPath: /etc/localtime
      name: localtime
      readOnly: true
      pathType: File
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 2m0s
    enabled: true
  x509:
    clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 5m0s
    cacheUnauthorizedTTL: 30s
cgroupDriver: systemd
cgroupsPerQOS: true
clusterDNS:
  - 10.96.0.10
clusterDomain: cluster.local
cpuManagerReconcilePeriod: 10s
evictionPressureTransitionPeriod: 5m0s
fileCheckFrequency: 20s
healthzBindAddress: 127.0.0.1
healthzPort: 10248
httpCheckFrequency: 20s
imageMinimumGCAge: 2m0s
nodeStatusReportFrequency: 10s
nodeStatusUpdateFrequency: 10s
rotateCertificates: true
runtimeRequestTimeout: 2m0s
staticPodPath: /etc/kubernetes/manifests
streamingConnectionIdleTimeout: 4h0m0s
syncFrequency: 1m0s
volumeStatsAggPeriod: 1m0s
configMapAndSecretChangeDetectionStrategy: Watch
containerLogMaxFiles: 5
containerLogMaxSize: 10Mi
contentType: application/vnd.kubernetes.protobuf
cpuCFSQuota: true
cpuCFSQuotaPeriod: 100ms
cpuManagerPolicy: none
enableControllerAttachDetach: true
enableDebuggingHandlers: true
enforceNodeAllocatable:
  - pods
eventBurst: 10
eventRecordQPS: 5
evictionHard:
  imagefs.available: 15%
  memory.available: 100Mi
  nodefs.available: 10%
  nodefs.inodesFree: 5%
failSwapOn: true
hairpinMode: promiscuous-bridge
imageGCHighThresholdPercent: 85
imageGCLowThresholdPercent: 80
iptablesDropBit: 15
iptablesMasqueradeBit: 14
kubeAPIBurst: 10
kubeAPIQPS: 5
makeIPTablesUtilChains: true
maxOpenFiles: 1000000
maxPods: 110
nodeLeaseDurationSeconds: 40
oomScoreAdj: -999
podPidsLimit: -1
port: 10250
registryBurst: 10
registryPullQPS: 5
serializeImagePulls: true
featureGates:
  SupportNodePidsLimit: true
  EphemeralContainers: true
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
metricsBindAddress: "0.0.0.0:10249"
EOF

# runc相关组件
cd /root/dok-release/bin/runc/ || exit
wget -c https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.amd64 && mv runc.amd64 runc

# Containerd相关组件和配置文件
cd /root/dok-release/bin/containerd/ || exit
wget -c https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
wget -c https://github.com/containerd/containerd/releases/download/v1.5.5/containerd-1.5.5-linux-amd64.tar.gz && tar zxvf containerd-1.5.5-linux-amd64.tar.gz && rm -rf containerd-1.5.5-linux-amd64.tar.gz && mv bin/* . && rm -rf bin/
containerd config default > config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' config.toml

# CNI相关组件
cd /root/dok-release/bin/cni/ || exit
wget -c https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz && tar zxvf cni-plugins-linux-amd64-v1.1.1.tgz && rm -rf cni-plugins-linux-amd64-v1.1.1.tgz

# tools相关组件
cd /root/dok-release/bin/tools/ || exit
# nerdctl
wget -c https://github.com/containerd/nerdctl/releases/download/v0.21.0/nerdctl-0.21.0-linux-amd64.tar.gz && tar zxvf nerdctl-0.21.0-linux-amd64.tar.gz && rm -rf nerdctl-0.21.0-linux-amd64.tar.gz
# crictl
VERSION="v1.22.0"
wget -c https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz && tar zxvf crictl-$VERSION-linux-amd64.tar.gz && rm -f crictl-$VERSION-linux-amd64.tar.gz
# helm
wget -c https://get.helm.sh/helm-v3.6.0-linux-amd64.tar.gz && tar zxvf helm-v3.6.0-linux-amd64.tar.gz && mv linux-amd64/helm . && rm -rf helm-v3.6.0-linux-amd64.tar.gz linux-amd64/
# k9s
wget -c https://github.com/derailed/k9s/releases/download/v0.25.18/k9s_Linux_x86_64.tar.gz && tar zxvf k9s_Linux_x86_64.tar.gz && rm -rf k9s_Linux_x86_64.tar.gz LICENSE README.md
# octant
wget -c https://github.com/vmware-tanzu/octant/releases/download/v0.25.1/octant_0.25.1_Linux-64bit.tar.gz && tar zxvf octant_0.25.1_Linux-64bit.tar.gz && mv octant_0.25.1_Linux-64bit/octant . && rm -rf octant_0.25.1_Linux-64bit.tar.gz octant_0.25.1_Linux-64bit/
wget -c https://github.com/bloodorangeio/octant-helm/releases/download/v0.2.0/octant-helm_0.2.0_linux_amd64.tar.gz && tar zxvf octant-helm_0.2.0_linux_amd64.tar.gz && rm -rf LICENSE octant-helm_0.2.0_linux_amd64.tar.gz
tee octant.service <<-'EOF'
[Unit]
Description=octant

[Service]
Environment="HOME=/root"
#Environment="OCTANT_ACCEPTED_HOSTS=10.45.112.173"
Environment="KUBECONFIG=/root/.kube/config"
Environment="OCTANT_LISTENER_ADDR=0.0.0.0:8900"
Environment="OCTANT_DISABLE_OPEN_BROWSER=false"
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
WorkingDirectory=/usr/bin/
ExecStart=/usr/bin/octant
Type=simple
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
# etcdctl
wget -c https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz && tar zxvf etcd-v3.4.13-linux-amd64.tar.gz && mv etcd-v3.4.13-linux-amd64/etcdctl . && rm -rf etcd-v3.4.13-linux-amd64*
# buildkit
wget -c https://github.com/moby/buildkit/releases/download/v0.10.3/buildkit-v0.10.3.linux-amd64.tar.gz && tar zxvf buildkit-v0.10.3.linux-amd64.tar.gz && cp bin/buildctl . && cp bin/buildkitd . && rm -rf bin/ buildkit-v0.10.3.linux-amd64.tar.gz
cat > buildkit.service << EOF
[Unit]
Description=BuildKit
Documentation=https://github.com/moby/buildkit

[Service]
ExecStart=/usr/bin/buildkitd --oci-worker=false --containerd-worker=true

[Install]
WantedBy=multi-user.target
EOF
# kubectx
wget -c https://github.com/ahmetb/kubectx/releases/download/v0.9.4/kubectx_v0.9.4_linux_x86_64.tar.gz && tar zxvf kubectx_v0.9.4_linux_x86_64.tar.gz && rm -rf kubectx_v0.9.4_linux_x86_64.tar.gz LICENSE
# kubens
wget -c https://github.com/ahmetb/kubectx/releases/download/v0.9.4/kubens_v0.9.4_linux_x86_64.tar.gz && tar zxvf kubens_v0.9.4_linux_x86_64.tar.gz && rm -rf kubens_v0.9.4_linux_x86_64.tar.gz LICENSE
# kubecm
wget -c https://github.com/sunny0826/kubecm/releases/download/v0.17.0/kubecm_0.17.0_Linux_x86_64.tar.gz && tar zxvf kubecm_0.17.0_Linux_x86_64.tar.gz && rm -rf kubecm_0.17.0_Linux_x86_64.tar.gz LICENSE README.md
# kubectl plugin
wget -c https://github.com/gabeduke/kubectl-iexec/releases/download/v1.18.0/kubectl-iexec_v1.18.0_Linux_x86_64.tar.gz && tar zxvf kubectl-iexec_v1.18.0_Linux_x86_64.tar.gz && rm -rf LICENSE README.md kubectl-iexec_v1.18.0_Linux_x86_64.tar.gz
wget -c https://github.com/seredot/kubectl-example/releases/download/v1.1.0/kubectl-example_1.1.0_Linux_x86_64.tar.gz && tar zxvf kubectl-example_1.1.0_Linux_x86_64.tar.gz && rm -rf kubectl-example_1.1.0_Linux_x86_64.tar.gz LICENSE README.md
wget -c https://github.com/emirozer/kubectl-doctor/releases/download/0.3.1/kubectl-doctor_linux_amd64 && mv kubectl-doctor_linux_amd64 kubectl-doctor
wget -c https://github.com/yashbhutwala/kubectl-df-pv/releases/download/v0.3.0/kubectl-df-pv_v0.3.0_linux_amd64.tar.gz && tar zxvf kubectl-df-pv_v0.3.0_linux_amd64.tar.gz && rm -rf kubectl-df-pv_v0.3.0_linux_amd64.tar.gz LICENSE README.md
wget -c https://github.com/robscott/kube-capacity/releases/download/v0.7.1/kube-capacity_0.7.1_Linux_x86_64.tar.gz && tar zxvf kube-capacity_0.7.1_Linux_x86_64.tar.gz && rm -rf kube-capacity_0.7.1_Linux_x86_64.tar.gz LICENSE README.md
wget -c https://github.com/davidB/kubectl-view-allocations/releases/download/0.15.0/kubectl-view-allocations_0.15.0-x86_64-unknown-linux-gnu.tar.gz && tar zxvf kubectl-view-allocations_0.15.0-x86_64-unknown-linux-gnu.tar.gz && rm -rf kubectl-view-allocations_0.15.0-x86_64-unknown-linux-gnu.tar.gz LICENSE.txt
wget -c https://github.com/boz/kail/releases/download/v0.15.0/kail_0.15.0_linux_amd64.tar.gz && tar zxvf kail_0.15.0_linux_amd64.tar.gz && rm -rf kail_0.15.0_linux_amd64.tar.gz LICENSE.txt README.md
# bat
curl -Lo bat-0.7.0-1.of.el7.x86_64.rpm http://repo.openfusion.net/centos7-x86_64/bat-0.7.0-1.of.el7.x86_64.rpm
# fzf
wget -c https://github.com/junegunn/fzf/archive/refs/heads/master.zip && unzip master.zip && rm -rf master.zip && mv fzf-master fzf

# kernel相关
# conntrack-tools
# https://rpmfind.net/linux/RPM/index.html
cd /root/dok-release/bin/kernel/ || exit
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/conntrack-tools-1.4.4-7.el7.x86_64.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/libnetfilter_cthelper-1.0.0-11.el7.x86_64.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/libnetfilter_cttimeout-1.0.0-7.el7.x86_64.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/libnetfilter_queue-1.0.2-2.el7_2.x86_64.rpm
# 指定版本的内核rpm
wget -c https://linux.cc.iitk.ac.in/mirror/centos/elrepo/kernel/el7/x86_64/RPMS/kernel-lt-5.4.127-1.el7.elrepo.x86_64.rpm
# iscsi
wget -c https://rpmfind.net/linux/centos/7.9.2009/updates/x86_64/Packages/iscsi-initiator-utils-6.2.0.874-21.el7_9.x86_64.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/updates/x86_64/Packages/iscsi-initiator-utils-iscsiuio-6.2.0.874-21.el7_9.x86_64.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/updates/x86_64/Packages/util-linux-2.23.2-65.el7_9.1.x86_64.rpm
# zsh
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/zsh-5.0.2-34.el7_8.2.x86_64.rpm
# on-my-zsh
wget -c https://github.com/ohmyzsh/ohmyzsh/archive/refs/heads/master.zip && unzip master.zip && mv ohmyzsh-master oh-my-zsh && rm -rf master.zip
# git
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/git-1.8.3.1-23.el7_8.x86_64.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/perl-Error-0.17020-2.el7.noarch.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/perl-Git-1.8.3.1-23.el7_8.noarch.rpm
wget -c https://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/perl-TermReadKey-2.30-20.el7.x86_64.rpm

# 网络插件
cd /root/dok-release/network || exit
# flannel
wget -c https://raw.githubusercontent.com/flannel-io/flannel/v0.18.1/Documentation/kube-flannel.yml
# calico
wget -c https://projectcalico.docs.tigera.io/manifests/calico.yaml
# kube-ovn
wget -c https://raw.githubusercontent.com/kubeovn/kube-ovn/master/dist/images/install.sh
# multus
wget -c https://raw.githubusercontent.com/k8snetworkplumbingwg/multus-cni/master/deployments/multus-daemonset.yml
images=`grep -irn "image: " *|grep -v "#image"| awk -F "image: " '{print $2}'|sort|uniq`
kube_ovn_images=`grep -irn "image: " install.sh|grep -v "#image"| awk -F "image: " '{print $2}'|sort|uniq`
for i in $images $kube_ovn_images ; do
    echo "pulling image: $i"
    crictl pull $i
done

# nginx反向代理镜像
crictl pull nginx:1.20.1-alpine
# k8s组件的离线镜像
chmod +x /root/dok-release/bin/k8s/kubeadm
/root/dok-release/bin/k8s/kubeadm config images pull --kubernetes-version 1.21.7
cd /root/dok-release/image || exit
# crictl image | grep -v "IMAGE" | awk '{printf"%s:%s\n",$1,$2}' | sort | uniq
cat > kube-image-1.21.7 <<EOF
docker.io/calico/cni:v3.24.0
docker.io/calico/kube-controllers:v3.24.0
docker.io/calico/node:v3.24.0
docker.io/library/nginx:1.20.1-alpine
docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
docker.io/rancher/mirrored-flannelcni-flannel:v0.18.1
ghcr.io/k8snetworkplumbingwg/multus-cni:stable
k8s.gcr.io/coredns/coredns:v1.8.0
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/kube-apiserver:v1.21.7
k8s.gcr.io/kube-controller-manager:v1.21.7
k8s.gcr.io/kube-proxy:v1.21.7
k8s.gcr.io/kube-scheduler:v1.21.7
k8s.gcr.io/pause:3.4.1
EOF
tar=kube-v1-21-7-images.tar
rm -rf $tar
images=`cat kube-image-1.21.7`
ctr -n k8s.io i export $tar $images --platform linux/amd64 || exit

# 安装helm
curl -Lo helm.tar.gz https://get.helm.sh/helm-v3.6.0-linux-amd64.tar.gz && tar -zxvf helm.tar.gz -C /usr/bin && mv -f /usr/bin/linux-amd64/helm /usr/bin/ && rm -rf helm.tar.gz

# helm app
helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server/
metrics_server_images=`helm template metrics-server metrics-server/metrics-server --version 3.8.2 | grep -i "image: " | awk -F "image: " '{print $2}'`

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
ingress_nginx_images=`helm template ingress-nginx ingress-nginx/ingress-nginx --version 4.1.2 | grep -i "image: " | sort | uniq | awk -F "image: " '{print $2}' | awk -F "@" '{print $1}' | sed 's/\"//g'`

helm repo add longhorn https://charts.longhorn.io
longhorn_images=`helm template longhorn longhorn/longhorn --version 1.2.2 |grep -E "k8s.gcr.io|longhornio/longhorn"|awk '{print $2}'|grep -o '[[:alpha:]].*'|awk -F "\"" '{print $1}'|sort|uniq`

helm repo add minio https://charts.min.io
minio_images=`helm template minio minio/minio --version 3.6.1 | grep -i "image: " | sort | uniq | awk -F "image: " '{print $NF}' | sed 's/\"//g'`

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
kube_prometheus_stack_images=`helm template kube-prometheus-stack prometheus-community/kube-prometheus-stack --version 36.2.0|grep -E "image:|image=|reloader"|awk -F "image" '{print $2}'|uniq|grep -o '[[:alpha:]].*'|awk -F "\"" '{print $1}'`
reloader=`helm template kube-prometheus-stack prometheus-community/kube-prometheus-stack --version 36.2.0|grep -E "prometheus-config-reloader="|awk -F "=" '{print $2}'`
extra_images="busybox:1.31.1"

helm repo add harbor https://helm.goharbor.io
harbor_images=`helm template harbor harbor/harbor --version 1.8.2 --set metrics.enabled=true --set metrics.serviceMonitor.enabled=true | grep -i "image: "|awk -F "image: " '{print $2}'|sort|uniq`

wget -c https://github.com/runzhliu/nightingale-helm/archive/refs/heads/main.zip
unzip main.zip
rm -rf main.zip
mv nightingale-helm-main/ /tmp/n9e
n9e_images=`helm template n9e /tmp/n9e/|grep -i "image: "|awk -F "image: " '{print $2}'|sort|uniq|sed 's/\"//g'`

permission_manager_images=docker.io/runzhliu/permission-manager:v1.7.2

for i in ${metrics_server_images} ${ingress_nginx_images} ${longhorn_images} ${minio_images} ${kube_prometheus_stack_images} ${reloader} ${extra_images} ${harbor_images} ${n9e_images} ${permission_manager_images}; do
    echo "pulling image: $i"
    crictl pull $i
done

images=`cat kube-image-1.21.7`
ctr -n k8s.io i rm $images

# crictl image | grep -v "IMAGE" | grep -v none | awk '{printf"%s:%s\n",$1,$2}' | sort | uniq
cat > app-image <<EOF
docker.io/bats/bats:v1.4.1
docker.io/goharbor/chartmuseum-photon:v2.4.2
docker.io/goharbor/harbor-core:v2.4.2
docker.io/goharbor/harbor-db:v2.4.2
docker.io/goharbor/harbor-exporter:v2.4.2
docker.io/goharbor/harbor-jobservice:v2.4.2
docker.io/goharbor/harbor-portal:v2.4.2
docker.io/goharbor/harbor-registryctl:v2.4.2
docker.io/goharbor/notary-server-photon:v2.4.2
docker.io/goharbor/notary-signer-photon:v2.4.2
docker.io/goharbor/redis-photon:v2.4.2
docker.io/goharbor/registry-photon:v2.4.2
docker.io/goharbor/trivy-adapter-photon:v2.4.2
docker.io/grafana/grafana:9.0.1
docker.io/library/alpine:3.12
docker.io/library/busybox:1.31.1
docker.io/library/mysql:5.7
docker.io/library/redis:6.2
docker.io/longhornio/longhorn-engine:v1.2.2
docker.io/longhornio/longhorn-instance-manager:v1_20210731
docker.io/longhornio/longhorn-manager:v1.2.2
docker.io/longhornio/longhorn-share-manager:v1_20210914
docker.io/longhornio/longhorn-ui:v1.2.2
docker.io/runzhliu/n9e:v5.4.1
docker.io/runzhliu/permission-manager:v1.7.2
k8s.gcr.io/ingress-nginx/controller:v1.2.0
k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1
k8s.gcr.io/metrics-server/metrics-server:v0.6.1
k8s.gcr.io/sig-storage/csi-attacher:v3.2.1
k8s.gcr.io/sig-storage/csi-node-driver-registrar:v2.3.0
k8s.gcr.io/sig-storage/csi-provisioner:v2.1.2
k8s.gcr.io/sig-storage/csi-resizer:v1.2.0
k8s.gcr.io/sig-storage/csi-snapshotter:v3.0.3
quay.io/kiwigrid/k8s-sidecar:1.19.2
quay.io/minio/mc:RELEASE.2022-03-13T22-34-00Z
quay.io/minio/minio:RELEASE.2022-03-14T18-25-24Z
quay.io/prometheus/alertmanager:v0.24.0
quay.io/prometheus/node-exporter:v1.3.1
quay.io/prometheus-operator/prometheus-config-reloader:v0.57.0
quay.io/prometheus-operator/prometheus-operator:v0.57.0
quay.io/prometheus/prometheus:v2.36.1
quay.io/thanos/thanos:v0.25.2
registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.5.0
EOF

tar=app-images.tar
images=`cat app-image`
rm -rf $tar
ctr -n k8s.io i export $tar $images --platform linux/amd64 || exit

# 准备app
cd /root/dok-release/app/ || exit

helm pull longhorn/longhorn --version 1.2.2
helm pull minio/minio --version 3.6.1
helm pull prometheus-community/kube-prometheus-stack --version 36.2.0
helm pull harbor/harbor --version 1.8.2
helm pull ingress-nginx/ingress-nginx --version 4.1.2
helm pull metrics-server/metrics-server --version 3.8.2

for i in *;do tar zxf $i && rm -rf $i;done

# n9e helm
rm -rf n9e/
wget -c https://github.com/runzhliu/nightingale-helm/archive/refs/heads/main.zip
unzip main.zip
rm -rf main.zip
mv nightingale-helm-main/ n9e

# permission-manager helm
rm -rf permission-manager
wget -c https://github.com/runzhliu/permission-manager/archive/refs/heads/master.zip
unzip master.zip
rm -rf master.zip
mv permission-manager-master/helm_chart  permission-manager
rm -rf permission-manager-master

# 配置dok的默认值
cat > /root/dok-release/app/n9e/dok-values.yaml <<EOF
images:
  nserver: docker.io/runzhliu/n9e:v5.4.1
  nwebapi: docker.io/runzhliu/n9e:v5.4.1
service:
  ui:
    nodePort: 32500
    type: NodePort
EOF

cat > /root/dok-release/app/harbor/dok-values.yaml <<EOF
metrics:
  enabled: true
  serviceMonitor:
    enabled: true
EOF

cat > /root/dok-release/app/minio/dok-values.yaml <<EOF
consoleIngress:
  enabled: true
consoleService:
  nodePort: 32400
  type: NodePort
metrics:
  serviceMonitor:
    enabled: true
persistence:
  size: 10Gi
  storageClass: longhorn
replicas: 4
buckets:
  - name: backup
    policy: public
    purge: false
    versioning: false
resources:
  requests:
    memory: 1Gi
EOF

cat > /root/dok-release/app/longhorn/dok-values.yaml <<EOF
defaultSettings:
  defaultDataPath: /data/longhorn/var/lib/longhorn
  backupTarget: s3://backup@us-east-1/
  backupTargetCredentialSecret: minio-secret
ingress:
  enabled: true
  host: longhorn-ui.my.org
  ingressClassName: nginx
persistence:
  defaultClassReplicaCount: 2
  reclaimPolicy: Retain
service:
  ui:
    nodePort: 32100
    type: NodePort
EOF

cat > /root/dok-release/app/kube-prometheus-stack/dok-values.yaml <<EOF
grafana:
  adminPassword: admin
  ingress:
    enabled: true
    hosts:
    - grafana.my.org
  persistence:
    enabled: true
    storageClassName: longhorn
  service:
    nodePort: 32300
    type: NodePort
  sidecar:
    dashboards:
      enabled: true
kubeControllerManager:
  enabled: true
  service:
    port: 10257
    targetPort: 10257
  serviceMonitor:
    https: true
    insecureSkipVerify: true
kubeEtcd:
  enabled: true
  service:
    port: 2381
    targetPort: 2381
  serviceMonitor:
    https: true
    insecureSkipVerify: true
kubeProxy:
  service:
    enabled: true
  serviceMonitor:
    enabled: true
kubeScheduler:
  enabled: true
  service:
    enabled: true
    port: 10259
    targetPort: 10259
  serviceMonitor:
    https: true
    insecureSkipVerify: true
prometheus:
  prometheusSpec:
    serviceMonitorSelectorNilUsesHelmValues: false
    resources:
      requests:
        memory: 400Mi
    storageSpec:
      volumeClaimTemplate:
        spec:
          resources:
            requests:
              storage: 50Gi
          storageClassName: longhorn
  service:
    nodePort: 32200
    type: NodePort
prometheus-node-exporter:
  service:
    port: 19100
    targetPort: 19100
EOF

cat > /root/dok-release/app/ingress-nginx/dok-values.yaml <<EOF
controller:
  admissionWebhooks:
    enabled: false
  hostNetwork: true
  image:
    digest: ""
    digestChroot: ""
  ingressClassResource:
    default: true
  kind: DaemonSet
  metrics:
    enabled: true
    serviceMonitor:
      enabled: true
  service:
    nodePorts:
      http: 32080
      https: 32443
    type: NodePort
tcp:
  9000: "minio-cluster/minio-svc:9000"
  9001: "minio-cluster/minio-console:9001"
  9080: "kube-prometheus-stack/kube-prometheus-stack-grafana:80"
  9090: "kube-prometheus-stack/kube-prometheus-stack-prometheus:9090"
  9002: "n9e/nwebapi:18000"
  9081: "longhorn-system/longhorn-frontend:80"
EOF

cat > /root/dok-release/app/metrics-server/dok-values.yaml <<EOF
args:
  - --kubelet-insecure-tls
EOF

cat > /root/dok-release/app/permission-manager/dok-values.yaml <<EOF
image:
  repository: docker.io/runzhliu/permission-manager

service:
  type: NodePort
  port: 80
  nodePort: 32700

config:
  clusterName: "private-kubernetes"
  controlPlaneAddress: "https://127.0.0.1:8443"
  basicAuthPassword: admin
EOF

# 重置所有文件的权限
chown -R root:root /root/dok-release
chmod -R +x /root/dok-release/bin/

# 上传完整带有镜像组件的包
if [ ! -f /root/dok-release/image/app-images.tar ]; then
  exit 1
else
  cd || exit 1
  VERSION=$(date '+%Y-%m-%d-%H:%M:%S')
  echo $VERSION > /root/dok-release/version
  dir=dok-release
  gz=$dir-with-app-image.gz
  md5=$gz.md5sum
  tar -c --use-compress-program=pigz -f $gz $dir
  md5sum $gz > $md5
  if [ $? != 0 ]; then
    exit 1
  fi
  coscli cp $gz cos://dok-1258329892/$gz
  coscli cp $md5 cos://dok-1258329892/$md5
 fi