1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
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
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
|
class Projects::ClustersController < Projects::ApplicationController
# before_action :authenticate_google_api
before_action :cluster
# before_action :authorize_admin_clusters! # TODO: Authentication
def index
if cluster
redirect_to action: 'edit'
else
redirect_to action: 'new'
end
end
##
# TODO:
# - Show form for "Create on Google Container Engine"
# - Show form for "Use existing kubernets cluster"
# - If user has not authroized yet, Show "Sign in with Google" button
# - If user has already authroized, Skip "Sign in with Google" button
# - user.is_authenticated_for_gcp?
# - user.authenticate_for_gcp!
# - Create this module which can be used from view
def new
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
end
end
##
# TODO:
# - If create on GKE, Use Google::Apis::ContainerV1::ContainerService
# - If create manually, save in db (Prob, Project > Setting)
# - Dry up with Service
# - Transaction
def create
if params['creation_type'] == 'on_gke'
# Create a cluster on GKE
results = api_client.projects_zones_clusters_create(
project_id: params['gcp_project_id'],
zone: params['cluster_zone'],
cluster_name: params['cluster_name'],
cluster_size: params['cluster_size'],
machine_type: params['machine_type']
)
# Update service
kubernetes_service.attributes = service_params(
active: true,
api_url: results['end_point'],
ca_pem: results['ca_cert'], # TODO: Decode Base64
namespace: params['project_namespace'],
token: 'aaa' # TODO: username/password
)
kubernetes_service.save!
# Save info
project.clusters.create(
creation_type: params['creation_type'],
gcp_project_id: params['gcp_project_id'],
cluster_zone: params['cluster_zone'],
cluster_name: params['cluster_name'],
service: kubernetes_service
)
elsif params['creation_type'] == 'manual'
# TODO: Transaction
project.kubernetes_service.save(
end_point: params['end_point'],
ca_cert: params['ca_cert'],
token: params['token'],
username: params['username'],
password: params['password'],
project_namespace: params['project_namespace']
)
project.clusters.create(
creation_type: params['creation_type'],
kubernetes_service: project.kubernetes_service
)
end
redirect_to action: 'index'
end
# TODO: Show results/status. Edits Swtich for enable/disable.
# If created with GKE, non-editable form. enable/disable switch.
# If created manually, editable form. enable/disable switch.
# GKE params are on-off swtich
# Manul params are on-off swtich, Endpoint, CACert, k8s Token, Proj namespace.
def edit
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
render :edit
end
# Get cluster information
api_client.projects_zones_clusters_get(
project_id: cluster.gcp_project_id,
zone: cluster.cluster_zone,
cluster_id: cluster.cluster_name
)
end
def update
cluster.update(schedule_params)
render :edit
end
# In presenter
# TODO: Generate a link to the cluster on GKE
def gcp_projects
# api_client.blah
# TODO: Return all avaiable GCP Projects.
# TODO: Return json
# TODO: Dry with concern
end
def gke_zones
# api_client.blah
# TODO: Return all avaiable zones on GKE.
# TODO: Return json
# TODO: Dry with concern
end
private
# def authenticate_google_api
# if cluster&.on_gke? && session[access_token_key].blank?
# redirect_to api_client.authorize_url(callback_import_url)
# end
# end
def cluster
# Each project has only one cluster, for now. In the future iteraiton, we'll support multiple clusters
@cluster ||= project.clusters.last
end
# def cluster_params
# params.require(:cluster).permit(:aaa)
# end
def api_client
@api_client ||=
GoogleApi::CloudPlatform::Client.new(
session[GoogleApi::CloudPlatform::Client.token_in_session],
callback_google_api_authorizations_url,
state: namespace_project_clusters_url.to_s
)
end
def kubernetes_service
@kubernetes_service ||= project.find_or_initialize_service('kubernetes')
end
def service_params(active:, api_url:, ca_pem:, namespace:, token:)
{
active: active,
api_url: api_url,
ca_pem: ca_pem,
namespace: namespace,
token: token
}
end
end
|