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

Environments in graph #108

Merged
merged 14 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .configs/gqlgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ autobind:
- "github.com/nais/api/internal/auth/authz"
- "github.com/nais/api/internal/cost"
- "github.com/nais/api/internal/deployment"
- "github.com/nais/api/internal/environment"
- "github.com/nais/api/internal/feature"
- "github.com/nais/api/internal/github/repository"
- "github.com/nais/api/internal/graph/model"
Expand Down
295 changes: 295 additions & 0 deletions integration_tests/environments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
Helper.readK8sResources("k8s_resources/simple")

local user = User.new("usersen", "[email protected]", "ei")
Team.new("slug-1", "purpose", "#slack_channel")
Team.new("slug-2", "purpose", "#slack_channel")

Test.gql("all environments", function(t)
t.addHeader("x-user-email", user:email())

t.query([[
{
environments {
nodes {
name
}
}
}
]])

t.check {
data = {
environments = {
nodes = {
{
name = "dev",
},
{
name = "dev-fss",
},
{
name = "dev-gcp",
},
{
name = "staging",
},
},
},
},
}
end)

Test.gql("all environments with ordering", function(t)
t.addHeader("x-user-email", user:email())

t.query([[
{
environments(
orderBy: {
field: NAME,
direction: ASC
}
) {
nodes {
name
}
}
}
]])

t.check {
data = {
environments = {
nodes = {
{
name = "dev",
},
{
name = "dev-fss",
},
{
name = "dev-gcp",
},
{
name = "staging",
},
},
},
},
}

t.query([[
{
environments(
orderBy: {
field: NAME,
direction: DESC
}
) {
nodes {
name
}
}
}
]])

t.check {
data = {
environments = {
nodes = {
{
name = "staging",
},
{
name = "dev-gcp",
},
{
name = "dev-fss",
},
{
name = "dev",
},
},
},
},
}
end)

Test.gql("single environment", function(t)
t.addHeader("x-user-email", user:email())

t.query([[
{
environment(name: "dev") {
name
}
}
]])

t.check {
data = {
environment = {
name = "dev",
},
},
}
end)

Test.gql("single environment that does not exist", function(t)
t.addHeader("x-user-email", user:email())

t.query([[
{
environment(name: "some-non-existing-environment") {
name
}
}
]])

t.check {
data = Null,
errors = {
{
message = "Environment \"some-non-existing-environment\" not found",
path = { "environment" },
},
},
}
end)

Test.gql("workloads in environment", function(t)
t.addHeader("x-user-email", user:email())

t.query([[
{
environment(name: "dev") {
workloads {
pageInfo {
totalCount
}
nodes {
__typename
name
team {
slug
}
}
}
}
}
]])

t.check {
data = {
environment = {
workloads = {
pageInfo = {
totalCount = 5,
},
nodes = {
{
__typename = "Application",
name = "another-app",
team = {
slug = "slug-1",
},
},
{
__typename = "Application",
name = "app-name",
team = {
slug = "slug-1",
},
},
{
__typename = "Application",
name = "app-name",
team = {
slug = "slug-2",
},
},
{
__typename = "Job",
name = "jobname-1",
team = {
slug = "slug-1",
},
},
{
__typename = "Job",
name = "jobname-2",
team = {
slug = "slug-1",
},
},
},
},
},
},
}
end)

Test.gql("workloads in environment with custom order", function(t)
t.addHeader("x-user-email", user:email())

t.query([[
{
environment(name: "dev") {
workloads(
orderBy: {
field: TEAM_SLUG,
direction: DESC
}
) {
nodes {
name
team {
slug
}
}
}
}
}
]])

t.check {
data = {
environment = {
workloads = {
nodes = {
{
name = "app-name",
team = {
slug = "slug-2",
},
},
{
name = "another-app",
team = {
slug = "slug-1",
},
},
{
name = "app-name",
team = {
slug = "slug-1",
},
},
{
name = "jobname-1",
team = {
slug = "slug-1",
},
},
{
name = "jobname-2",
team = {
slug = "slug-1",
},
},
},
},
},
},
}
end)
2 changes: 2 additions & 0 deletions internal/cmd/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/database/notify"
"github.com/nais/api/internal/deployment"
"github.com/nais/api/internal/environment"
"github.com/nais/api/internal/feature"
"github.com/nais/api/internal/github/repository"
"github.com/nais/api/internal/graph/loader"
Expand Down Expand Up @@ -291,6 +292,7 @@ func ConfigureGraph(
ctx = search.NewLoaderContext(ctx, pool, searcher)
ctx = unleash.NewLoaderContext(ctx, tenantName, unleashWatcher, bifrostAPIURL, log)
ctx = logging.NewPackageContext(ctx, tenantName, defaultLogDestinations)
ctx = environment.NewLoaderContext(ctx, pool)
ctx = feature.NewLoaderContext(
ctx,
unleashWatcher.Enabled(),
Expand Down
4 changes: 4 additions & 0 deletions internal/deployment/sortfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ func init() {
workload.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a workload.Workload) int {
return sortByTimestamp(ctx, a)
})

workload.SortFilterEnvironment.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a workload.Workload) int {
return sortByTimestamp(ctx, a)
})
}
28 changes: 24 additions & 4 deletions internal/environment/dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,44 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/environment/environmentsql"
"github.com/nais/api/internal/graph/loader"
"github.com/vikstrous/dataloadgen"
)

type ctxKey int

const loadersKey ctxKey = iota

func NewLoaderContext(ctx context.Context, dbConn *pgxpool.Pool) context.Context {
return context.WithValue(ctx, loadersKey, &loaders{
internalQuerier: environmentsql.New(dbConn),
})
return context.WithValue(ctx, loadersKey, newLoaders(dbConn))
}

func fromContext(ctx context.Context) *loaders {
return ctx.Value(loadersKey).(*loaders)
}

type dataloader struct {
db environmentsql.Querier
}

type loaders struct {
internalQuerier *environmentsql.Queries
internalQuerier *environmentsql.Queries
environmentLoader *dataloadgen.Loader[string, *Environment]
}

func newLoaders(dbConn *pgxpool.Pool) *loaders {
db := environmentsql.New(dbConn)
environmentLoader := &dataloader{db: db}

return &loaders{
internalQuerier: db,
environmentLoader: dataloadgen.NewLoader(environmentLoader.list, loader.DefaultDataLoaderOptions...),
}
}

func (l dataloader) list(ctx context.Context, names []string) ([]*Environment, []error) {
makeKey := func(obj *Environment) string { return obj.Name }
return loader.LoadModels(ctx, names, l.db.ListByNames, toGraphEnvironment, makeKey)
}

func db(ctx context.Context) *environmentsql.Queries {
Expand Down
Loading