Module: Pangea::Kubernetes::Backends::AzureNixos

Extended by:
NixosBase
Includes:
Base
Defined in:
lib/pangea/kubernetes/backends/azure_nixos.rb

Overview

Azure NixOS backend — Azure VMs running NixOS with k3s/k8s via blackmatter-kubernetes modules.

Uses:

- Azure VMs for control plane (static)
- VM Scale Sets (VMSS) for worker node pools
- VNet + NSG for networking

No managed K8s services (AKS) — all k3s/k8s managed by NixOS.

Constant Summary

Constants included from NixosBase

NixosBase::AGENT_BOOTSTRAP_KEYS, NixosBase::COMMON_PORTS, NixosBase::JOIN_SERVER_PLACEHOLDER, NixosBase::VANILLA_K8S_PORTS

Class Method Summary collapse

Methods included from NixosBase

base_firewall_ports, build_agent_bootstrap_secrets, build_agent_cloud_init, build_bootstrap_secrets, build_secrets_hash, build_server_cloud_init, create_compute_instance, create_worker_pool, nixos_create_cluster, nixos_create_node_pool, post_create_instance

Methods included from Base

included

Class Method Details

.backend_nameObject



37
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 37

def backend_name = :azure_nixos

.create_cluster(ctx, name, config, result, tags) ⇒ Object

Create control plane Azure VMs (static)



107
108
109
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 107

def create_cluster(ctx, name, config, result, tags)
  nixos_create_cluster(ctx, name, config, result, tags)
end

.create_compute_instance(ctx, name, config, result, cloud_init, index, tags) ⇒ Object

— NixosBase template hooks —



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 118

def create_compute_instance(ctx, name, config, result, cloud_init, index, tags)
  system_pool = config.system_node_pool
  vm_size = system_pool.instance_types.first
  image_id = config.azure_image_id || config.nixos&.image_id
  rg_name = config.resource_group_name || result.network&.dig(:resource_group)&.name || "#{name}-rg"

  # Network interface
  nic = ctx.azurerm_network_interface(
    :"#{name}_cp_#{index}_nic",
    name: "#{name}-cp-#{index}-nic",
    resource_group_name: rg_name,
    location: config.region,
    ip_configuration: {
      name: 'internal',
      subnet_id: result.network&.dig(:subnet)&.id,
      private_ip_address_allocation: 'Dynamic',
      public_ip_address_id: nil
    },
    tags: tags
  )

  ctx.azurerm_linux_virtual_machine(
    :"#{name}_cp_#{index}",
    name: "#{name}-cp-#{index}",
    resource_group_name: rg_name,
    location: config.region,
    size: vm_size,
    network_interface_ids: [nic.id],
    admin_username: 'nixos',
    admin_ssh_key: {
      username: 'nixos',
      public_key: '${file("~/.ssh/id_ed25519.pub")}'
    },
    os_disk: {
      caching: 'ReadWrite',
      storage_account_type: 'Premium_LRS',
      disk_size_gb: system_pool.disk_size_gb
    },
    source_image_id: image_id,
    custom_data: cloud_init,
    identity: { type: 'SystemAssigned' },
    tags: tags.merge(
      Role: 'control-plane',
      NodeIndex: index.to_s,
      Distribution: config.distribution.to_s
    )
  )
end

.create_iam(_ctx, _name, _config, _tags) ⇒ Object

No standalone IAM — Azure VMs use Managed Identity



102
103
104
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 102

def create_iam(_ctx, _name, _config, _tags)
  Architecture::IamResult.new
end

.create_network(ctx, name, config, tags) ⇒ Object

Create Resource Group + VNet + Subnet + NSG



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 51

