Module: Rails::Gke

Defined in:
lib/rails/gke.rb,
lib/rails/gke/version.rb,
lib/rails/gke/initialize.rb

Defined Under Namespace

Modules: Initialize Classes: Configuration, Error

Constant Summary collapse

VERSION =
"0.6.9"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



8
9
10
# File 'lib/rails/gke.rb', line 8

def configuration
  @configuration
end

Class Method Details

.add_backend_service(service_name: "grpc-gke-helloworld-service", zone: "us-central1-a", neg_name: "") ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/rails/gke.rb', line 50

def add_backend_service service_name: "grpc-gke-helloworld-service", zone: "us-central1-a", neg_name: ""
  system "gcloud compute -q backend-services add-backend #{service_name} \
          --global \
          --network-endpoint-group #{neg_name} \
          --network-endpoint-group-zone #{zone} \
          --balancing-mode RATE \
          --max-rate-per-endpoint 5"
end

.apply_deploymentObject



164
165
166
167
# File 'lib/rails/gke.rb', line 164

def apply_deployment
  app = Rails::Gke.configuration.app
  system("kubectl apply -f deployment.yml --namespace=#{app}")
end

.apply_ingressObject



179
180
181
182
# File 'lib/rails/gke.rb', line 179

def apply_ingress
  app = Rails::Gke.configuration.app
  system("kubectl apply -f ingress.yml --namespace=#{app}")
end

.apply_secretObject



169
170
171
172
# File 'lib/rails/gke.rb', line 169

def apply_secret
  app = Rails::Gke.configuration.app
  system("kubectl apply -f secret.yml --namespace=#{app}")
end

.apply_serviceObject



174
175
176
177
# File 'lib/rails/gke.rb', line 174

def apply_service
  app = Rails::Gke.configuration.app
  system("kubectl apply -f service.yml --namespace=#{app}")
end

.configure {|configuration| ... } ⇒ Object

Yields:



264
265
266
267
# File 'lib/rails/gke.rb', line 264

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.create_backend_service(service_name: "grpc-gke-helloworld-service", health_check_name: "grpc-gke-helloworld-hc") ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rails/gke.rb', line 63

def create_backend_service service_name: "grpc-gke-helloworld-service", health_check_name: "grpc-gke-helloworld-hc"
  system "gcloud compute -q backend-services create #{service_name} \
          --global \
          --load-balancing-scheme=INTERNAL_SELF_MANAGED \
          --protocol=GRPC \
          --health-checks #{health_check_name}"
end

.create_clusterObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rails/gke.rb', line 127

def create_cluster
  app = Rails::Gke.configuration.app
  network = Rails::Gke.configuration.network
  sub_network = Rails::Gke.configuration.network
  machine_type = Rails::Gke.configuration.machine_type
  zone = Rails::Gke.configuration.zone
  system("gcloud container clusters create #{app} \
          --network #{network} \
          --subnetwork #{sub_network} \
          --zone #{zone} \
          --scopes=https://www.googleapis.com/auth/cloud-platform \
          --machine-type #{machine_type} \
          --enable-autorepair \
          --enable-ip-alias \
          --num-nodes 2 \
          --enable-autoscaling \
          --min-nodes 1 \
          --max-nodes 4 \
          --tags=allow-health-checks")
end

.create_firewall_rule(firewall_rule_name: "grpc-gke-allow-health-checks") ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/rails/gke.rb', line 75

def create_firewall_rule firewall_rule_name: "grpc-gke-allow-health-checks"
  system "gcloud compute -q firewall-rules create #{firewall_rule_name} \
          --network #{Rails::Gke.configuration.network} \
          --action allow \
          --direction INGRESS \
          --source-ranges 35.191.0.0/16,130.211.0.0/22 \
          --target-tags allow-health-checks \
          --rules tcp:50051"
end

.create_forwarding_rule(forwarding_rule_name: "grpc-gke-forwarding-rule", proxy_name: "grpc-gke-proxy", port: 8000) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rails/gke.rb', line 14

def create_forwarding_rule forwarding_rule_name: "grpc-gke-forwarding-rule", proxy_name: "grpc-gke-proxy", port: 8000
  system "gcloud compute -q forwarding-rules create #{forwarding_rule_name} \
          --global \
          --load-balancing-scheme=INTERNAL_SELF_MANAGED \
          --address=0.0.0.0 \
          --target-grpc-proxy=#{proxy_name} \
          --ports #{port} \
          --network #{Rails::Gke.configuration.network}"
end

.create_health_check(health_check_name: "grpc-gke-helloworld-hc") ⇒ Object



89
90
91
# File 'lib/rails/gke.rb', line 89

def create_health_check health_check_name: "grpc-gke-helloworld-hc"
  system "gcloud compute -q health-checks create grpc #{health_check_name} --use-serving-port"
end

.create_ipObject



159
160
161
162
# File 'lib/rails/gke.rb', line 159

