Commit 5a042ef2 authored by Thong Kuah's avatar Thong Kuah

Only project clusters has Project Namespace field

Group clusters should not allow Project Namespace so don't show that
field input too
parent 53037184
...@@ -183,13 +183,13 @@ class Clusters::ClustersController < Clusters::BaseController ...@@ -183,13 +183,13 @@ class Clusters::ClustersController < Clusters::BaseController
def gcp_cluster def gcp_cluster
@gcp_cluster = ::Clusters::Cluster.new.tap do |cluster| @gcp_cluster = ::Clusters::Cluster.new.tap do |cluster|
cluster.build_provider_gcp cluster.build_provider_gcp
end end.present(current_user: current_user)
end end
def user_cluster def user_cluster
@user_cluster = ::Clusters::Cluster.new.tap do |cluster| @user_cluster = ::Clusters::Cluster.new.tap do |cluster|
cluster.build_platform_kubernetes cluster.build_platform_kubernetes
end end.present(current_user: current_user)
end end
def validate_gcp_token def validate_gcp_token
......
...@@ -29,7 +29,7 @@ module Clusters ...@@ -29,7 +29,7 @@ module Clusters
# we force autosave to happen when we save `Cluster` model # we force autosave to happen when we save `Cluster` model
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', autosave: true has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', inverse_of: :cluster, autosave: true
has_one :application_helm, class_name: 'Clusters::Applications::Helm' has_one :application_helm, class_name: 'Clusters::Applications::Helm'
has_one :application_ingress, class_name: 'Clusters::Applications::Ingress' has_one :application_ingress, class_name: 'Clusters::Applications::Ingress'
......
...@@ -38,6 +38,8 @@ module Clusters ...@@ -38,6 +38,8 @@ module Clusters
validates :namespace, exclusion: { in: RESERVED_NAMESPACES } validates :namespace, exclusion: { in: RESERVED_NAMESPACES }
validate :no_namespace, unless: :project_type?
# We expect to be `active?` only when enabled and cluster is created (the api_url is assigned) # We expect to be `active?` only when enabled and cluster is created (the api_url is assigned)
validates :api_url, url: true, presence: true validates :api_url, url: true, presence: true
validates :token, presence: true validates :token, presence: true
...@@ -52,6 +54,7 @@ module Clusters ...@@ -52,6 +54,7 @@ module Clusters
delegate :project, to: :cluster, allow_nil: true delegate :project, to: :cluster, allow_nil: true
delegate :enabled?, to: :cluster, allow_nil: true delegate :enabled?, to: :cluster, allow_nil: true
delegate :managed?, to: :cluster, allow_nil: true delegate :managed?, to: :cluster, allow_nil: true
delegate :project_type?, to: :cluster, allow_nil: true
delegate :kubernetes_namespace, to: :cluster delegate :kubernetes_namespace, to: :cluster
alias_method :active?, :enabled? alias_method :active?, :enabled?
...@@ -207,6 +210,12 @@ module Clusters ...@@ -207,6 +210,12 @@ module Clusters
self.token = self.token&.strip self.token = self.token&.strip
end end
def no_namespace
if namespace
errors.add(:namespace, 'only allowed for project cluster')
end
end
def prevent_modification def prevent_modification
return unless managed? return unless managed?
......
...@@ -8,6 +8,10 @@ module Clusters ...@@ -8,6 +8,10 @@ module Clusters
"https://console.cloud.google.com/kubernetes/clusters/details/#{provider.zone}/#{name}" if gcp? "https://console.cloud.google.com/kubernetes/clusters/details/#{provider.zone}/#{name}" if gcp?
end end
def allow_project_namespace?
cluster.project_type?
end
def can_toggle_cluster? def can_toggle_cluster?
can?(current_user, :update_cluster, cluster) && created? can?(current_user, :update_cluster, cluster) && created?
end end
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
= s_('ClusterIntegration|Show') = s_('ClusterIntegration|Show')
= clipboard_button(text: @cluster.platform_kubernetes.token, title: s_('ClusterIntegration|Copy Token'), class: 'btn-default') = clipboard_button(text: @cluster.platform_kubernetes.token, title: s_('ClusterIntegration|Copy Token'), class: 'btn-default')
- if @cluster.allow_project_namespace?
.form-group .form-group
= platform_kubernetes_field.label :namespace, s_('ClusterIntegration|Project namespace (optional, unique)') = platform_kubernetes_field.label :namespace, s_('ClusterIntegration|Project namespace (optional, unique)')
= platform_kubernetes_field.text_field :namespace, class: 'form-control', placeholder: s_('ClusterIntegration|Project namespace') = platform_kubernetes_field.text_field :namespace, class: 'form-control', placeholder: s_('ClusterIntegration|Project namespace')
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
= platform_kubernetes_field.label :token, s_('ClusterIntegration|Token'), class: 'label-bold' = platform_kubernetes_field.label :token, s_('ClusterIntegration|Token'), class: 'label-bold'
= platform_kubernetes_field.text_field :token, class: 'form-control', placeholder: s_('ClusterIntegration|Service token'), autocomplete: 'off' = platform_kubernetes_field.text_field :token, class: 'form-control', placeholder: s_('ClusterIntegration|Service token'), autocomplete: 'off'
- if @user_cluster.allow_project_namespace?
.form-group .form-group
= platform_kubernetes_field.label :namespace, s_('ClusterIntegration|Project namespace (optional, unique)'), class: 'label-bold' = platform_kubernetes_field.label :namespace, s_('ClusterIntegration|Project namespace (optional, unique)'), class: 'label-bold'
= platform_kubernetes_field.text_field :namespace, class: 'form-control', placeholder: s_('ClusterIntegration|Project namespace') = platform_kubernetes_field.text_field :namespace, class: 'form-control', placeholder: s_('ClusterIntegration|Project namespace')
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
%button.js-show-cluster-token.btn-blank{ type: 'button' } %button.js-show-cluster-token.btn-blank{ type: 'button' }
= s_('ClusterIntegration|Show') = s_('ClusterIntegration|Show')
- if @cluster.allow_project_namespace?
.form-group .form-group
= platform_kubernetes_field.label :namespace, s_('ClusterIntegration|Project namespace (optional, unique)'), class: 'label-bold' = platform_kubernetes_field.label :namespace, s_('ClusterIntegration|Project namespace (optional, unique)'), class: 'label-bold'
= platform_kubernetes_field.text_field :namespace, class: 'form-control', placeholder: s_('ClusterIntegration|Project namespace') = platform_kubernetes_field.text_field :namespace, class: 'form-control', placeholder: s_('ClusterIntegration|Project namespace')
......
...@@ -146,7 +146,7 @@ describe Groups::ClustersController do ...@@ -146,7 +146,7 @@ describe Groups::ClustersController do
it 'has new object' do it 'has new object' do
go go
expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::Cluster) expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
end end
end end
...@@ -167,7 +167,7 @@ describe Groups::ClustersController do ...@@ -167,7 +167,7 @@ describe Groups::ClustersController do
it 'has new object' do it 'has new object' do
go go
expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::Cluster) expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
end end
end end
...@@ -281,7 +281,6 @@ describe Groups::ClustersController do ...@@ -281,7 +281,6 @@ describe Groups::ClustersController do
platform_kubernetes_attributes: { platform_kubernetes_attributes: {
api_url: 'http://my-url', api_url: 'http://my-url',
token: 'test', token: 'test',
namespace: 'aaa'
} }
} }
} }
...@@ -315,7 +314,6 @@ describe Groups::ClustersController do ...@@ -315,7 +314,6 @@ describe Groups::ClustersController do
platform_kubernetes_attributes: { platform_kubernetes_attributes: {
api_url: 'http://my-url', api_url: 'http://my-url',
token: 'test', token: 'test',
namespace: 'aaa',
authorization_type: 'rbac' authorization_type: 'rbac'
} }
} }
...@@ -433,9 +431,6 @@ describe Groups::ClustersController do ...@@ -433,9 +431,6 @@ describe Groups::ClustersController do
cluster: { cluster: {
enabled: false, enabled: false,
name: 'my-new-cluster-name', name: 'my-new-cluster-name',
platform_kubernetes_attributes: {
namespace: 'my-namespace'
}
} }
} }
end end
...@@ -448,7 +443,6 @@ describe Groups::ClustersController do ...@@ -448,7 +443,6 @@ describe Groups::ClustersController do
expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.') expect(flash[:notice]).to eq('Kubernetes cluster was successfully updated.')
expect(cluster.enabled).to be_falsey expect(cluster.enabled).to be_falsey
expect(cluster.name).to eq('my-new-cluster-name') expect(cluster.name).to eq('my-new-cluster-name')
expect(cluster.platform_kubernetes.namespace).to eq('my-namespace')
end end
context 'when format is json' do context 'when format is json' do
...@@ -459,9 +453,6 @@ describe Groups::ClustersController do ...@@ -459,9 +453,6 @@ describe Groups::ClustersController do
cluster: { cluster: {
enabled: false, enabled: false,
name: 'my-new-cluster-name', name: 'my-new-cluster-name',
platform_kubernetes_attributes: {
namespace: 'my-namespace'
}
} }
} }
end end
...@@ -473,7 +464,6 @@ describe Groups::ClustersController do ...@@ -473,7 +464,6 @@ describe Groups::ClustersController do
expect(response).to have_http_status(:no_content) expect(response).to have_http_status(:no_content)
expect(cluster.enabled).to be_falsey expect(cluster.enabled).to be_falsey
expect(cluster.name).to eq('my-new-cluster-name') expect(cluster.name).to eq('my-new-cluster-name')
expect(cluster.platform_kubernetes.namespace).to eq('my-namespace')
end end
end end
...@@ -482,9 +472,7 @@ describe Groups::ClustersController do ...@@ -482,9 +472,7 @@ describe Groups::ClustersController do
{ {
cluster: { cluster: {
enabled: false, enabled: false,
platform_kubernetes_attributes: { name: ''
namespace: 'my invalid namespace #@'
}
} }
} }
end end
......
...@@ -122,7 +122,7 @@ describe Projects::ClustersController do ...@@ -122,7 +122,7 @@ describe Projects::ClustersController do
it 'has new object' do it 'has new object' do
go go
expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::Cluster) expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
end end
end end
...@@ -143,7 +143,7 @@ describe Projects::ClustersController do ...@@ -143,7 +143,7 @@ describe Projects::ClustersController do
it 'has new object' do it 'has new object' do
go go
expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::Cluster) expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::ClusterPresenter)
end end
end end
......
...@@ -10,7 +10,7 @@ FactoryBot.define do ...@@ -10,7 +10,7 @@ FactoryBot.define do
username 'xxxxxx' username 'xxxxxx'
password 'xxxxxx' password 'xxxxxx'
after(:create) do |platform_kubernetes, evaluator| before(:create) do |platform_kubernetes, evaluator|
pem_file = File.expand_path(Rails.root.join('spec/fixtures/clusters/sample_cert.pem')) pem_file = File.expand_path(Rails.root.join('spec/fixtures/clusters/sample_cert.pem'))
platform_kubernetes.ca_cert = File.read(pem_file) platform_kubernetes.ca_cert = File.read(pem_file)
end end
......
...@@ -58,6 +58,18 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching ...@@ -58,6 +58,18 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end end
context 'for group cluster' do
let(:namespace) { 'namespace-123' }
let(:cluster) { build(:cluster, :group, :provided_by_user) }
let(:kubernetes) { cluster.platform_kubernetes }
before do
kubernetes.namespace = namespace
end
it { is_expected.to be_falsey }
end
end end
context 'when validates api_url' do context 'when validates api_url' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment