ps/Modules/Alkami.DevOps.Minikube/Public/Start-Minikube.ps1

207 lines
8.8 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Start-Minikube {
<#
.SYNOPSIS
Starts a Minikube Kubernetes cluster ready to host Alkami applications
.DESCRIPTION
Starts a Minikube Kubernetes cluster ready to host Alkami applications
.PARAMETER InstallDependencies
[switch] Will install all dependencies for running Minikube locally. Run this the first time starting Minikube.
.PARAMETER IncludeMetricsServer
[switch] Will include metrics server as part of the Minikube cluster.
.PARAMETER IncludeKibana
[switch] Will include Kibana as part of the Minikube cluster.
.PARAMETER IncludeArgoCD
[switch] Will include ArgoCD and Argo Rollouts as part of the Minikube cluster
.PARAMETER ForceReset
[switch] Will delete and recreate the entire minikube cluster. Required when wanting to edit the CPU and Memory allocation.
.PARAMETER Cpus
[uint32] Will set the Minikube CPU resource allocation to the specificed number of CPUs.
.PARAMETER Memory
[uint32] Will set the Minikube memory resource allocation to the specificed number of MegaBytes.
.PARAMETER IngressPort
[uint32] Will expose ingress traffic into the Minikube cluster on the specified port. Default: 10000
.EXAMPLE
Start-Minikube -InstallDependencies -IncludeMetricsServer -Cpus 6 -Memory 8 -IngressPort 7000
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[switch]$InstallDependencies,
[Parameter(Mandatory = $false)]
[switch]$IncludeMetricsServer,
[Parameter(Mandatory = $false)]
[switch]$IncludeKibana,
[Parameter(Mandatory = $false)]
[switch]$IncludeArgoCD,
[Parameter(Mandatory = $false)]
[switch]$ForceReset,
[Parameter(Mandatory = $false)]
[uint32]$Cpus = 5,
[Parameter(Mandatory = $false)]
[uint32]$Memory = 6,
[Parameter(Mandatory = $false)]
[uint32]$IngressPort = 10000
)
$ErrorActionPreference = "Stop"
$logLead = (Get-LogLeadName)
$resourcesPath = Join-Path $PSScriptRoot "Resources"
$config = Get-MinikubeConfigurationInformation
if ($InstallDependencies.IsPresent) {
Install-MinikubeDependencies
}
if ($ForceReset.IsPresent) {
minikube delete
}
# Configure WSL memory usage.
$wslConfigPath = "${Env:HOMEPATH}\.wslconfig"
Write-Host "$logLead : Configuring WSL resource settings at: $wslConfigPath."
$memoryLimit = "$($Memory)GB"
$wslConfig = @"
[wsl2]
memory=$memoryLimit
swap=0
"@
Set-Content -Path $wslConfigPath -Value $wslConfig -Force
Restart-Wsl
[string]$certsDirectory= "$resourcesPath\cacerts\*"
[string]$miniKubeCertsDirectory = "${Env:HOMEPATH}\.minikube\certs"
if (!(Test-Path $miniKubeCertsDirectory))
{
New-Item $miniKubeCertsDirectory -ItemType Directory
}
Write-Host "$logLead : Copying CA Certs to minikube cert location: ${miniKubeCertsDirectory}"
Copy-item -Force -Recurse $certsDirectory -Destination $miniKubeCertsDirectory
# The wslconfig allocates memory for wsl. Docker desktop doesn't get all of that memory so minikube will need less than 1024 bytes per gig allocated to wsl.
$memoryBytes = $Memory * 800
minikube config set cpus $Cpus
minikube config set memory $memoryBytes
minikube start --driver=docker `
--embed-certs `
--addons dashboard `
--addons ingress `
--docker-opt=dns=10.0.16.42 `
--docker-opt=dns=10.0.16.43 `
--docker-opt=dns-search=corp.alkamitech.com `
--docker-opt=dns-search=fh.local `
--ports=$($IngressPort):443 `
--ports=32000:32000 `
--extra-config=kubelet.housekeeping-interval=10s
if ($IncludeMetricsServer.IsPresent) {
minikube addons enable metrics-server
}
Write-Host "$logLead : Copying Kerberos configuration file. (krb5.ini)"
Copy-Item (Join-Path $resourcesPath "\krb5.ini") -Destination "C:\ProgramData\MIT\Kerberos5" -Force
Write-Host "$logLead : Configuring Kerberos ticket cache location environment variable."
# Set env variable for current session
$Env:KRB5CCNAME = "c:\ProgramData\MIT\Kerberos5\krb5cc_0"
# Set env variable for future sessions
[System.Environment]::SetEnvironmentVariable('KRB5CCNAME','c:\ProgramData\MIT\Kerberos5\krb5cc_0', [System.EnvironmentVariableTarget]::Machine)
Write-Host "$logLead : Creating localhost namespace..."
kubectl create ns localhost
Write-Host "$logLead : Initializing Kubernetes secrets..."
Reset-MinikubeSecrets
Write-Host "$logLead : Configuring tls for ingress"
kubectl apply -f (Join-Path $resourcesPath "ingress-tls-secret.yaml")
$ingressDeploy = kubectl get deployment/ingress-nginx-controller -n ingress-nginx -o yaml | ConvertFrom-Yaml
Write-Host "$logLead : Updating Ingress Nginx with proxy forwarding enabled..."
$ingressDeets = kubectl get cm ingress-nginx-controller -n ingress-nginx -o yaml | ConvertFrom-Yaml
$ingressDeets.Data.Add('use-forwarded-headers', 'true')
$ingressDeets | ConvertTo-Yaml | kubectl apply -f -
Write-Host "$logLead : Deleting ingress nginx deployment"
kubectl delete deployment/ingress-nginx-controller -n ingress-nginx
$ingressDeploy.spec.template.spec.containers[0].args += "--default-ssl-certificate=ingress-nginx/ingress-tls-secret"
$ingressDeploy.metadata.Remove('managedFields')
$ingressDeploy.Remove('status')
$modifiedDeploy = $ingressDeploy | ConvertTo-Yaml
Write-Host "$logLead : Recreating ingress nginx deployment with default ssl certificiate"
$modifiedDeploy | kubectl apply -f -
Write-Host "$logLead : Waiting for ingress nginx controller to be healthy before proceeding..."
kubectl rollout status deployment/ingress-nginx-controller -n ingress-nginx
kubectl apply -f (Join-Path $resourcesPath "ingress-dashboard.yaml")
Write-Host "$logLead : Checking for local kubernetes configuration file path: ${$localK8sConfigurationPath}"
if (-not (Test-Path $config.localK8sConfigurationPath)) {
Write-Host "$logLead : Missing local kubernetes configuration file path. Creating..."
New-Item -path $HOME -name $config.localKubernetesConfigurationFolder -type "directory"
New-Item -path $config.localK8sConfigurationPath -name $config.localServiceVersionsFileName -type "file" -value "# Use this file to configure which services to install. You can copy any existing gitops values file for any environment - for example: https://bitbucket.corp.alkami.net/projects/AUTO/repos/alkami.gitops.kubernetes/browse/alkami-services/environments/tde/values.tde.yaml"
New-Item -path $config.localK8sConfigurationPath -name $config.localServiceCustomizationsFileName -type "file" -value "# Use this file to customize any service definitions via env variables etc. You can also exclude a service from being installed into your local environment (exclude: true)"
} else {
Write-Host "$logLead : Found existing local kubernetes configuration file path at: $localK8sConfigurationPath"
}
if (!(helm repo list | select-string proget)) {
Write-Host "$logLead : Missing helm repo configuration. Adding proget repo..."
helm repo add proget "https://packagerepo.orb.alkamitech.com/helm/helm-charts"
}
if ($IncludeArgoCD.IsPresent) {
Write-Host "$logLead : Adding ArgoCD helm repo configuration..."
helm repo add argo https://argoproj.github.io/argo-helm
helm repo add argo-rollouts https://argoproj.github.io/argo-helm
}
Write-Host "$logLead : Updating helm repos..."
helm repo update
Write-Host "$logLead : Installing alkami-local-dev helm chart..."
helm install alkami-local-dev --repo https://packagerepo.orb.alkamitech.com/helm/helm-charts $config.helmChartName -n localhost --create-namespace --dependency-update -f $config.localServiceVersionsFilePath -f $config.localServiceCustomizationsFilePath
if ($IncludeKibana.IsPresent) {
helm install alkami-dev-kibana --repo https://packagerepo.orb.alkamitech.com/helm/helm-charts alkami-dev-kibana --dependency-update
}
if ($IncludeArgoCD.IsPresent) {
$currentDate = (get-date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
helm install argocd argo/argo-cd -n argocd --create-namespace --set server.ingress.enabled=true --set server.ingress.hosts[0]=argocd.localhost.dev.alkami.net --set server.extraArgs[0]='--insecure' --set config.secret.extraArgs[1]='--disable-auth' --set configs.secret.argocdServerAdminPassword='$2y$10$CyYdVLTiR8OO2gGwkQsAeuwAFYeSOzPH6Kf/aan7fLau57fgVaUaq' --set configs.secret.argocdServerAdminPasswordMtime="$currentDate"
helm install argo-rollouts argo-rollouts/argo-rollouts -n argo-rollouts --create-namespace
}
}