def create_ip
  ip_name = Rails::Gke.configuration.app.to_s + "-ip"
  system("gcloud compute addresses create #{ip_name} --global")
end

.create_namespaceObject



154
155
156
157
# File 'lib/rails/gke.rb', line 154

def create_namespace
  app = Rails::Gke.configuration.app
  system("kubectl create namespace #{app}")
end

.create_networkObject



93
94
95
96
# File 'lib/rails/gke.rb', line 93

def create_network
  return "Error: Please Set Rails::Gke.configuration" if Rails::Gke.configuration.nil?
  system("gcloud compute networks create #{Rails::Gke.configuration.network}")
end

.create_network_groupObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/rails/gke.rb', line 102

def create_network_group
  app = Rails::Gke.configuration.app
  network = Rails::Gke.configuration.network
  sub_network = Rails::Gke.configuration.network
  system("gcloud compute network-endpoint-groups create #{app} \
          --default-port=0 \
          --network #{network} \
          --subnet #{sub_network} \
          --global")
end

.create_path_matcher(url_map_name: "grpc-gke-url-map", service_name: "grpc-gke-helloworld-service", path_matcher_name: "grpc-gke-path-matcher", hostname: "helloworld-gke", port: "8000") ⇒ Object



34
35
36
37
38
39
# File 'lib/rails/gke.rb', line 34

def create_path_matcher url_map_name: "grpc-gke-url-map", service_name: "grpc-gke-helloworld-service", path_matcher_name: "grpc-gke-path-matcher", hostname: "helloworld-gke", port: "8000"
  system "gcloud compute -q url-maps add-path-matcher #{url_map_name} \
          --default-service #{service_name} \
          --path-matcher-name #{path_matcher_name} \
          --new-hosts #{hostname}:#{port}"
end

.create_sslObject



252
253
254
# File 'lib/rails/gke.rb', line 252

def create_ssl
  system("gcloud compute ssl-certificates create #{Rails::Gke.configuration.app}-ssl --domains=#{Rails::Gke.configuration.domain} --global")
end

.create_target_grpc_proxy(proxy_name: "grpc-gke-proxy", url_map_name: "grpc-gke-url-map") ⇒ Object



28
29
30
31
32
# File 'lib/rails/gke.rb', line 28

def create_target_grpc_proxy proxy_name: "grpc-gke-proxy", url_map_name: "grpc-gke-url-map"
  system "gcloud compute -q target-grpc-proxies create #{proxy_name} \
          --url-map #{url_map_name} \
          --validate-for-proxyless"
end

.create_url_map(url_map_name: "grpc-gke-url-map", service_name: "grpc-gke-helloworld-service") ⇒ Object



45
46
47
48
# File 'lib/rails/gke.rb', line 45

def create_url_map url_map_name: "grpc-gke-url-map", service_name: "grpc-gke-helloworld-service"
  system "gcloud compute -q url-maps create #{url_map_name} \
          --default-service #{service_name}"
end

.delete_backend_service(service_name: "grpc-gke-helloworld-service") ⇒ Object



59
60
61
# File 'lib/rails/gke.rb', line 59

def delete_backend_service service_name: "grpc-gke-helloworld-service"
  system "gcloud compute -q backend-services delete #{service_name} --global"
end

.delete_cluster(cluster_name: "grpc-td-cluster") ⇒ Object



123
124
125
# File 'lib/rails/gke.rb', line 123

def delete_cluster cluster_name: "grpc-td-cluster"
  system "gcloud container clusters delete #{cluster_name} --zone #{Rails::Gke.configuration.zone} -q"
end

.delete_deploymentObject



184
185
186
187
# File 'lib/rails/gke.rb', line 184

def delete_deployment
  app = Rails::Gke.configuration.app
  system("kubectl delete -f deployment.yml --namespace=#{app}")
end

.delete_firewall_rule(firewall_rule_name: "grpc-gke-allow-health-checks") ⇒ Object



71
72
73
# File 'lib/rails/gke.rb', line 71

def delete_firewall_rule firewall_rule_name: "grpc-gke-allow-health-checks"
  system "gcloud compute -q firewall-rules delete #{firewall_rule_name}"
end

.delete_forwarding_rule(forwarding_rule_name: "grpc-gke-forwarding-rule") ⇒ Object



10
11
12
# File 'lib/rails/gke.rb', line 10

def delete_forwarding_rule forwarding_rule_name: "grpc-gke-forwarding-rule"
  system "gcloud compute -q forwarding-rules delete #{forwarding_rule_name} --global"
end

.delete_health_check(health_check_name: "grpc-gke-helloworld-hc") ⇒ Object



85
86
87
# File 'lib/rails/gke.rb', line 85

def delete_health_check health_check_name: "grpc-gke-helloworld-hc"
  system "gcloud compute -q health-checks delete #{health_check_name}"
end

.delete_ingressObject



199
200
201
202
# File 'lib/rails/gke.rb', line 199

def delete_ingress
  app = Rails::Gke.configuration.app
  system("kubectl delete -f ingress.yml --namespace=#{app}")
