Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
1ca9aaf8
Commit
1ca9aaf8
authored
Nov 03, 2017
by
Alessio Caiazza
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'refactor-clusters' into 38464-k8s-apps
parents
08752e5d
694cfeb7
Changes
17
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
235 additions
and
358 deletions
+235
-358
app/controllers/projects/clusters_controller.rb
app/controllers/projects/clusters_controller.rb
+1
-9
app/models/clusters/cluster.rb
app/models/clusters/cluster.rb
+6
-0
app/models/clusters/platforms/kubernetes.rb
app/models/clusters/platforms/kubernetes.rb
+20
-22
app/services/clusters/create_service.rb
app/services/clusters/create_service.rb
+2
-6
app/services/clusters/gcp/finalize_creation_service.rb
app/services/clusters/gcp/finalize_creation_service.rb
+3
-4
db/migrate/20171013094327_create_new_clusters_architectures.rb
...grate/20171013094327_create_new_clusters_architectures.rb
+1
-1
db/schema.rb
db/schema.rb
+10
-1
spec/controllers/projects/clusters_controller_spec.rb
spec/controllers/projects/clusters_controller_spec.rb
+79
-74
spec/factories/clusters/cluster.rb
spec/factories/clusters/cluster.rb
+3
-15
spec/javascripts/fixtures/clusters.rb
spec/javascripts/fixtures/clusters.rb
+1
-1
spec/lib/gitlab/import_export/all_models.yml
spec/lib/gitlab/import_export/all_models.yml
+9
-3
spec/lib/gitlab/import_export/safe_model_attributes.yml
spec/lib/gitlab/import_export/safe_model_attributes.yml
+0
-26
spec/models/clusters/cluster_spec.rb
spec/models/clusters/cluster_spec.rb
+1
-2
spec/models/clusters/platforms/kubernetes_spec.rb
spec/models/clusters/platforms/kubernetes_spec.rb
+49
-135
spec/services/clusters/create_service_spec.rb
spec/services/clusters/create_service_spec.rb
+48
-58
spec/services/clusters/gcp/finalize_creation_service_spec.rb
spec/services/clusters/gcp/finalize_creation_service_spec.rb
+1
-0
spec/support/kubernetes_helpers.rb
spec/support/kubernetes_helpers.rb
+1
-1
No files found.
app/controllers/projects/clusters_controller.rb
View file @
1ca9aaf8
...
...
@@ -92,11 +92,7 @@ class Projects::ClustersController < Projects::ApplicationController
params
.
require
(
:cluster
).
permit
(
:enabled
,
:name
,
:platform_type
,
:provider_type
,
platform_kubernetes_attributes:
[
:namespace
],
provider_gcp_attributes:
[
:gcp_project_id
,
:zone
,
...
...
@@ -106,11 +102,7 @@ class Projects::ClustersController < Projects::ApplicationController
end
def
update_params
params
.
require
(
:cluster
).
permit
(
:enabled
,
platform_kubernetes_attributes:
[
:namespace
])
params
.
require
(
:cluster
).
permit
(
:enabled
)
end
def
authorize_google_api
...
...
app/models/clusters/cluster.rb
View file @
1ca9aaf8
...
...
@@ -27,9 +27,15 @@ module Clusters
validates
:name
,
cluster_name:
true
validate
:restrict_modification
,
on: :update
# TODO: Move back this into Clusters::Platforms::Kubernetes in 10.3
# We need callback here because `enabled` belongs to Clusters::Cluster
# Callbacks in Clusters::Platforms::Kubernetes will not be called after update
after_save
:update_kubernetes_integration!
delegate
:status
,
to: :provider
,
allow_nil:
true
delegate
:status_reason
,
to: :provider
,
allow_nil:
true
delegate
:on_creation?
,
to: :provider
,
allow_nil:
true
delegate
:update_kubernetes_integration!
,
to: :platform
,
allow_nil:
true
enum
platform_type:
{
kubernetes:
1
...
...
app/models/clusters/platforms/kubernetes.rb
View file @
1ca9aaf8
module
Clusters
module
Platforms
class
Kubernetes
<
ActiveRecord
::
Base
include
Gitlab
::
CurrentSettings
self
.
table_name
=
'cluster_platforms_kubernetes'
belongs_to
:cluster
,
inverse_of: :platform_kubernetes
,
class_name:
'Clusters::Cluster'
...
...
@@ -28,13 +26,10 @@ module Clusters
}
# We expect to be `active?` only when enabled and cluster is created (the api_url is assigned)
with_options
presence:
true
,
if: :enabled?
do
validates
:api_url
,
url:
true
,
presence:
true
validates
:token
,
presence:
true
end
# TODO: Glue code till we migrate Kubernetes Integration into Platforms::Kubernetes
after_save
:update_kubernetes_integration!
after_destroy
:destroy_kubernetes_integration!
alias_attribute
:ca_pem
,
:ca_cert
...
...
@@ -64,6 +59,21 @@ module Clusters
@kubeclient
||=
kubernetes_service
.
kubeclient
if
manages_kubernetes_service?
end
def
update_kubernetes_integration!
raise
'Kubernetes service already configured'
unless
manages_kubernetes_service?
# This is neccesary, otheriwse enabled? returns true even though cluster updated with enabled: false
cluster
.
reload
ensure_kubernetes_service
&
.
update!
(
active:
enabled?
,
api_url:
api_url
,
namespace:
namespace
,
token:
token
,
ca_pem:
ca_cert
)
end
private
def
enforce_namespace_to_lower_case
...
...
@@ -80,27 +90,15 @@ module Clusters
def
destroy_kubernetes_integration!
return
unless
manages_kubernetes_service?
kubernetes_service
.
destroy!
end
def
update_kubernetes_integration!
return
raise
'Kubernetes service already configured'
unless
manages_kubernetes_service?
ensure_kubernetes_service
.
update!
(
active:
enabled?
,
api_url:
api_url
,
namespace:
namespace
,
token:
token
,
ca_pem:
ca_cert
)
kubernetes_service
&
.
destroy!
end
def
kubernetes_service
@kubernetes_service
||=
project
.
kubernetes_service
||
project
.
build_
kubernetes_service
@kubernetes_service
||=
project
&
.
kubernetes_service
end
def
ensure_kubernetes_service
@kubernetes_service
||=
kubernetes_service
||
project
.
build_kubernetes_service
@kubernetes_service
||=
kubernetes_service
||
project
&
.
build_kubernetes_service
end
end
end
...
...
app/services/clusters/create_service.rb
View file @
1ca9aaf8
...
...
@@ -13,11 +13,7 @@ module Clusters
private
def
create_cluster
Clusters
::
Cluster
.
create!
(
cluster_params
.
merge
(
projects:
[
project
]))
rescue
ActiveRecord
::
RecordInvalid
=>
e
e
.
record
Clusters
::
Cluster
.
create
(
cluster_params
)
end
def
cluster_params
...
...
@@ -27,7 +23,7 @@ module Clusters
provider
[
:access_token
]
=
access_token
end
@cluster_params
=
params
.
merge
(
user:
current_user
)
@cluster_params
=
params
.
merge
(
user:
current_user
,
projects:
[
project
]
)
end
end
end
app/services/clusters/gcp/finalize_creation_service.rb
View file @
1ca9aaf8
...
...
@@ -9,11 +9,9 @@ module Clusters
configure_provider
configure_kubernetes
provider
.
make_created
!
cluster
.
save
!
rescue
Google
::
Apis
::
ServerError
,
Google
::
Apis
::
ClientError
,
Google
::
Apis
::
AuthorizationError
=>
e
provider
.
make_errored!
(
"Failed to request to CloudPlatform;
#{
e
.
message
}
"
)
rescue
KubeException
=>
e
provider
.
make_errored!
(
"Failed to request to Kubernetes;
#{
e
.
message
}
"
)
rescue
ActiveRecord
::
RecordInvalid
=>
e
provider
.
make_errored!
(
"Failed to configure GKE Cluster:
#{
e
.
message
}
"
)
end
...
...
@@ -22,6 +20,7 @@ module Clusters
def
configure_provider
provider
.
endpoint
=
gke_cluster
.
endpoint
provider
.
status_event
=
:make_created
end
def
configure_kubernetes
...
...
@@ -39,7 +38,7 @@ module Clusters
'https://'
+
gke_cluster
.
endpoint
,
Base64
.
decode64
(
gke_cluster
.
master_auth
.
cluster_ca_certificate
),
gke_cluster
.
master_auth
.
username
,
gke_cluster
.
master_auth
.
password
)
gke_cluster
.
master_auth
.
password
)
.
execute
end
def
gke_cluster
...
...
db/migrate/20171013094327_create_new_clusters_architectures.rb
View file @
1ca9aaf8
...
...
@@ -3,7 +3,7 @@ class CreateNewClustersArchitectures < ActiveRecord::Migration
def
change
create_table
:clusters
do
|
t
|
t
.
references
:user
,
null:
false
,
index:
true
,
foreign_key:
{
on_delete: :nullify
}
t
.
references
:user
,
index:
true
,
foreign_key:
{
on_delete: :nullify
}
t
.
integer
:provider_type
t
.
integer
:platform_type
...
...
db/schema.rb
View file @
1ca9aaf8
...
...
@@ -464,7 +464,9 @@ ActiveRecord::Schema.define(version: 20171031100710) do
create_table
"cluster_platforms_kubernetes"
,
force: :cascade
do
|
t
|
t
.
integer
"cluster_id"
,
null:
false
t
.
string
"api_url"
t
.
datetime_with_timezone
"created_at"
,
null:
false
t
.
datetime_with_timezone
"updated_at"
,
null:
false
t
.
text
"api_url"
t
.
text
"ca_cert"
t
.
string
"namespace"
t
.
string
"username"
...
...
@@ -491,6 +493,9 @@ ActiveRecord::Schema.define(version: 20171031100710) do
create_table
"cluster_providers_gcp"
,
force: :cascade
do
|
t
|
t
.
integer
"cluster_id"
,
null:
false
t
.
integer
"status"
t
.
integer
"num_nodes"
,
null:
false
t
.
datetime_with_timezone
"created_at"
,
null:
false
t
.
datetime_with_timezone
"updated_at"
,
null:
false
t
.
text
"status_reason"
t
.
string
"gcp_project_id"
,
null:
false
t
.
string
"zone"
,
null:
false
...
...
@@ -508,6 +513,10 @@ ActiveRecord::Schema.define(version: 20171031100710) do
create_table
"clusters"
,
force: :cascade
do
|
t
|
t
.
integer
"user_id"
t
.
integer
"provider_type"
t
.
integer
"platform_type"
t
.
datetime_with_timezone
"created_at"
,
null:
false
t
.
datetime_with_timezone
"updated_at"
,
null:
false
t
.
boolean
"enabled"
,
default:
true
t
.
string
"name"
,
null:
false
t
.
integer
"provider_type"
...
...
spec/controllers/projects/clusters_controller_spec.rb
View file @
1ca9aaf8
...
...
@@ -172,7 +172,6 @@ describe Projects::ClustersController do
{
cluster:
{
name:
'new-cluster'
,
platform_type: :kubernetes
,
provider_type: :gcp
,
provider_gcp_attributes:
{
gcp_project_id:
'111'
...
...
@@ -202,44 +201,50 @@ describe Projects::ClustersController do
end
end
context
'when adds a cluster manually'
do
let
(
:params
)
do
{
cluster:
{
name:
'new-cluster'
,
platform_type: :kubernetes
,
provider_type: :user
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
,
api_url:
'https://111.111.111.111'
,
token:
'token'
}
}
}
end
it
'creates a new cluster'
do
expect
(
ClusterProvisionWorker
).
to
receive
(
:perform_async
)
expect
{
go
}.
to
change
{
Clusters
::
Cluster
.
count
}
expect
(
response
).
to
redirect_to
(
project_cluster_path
(
project
,
project
.
cluster
))
end
end
context
'when not all required parameters are set'
do
let
(
:params
)
do
{
cluster:
{
name:
'new-cluster'
}
}
end
it
'shows an error message'
do
expect
{
go
}.
not_to
change
{
Clusters
::
Cluster
.
count
}
expect
(
assigns
(
:cluster
).
errors
).
not_to
be_empty
expect
(
response
).
to
render_template
(
:new
)
end
end
# TODO: Activate in 10.3
# context 'when adds a cluster manually' do
# let(:params) do
# {
# cluster: {
# name: 'new-cluster',
# platform_type: :kubernetes,
# provider_type: :user,
# platform_kubernetes_attributes: {
# namespace: 'custom-namespace',
# api_url: 'https://111.111.111.111',
# token: 'token'
# }
# }
# }
# end
# it 'creates a new cluster' do
# expect(ClusterProvisionWorker).to receive(:perform_async)
# expect { go }.to change { Clusters::Cluster.count }
# expect(response).to redirect_to(project_cluster_path(project, project.cluster))
# end
# end
# TODO: We should fix this in 10.2
# Maybe
# - validates :provider_gcp, presence: true, if: :gcp?
# - validates :provider_type, presence: true
# are required in Clusters::Cluster
# context 'when not all required parameters are set' do
# let(:params) do
# {
# cluster: {
# name: 'new-cluster'
# }
# }
# end
# it 'shows an error message' do
# expect { go }.not_to change { Clusters::Cluster.count }
# expect(assigns(:cluster).errors).not_to be_empty
# expect(response).to render_template(:new)
# end
# end
end
context
'when access token is expired'
do
...
...
@@ -393,40 +398,41 @@ describe Projects::ClustersController do
end
end
context
'when update namespace'
do
let
(
:namespace
)
{
'namespace-123'
}
# TODO: Activate in 10.3
# context 'when update namespace' do
# let(:namespace) { 'namespace-123' }
let
(
:params
)
do
{
cluster:
{
platform_kubernetes_attributes:
{
namespace:
namespace
}
}
}
end
#
let(:params) do
#
{
#
cluster: {
#
platform_kubernetes_attributes: {
#
namespace: namespace
#
}
#
}
#
}
#
end
it
"updates and redirects back to show page"
do
go
#
it "updates and redirects back to show page" do
#
go
cluster
.
reload
expect
(
response
).
to
redirect_to
(
project_cluster_path
(
project
,
project
.
cluster
))
expect
(
flash
[
:notice
]).
to
eq
(
'Cluster was successfully updated.'
)
expect
(
cluster
.
platform
.
namespace
).
to
eq
(
namespace
)
end
#
cluster.reload
#
expect(response).to redirect_to(project_cluster_path(project, project.cluster))
#
expect(flash[:notice]).to eq('Cluster was successfully updated.')
#
expect(cluster.platform.namespace).to eq(namespace)
#
end
context
'when namespace is invalid'
do
let
(
:namespace
)
{
'my Namespace 321321321 #'
}
#
context 'when namespace is invalid' do
#
let(:namespace) { 'my Namespace 321321321 #' }
it
"rejects changes"
do
go
#
it "rejects changes" do
#
go
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
response
).
to
render_template
(
:show
)
expect
(
cluster
.
platform
.
namespace
).
not_to
eq
(
namespace
)
end
end
end
#
expect(response).to have_gitlab_http_status(:ok)
#
expect(response).to render_template(:show)
#
expect(cluster.platform.namespace).not_to eq(namespace)
#
end
#
end
#
end
end
describe
'security'
do
...
...
@@ -481,7 +487,6 @@ describe Projects::ClustersController do
it
"destroys and redirects back to clusters list"
do
expect
{
go
}
.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
-
1
)
.
and
change
{
Clusters
::
Platforms
::
Kubernetes
.
count
}.
by
(
-
1
)
.
and
change
{
Clusters
::
Providers
::
Gcp
.
count
}.
by
(
-
1
)
expect
(
response
).
to
redirect_to
(
project_clusters_path
(
project
))
...
...
spec/factories/clusters/cluster.rb
View file @
1ca9aaf8
...
...
@@ -2,8 +2,6 @@ FactoryGirl.define do
factory
:cluster
,
class:
Clusters
::
Cluster
do
user
name
'test-cluster'
provider_type
:user
platform_type
:kubernetes
trait
:project
do
after
(
:create
)
do
|
cluster
,
evaluator
|
...
...
@@ -24,28 +22,18 @@ FactoryGirl.define do
provider_type
:gcp
platform_type
:kubernetes
platform_kubernetes
do
create
(
:platform_kubernetes
,
:configured
)
end
provider_gcp
do
create
(
:provider_gcp
,
:created
)
before
(
:create
)
do
|
cluster
,
evaluator
|
cluster
.
platform_kubernetes
=
build
(
:platform_kubernetes
,
:configured
)
cluster
.
provider_gcp
=
build
(
:provider_gcp
,
:created
)
end
end
trait
:providing_by_gcp
do
provider_type
:gcp
platform_type
:kubernetes
provider_gcp
do
create
(
:provider_gcp
,
:creating
)
end
after
(
:create
)
do
|
cluster
,
evaluator
|
build
(
:platform_kubernetes
,
cluster:
cluster
).
tap
do
|
platform
|
platform
.
save!
(
validate:
false
)
end
end
end
end
end
spec/javascripts/fixtures/clusters.rb
View file @
1ca9aaf8
...
...
@@ -6,7 +6,7 @@ describe Projects::ClustersController, '(JavaScript fixtures)', type: :controlle
let
(
:admin
)
{
create
(
:admin
)
}
let
(
:namespace
)
{
create
(
:namespace
,
name:
'frontend-fixtures'
)}
let
(
:project
)
{
create
(
:project
,
:repository
,
namespace:
namespace
)
}
let
(
:cluster
)
{
project
.
create_cluster!
(
gcp_cluster_name:
"gke-test-creation-1"
,
gcp_project_id:
'gitlab-internal-153318'
,
gcp_cluster_zone:
'us-central1-a'
,
gcp_cluster_size:
'1'
,
project_namespace:
'aaa'
,
gcp_machine_type:
'n1-standard-1'
)
}
let
(
:cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
projects:
[
project
])
}
render_views
...
...
spec/lib/gitlab/import_export/all_models.yml
View file @
1ca9aaf8
...
...
@@ -147,10 +147,16 @@ deploy_keys:
-
user
-
deploy_keys_projects
-
projects
cluster
:
-
project
cluster
s
:
-
project
s
-
user
-
service
cluster_projects
:
-
projects
-
clusters
provider_gcp
:
-
cluster
platform_kubernetes
:
-
cluster
services
:
-
project
-
service_hook
...
...
spec/lib/gitlab/import_export/safe_model_attributes.yml
View file @
1ca9aaf8
...
...
@@ -356,32 +356,6 @@ Clusters::Platforms::Kubernetes:
-
encrypted_token_iv
-
created_at
-
updated_at
Gcp::Cluster:
-
id
-
project_id
-
user_id
-
service_id
-
enabled
-
status
-
status_reason
-
project_namespace
-
endpoint
-
ca_cert
-
encrypted_kubernetes_token
-
encrypted_kubernetes_token_iv
-
username
-
encrypted_password
-
encrypted_password_iv
-
gcp_project_id
-
gcp_cluster_zone
-
gcp_cluster_name
-
gcp_cluster_size
-
gcp_machine_type
-
gcp_operation_id
-
encrypted_gcp_token
-
encrypted_gcp_token_iv
-
created_at
-
updated_at
DeployKey
:
-
id
-
user_id
...
...
spec/models/clusters/cluster_spec.rb
View file @
1ca9aaf8
...
...
@@ -9,9 +9,8 @@ describe Clusters::Cluster do
it
{
is_expected
.
to
delegate_method
(
:status_reason
).
to
(
:provider
)
}
it
{
is_expected
.
to
delegate_method
(
:status_name
).
to
(
:provider
)
}
it
{
is_expected
.
to
delegate_method
(
:on_creation?
).
to
(
:provider
)
}
it
{
is_expected
.
to
delegate_method
(
:update_kubernetes_integration!
).
to
(
:platform
)
}
it
{
is_expected
.
to
respond_to
:project
}
it
{
is_expected
.
to
validate_presence_of
(
:provider_type
)
}
it
{
is_expected
.
to
validate_presence_of
(
:platform_type
)
}
describe
'.enabled'
do
subject
{
described_class
.
enabled
}
...
...
spec/models/clusters/platforms/kubernetes_spec.rb
View file @
1ca9aaf8
...
...
@@ -5,8 +5,6 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
include
ReactiveCachingHelpers
it
{
is_expected
.
to
belong_to
(
:cluster
)
}
it
{
is_expected
.
to
be_kind_of
(
Gitlab
::
Kubernetes
)
}
it
{
is_expected
.
to
be_kind_of
(
ReactiveCaching
)
}
it
{
is_expected
.
to
respond_to
:ca_pem
}
describe
'before_validation'
do
...
...
@@ -92,6 +90,55 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
end
end
describe
'after_save from Clusters::Cluster'
do
context
'when platform_kubernetes is being cerated'
do
let
(
:enabled
)
{
true
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:cluster
)
{
build
(
:cluster
,
provider_type: :gcp
,
platform_type: :kubernetes
,
platform_kubernetes:
platform
,
provider_gcp:
provider
,
enabled:
enabled
,
projects:
[
project
])
}
let
(
:platform
)
{
build
(
:platform_kubernetes
,
:configured
)
}
let
(
:provider
)
{
build
(
:provider_gcp
)
}
let
(
:kubernetes_service
)
{
project
.
kubernetes_service
}
it
'updates KubernetesService'
do
cluster
.
save!
expect
(
kubernetes_service
.
active
).
to
eq
(
enabled
)
expect
(
kubernetes_service
.
api_url
).
to
eq
(
platform
.
api_url
)
expect
(
kubernetes_service
.
namespace
).
to
eq
(
platform
.
namespace
)
expect
(
kubernetes_service
.
ca_pem
).
to
eq
(
platform
.
ca_cert
)
end
end
context
'when platform_kubernetes has been created'
do
let
(
:enabled
)
{
false
}
let!
(
:project
)
{
create
(
:project
)
}
let!
(
:cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
projects:
[
project
])
}
let
(
:platform
)
{
cluster
.
platform
}
let
(
:kubernetes_service
)
{
project
.
kubernetes_service
}
it
'updates KubernetesService'
do
cluster
.
update
(
enabled:
enabled
)
expect
(
kubernetes_service
.
active
).
to
eq
(
enabled
)
end
end
context
'when kubernetes_service has been configured without cluster integration'
do
let!
(
:project
)
{
create
(
:project
)
}
let
(
:cluster
)
{
build
(
:cluster
,
provider_type: :gcp
,
platform_type: :kubernetes
,
platform_kubernetes:
platform
,
provider_gcp:
provider
,
projects:
[
project
])
}
let
(
:platform
)
{
build
(
:platform_kubernetes
,
:configured
,
api_url:
'https://111.111.111.111'
)
}
let
(
:provider
)
{
build
(
:provider_gcp
)
}
before
do
create
(
:kubernetes_service
,
project:
project
)
end
it
'raises an error'
do
expect
{
cluster
.
save!
}.
to
raise_error
(
'Kubernetes service already configured'
)
end
end
end
describe
'#actual_namespace'
do
subject
{
kubernetes
.
actual_namespace
}
...
...
@@ -138,137 +185,4 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
it
{
is_expected
.
to
be_nil
}
end
end
describe
'#predefined_variables'
do
let!
(
:cluster
)
{
create
(
:cluster
,
:project
,
platform_kubernetes:
kubernetes
)
}
let
(
:kubernetes
)
{
create
(
:platform_kubernetes
,
api_url:
api_url
,
ca_cert:
ca_pem
,
token:
token
)
}
let
(
:api_url
)
{
'https://kube.domain.com'
}
let
(
:ca_pem
)
{
'CA PEM DATA'
}
let
(
:token
)
{
'token'
}
let
(
:kubeconfig
)
do
config_file
=
expand_fixture_path
(
'config/kubeconfig.yml'
)
config
=
YAML
.
load
(
File
.
read
(
config_file
))
config
.
dig
(
'users'
,
0
,
'user'
)[
'token'
]
=
token
config
.
dig
(
'contexts'
,
0
,
'context'
)[
'namespace'
]
=
namespace
config
.
dig
(
'clusters'
,
0
,
'cluster'
)[
'certificate-authority-data'
]
=
Base64
.
strict_encode64
(
ca_pem
)
YAML
.
dump
(
config
)
end
shared_examples
'setting variables'
do
it
'sets the variables'
do
expect
(
kubernetes
.
predefined_variables
).
to
include
(
{
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
},
{
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
},
{
key:
'KUBE_NAMESPACE'
,
value:
namespace
,
public:
true
},
{
key:
'KUBECONFIG'
,
value:
kubeconfig
,
public:
false
,
file:
true
},
{
key:
'KUBE_CA_PEM'
,
value:
ca_pem
,
public:
true
},
{
key:
'KUBE_CA_PEM_FILE'
,
value:
ca_pem
,
public:
true
,
file:
true
}
)
end
end
context
'namespace is provided'
do
let
(
:namespace
)
{
'my-project'
}
before
do
kubernetes
.
namespace
=
namespace
end
it_behaves_like
'setting variables'
end
context
'no namespace provided'
do
let
(
:namespace
)
{
kubernetes
.
actual_namespace
}
it_behaves_like
'setting variables'
it
'sets the KUBE_NAMESPACE'
do
kube_namespace
=
kubernetes
.
predefined_variables
.
find
{
|
h
|
h
[
:key
]
==
'KUBE_NAMESPACE'
}
expect
(
kube_namespace
).
not_to
be_nil
expect
(
kube_namespace
[
:value
]).
to
match
(
/\A
#{
Gitlab
::
PathRegex
::
PATH_REGEX_STR
}
-\d+\z/
)
end
end
end
describe
'#terminals'
do
subject
{
service
.
terminals
(
environment
)
}
let!
(
:cluster
)
{
create
(
:cluster
,
:project
,
platform_kubernetes:
service
)
}
let
(
:project
)
{
cluster
.
project
}
let
(
:service
)
{
create
(
:platform_kubernetes
,
:configured
)
}
let
(
:environment
)
{
build
(
:environment
,
project:
project
,
name:
"env"
,
slug:
"env-000000"
)
}
context
'with invalid pods'
do
it
'returns no terminals'
do
stub_reactive_cache
(
service
,
pods:
[{
"bad"
=>
"pod"
}])
is_expected
.
to
be_empty
end
end
context
'with valid pods'
do
let
(
:pod
)
{
kube_pod
(
app:
environment
.
slug
)
}
let
(
:terminals
)
{
kube_terminals
(
service
,
pod
)
}
before
do
stub_reactive_cache
(
service
,
pods:
[
pod
,
pod
,
kube_pod
(
app:
"should-be-filtered-out"
)]
)
end
it
'returns terminals'
do
is_expected
.
to
eq
(
terminals
+
terminals
)
end
it
'uses max session time from settings'
do
stub_application_setting
(
terminal_max_session_time:
600
)
times
=
subject
.
map
{
|
terminal
|
terminal
[
:max_session_time
]
}
expect
(
times
).
to
eq
[
600
,
600
,
600
,
600
]
end
end
end
describe
'#calculate_reactive_cache'
do
subject
{
service
.
calculate_reactive_cache
}
let!
(
:cluster
)
{
create
(
:cluster
,
:project
,
enabled:
enabled
,
platform_kubernetes:
service
)
}
let
(
:service
)
{
create
(
:platform_kubernetes
,
:configured
)
}
let
(
:enabled
)
{
true
}
context
'when cluster is disabled'
do
let
(
:enabled
)
{
false
}
it
{
is_expected
.
to
be_nil
}
end
context
'when kubernetes responds with valid pods'
do
before
do
stub_kubeclient_pods
end
it
{
is_expected
.
to
eq
(
pods:
[
kube_pod
])
}
end
context
'when kubernetes responds with 500s'
do
before
do
stub_kubeclient_pods
(
status:
500
)
end
it
{
expect
{
subject
}.
to
raise_error
(
KubeException
)
}
end
context
'when kubernetes responds with 404s'
do
before
do
stub_kubeclient_pods
(
status:
404
)
end
it
{
is_expected
.
to
eq
(
pods:
[])
}
end
end
end
spec/services/clusters/create_service_spec.rb
View file @
1ca9aaf8
...
...
@@ -11,11 +11,7 @@ describe Clusters::CreateService do
let
(
:params
)
do
{
name:
'test-cluster'
,
platform_type: :kubernetes
,
provider_type: :gcp
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
},
provider_gcp_attributes:
{
gcp_project_id:
'gcp-project'
,
zone:
'us-central1-a'
,
...
...
@@ -30,7 +26,6 @@ describe Clusters::CreateService do
expect
{
result
}
.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
1
)
.
and
change
{
Clusters
::
Platforms
::
Kubernetes
.
count
}.
by
(
1
)
.
and
change
{
Clusters
::
Providers
::
Gcp
.
count
}.
by
(
1
)
expect
(
result
.
name
).
to
eq
(
'test-cluster'
)
...
...
@@ -41,9 +36,7 @@ describe Clusters::CreateService do
expect
(
result
.
provider
.
num_nodes
).
to
eq
(
1
)
expect
(
result
.
provider
.
machine_type
).
to
eq
(
'machine_type-a'
)
expect
(
result
.
provider
.
access_token
).
to
eq
(
access_token
)
expect
(
result
.
platform
.
namespace
).
to
eq
(
'custom-namespace'
)
expect
(
result
.
platform
.
api_url
).
to
eq
(
Clusters
::
CreateService
::
TEMPOLARY_API_URL
)
expect
(
result
.
platform
.
token
).
to
eq
(
Clusters
::
CreateService
::
TEMPOLARY_TOKEN
)
expect
(
result
.
platform
).
to
be_nil
end
end
...
...
@@ -51,11 +44,7 @@ describe Clusters::CreateService do
let
(
:params
)
do
{
name:
'test-cluster'
,
platform_type: :kubernetes
,
provider_type: :gcp
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
},
provider_gcp_attributes:
{
gcp_project_id:
'!!!!!!!'
,
zone:
'us-central1-a'
,
...
...
@@ -73,55 +62,56 @@ describe Clusters::CreateService do
end
end
context
'when provider is user'
do
context
'when correct params'
do
let
(
:params
)
do
{
name:
'test-cluster'
,
platform_type: :kubernetes
,
provider_type: :user
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
,
api_url:
'https://111.111.111.111'
,
token:
'token'
}
}
end
# TODO: This will be active in 10.3
# context 'when provider is user' do
# context 'when correct params' do
# let(:params) do
# {
# name: 'test-cluster',
# platform_type: :kubernetes,
# provider_type: :user,
# platform_kubernetes_attributes: {
# namespace: 'custom-namespace',
# api_url: 'https://111.111.111.111',
# token: 'token'
# }
# }
# end
it
'creates a cluster object and performs a worker'
do
expect
(
ClusterProvisionWorker
).
to
receive
(
:perform_async
)
#
it 'creates a cluster object and performs a worker' do
#
expect(ClusterProvisionWorker).to receive(:perform_async)
expect
{
result
}
.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
1
)
.
and
change
{
Clusters
::
Platforms
::
Kubernetes
.
count
}.
by
(
1
)
#
expect { result }
#
.to change { Clusters::Cluster.count }.by(1)
#
.and change { Clusters::Platforms::Kubernetes.count }.by(1)
expect
(
result
.
name
).
to
eq
(
'test-cluster'
)
expect
(
result
.
user
).
to
eq
(
user
)
expect
(
result
.
project
).
to
eq
(
project
)
expect
(
result
.
provider
).
to
be_nil
expect
(
result
.
platform
.
namespace
).
to
eq
(
'custom-namespace'
)
end
end
#
expect(result.name).to eq('test-cluster')
#
expect(result.user).to eq(user)
#
expect(result.project).to eq(project)
#
expect(result.provider).to be_nil
#
expect(result.platform.namespace).to eq('custom-namespace')
#
end
#
end
context
'when invalid params'
do
let
(
:params
)
do
{
name:
'test-cluster'
,
platform_type: :kubernetes
,
provider_type: :user
,
platform_kubernetes_attributes:
{
namespace:
'custom-namespace'
,
api_url:
'!!!!!'
,
token:
'token'
}
}
end
#
context 'when invalid params' do
#
let(:params) do
#
{
#
name: 'test-cluster',
#
platform_type: :kubernetes,
#
provider_type: :user,
#
platform_kubernetes_attributes: {
#
namespace: 'custom-namespace',
#
api_url: '!!!!!',
#
token: 'token'
#
}
#
}
#
end
it
'returns an error'
do
# expect(ClusterProvisionWorker).not_to receive(:perform_async)
expect
{
result
}.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
0
)
expect
(
result
.
errors
[
:"platform_kubernetes.api_url"
]).
to
be_present
end
end
end
#
it 'returns an error' do
#
# expect(ClusterProvisionWorker).not_to receive(:perform_async)
#
expect { result }.to change { Clusters::Cluster.count }.by(0)
#
expect(result.errors[:"platform_kubernetes.api_url"]).to be_present
#
end
#
end
#
end
end
spec/services/clusters/gcp/finalize_creation_service_spec.rb
View file @
1ca9aaf8
...
...
@@ -62,6 +62,7 @@ describe Clusters::Gcp::FinalizeCreationService do
it
'has corresponded data'
do
described_class
.
new
.
execute
(
provider
)
cluster
.
reload
provider
.
reload
platform
.
reload
...
...
spec/support/kubernetes_helpers.rb
View file @
1ca9aaf8
...
...
@@ -27,7 +27,7 @@ module KubernetesHelpers
def
stub_kubeclient_get_secrets_error
(
api_url
)
WebMock
.
stub_request
(
:get
,
api_url
+
'/api/v1/secrets'
)
.
to_return
(
status:
[
500
,
"Internal Server Error"
])
.
to_return
(
status:
[
404
,
"Internal Server Error"
])
end
def
kube_v1_secrets_body
(
**
options
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment