export async function deploySMICanary()

in src/strategyHelpers/canary/smiCanaryHelper.ts [19:113]


export async function deploySMICanary(
   filePaths: string[],
   kubectl: Kubectl,
   onlyDeployStable: boolean = false
): Promise<DeployResult> {
   const canaryReplicasInput = core.getInput('baseline-and-canary-replicas')
   let canaryReplicaCount
   let calculateReplicas = true
   if (canaryReplicasInput !== '') {
      canaryReplicaCount = parseInt(canaryReplicasInput)
      calculateReplicas = false
      core.debug(
         `read replica count ${canaryReplicaCount} from input: ${canaryReplicasInput}`
      )
   }

   if (canaryReplicaCount < 0 && canaryReplicaCount > 100)
      throw Error('Baseline-and-canary-replicas must be between 0 and 100')

   const newObjectsList = []
   for await (const filePath of filePaths) {
      try {
         const fileContents = fs.readFileSync(filePath).toString()
         const inputObjects: K8sObject[] = yaml.loadAll(
            fileContents
         ) as K8sObject[]
         for (const inputObject of inputObjects) {
            const name = inputObject.metadata.name
            const kind = inputObject.kind

            if (!onlyDeployStable && isDeploymentEntity(kind)) {
               if (calculateReplicas) {
                  // calculate for each object
                  const percentage = parseInt(
                     core.getInput('percentage', {required: true})
                  )
                  canaryReplicaCount =
                     podCanaryHelper.calculateReplicaCountForCanary(
                        inputObject,
                        percentage
                     )
                  core.debug(`calculated replica count ${canaryReplicaCount}`)
               }

               core.debug('Creating canary object')
               const newCanaryObject =
                  canaryDeploymentHelper.getNewCanaryResource(
                     inputObject,
                     canaryReplicaCount
                  )
               newObjectsList.push(newCanaryObject)

               const stableObject = await canaryDeploymentHelper.fetchResource(
                  kubectl,
                  kind,
                  canaryDeploymentHelper.getStableResourceName(name)
               )
               if (stableObject) {
                  core.debug(
                     `Stable object found for ${kind} ${name}. Creating baseline objects`
                  )
                  const newBaselineObject =
                     canaryDeploymentHelper.getBaselineDeploymentFromStableDeployment(
                        stableObject,
                        canaryReplicaCount
                     )
                  newObjectsList.push(newBaselineObject)
               }
            } else if (isDeploymentEntity(kind)) {
               core.debug(
                  `creating stable deployment with ${inputObject.spec.replicas} replicas`
               )
               const stableDeployment =
                  canaryDeploymentHelper.getStableResource(inputObject)
               newObjectsList.push(stableDeployment)
            } else {
               // Update non deployment entity or stable deployment as it is
               newObjectsList.push(inputObject)
            }
         }
      } catch (error) {
         core.error(`Failed to process file at ${filePath}: ${error.message}`)
         throw error
      }
   }
   core.debug(
      `deploying canary objects with SMI: \n ${JSON.stringify(newObjectsList)}`
   )
   const newFilePaths = fileHelper.writeObjectsToFile(newObjectsList)
   const forceDeployment = core.getInput('force').toLowerCase() === 'true'
   const result = await kubectl.apply(newFilePaths, forceDeployment)
   const svcDeploymentFiles = await createCanaryService(kubectl, filePaths)
   newFilePaths.push(...svcDeploymentFiles)
   return {execResult: result, manifestFiles: newFilePaths}
}