Commit 9753741d authored by Kamil Trzciński's avatar Kamil Trzciński Committed by Shinya Maeda

Allow to use only defined feature flags

This changes the API for feature flags to:
- allow to use only defined feature flags
- allow to skip this validation with `?force=1`
- returns information if the feature flag is defined
parent 3b46ce3c
......@@ -37,7 +37,8 @@ Example response:
"key": "boolean",
"value": false
}
]
],
"definition": null
},
{
"name": "my_user_feature",
......@@ -47,7 +48,15 @@ Example response:
"key": "percentage_of_actors",
"value": 34
}
]
],
"definition": {
"name": "my_user_feature",
"introduced_by_url": "https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40880",
"rollout_issue_url": "https://gitlab.com/gitlab-org/gitlab/-/issues/244905",
"group": "group::ci",
"type": "development",
"default_enabled": false
}
},
{
"name": "new_library",
......@@ -57,7 +66,45 @@ Example response:
"key": "boolean",
"value": true
}
]
],
"definition": null
}
]
```
## List all feature definitions
Get a list of all feature definitions.
```plaintext
GET /features/definitions
```
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/features/definitions"
```
Example response:
```json
[
{
"name": "api_kaminari_count_with_limit",
"introduced_by_url": "https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/23931",
"rollout_issue_url": null,
"milestone": "11.8",
"type": "ops",
"group": "group::ecosystem",
"default_enabled": false
},
{
"name": "marginalia",
"introduced_by_url": null,
"rollout_issue_url": null,
"milestone": null,
"type": "ops",
"group": null,
"default_enabled": false
}
]
```
......@@ -81,6 +128,7 @@ POST /features/:name
| `user` | string | no | A GitLab username |
| `group` | string | no | A GitLab group's path, for example `gitlab-org` |
| `project` | string | no | A projects path, for example `gitlab-org/gitlab-foss` |
| `force` | boolean | no | Skip feature flag validation checks, ie. YAML definition |
Note that you can enable or disable a feature for a `feature_group`, a `user`,
a `group`, and a `project` in a single API call.
......@@ -104,7 +152,15 @@ Example response:
"key": "percentage_of_time",
"value": 30
}
]
],
"definition": {
"name": "my_user_feature",
"introduced_by_url": "https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40880",
"rollout_issue_url": "https://gitlab.com/gitlab-org/gitlab/-/issues/244905",
"group": "group::ci",
"type": "development",
"default_enabled": false
}
}
```
......@@ -133,7 +189,15 @@ Example response:
"key": "percentage_of_actors",
"value": 42
}
]
],
"definition": {
"name": "my_user_feature",
"introduced_by_url": "https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40880",
"rollout_issue_url": "https://gitlab.com/gitlab-org/gitlab/-/issues/244905",
"group": "group::ci",
"type": "development",
"default_enabled": false
}
}
```
......
......@@ -18,7 +18,10 @@ RSpec.describe API::Features, stub_feature_flags: false do
end
describe 'POST /feature' do
let(:feature_name) { 'my_feature' }
let(:feature_name) do
Feature::Definition.definitions
.values.find(&:development?).name
end
context 'when running on a Geo primary node' do
before do
......@@ -43,6 +46,14 @@ RSpec.describe API::Features, stub_feature_flags: false do
expect(response).to have_gitlab_http_status(:bad_request)
end
context 'when force=1 is set' do
it 'allows to change state' do
post api("/features/#{feature_name}", admin), params: { value: 'true', force: true }
expect(response).to have_gitlab_http_status(:created)
end
end
end
end
......
......@@ -17,6 +17,16 @@ module API
{ key: gate.key, value: value }
end.compact
end
class Definition < Grape::Entity
::Feature::Definition::PARAMS.each do |param|
expose param
end
end
expose :definition, using: Definition do |feature|
::Feature::Definition.definitions[feature.name.to_sym]
end
end
end
end
......@@ -46,6 +46,15 @@ module API
present features, with: Entities::Feature, current_user: current_user
end
desc 'Get a list of all feature definitions' do
success Entities::Feature::Definition
end
get :definitions do
definitions = ::Feature::Definition.definitions.values.map(&:to_h)
present definitions, with: Entities::Feature::Definition, current_user: current_user
end
desc 'Set the gate value for the given feature' do
success Entities::Feature
end
......@@ -56,6 +65,7 @@ module API
optional :user, type: String, desc: 'A GitLab username'
optional :group, type: String, desc: "A GitLab group's path, such as 'gitlab-org'"
optional :project, type: String, desc: 'A projects path, like gitlab-org/gitlab-ce'
optional :force, type: Boolean, desc: 'Skip feature flag validation checks, ie. YAML definition'
mutually_exclusive :key, :feature_group
mutually_exclusive :key, :user
......@@ -63,7 +73,7 @@ module API
mutually_exclusive :key, :project
end
post ':name' do
validate_feature_flag_name!(params[:name])
validate_feature_flag_name!(params[:name]) unless params[:force]
feature = Feature.get(params[:name]) # rubocop:disable Gitlab/AvoidFeatureGet
targets = gate_targets(params)
......
......@@ -136,8 +136,6 @@ class Feature
end
def register_definitions
return unless check_feature_flags_definition?
Feature::Definition.reload!
end
......
......@@ -13,6 +13,12 @@ class Feature
end
end
TYPES.each do |type, _|
define_method("#{type}?") do
attributes[:type].to_sym == type
end
end
def initialize(path, opts = {})
@path = path
@attributes = {}
......@@ -94,6 +100,10 @@ class Feature
@definitions = load_all!
end
def has_definition?(key)
definitions.has_key?(key.to_sym)
end
def valid_usage!(key, type:, default_enabled:)
if definition = definitions[key.to_sym]
definition.valid_usage!(type_in_code: type, default_enabled_in_code: default_enabled)
......@@ -119,10 +129,6 @@ class Feature
private
def load_all!
# We currently do not load feature flag definitions
# in production environments
return [] unless Gitlab.dev_or_test_env?
paths.each_with_object({}) do |glob_path, definitions|
load_all_from_path!(definitions, glob_path)
end
......
This diff is collapsed.
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