pipeline {
    agent none

    options {
        timestamps()
    }

    parameters{
        string defaultValue: 'master', description: 'server image version', name: 'IMAGE_VERSION', trim: true
        choice choices: ['kubernetes', 'idc-kubernetes'], description: 'cluster name', name: 'CLUSTER_NAME'
        string defaultValue: '2_data.json', description: 'test suite config yaml', name: 'CONFIG_FILE', trim: true
        string defaultValue: 'd0928627-efb6-4cfd-8030-9bf635988d85', description: 'git credentials', name: 'GIT_USER', trim: true
    }

    environment {
        HELM_URL = "https://github.com/zilliztech/milvus-helm-charts.git"
        HELM_BRANCH = "main"
        TEST_URL = "https://github.com/zilliztech/milvus_benchmark.git"
        TEST_BRANCH = "distributed"
        HELM_RELEASE_NAME = "distributed-benchmark-test-${env.BUILD_NUMBER}"
    }

    stages {
        stage("Setup env") {
            agent {
                kubernetes {
                    cloud "${params.CLUSTER_NAME}"
                    label "test-benchmark-${env.JOB_NAME}-${env.BUILD_NUMBER}"
                    defaultContainer 'jnlp'
                    yaml """
                        apiVersion: v1
                        kind: Pod
                        metadata:
                          labels:
                            app: milvus
                            componet: test
                        spec:
                          containers:
                          - name: milvus-test-env
                            image: registry.milvus.io/milvus/milvus-test-env:v0.3
                            command:
                            - cat
                            tty: true
                            volumeMounts:
                            - name: kubeconf
                              mountPath: /root/.kube/
                              readOnly: true
                            - name: kubeconf2
                              mountPath: /root/kube/.kube/
                              readOnly: true
                            - name: db-data-path
                              mountPath: /test
                              readOnly: false
                          nodeSelector:
                            kubernetes.io/hostname: idc-sh002
                          tolerations:
                          - key: worker
                            operator: Equal
                            value: performance
                            effect: NoSchedule
                          volumes:
                          - name: kubeconf
                            secret:
                              secretName: test-cluster-config
                          - name: kubeconf2
                            secret:
                              secretName: idc-cluster-config
                          - name: db-data-path
                            flexVolume:
                              driver: "fstab/cifs"
                              fsType: "cifs"
                              secretRef:
                                name: "cifs-test-secret"
                              options:
                                networkPath: "//172.16.70.249/test"
                                mountOptions: "vers=1.0"
                        """
                }
            }

            stages {
                stage("Publish Daily Docker images") {
                    steps {
                        container('milvus-test-env') {
                            script {
                                boolean isNightlyTest = isTimeTriggeredBuild()
                                if (isNightlyTest) {
                                    // build job: 'milvus-publish-daily-docker', parameters: [[$class: 'StringParameterValue', name: 'BRANCH', value: "${params.IMAGE_VERSION}"]], wait: false
                                    build job: 'milvus-publish-daily-docker', parameters: [string(name: 'LOCAL_DOKCER_REGISTRY_URL', value: 'registry.milvus.io'), string(name: 'REMOTE_DOKCER_REGISTRY_URL', value: 'registry-1.docker.io'), string(name: 'REMOTE_DOCKER_CREDENTIALS_ID', value: 'milvus-docker-access-token'), string(name: 'BRANCH', value: String.valueOf(IMAGE_VERSION))], wait: false
                                } else {
                                    echo "Skip publish daily docker images ..."
                                }
                            }
                        }
                    }
                }

                stage("Deploy Test") {
                    steps {
                        container('milvus-test-env') {
                            script {
                                print "In Deploy Test Stage"
                                // use the idc context
                                // sh 'kubectl config use-context idc001'
                                if ("${params.CLUSTER_NAME}" == "idc-kubernetes") {
                                    print "Use cluster name idc001"
                                    sh 'export KUBECONFIG=/root/kube/.kube/config'
                                }
                                load "${env.WORKSPACE}/ci/jenkinsfile/deploy_test.groovy"
                            }
                        }
                    }
                }

                stage ("Cleanup Env") {
                    steps {
                        container('milvus-test-env') {
                            script {
                                load "${env.WORKSPACE}/ci/jenkinsfile/cleanup.groovy"
                            }
                        }
                    }
                }
            }
            post {
                success {
                    script {
                        echo "Milvus benchmark test success !"
                    }
                }
                aborted {
                    script {
                        echo "Milvus benchmark test aborted !"
                    }
                }
                failure {
                    script {
                        echo "Milvus benchmark test failed !"
                    }
                }
            }
        }
    }
}

boolean isTimeTriggeredBuild() {
    if (currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause').size() != 0) {
        return true
    }
    return false
}