def create_network(ctx, name, config, tags)
  network = Architecture::AzureNetworkResult.new

  network.resource_group = ctx.azurerm_resource_group(
    :"#{name}_rg",
    name: "#{name}-rg",
    location: config.region,
    tags: tags
  )

  network.vnet = ctx.azurerm_virtual_network(
    :"#{name}_vnet",
    name: "#{name}-vnet",
    resource_group_name: network.resource_group.ref(:name),
    location: config.region,
    address_space: [config.network&.vpc_cidr || '10.0.0.0/16'],
    tags: tags
  )
  network.vpc = network.vnet

  subnet = ctx.azurerm_subnet(
    :"#{name}_subnet",
    name: "#{name}-subnet",
    resource_group_name: network.resource_group.ref(:name),
    virtual_network_name: network.vnet.ref(:name),
    address_prefixes: [config.network&.pod_cidr || '10.0.1.0/24']
  )
  network.add_subnet(:subnet, subnet)

  # Network Security Group
  network.nsg = ctx.azurerm_network_security_group(
    :"#{name}_nsg",
    name: "#{name}-nsg",
    resource_group_name: network.resource_group.ref(:name),
    location: config.region,
    security_rule: azure_nsg_rules(config.distribution),
    tags: tags
  )
  network.sg = network.nsg

  # Associate NSG with subnet
  ctx.azurerm_subnet_network_security_group_association(
    :"#{name}_nsg_assoc",
    subnet_id: subnet.id,
    network_security_group_id: network.nsg.id
  )

  network
end

.create_node_pool(ctx, name, cluster_ref, pool_config, tags) ⇒ Object

Create worker node pool via VMSS (VM Scale Set)



112
113
114
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 112

def create_node_pool(ctx, name, cluster_ref, pool_config, tags)
  nixos_create_node_pool(ctx, name, cluster_ref, pool_config, tags)
end

.create_worker_pool(ctx, name, _cluster_ref, pool_config, cloud_init, tags) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 167

def create_worker_pool(ctx, name, _cluster_ref, pool_config, cloud_init, tags)
  pool_name = :"#{name}_#{pool_config.name}"
  vm_size = pool_config.instance_types.first

  vmss = ctx.azurerm_linux_virtual_machine_scale_set(
    pool_name,
    name: "#{name}-#{pool_config.name}-vmss",
    resource_group_name: tags[:ResourceGroupName] || "#{name}-rg",
    location: tags[:Region] || 'eastus',
    sku: vm_size,
    instances: pool_config.effective_desired_size,
    admin_username: 'nixos',
    admin_ssh_key: {
      username: 'nixos',
      public_key: '${file("~/.ssh/id_ed25519.pub")}'
    },
    os_disk: {
      caching: 'ReadWrite',
      storage_account_type: 'Premium_LRS',
      disk_size_gb: pool_config.disk_size_gb
    },
    source_image_id: tags[:ImageId],
    custom_data: cloud_init,
    network_interface: {
      name: "#{name}-#{pool_config.name}-nic",
      primary: true,
      ip_configuration: {
        name: 'internal',
        subnet_id: tags[:SubnetId],
        primary: true
      }
    },
    identity: { type: 'SystemAssigned' },
    tags: tags.merge(
      NodePool: pool_config.name.to_s,
      Role: 'worker'
    )
  )

  # Autoscale setting
  ctx.azurerm_monitor_autoscale_setting(
    :"#{pool_name}_autoscale",
    name: "#{name}-#{pool_config.name}-autoscale",
    resource_group_name: tags[:ResourceGroupName] || "#{name}-rg",
    location: tags[:Region] || 'eastus',
    target_resource_id: vmss.id,
    profile: {
      name: 'default',
      capacity: {
        default: pool_config.effective_desired_size,
        minimum: pool_config.min_size,
        maximum: pool_config.max_size
      },
      rule: [{
        metric_trigger: {
          metric_name: 'Percentage CPU',
          metric_resource_id: vmss.id,
          operator: 'GreaterThan',
          threshold: 70,
          time_aggregation: 'Average',
          time_grain: 'PT1M',
          time_window: 'PT5M',
          statistic: 'Average'
        },
        scale_action: {
          direction: 'Increase',
          type: 'ChangeCount',
          value: 1,
          cooldown: 'PT5M'
        }
      }]
    }
  )

  vmss
end

.load_provider!Object



41
42
43
44
45
46
47
48
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 41

def load_provider!
  require required_gem
rescue LoadError => e
  raise LoadError,
        "Backend :azure_nixos requires gem 'pangea-azure'. " \
        "Add it to your Gemfile: gem 'pangea-azure'\n" \
        "Original error: #{e.message}"
end

.managed_kubernetes?Boolean

Returns:

  • (Boolean)


38
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 38

def managed_kubernetes? = false

.required_gemObject



39
# File 'lib/pangea/kubernetes/backends/azure_nixos.rb', line 39

def required_gem = 'pangea-azure'