diff --git a/ee/app/models/remote_development/workspaces_agent_config.rb b/ee/app/models/remote_development/workspaces_agent_config.rb index d0e373830cfe235bf5525ae6d8cd0ffe53922bd5..982cdafd1b1faf18fc4f56d30ca02feba01c4b16 100644 --- a/ee/app/models/remote_development/workspaces_agent_config.rb +++ b/ee/app/models/remote_development/workspaces_agent_config.rb @@ -65,6 +65,8 @@ class WorkspacesAgentConfig < ApplicationRecord validate :validate_image_pull_secrets_namespace + validate :validate_max_resources_per_workspace + scope :by_cluster_agent_ids, ->(ids) { where(cluster_agent_id: ids) } private @@ -118,5 +120,20 @@ def validate_image_pull_secrets_namespace ) nil end + + # max_resources_per_workspace must be an empty hash if shared_namespace is specified + # @return [void] + def validate_max_resources_per_workspace + return if shared_namespace.blank? + return if max_resources_per_workspace.nil? || max_resources_per_workspace.empty? + + errors.add( + :max_resources_per_workspace, + format( + _("max_resources_per_workspace must be an empty hash if shared_namespace is specified") + ) + ) + nil + end end end diff --git a/ee/spec/fixtures/remote_development/example.agent_config.yaml b/ee/spec/fixtures/remote_development/example.agent_config.yaml index 795def76a972860514e75542f132f043dcf8e79f..7dc257701efc664b1e2f5543d24edcdccd111e6b 100644 --- a/ee/spec/fixtures/remote_development/example.agent_config.yaml +++ b/ee/spec/fixtures/remote_development/example.agent_config.yaml @@ -54,4 +54,4 @@ remote_development: max_active_hours_before_stop: 60 max_stopped_hours_before_termination: 4332 - shared_namespace: "default" + shared_namespace: "" diff --git a/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb b/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb index 6d709e6f1351d43a56babb725c982b19ff8e3c36..a14c9a1fb43d5062a08026dda8a2cb52ef86b8be 100644 --- a/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb +++ b/ee/spec/lib/remote_development/workspace_operations/create/main_integration_spec.rb @@ -124,7 +124,8 @@ expect(workspace.actual_state).to eq(states_module::CREATION_REQUESTED) expect(workspace.actual_state_updated_at).to eq(Time.current) expect(workspace.name).to eq("workspace-#{agent.id}-#{user.id}-#{random_string}") - expect(workspace.namespace).to eq("default") + expect(workspace.namespace) + .to eq("#{create_constants_module::NAMESPACE_PREFIX}-#{agent.id}-#{user.id}-#{random_string}") expect(workspace.workspaces_agent_config_version).to eq(expected_workspaces_agent_config_version) expect(workspace.url).to eq(URI::HTTPS.build({ host: "#{create_constants_module::WORKSPACE_EDITOR_PORT}-#{workspace.name}." \ @@ -165,15 +166,18 @@ end end - context "without shared namespace" do + context "with shared namespace" do before do - agent.unversioned_latest_workspaces_agent_config.update!(shared_namespace: "") + # max_resources_per_workspace must be an empty hash if shared_namespace is specified + agent.unversioned_latest_workspaces_agent_config.update!( + shared_namespace: "default", + max_resources_per_workspace: {} + ) end it 'uses a unique namespace', :aggregate_failures do workspace = response.fetch(:payload).fetch(:workspace) - expect(workspace.namespace) - .to eq("#{create_constants_module::NAMESPACE_PREFIX}-#{agent.id}-#{user.id}-#{random_string}") + expect(workspace.namespace).to eq("default") end end end diff --git a/ee/spec/models/remote_development/workspaces_agent_config_spec.rb b/ee/spec/models/remote_development/workspaces_agent_config_spec.rb index 683c1d9356d323b6abac3f3f563192ce6e3a2545..1a5fe93e7a3f106df70efd2ff208c11c3e813052 100644 --- a/ee/spec/models/remote_development/workspaces_agent_config_spec.rb +++ b/ee/spec/models/remote_development/workspaces_agent_config_spec.rb @@ -205,6 +205,32 @@ end end + context 'when max_resources_per_workspace and shared_namespace are specified' do + using RSpec::Parameterized::TableSyntax + + where(:shared_namespace, :max_resources_per_workspace, :validity, :error_field, :errors) do + # rubocop:disable Layout/LineLength -- The RSpec table syntax often requires long lines for errors + '' | {} | true | :max_resources_per_workspace | [] + '' | { requests: { cpu: "1", memory: "1Gi" }, limits: { cpu: "2", memory: "2Gi" } } | true | :max_resources_per_workspace | [] + '' | nil | false | :max_resources_per_workspace | ["must be a valid json schema", "must be a hash"] + 'my-namespace' | {} | true | :max_resources_per_workspace | [] + 'my-namespace' | nil | false | :max_resources_per_workspace | ["must be a valid json schema", "must be a hash"] + 'my-namespace' | { requests: { cpu: "1", memory: "1Gi" }, limits: { cpu: "2", memory: "2Gi" } } | false | :max_resources_per_workspace | ["max_resources_per_workspace must be an empty hash if shared_namespace is specified"] + # rubocop:enable Layout/LineLength + end + + with_them do + before do + config.shared_namespace = shared_namespace + config.max_resources_per_workspace = max_resources_per_workspace + config.validate + end + + it { expect(config.valid?).to eq(validity) } + it { expect(config.errors[error_field]).to eq(errors) } + end + end + it 'when network_policy_egress is not specified explicitly' do expect(config).to be_valid expect(config.network_policy_egress).to eq(default_network_policy_egress) diff --git a/locale/gitlab.pot b/locale/gitlab.pot index e3c990c82f13f9d8a0326c31735b2302dc9dcf0f..2408c0427154204568a864b804d427dc45beddf4 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -72301,6 +72301,9 @@ msgstr "" msgid "math|Too many expansions. Consider using multiple math blocks." msgstr "" +msgid "max_resources_per_workspace must be an empty hash if shared_namespace is specified" +msgstr "" + msgid "member" msgid_plural "members" msgstr[0] ""