summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2017-09-26 02:11:26 +0900
committerShinya Maeda <shinya@gitlab.com>2017-09-26 02:11:26 +0900
commitbdc618c289b8c2b996a3ef92b7748966c311d28a (patch)
tree62fbc8778f67c14c907b50504c051433bd4536f8
parentcf8140a752afe9a00197b11c9f9d38098717e6df (diff)
downloadgitlab-ce-bdc618c289b8c2b996a3ef92b7748966c311d28a.tar.gz
ok
-rw-r--r--app/controllers/google_api/authorizations_controller.rb20
-rw-r--r--app/controllers/projects/clusters_controller.rb59
-rw-r--r--app/views/projects/clusters/new.html.haml6
-rw-r--r--db/migrate/20170924094327_create_ci_clusters.rb34
-rw-r--r--db/schema.rb13
-rw-r--r--lib/google_api/cloud_platform/client.rb19
6 files changed, 110 insertions, 41 deletions
diff --git a/app/controllers/google_api/authorizations_controller.rb b/app/controllers/google_api/authorizations_controller.rb
index e99c38025b8..1fafd7e88be 100644
--- a/app/controllers/google_api/authorizations_controller.rb
+++ b/app/controllers/google_api/authorizations_controller.rb
@@ -1,11 +1,11 @@
module GoogleApi
class AuthorizationsController < ApplicationController
- # callback_google_api_authorizations GET|POST /google_api/authorizations/callback(.:format) google_api/authorizations#callback
- ##
- # TODO:
- # - Is it ok to use both "http://localhost:3000/google_api/authorizations/callback"(For login) and "http://localhost:3000/google_api/authorizations/callback"(For API token)
+ # /google_api/authorizations/callback(.:format)
def callback
- session[access_token_key] = api_client.get_token(params[:code])
+ # TODO: Error handling
+ session[GoogleApi::CloudPlatform::Client.token_in_session] =
+ GoogleApi::Authentication.new(nil, callback_google_api_authorizations_url)
+ .get_token(params[:code])
if params[:state]
redirect_to params[:state]
@@ -13,15 +13,5 @@ module GoogleApi
redirect_to root_url
end
end
-
- def api_client
- @api_client ||=
- GoogleApi::Authentication.new(nil, callback_google_api_authorizations_url)
- end
-
- def access_token_key
- # :"#{api_client.scope}_access_token"
- :"hoge_access_token" # TODO:
- end
end
end
diff --git a/app/controllers/projects/clusters_controller.rb b/app/controllers/projects/clusters_controller.rb
index 5c9319f661a..2aafc6364eb 100644
--- a/app/controllers/projects/clusters_controller.rb
+++ b/app/controllers/projects/clusters_controller.rb
@@ -22,7 +22,7 @@ class Projects::ClustersController < Projects::ApplicationController
# - user.authenticate_for_gcp!
# - Create this module which can be used from view
def new
- unless session[access_token_key]
+ unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
end
end
@@ -33,6 +33,48 @@ class Projects::ClustersController < Projects::ApplicationController
# - If create manually, save in db (Prob, Project > Setting)
# - Dry up with Service
def create
+ if params['creation_type'] == 'on_gke'
+ results = api_client.projects_zones_clusters_create(
+ params['gcp_project_id'],
+ params['cluster_zone'],
+ params['cluster_name'],
+ params['cluster_size']
+ )
+
+ # TODO: How to create
+ project.kubernetes_service.save(
+ end_point: results['end_point'],
+ ca_cert: results['ca_cert'],
+ token: nil,
+ username: results['username'],
+ password: results['password'],
+ project_namespace: params['project_namespace']
+ )
+
+ 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'],
+ kubernetes_service: project.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
@@ -42,7 +84,7 @@ class Projects::ClustersController < Projects::ApplicationController
# GKE params are on-off swtich
# Manul params are on-off swtich, Endpoint, CACert, k8s Token, Proj namespace.
def edit
- unless session[access_token_key]
+ unless session[GoogleApi::CloudPlatform::Client.token_in_session]
@authorize_url = api_client.authorize_url
end
end
@@ -82,21 +124,16 @@ class Projects::ClustersController < Projects::ApplicationController
@cluster ||= project.clusters.first
end
- def cluster_params
- params.require(:cluster).permit(:aaa)
- end
+ # def cluster_params
+ # params.require(:cluster).permit(:aaa)
+ # end
def api_client
@api_client ||=
GoogleApi::CloudPlatform::Client.new(
- session[access_token_key],
+ session[GoogleApi::CloudPlatform::Client.token_in_session],
callback_google_api_authorizations_url,
state: namespace_project_clusters_url.to_s
)
end
-
- def access_token_key
- # :"#{api_client.scope}_access_token"
- :"hoge_access_token" # TODO:
- end
end
diff --git a/app/views/projects/clusters/new.html.haml b/app/views/projects/clusters/new.html.haml
index 5e291d07835..b716cc51c20 100644
--- a/app/views/projects/clusters/new.html.haml
+++ b/app/views/projects/clusters/new.html.haml
@@ -12,6 +12,6 @@ Create a new cluster
%br
Avaiable zones
%br
- = link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, param1: 'value1', param2: 'value2'), method: :post
- = link_to "Use existing kubernets cluster", namespace_project_clusters_path(@project.namespace, @project, param1: 'value1', param2: 'value2'), method: :post
-
+ = link_to "Create on Google Container Engine", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'on_gke', cluster_name: 'new-cluster-shinya', gcp_project_id: 'gitlab-internal', cluster_zone: 'gitlab-internal', cluster_size: 'gitlab-internal', project_namespace: 'aaa'), method: :post
+ %br
+ = link_to "Use existing kubernets cluster", namespace_project_clusters_path(@project.namespace, @project, creation_type: 'manual', end_point: 'xxx.xxx.xxx.xxx', ca_cert: 'xxx...xxx', token: 'xxx', project_namespace: 'aaa'), method: :post
diff --git a/db/migrate/20170924094327_create_ci_clusters.rb b/db/migrate/20170924094327_create_ci_clusters.rb
index 86e75edf203..7a35fe35605 100644
--- a/db/migrate/20170924094327_create_ci_clusters.rb
+++ b/db/migrate/20170924094327_create_ci_clusters.rb
@@ -1,26 +1,48 @@
class CreateCiClusters < ActiveRecord::Migration
DOWNTIME = false
- def change
+ def up
create_table :ci_clusters do |t|
t.integer :project_id
t.integer :owner_id
- t.datetime_with_timezone :created_at, null: false
- t.datetime_with_timezone :updated_at, null: false
+ t.integer :service_id
+
+ # General
t.boolean :enabled, default: true
+ t.integer :creation_type # manual or on_gke
+
+ # k8s integration specific
+ t.string :project_namespace
+
+ # Cluster details
t.string :end_point
- t.text :ca_cert # Base64?
+ t.text :ca_cert
t.string :token
t.string :username
t.string :password
- t.string :project_namespace
- t.integer :creation_type # manual or on_gke
+
+ # GKE
+ t.string :gcp_project_id
+ t.string :cluster_zone
+ t.string :cluster_name
+
+ t.datetime_with_timezone :created_at, null: false
+ t.datetime_with_timezone :updated_at, null: false
end
+ # create_table :ci_gke_clusters do |t|
+ # t.integer :ci_cluster_id
+ # t.string :gcp_project_id
+ # t.string :cluster_zone
+ # t.string :cluster_name
+ # end
+ # add_foreign_key :ci_gke_clusters, :ci_clusters
+
# TODO: fk, index, encypt
add_foreign_key :ci_clusters, :projects
add_foreign_key :ci_clusters, :users, column: :owner_id
+ add_foreign_key :ci_clusters, :services
end
def down
diff --git a/db/schema.rb b/db/schema.rb
index f3e174c847f..8000229d5fa 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -270,16 +270,20 @@ ActiveRecord::Schema.define(version: 20170924094327) do
create_table "ci_clusters", force: :cascade do |t|
t.integer "project_id"
t.integer "owner_id"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
+ t.integer "service_id"
t.boolean "enabled", default: true
+ t.integer "creation_type"
+ t.string "project_namespace"
t.string "end_point"
t.text "ca_cert"
t.string "token"
t.string "username"
t.string "password"
- t.string "project_namespace"
- t.integer "creation_type"
+ t.string "gcp_project_id"
+ t.string "cluster_zone"
+ t.string "cluster_name"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
create_table "ci_group_variables", force: :cascade do |t|
@@ -1701,6 +1705,7 @@ ActiveRecord::Schema.define(version: 20170924094327) do
add_foreign_key "ci_builds", "ci_stages", column: "stage_id", name: "fk_3a9eaa254d", on_delete: :cascade
add_foreign_key "ci_builds", "projects", name: "fk_befce0568a", on_delete: :cascade
add_foreign_key "ci_clusters", "projects"
+ add_foreign_key "ci_clusters", "services"
add_foreign_key "ci_clusters", "users", column: "owner_id"
add_foreign_key "ci_group_variables", "namespaces", column: "group_id", name: "fk_33ae4d58d8", on_delete: :cascade
add_foreign_key "ci_pipeline_schedule_variables", "ci_pipeline_schedules", column: "pipeline_schedule_id", name: "fk_41c35fda51", on_delete: :cascade
diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb
index 2c2aefc542c..f2305eab3b7 100644
--- a/lib/google_api/cloud_platform/client.rb
+++ b/lib/google_api/cloud_platform/client.rb
@@ -2,6 +2,13 @@ module GoogleApi
module CloudPlatform
class Client < GoogleApi::Authentication
# Google::Apis::ContainerV1::ContainerService.new
+
+ class << self
+ def token_in_session
+ :cloud_platform_access_token
+ end
+ end
+
def scope
'https://www.googleapis.com/auth/cloud-platform'
end
@@ -16,8 +23,16 @@ module GoogleApi
response
end
- def projects_zones_clusters_create
- # TODO
+ def projects_zones_clusters_create(gcp_project_id, cluster_zone, cluster_name, cluster_size)
+ # TODO: Google::Apis::ContainerV1::ContainerService.new
+
+ # TODO: Debug
+ {
+ 'end_point' => '111.111.111.111',
+ 'ca_cert' => 'XXXXXXXXXXXXXXXXXX',
+ 'username' => 'AAA',
+ 'password' => 'BBB'
+ }
end
end
end