-
Notifications
You must be signed in to change notification settings - Fork 415
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
Changes from 9 commits
59c0c6c
cf47fd0
192b4ce
4f0bdde
379f5f1
e36f928
17e95d8
69fadac
5c106d5
a09afbe
File filter
Filter by extension
Conversations
Jump to
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.
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 | ||
} | ||
} |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why public? If public, need some doc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Public because you will need it on code |
||
|
||
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) | ||
} |
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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