-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add izanami as feature flipping provider #2507
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
59c0c6c
feat :add feature flipping
sguiheux cf47fd0
feat :add NeedToken
sguiheux 192b4ce
Merge branch 'master' into featureFlipping
sguiheux 4f0bdde
feat: load features with project
sguiheux 379f5f1
Merge branch 'master' into featureFlipping
sguiheux e36f928
add vendor
sguiheux 17e95d8
fix cr
sguiheux 69fadac
fix cr
sguiheux 5c106d5
fix cr
sguiheux a09afbe
add tu
sguiheux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/ovh/cds/engine/api/feature" | ||
) | ||
|
||
func (api *API) cleanFeatureHandler() Handler { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
feature.Clean(api.Cache) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package feature | ||
|
||
import ( | ||
"github.com/ovhlabs/izanami-go-client" | ||
|
||
"github.com/ovh/cds/engine/api/cache" | ||
"github.com/ovh/cds/sdk" | ||
"github.com/ovh/cds/sdk/log" | ||
) | ||
|
||
const ( | ||
// FeatWorkflowAsCode is workflow as code feature id | ||
FeatWorkflowAsCode = "cds:wasc" | ||
|
||
cacheFeatureKey = "feature:" | ||
) | ||
|
||
var c *client.Client | ||
|
||
// CheckContext represents the context send to izanami to check if the feature is enabled | ||
type CheckContext struct { | ||
Key string `json:"key"` | ||
} | ||
|
||
// List all features | ||
func List() []string { | ||
return []string{FeatWorkflowAsCode} | ||
} | ||
|
||
// Init initialize izanami client | ||
func Init(apiURL, clientID, clientSecret string) error { | ||
var errC error | ||
c, errC = client.New(apiURL, clientID, clientSecret) | ||
return errC | ||
} | ||
|
||
// GetFromCache get feature tree for the given project from cache | ||
func GetFromCache(store cache.Store, projectKey string) sdk.ProjectFeatures { | ||
projFeats := sdk.ProjectFeatures{} | ||
store.Get(cacheFeatureKey+projectKey, &projFeats) | ||
return projFeats | ||
} | ||
|
||
// IsEnabled check if feature is enabled for the given project | ||
func IsEnabled(cache cache.Store, featureID string, projectKey string) bool { | ||
// No feature flipping | ||
if c == nil { | ||
return true | ||
} | ||
|
||
var projFeats sdk.ProjectFeatures | ||
|
||
// Get from cache | ||
if !cache.Get(cacheFeatureKey+projectKey, &projFeats) { | ||
if v, ok := projFeats.Features[featureID]; ok { | ||
return v | ||
} | ||
} | ||
|
||
// Get from izanami | ||
resp, errCheck := c.Feature().CheckWithContext(featureID, CheckContext{projectKey}) | ||
if errCheck != nil { | ||
log.Warning("Feature.IsEnabled > Cannot check feature %s: %s", featureID, errCheck) | ||
return false | ||
} | ||
projFeats.Key = projectKey | ||
if projFeats.Features == nil { | ||
projFeats.Features = make(map[string]bool) | ||
} | ||
projFeats.Features[featureID] = resp.Active | ||
|
||
// Push in cache | ||
cache.Set(projectKey, projFeats) | ||
|
||
return resp.Active | ||
} | ||
|
||
// Clean the feature cache | ||
func Clean(store cache.Store) { | ||
keys := cache.Key(cacheFeatureKey, "*") | ||
store.DeleteAll(keys) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/go-gorp/gorp" | ||
"github.com/gorilla/mux" | ||
"github.com/ovh/cds/engine/api/auth" | ||
"github.com/ovh/cds/engine/api/bootstrap" | ||
"github.com/ovh/cds/engine/api/event" | ||
"github.com/ovh/cds/engine/api/sessionstore" | ||
"github.com/ovh/cds/engine/api/test" | ||
"time" | ||
) | ||
|
||
func newTestAPIWithIzanamiToken(t *testing.T, token string, bootstrapFunc ...test.Bootstrapf) (*API, *gorp.DbMap, *Router) { | ||
bootstrapFunc = append(bootstrapFunc, bootstrap.InitiliazeDB) | ||
db, cache := test.SetupPG(t, bootstrapFunc...) | ||
router := newRouter(auth.TestLocalAuth(t, db, sessionstore.Options{Cache: cache, TTL: 30}), mux.NewRouter(), "/"+test.GetTestName(t)) | ||
api := &API{ | ||
StartupTime: time.Now(), | ||
Router: router, | ||
DBConnectionFactory: test.DBConnectionFactory, | ||
Config: Configuration{}, | ||
Cache: cache, | ||
} | ||
api.Config.FeaturesFlipping.Izanami.Token = token | ||
event.Cache = api.Cache | ||
api.InitRouter() | ||
return api, db, router | ||
} | ||
|
||
func TestFeatureClean(t *testing.T) { | ||
api, _, router := newTestAPIWithIzanamiToken(t, "mytoken", bootstrap.InitiliazeDB) | ||
|
||
vars := map[string]string{} | ||
uri := router.GetRoute("POST", api.cleanFeatureHandler, vars) | ||
req, err := http.NewRequest("POST", uri, nil) | ||
test.NoError(t, err) | ||
req.Header.Set("X-Izanami-Token", "666") | ||
|
||
w := httptest.NewRecorder() | ||
router.Mux.ServeHTTP(w, req) | ||
assert.Equal(t, 401, w.Code) | ||
|
||
w = httptest.NewRecorder() | ||
req.Header.Set("X-Izanami-Token", "mytoken") | ||
router.Mux.ServeHTTP(w, req) | ||
assert.Equal(t, 204, w.Code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package project | ||
|
||
import ( | ||
"github.com/ovh/cds/engine/api/cache" | ||
"github.com/ovh/cds/engine/api/feature" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func LoadFeatures(store cache.Store, p *sdk.Project) error { | ||
for _, f := range feature.List() { | ||
// force load cache for the given project | ||
feature.IsEnabled(store, f, p.Key) | ||
} | ||
p.Features = feature.GetFromCache(store, p.Key) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test (to cover the router)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will be overkill no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done