Module: Pangea::Kubernetes::Backends::GcpNixos

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

Overview

GCP NixOS backend — GCE instances running NixOS with k3s/k8s via blackmatter-kubernetes modules.

Uses:

- GCE instances for control plane (static)
- Managed Instance Groups (MIGs) for worker node pools
- VPC + Firewall rules for networking
- Instance Templates for NixOS image + cloud-init

No managed K8s services (GKE) — 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



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

def backend_name = :gcp_nixos

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

Create control plane GCE instances (static, no MIG)



124
125
126
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 124

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 —



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
166
167
168
169
170
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 135

def create_compute_instance(ctx, name, config, result, cloud_init, index, tags)
  system_pool = config.system_node_pool
  machine_type = system_pool.instance_types.first
  image = config.gce_image || config.nixos&.image_id || 'nixos-24-05'

  ctx.google_compute_instance(
    :"#{name}_cp_#{index}",
    name: "#{name}-cp-#{index}",
    machine_type: machine_type,
    zone: "#{config.region}-a",
    project: config.project,
    boot_disk: {
      initialize_params: {
        image: image,
        size: system_pool.disk_size_gb
      }
    },
    network_interface: {
      network: result.network&.dig(:vpc)&.id,
      subnetwork: result.network&.dig(:subnet)&.id,
      access_config: {}
    },
    metadata: {
      'user-data' => cloud_init
    },
    service_account: {
      email: result.iam&.dig(:node_sa)&.email,
      scopes: ['cloud-platform']
    },
    labels: gce_labels(tags.merge(
      role: 'control-plane',
      node_index: index.to_s,
      distribution: config.distribution.to_s
    ))
  )
end

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

Service account for GCE instances (minimal permissions)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 101

def create_iam(ctx, name, config, tags)
  iam = Architecture::GcpIamResult.new

  iam.node_sa = ctx.(
    :"#{name}_node_sa",
    account_id: "#{name}-nixos-nodes",
    display_name: "#{name} NixOS K8s Node Service Account",
    project: config.project
  )

  %w[logging.logWriter monitoring.metricWriter].each do |role|
    ctx.google_project_iam_member(
      :"#{name}_node_#{role.gsub('.', '_')}",
      project: config.project,
      role: "roles/#{role}",
      member: "serviceAccount:#{iam.node_sa.email}"
    )
  end

  iam
end

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

Create VPC network + subnet + firewall rules



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
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 52

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

  network.vpc = ctx.google_compute_network(
    :"#{name}_network",
    name: "#{name}-network",
    auto_create_subnetworks: false,
    project: config.project
  )

  subnet = ctx.google_compute_subnetwork(
    :"#{name}_subnet",
    name: "#{name}-subnet",
    ip_cidr_range: config.network&.vpc_cidr || '10.0.0.0/20',
    region: config.region,
    network: network.vpc.id,
    project: config.project
  )
  network.add_subnet(:subnet, subnet)

  # Firewall rules for k3s/k8s
  network.firewall_internal = ctx.google_compute_firewall(
    :"#{name}_fw_internal",
    name: "#{name}-allow-internal",
    network: network.vpc.id,
    project: config.project,
    allow: [
      { protocol: 'tcp', ports: %w[0-65535] },
      { protocol: 'udp', ports: %w[0-65535] },
      { protocol: 'icmp' }
    ],
    source_ranges: [config.network&.vpc_cidr || '10.0.0.0/20']
  )

  network.firewall_external = ctx.google_compute_firewall(
    :"#{name}_fw_external",
    name: "#{name}-allow-external",
    network: network.vpc.id,
    project: config.project,
    allow: [
      { protocol: 'tcp', ports: %w[22 80 443 6443] }
    ],
    source_ranges: ['0.0.0.0/0']
  )

  network
end

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

Create worker node pool via Instance Template + MIG + Autoscaler



129
130
131
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 129

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



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
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 172

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

  # Instance Template
  template = ctx.google_compute_instance_template(
    :"#{pool_name}_template",
    name: "#{name}-#{pool_config.name}-template",
    machine_type: machine_type,
    project: tags[:Project],
    disk: [{
      source_image: tags[:Image] || 'nixos-24-05',
      disk_size_gb: pool_config.disk_size_gb,
      auto_delete: true,
      boot: true
    }],
    network_interface: {
      network: tags[:NetworkId],
      subnetwork: tags[:SubnetId],
      access_config: {}
    },
    metadata: { 'user-data' => cloud_init },
    labels: gce_labels(tags.merge(
      role: 'worker',
      node_pool: pool_config.name.to_s
    ))
  )

  # Managed Instance Group
  mig = ctx.google_compute_instance_group_manager(
    :"#{pool_name}_mig",
    name: "#{name}-#{pool_config.name}-mig",
    base_instance_name: "#{name}-#{pool_config.name}",
    zone: "#{tags[:Region] || 'us-central1'}-a",
    project: tags[:Project],
    target_size: pool_config.effective_desired_size,
    version: [{
      instance_template: template.id
    }]
  )

  # Autoscaler
  ctx.google_compute_autoscaler(
    :"#{pool_name}_autoscaler",
    name: "#{name}-#{pool_config.name}-autoscaler",
    zone: "#{tags[:Region] || 'us-central1'}-a",
    project: tags[:Project],
    target: mig.id,
    autoscaling_policy: {
      min_replicas: pool_config.min_size,
      max_replicas: pool_config.max_size,
      cpu_utilization: { target: 0.7 }
    }
  )

  mig
end

.load_provider!Object



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

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

.managed_kubernetes?Boolean

Returns:

  • (Boolean)


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

def managed_kubernetes? = false

.required_gemObject



40
# File 'lib/pangea/kubernetes/backends/gcp_nixos.rb', line 40

def required_gem = 'pangea-gcp'