Module: Pangea::Kubernetes::Backends::GcpGke

Includes:
Base
Defined in:
lib/pangea/kubernetes/backends/gcp_gke.rb

Overview

GCP GKE backend — creates managed GKE clusters with VPC-native networking.

Class Method Summary collapse

Methods included from Base

included

Class Method Details

.backend_nameObject



27
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 27

def backend_name = :gcp

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

Create the GKE cluster



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 94

def create_cluster(ctx, name, config, result, tags)
  cluster_attrs = {
    name: "#{name}-cluster",
    location: config.region,
    project: config.project,
    initial_node_count: 1,
    remove_default_node_pool: true,
    min_master_version: config.kubernetes_version,
    deletion_protection: false,
    networking_mode: 'VPC_NATIVE',
    resource_labels: gke_labels(tags)
  }

  # VPC-native networking
  if result.network
    cluster_attrs[:network] = result.network[:vpc]&.id
    cluster_attrs[:subnetwork] = result.network[:subnet]&.id
    cluster_attrs[:ip_allocation_policy] = {
      cluster_secondary_range_name: "#{name}-pods",
      services_secondary_range_name: "#{name}-services"
    }
  end

  # Private cluster
  if config.network&.private_endpoint
    cluster_attrs[:private_cluster_config] = {
      enable_private_nodes: true,
      enable_private_endpoint: !config.network.public_endpoint,
      master_ipv4_cidr_block: '172.16.0.0/28'
    }
  end

  # Workload Identity
  if config.project
    cluster_attrs[:workload_identity_config] = {
      workload_pool: "#{config.project}.svc.id.goog"
    }
  end

  # Release channel
  cluster_attrs[:release_channel] = { channel: 'REGULAR' }

  ctx.google_container_cluster(:"#{name}_cluster", cluster_attrs)
end

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

GKE uses Workload Identity — no standalone IAM resources needed



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 69

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

  # Service account for GKE nodes
  iam.node_sa = ctx.(
    :"#{name}_node_sa",
    account_id: "#{name}-gke-nodes",
    display_name: "#{name} GKE Node Service Account",
    project: config.project
  )

  # Bind minimum required roles
  %w[logging.logWriter monitoring.metricWriter monitoring.viewer].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 GCP VPC network + subnet



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 41

def create_network(ctx, name, config, tags)
  network = Architecture::NetworkResult.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,
    secondary_ip_range: [
      { range_name: "#{name}-pods", ip_cidr_range: config.network&.pod_cidr || '10.1.0.0/16' },
      { range_name: "#{name}-services", ip_cidr_range: config.network&.service_cidr || '10.2.0.0/20' }
    ]
  )
  network.add_subnet(:subnet, subnet)

  network
end

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

Create a GKE node pool



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 140

def create_node_pool(ctx, name, cluster_ref, pool_config, tags)
  pool_name = :"#{name}_#{pool_config.name}"

  node_pool_attrs = {
    name: "#{name}-#{pool_config.name}",
    cluster: cluster_ref.id,
    location: tags[:Region] || 'us-central1',
    initial_node_count: pool_config.effective_desired_size,
    node_config: {
      machine_type: pool_config.instance_types.first,
      disk_size_gb: pool_config.disk_size_gb,
      oauth_scopes: ['https://www.googleapis.com/auth/cloud-platform'],
      labels: pool_config.labels.merge(
        'node-pool' => pool_config.name.to_s
      )
    },
    autoscaling: {
      min_node_count: pool_config.min_size,
      max_node_count: pool_config.max_size
    }
  }

  ctx.google_container_node_pool(pool_name, node_pool_attrs)
end

.load_provider!Object



31
32
33
34
35
36
37
38
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 31

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

.managed_kubernetes?Boolean

Returns:

  • (Boolean)


28
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 28

def managed_kubernetes? = true

.required_gemObject



29
# File 'lib/pangea/kubernetes/backends/gcp_gke.rb', line 29

def required_gem = 'pangea-gcp'