Skip to content
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

Implement flag metadata #3196

Merged
merged 14 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Then you need to enable CGO.

1. Clone this repo: `git clone https://github.com/flipt-io/flipt`.
1. Run `mage bootstrap` to install required development tools. See [#bootstrap](#bootstrap) below.
1. Run `mage go:test` to execute the Go test suite.
1. Run `mage go:test` to execute the Go test suite. For more information on tests, see also [here](build/README.md)
1. Run `mage` to build the binary with embedded assets.
1. Run `mage -l` to see a full list of possible commands.

Expand Down
5 changes: 5 additions & 0 deletions build/testing/integration/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.flipt.io/flipt/rpc/flipt"
"go.flipt.io/flipt/rpc/flipt/evaluation"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/structpb"
)

func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
Expand Down Expand Up @@ -105,12 +106,16 @@ func API(t *testing.T, ctx context.Context, opts integration.TestOpts) {
t.Run("Flags and Variants", func(t *testing.T) {
t.Log("Create a new enabled flag with key \"test\".")

metadata, err := structpb.NewStruct(nil)
assert.NoError(t, err)

enabled, err := client.Flipt().CreateFlag(ctx, &flipt.CreateFlagRequest{
NamespaceKey: namespace.Key,
Key: "test",
Name: "Test",
Description: "This is a test flag",
Enabled: true,
Metadata: metadata,
})
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions config/migrations/cockroachdb/10_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON;
1 change: 1 addition & 0 deletions config/migrations/mysql/12_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON AFTER enabled;
1 change: 1 addition & 0 deletions config/migrations/postgres/13_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON;
1 change: 1 addition & 0 deletions config/migrations/sqlite3/12_flag_metadata.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE flags ADD COLUMN metadata JSON;
19 changes: 11 additions & 8 deletions internal/ext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ext
import (
"encoding/json"
"errors"

"google.golang.org/protobuf/types/known/structpb"
)

type Document struct {
Expand All @@ -13,14 +15,15 @@ type Document struct {
}

type Flag struct {
Key string `yaml:"key,omitempty" json:"key,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
Enabled bool `yaml:"enabled" json:"enabled"`
Variants []*Variant `yaml:"variants,omitempty" json:"variants,omitempty"`
Rules []*Rule `yaml:"rules,omitempty" json:"rules,omitempty"`
Rollouts []*Rollout `yaml:"rollouts,omitempty" json:"rollouts,omitempty"`
Key string `yaml:"key,omitempty" json:"key,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
Enabled bool `yaml:"enabled" json:"enabled"`
Metadata *structpb.Struct `yaml:"metadata,omitempty" json:"metadata,omitempty"`
erka marked this conversation as resolved.
Show resolved Hide resolved
Variants []*Variant `yaml:"variants,omitempty" json:"variants,omitempty"`
Rules []*Rule `yaml:"rules,omitempty" json:"rules,omitempty"`
Rollouts []*Rollout `yaml:"rollouts,omitempty" json:"rollouts,omitempty"`
}

type Variant struct {
Expand Down
1 change: 1 addition & 0 deletions internal/ext/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (e *Exporter) Export(ctx context.Context, encoding Encoding, w io.Writer) e
Type: f.Type.String(),
Description: f.Description,
Enabled: f.Enabled,
Metadata: f.Metadata,
erka marked this conversation as resolved.
Show resolved Hide resolved
}

// map variant id => variant key
Expand Down
10 changes: 10 additions & 0 deletions internal/ext/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.flipt.io/flipt/rpc/flipt"
"google.golang.org/protobuf/types/known/structpb"
)

type mockLister struct {
Expand Down Expand Up @@ -88,6 +89,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -227,6 +229,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -259,6 +262,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
"foo": {
Expand All @@ -268,6 +272,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -300,6 +305,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
},
Expand Down Expand Up @@ -499,6 +505,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -530,6 +537,7 @@ func TestExport(t *testing.T) {
Name: "flag2",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Enabled: false,
},
},
Expand All @@ -540,6 +548,7 @@ func TestExport(t *testing.T) {
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Description: "description",
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Variants: []*flipt.Variant{
{
Id: "1",
Expand Down Expand Up @@ -571,6 +580,7 @@ func TestExport(t *testing.T) {
Name: "flag2",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Description: "a boolean flag",
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Enabled: false,
},
},
Expand Down
1 change: 1 addition & 0 deletions internal/ext/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (i *Importer) Import(ctx context.Context, enc Encoding, r io.Reader) (err e
Name: f.Name,
Description: f.Description,
Enabled: f.Enabled,
Metadata: f.Metadata,
NamespaceKey: namespace,
}

Expand Down
13 changes: 13 additions & 0 deletions internal/ext/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.flipt.io/flipt/rpc/flipt"
"google.golang.org/protobuf/types/known/structpb"
)

var extensions = []Encoding{EncodingYML, EncodingJSON}
Expand Down Expand Up @@ -65,6 +66,7 @@ func (m *mockCreator) CreateFlag(ctx context.Context, r *flipt.CreateFlagRequest
Description: r.Description,
Type: r.Type,
Enabled: r.Enabled,
Metadata: r.Metadata,
}, nil
}

Expand Down Expand Up @@ -199,13 +201,15 @@ func TestImport(t *testing.T) {
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
{
Key: "flag2",
Name: "flag2",
Description: "a boolean flag",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
variantReqs: []*flipt.CreateVariantRequest{
Expand Down Expand Up @@ -286,13 +290,15 @@ func TestImport(t *testing.T) {
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
{
Key: "flag2",
Name: "flag2",
Description: "a boolean flag",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
variantReqs: []*flipt.CreateVariantRequest{
Expand Down Expand Up @@ -372,13 +378,15 @@ func TestImport(t *testing.T) {
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
{
Key: "flag2",
Name: "flag2",
Description: "a boolean flag",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
variantReqs: []*flipt.CreateVariantRequest{
Expand Down Expand Up @@ -459,13 +467,15 @@ func TestImport(t *testing.T) {
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
{
Key: "flag2",
Name: "flag2",
Description: "a boolean flag",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
variantReqs: []*flipt.CreateVariantRequest{
Expand Down Expand Up @@ -545,6 +555,7 @@ func TestImport(t *testing.T) {
Name: "flag1",
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
Enabled: true,
},
},
Expand Down Expand Up @@ -602,13 +613,15 @@ func TestImport(t *testing.T) {
Description: "description",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
Enabled: true,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
{
Key: "flag2",
Name: "flag2",
Description: "a boolean flag",
Type: flipt.FlagType_BOOLEAN_FLAG_TYPE,
Enabled: false,
Metadata: structpb.NewStringValue("foobar").GetStructValue(),
},
},
variantReqs: []*flipt.CreateVariantRequest{
Expand Down
19 changes: 15 additions & 4 deletions internal/storage/sql/common/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

sq "github.com/Masterminds/squirrel"
"github.com/gofrs/uuid"
"google.golang.org/protobuf/types/known/structpb"

errs "go.flipt.io/flipt/errors"
"go.flipt.io/flipt/internal/storage"
Expand All @@ -36,12 +37,13 @@ func emptyAsNil(str string) *string {
// GetFlag gets a flag with variants by key
func (s *Store) GetFlag(ctx context.Context, p storage.ResourceRequest) (*flipt.Flag, error) {
var (
metadata fliptsql.JSONField[map[string]any]
createdAt fliptsql.Timestamp
updatedAt fliptsql.Timestamp

flag = &flipt.Flag{}

err = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, created_at, updated_at").
err = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, metadata, created_at, updated_at").
From("flags").
Where(sq.Eq{"namespace_key": p.Namespace(), "\"key\"": p.Key}).
QueryRowContext(ctx).
Expand All @@ -52,8 +54,10 @@ func (s *Store) GetFlag(ctx context.Context, p storage.ResourceRequest) (*flipt.
&flag.Name,
&flag.Description,
&flag.Enabled,
&metadata,
&createdAt,
&updatedAt)
&updatedAt,
)
)

if err != nil {
Expand All @@ -66,6 +70,7 @@ func (s *Store) GetFlag(ctx context.Context, p storage.ResourceRequest) (*flipt.

flag.CreatedAt = createdAt.Timestamp
flag.UpdatedAt = updatedAt.Timestamp
flag.Metadata, _ = structpb.NewStruct(metadata.T)

query := s.builder.Select("id, namespace_key, flag_key, \"key\", name, description, attachment, created_at, updated_at").
From("variants").
Expand Down Expand Up @@ -137,7 +142,7 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
flags []*flipt.Flag
results = storage.ResultSet[*flipt.Flag]{}

query = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, created_at, updated_at").
query = s.builder.Select("namespace_key, \"key\", \"type\", name, description, enabled, metadata, created_at, updated_at").
From("flags").
Where(sq.Eq{"namespace_key": req.Predicate.Namespace()}).
OrderBy(fmt.Sprintf("created_at %s", req.QueryParams.Order))
Expand Down Expand Up @@ -179,6 +184,7 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
var (
flag = &flipt.Flag{}

fMetadata fliptsql.JSONField[map[string]any]
fCreatedAt fliptsql.Timestamp
fUpdatedAt fliptsql.Timestamp
)
Expand All @@ -190,13 +196,15 @@ func (s *Store) ListFlags(ctx context.Context, req *storage.ListRequest[storage.
&flag.Name,
&flag.Description,
&flag.Enabled,
&fMetadata,
&fCreatedAt,
&fUpdatedAt); err != nil {
return results, err
}

flag.CreatedAt = fCreatedAt.Timestamp
flag.UpdatedAt = fUpdatedAt.Timestamp
flag.Metadata, _ = structpb.NewStruct(fMetadata.T)

flags = append(flags, flag)
flagsByKey[flag.Key] = flag
Expand Down Expand Up @@ -328,20 +336,22 @@ func (s *Store) CreateFlag(ctx context.Context, r *flipt.CreateFlagRequest) (*fl
Name: r.Name,
Description: r.Description,
Enabled: r.Enabled,
Metadata: r.Metadata,
CreatedAt: now,
UpdatedAt: now,
}
)

if _, err := s.builder.Insert("flags").
Columns("namespace_key", "\"key\"", "\"type\"", "name", "description", "enabled", "created_at", "updated_at").
Columns("namespace_key", "\"key\"", "\"type\"", "name", "description", "enabled", "metadata", "created_at", "updated_at").
Values(
flag.NamespaceKey,
flag.Key,
flag.Type,
flag.Name,
flag.Description,
flag.Enabled,
&fliptsql.JSONField[map[string]any]{T: flag.Metadata.AsMap()},
&fliptsql.Timestamp{Timestamp: flag.CreatedAt},
&fliptsql.Timestamp{Timestamp: flag.UpdatedAt},
).
Expand All @@ -362,6 +372,7 @@ func (s *Store) UpdateFlag(ctx context.Context, r *flipt.UpdateFlagRequest) (*fl
Set("name", r.Name).
Set("description", r.Description).
Set("enabled", r.Enabled).
Set("metadata", &fliptsql.JSONField[map[string]any]{T: r.Metadata.AsMap()}).
Set("updated_at", &fliptsql.Timestamp{Timestamp: flipt.Now()}).
Where(sq.And{sq.Eq{"namespace_key": r.NamespaceKey}, sq.Eq{"\"key\"": r.Key}})

Expand Down
Loading
Loading