{{- $count := conv.ToInt .Env.LINUX_NODE_COUNT -}}
{{- range $i := seq 0 (sub $count 1) -}}
{{- $nodeNum := add $i 1 -}}
{{- $nodeName := printf "vm-linux-%d" $nodeNum -}}
{{- $nicName := printf "nic-linux-%d" $nodeNum -}}
{{- $pipName := printf "pip-linux-%d" $nodeNum -}}
{{- $extContainerdName := printf "vm-linux-%d-containerd" $nodeNum -}}
{{- $extDockerName := printf "vm-linux-%d-docker-legacy" $nodeNum -}}
{{- $computerName := printf "asovm-linux-%d" $nodeNum -}}{{/* NOTE: asovm-linux-%d here would have the l2bridge cni-plugin hang, maybe there's a problem with the name being too short? */}}
---
apiVersion: compute.azure.com/v1api20220301
kind: VirtualMachine
metadata:
  name: {{$nodeName}}
  namespace: aso
spec:
  location: {{$.Env.AZURE_LOCATION}}
  owner:
    name: {{$.Env.AZURE_RESOURCE_GROUP}}
  hardwareProfile:
    vmSize: Standard_D4s_v3
  networkProfile:
    networkInterfaces:
      - reference:
          group: network.azure.com
          kind: NetworkInterface
          name: {{$nicName}}
  osProfile:
    computerName: {{$computerName}}
    adminUsername: aso
    adminPassword:
      key: password
      name: aso-secret-windows
    linuxConfiguration:
      disablePasswordAuthentication: true
      ssh:
        publicKeys:
          - keyData: {{$.Env.PUBLIC_KEY}}
            path: /home/aso/.ssh/authorized_keys
  storageProfile:
    imageReference:
      publisher: Canonical
      offer: 0001-com-ubuntu-server-jammy
      sku: 22_04-lts
      version: latest
    osDisk:
      createOption: FromImage
      managedDisk:
        storageAccountType: Premium_LRS
---
apiVersion: compute.azure.com/v1api20220301
kind: VirtualMachinesExtension
metadata:
  name: {{$extContainerdName}}
  namespace: aso
spec:
  location: {{$.Env.AZURE_LOCATION}}
  owner:
    name: {{$nodeName}}
  publisher: Microsoft.Azure.Extensions
  type: CustomScript
  typeHandlerVersion: "2.1"
  autoUpgradeMinorVersion: true
  settings:
    commandToExecute: |
      /bin/bash -c '
      set -e
      echo "Installing containerd and Docker..."
      
      # Update package list
      apt-get update
      
      # Install prerequisites
      apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
      
      # Install Docker (includes containerd)
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
      echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
      apt-get update
      apt-get install -y docker-ce docker-ce-cli containerd.io
      
      # Configure containerd for Kubernetes
      mkdir -p /etc/containerd
      containerd config default | tee /etc/containerd/config.toml
      
      # Enable systemd cgroup driver (required for Kubernetes)
      sed -i "s/SystemdCgroup = false/SystemdCgroup = true/g" /etc/containerd/config.toml
      
      # Restart containerd
      systemctl restart containerd
      systemctl enable containerd
      
      # Enable and start Docker
      systemctl enable docker
      systemctl start docker
      
      # Verify installations
      containerd --version
      docker --version
      
      echo "Containerd and Docker installation completed successfully"
      '
---
apiVersion: compute.azure.com/v1api20220301
kind: VirtualMachinesExtension
metadata:
  name: {{$extDockerName}}
  namespace: aso
spec:
  location: {{$.Env.AZURE_LOCATION}}
  owner:
    name: {{$nodeName}}
  publisher: Microsoft.Azure.Extensions
  type: DockerExtension
  typeHandlerVersion: "1.0"
  autoUpgradeMinorVersion: true
---
apiVersion: network.azure.com/v1api20240301
kind: NetworkInterface
metadata:
  name: {{$nicName}}
  namespace: aso
spec:
  location: {{$.Env.AZURE_LOCATION}}
  owner:
    name: {{$.Env.AZURE_RESOURCE_GROUP}}
  networkSecurityGroup:
    reference:
      group: network.azure.com
      kind: NetworkSecurityGroup
      name: aso-sg
  ipConfigurations:
    - name: ipconfig-linux-{{$nodeNum}}
      subnet:
        reference:
          group: network.azure.com
          kind: VirtualNetworksSubnet
          name: subnet-aso
      publicIPAddress:
        reference:
          group: network.azure.com
          kind: PublicIPAddress
          name: {{$pipName}}
---
apiVersion: network.azure.com/v1api20240301
kind: PublicIPAddress
metadata:
  name: {{$pipName}}
  namespace: aso
spec:
  location: {{$.Env.AZURE_LOCATION}}
  owner:
    name: {{$.Env.AZURE_RESOURCE_GROUP}}
  publicIPAllocationMethod: Static
  sku:
    name: Standard
{{end}}