end

.delete_network_group_list(neg_name: "") ⇒ Object



119
120
121
# File 'lib/rails/gke.rb', line 119

def delete_network_group_list neg_name: ""
  system "gcloud compute network-endpoint-groups delete #{neg_name} --zone #{Rails::Gke.configuration.zone} -q"
end

.delete_secretObject



189
190
191
192
# File 'lib/rails/gke.rb', line 189

def delete_secret
  app = Rails::Gke.configuration.app
  system("kubectl delete -f secret.yml --namespace=#{app}")
end

.delete_serviceObject



194
195
196
197
# File 'lib/rails/gke.rb', line 194

def delete_service
  app = Rails::Gke.configuration.app
  system("kubectl delete -f service.yml --namespace=#{app}")
end

.delete_target_grpc_proxy(proxy_name: "grpc-gke-proxy") ⇒ Object



24
25
26
# File 'lib/rails/gke.rb', line 24

def delete_target_grpc_proxy proxy_name: "grpc-gke-proxy"
  system "gcloud compute -q target-grpc-proxies delete #{proxy_name}"
end

.delete_url_map(url_map_name: "grpc-gke-url-map") ⇒ Object



41
42
43
# File 'lib/rails/gke.rb', line 41

def delete_url_map url_map_name: "grpc-gke-url-map"
  system "gcloud compute -q url-maps delete #{url_map_name}"
end

.get_clustersObject



234
235
236
# File 'lib/rails/gke.rb', line 234

def get_clusters
  system("kubectl config get-clusters")
end

.get_credentialsObject



246
247
248
249
250
# File 'lib/rails/gke.rb', line 246

def get_credentials
  app = Rails::Gke.configuration.app
  zone = Rails::Gke.configuration.zone
  system("gcloud container clusters get-credentials #{app} -cluster --zone #{zone}")
end

.get_current_clusterObject



238
239
240
# File 'lib/rails/gke.rb', line 238

def get_current_cluster
  system("kubectl config current-context")
end

.get_ingressObject



222
223
224
225
# File 'lib/rails/gke.rb', line 222

def get_ingress
  app = Rails::Gke.configuration.app
  system("kubectl get ingress --namespace=#{app}")
end

.get_network_group_listObject



98
99
100
# File 'lib/rails/gke.rb', line 98

def get_network_group_list
  system "gcloud compute network-endpoint-groups list"
end

.get_podsObject



212
213
214
215
# File 'lib/rails/gke.rb', line 212

def get_pods
  app = Rails::Gke.configuration.app
  system("kubectl get pods --namespace=#{app}")
end

.get_svcObject



217
218
219
220
# File 'lib/rails/gke.rb', line 217

def get_svc
  app = Rails::Gke.configuration.app
  system("kubectl get svc --namespace=#{app}")
end

.resize_cluster(pool_name: "default-pool", node_num: 1) ⇒ Object



148
149
150
151
152
# File 'lib/rails/gke.rb', line 148

def resize_cluster pool_name: "default-pool", node_num: 1
  app = Rails::Gke.configuration.app
  zone = Rails::Gke.configuration.zone
  system "gcloud container clusters resize #{app} --node-pool #{pool_name} --num-nodes #{node_num} --zone #{zone}"
end

.run_testObject



227
228
229
230
231
232
# File 'lib/rails/gke.rb', line 227

def run_test
  app = Rails::Gke.configuration.app
  system("docker rm -f web")
  system("docker build . -t #{app}:latest")
  system("docker run --name web -it --env-file $PWD/.local_env -p 3000:3000 #{app}:latest")
end

.set_network_group_list_envObject



113
114
115
116
117
# File 'lib/rails/gke.rb', line 113

def set_network_group_list_env
  app = Rails::Gke.configuration.app
  system "NEG_NAME=$(gcloud compute network-endpoint-groups list | grep #{app} | awk '{print $1}')"
  `echo $NEG_NAME`
end

.update_container(version: "latest") ⇒ Object



204
205
206
207
208
209
210
# File 'lib/rails/gke.rb', line 204

def update_container version: "latest"
  app = Rails::Gke.configuration.app
  project_id = Rails::Gke.configuration.project_id
  system("docker build . -t #{app}:#{version}")
  system("docker tag #{app}:#{version} asia.gcr.io/#{project_id}/#{app}:#{version}")
  system("docker push asia.gcr.io/#{project_id}/#{app}:#{version}")
end

.update_proxyObject



256
257
258
259
260
261
# File 'lib/rails/gke.rb', line 256

def update_proxy
  system("gcloud compute target-https-proxies update TARGET_PROXY_NAME \
  --ssl-certificates SSL_CERTIFICATE_LIST \
  --global-ssl-certificates \
  --global")
end

.use_context(cluster:) ⇒ Object



242
243
244
# File 'lib/rails/gke.rb', line 242

def use_context cluster:
  system("kubectl config use-context #{cluster}")
end