Skip to content

Commit

Permalink
Self review, change hub-manifests repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Feb 11, 2022
1 parent c9f0c7f commit 7066856
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 43 deletions.
4 changes: 2 additions & 2 deletions hack/lib/const.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ readonly CAPACT_USE_TEST_SETUP="false"
#

readonly CAPACT_INCREASE_RESOURCE_LIMITS="true"
readonly CAPACT_HUB_MANIFESTS_SOURCE_REPO_URL="github.com/capactio/hub-manifests"
readonly CAPACT_HUB_MANIFESTS_SOURCE_REPO_URL="github.com/mszostok/os-hub-manifests"
# The git ref to checkout. It can point to a commit SHA, a branch name, or a tag.
# If you want to use your forked version, remember to update CAPACT_HUB_MANIFESTS_SOURCE_REPO_URL respectively.
readonly CAPACT_HUB_MANIFESTS_SOURCE_REPO_REF="main"
readonly CAPACT_HUB_MANIFESTS_SOURCE_REPO_REF="policy-syntax/type-instances"
2 changes: 1 addition & 1 deletion hub-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function main() {
logger.info("Starting Hub", {mode: config.hubMode});

if (config.hubMode === HubMode.Local) {
await ensureCoreStorageTypeInstance({driver}, `http://${bindAddress}:${bindPort}/graphql`)
await ensureCoreStorageTypeInstance({driver})
logger.info("Successfully registered TypeInstance for core backend storage");
}

Expand Down
49 changes: 25 additions & 24 deletions hub-js/src/schema/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "fs";
import { makeAugmentedSchema, neo4jgraphql } from "neo4j-graphql-js";
import { Driver, Transaction } from "neo4j-driver";
import {readFileSync} from "fs";
import {makeAugmentedSchema, neo4jgraphql} from "neo4j-graphql-js";
import {Driver, Transaction} from "neo4j-driver";

const typeDefs = readFileSync("./graphql/local/schema.graphql", "utf-8");

Expand Down Expand Up @@ -54,7 +54,7 @@ export const schema = makeAugmentedSchema({
args: CreateTypeInstancesArgs,
context: ContextWithDriver
) => {
const { typeInstances, usesRelations } = args.in;
const {typeInstances, usesRelations} = args.in;

const aliases = typeInstances
.filter((x) => x.alias !== undefined)
Expand Down Expand Up @@ -116,7 +116,7 @@ export const schema = makeAugmentedSchema({
RETURN ti.id as uuid, typeInstance.alias as alias
`,
{ typeInstances }
{typeInstances}
);

if (
Expand All @@ -142,7 +142,7 @@ export const schema = makeAugmentedSchema({
{}
);
const usesRelationsParams = usesRelations.map(
({ from, to }: { from: string; to: string }) => ({
({from, to}: { from: string; to: string }) => ({
from: aliasMappings[from] || from,
to: aliasMappings[to] || to,
})
Expand Down Expand Up @@ -203,7 +203,7 @@ export const schema = makeAugmentedSchema({
);
break;
case UpdateTypeInstanceErrorCode.NotFound: {
const ids = args.in.map(({ id }) => id);
const ids = args.in.map(({id}) => id);
const notFoundIDs = ids.filter(
(x) => !customErr.ids.includes(x)
);
Expand Down Expand Up @@ -271,7 +271,7 @@ export const schema = makeAugmentedSchema({
}
RETURN $id`,
{ id: args.id, ownerID: args.ownerID || null }
{id: args.id, ownerID: args.ownerID || null}
);
return args.id;
}
Expand Down Expand Up @@ -404,7 +404,7 @@ async function switchLocking(
) YIELD value as lockingProcess
RETURN allIDs, lockedIDs, lockingProcess`,
{ in: args.in }
{in: args.in}
);

if (!instanceLockedByOthers.records.length) {
Expand Down Expand Up @@ -479,28 +479,29 @@ function tryToExtractCustomError(
return null;
}

export async function ensureCoreStorageTypeInstance(context: ContextWithDriver, uri: string) {
export async function ensureCoreStorageTypeInstance(context: ContextWithDriver) {
const neo4jSession = context.driver.session();
const value = {
uri: uri
acceptValue: false,
contextSchema: null,
}
try {
await neo4jSession.writeTransaction(
async (tx: Transaction) => {
await tx.run(`
MERGE (ti:TypeInstance {id: "318b99bd-9b26-4bc1-8259-0a7ff5dae61c"})
MERGE (typeRef:TypeInstanceTypeReference {path: "cap.core.type.hub.storage.neo4j", revision: "0.1.0"})
MERGE (backend:TypeInstanceBackendReference {abstract: true, id: ti.id, description: "Built-in Hub storage"})
MERGE (tir: TypeInstanceResourceVersion {resourceVersion: 1, createdBy: "core"})
MERGE (spec: TypeInstanceResourceVersionSpec {value: apoc.convert.toJson($value)})
MERGE (ti)-[:OF_TYPE]->(typeRef)
MERGE (ti)-[:STORED_IN]->(backend)
MERGE (ti)-[:CONTAINS]->(tir)
MERGE (tir)-[:DESCRIBED_BY]->(metadata:TypeInstanceResourceVersionMetadata)
MERGE (tir)-[:SPECIFIED_BY]->(spec)
RETURN ti
MERGE (ti:TypeInstance {id: "318b99bd-9b26-4bc1-8259-0a7ff5dae61c"})
MERGE (typeRef:TypeInstanceTypeReference {path: "cap.core.type.hub.storage.neo4j", revision: "0.1.0"})
MERGE (backend:TypeInstanceBackendReference {abstract: true, id: ti.id, description: "Built-in Hub storage"})
MERGE (tir: TypeInstanceResourceVersion {resourceVersion: 1, createdBy: "core"})
MERGE (spec: TypeInstanceResourceVersionSpec {value: apoc.convert.toJson($value)})
MERGE (ti)-[:OF_TYPE]->(typeRef)
MERGE (ti)-[:STORED_IN]->(backend)
MERGE (ti)-[:CONTAINS]->(tir)
MERGE (tir)-[:DESCRIBED_BY]->(metadata:TypeInstanceResourceVersionMetadata)
MERGE (tir)-[:SPECIFIED_BY]->(spec)
RETURN ti
`, {value});
}
);
Expand Down
50 changes: 49 additions & 1 deletion internal/k8s-engine/graphql/domain/action/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ func fixGQLAction(t *testing.T, name string) graphql.Action {
Path: "path1",
Revision: "0.1.0",
},
Backend: &graphql.TypeInstanceBackendDetails{
ID: "id11",
Abstract: true,
},
},
{
ID: "id2",
TypeRef: &graphql.ManifestReference{
Path: "path2",
Revision: "0.1.0",
},

Backend: &graphql.TypeInstanceBackendDetails{
ID: "id22",
Abstract: false,
},
},
},
},
Expand Down Expand Up @@ -167,13 +176,21 @@ func fixK8sAction(t *testing.T, name, namespace string) v1alpha1.Action {
Path: "path1",
Revision: ptr.String("0.1.0"),
},
Backend: v1alpha1.TypeInstanceBackend{
ID: "id11",
Abstract: true,
},
},
{
ID: "id2",
TypeRef: &v1alpha1.ManifestReference{
Path: "path2",
Revision: ptr.String("0.1.0"),
},
Backend: v1alpha1.TypeInstanceBackend{
ID: "id22",
Abstract: false,
},
},
},
},
Expand Down Expand Up @@ -338,6 +355,37 @@ func fixGQLInputActionPolicy() *graphql.PolicyInput {
},
},
},
TypeInstance: &graphql.TypeInstancePolicyInput{
Rules: []*graphql.RulesForTypeInstanceInput{
{
TypeRef: &graphql.ManifestReferenceInput{
Path: "cap.type.aws.auth.credentials",
Revision: ptr.String("0.1.0"),
},
Backend: &graphql.TypeInstanceBackendRuleInput{
ID: "00fd161c-01bd-47a6-9872-47490e11f996",
Description: ptr.String("Vault TI"),
},
},
{
TypeRef: &graphql.ManifestReferenceInput{
Path: "cap.type.aws.*",
},
Backend: &graphql.TypeInstanceBackendRuleInput{
ID: "31bb8355-10d7-49ce-a739-4554d8a40b63",
},
},
{
TypeRef: &graphql.ManifestReferenceInput{
Path: "cap.*",
},
Backend: &graphql.TypeInstanceBackendRuleInput{
ID: "a36ed738-dfe7-45ec-acd1-8e44e8db893b",
Description: ptr.String("Default Capact PostgreSQL backend"),
},
},
},
},
}
}

Expand Down Expand Up @@ -432,7 +480,7 @@ func fixModelInputSecret(name string, paramsEnabled, policyEnabled bool) *corev1
sec.StringData["parameter-input-parameters"] = `{"param":"one"}`
}
if policyEnabled {
sec.StringData["action-policy.json"] = `{"interface":{"rules":[{"interface":{"path":"cap.interface.dummy","revision":null},"oneOf":[{"implementationConstraints":{"requires":null,"attributes":null,"path":"cap.implementation.dummy"},"inject":{"requiredTypeInstances":[{"id":"policy-ti-id","description":"Sample description"}],"additionalParameters":[{"name":"additional-parameters","value":{"snapshot":true}}],"additionalTypeInstances":[{"name":"additional-ti","id":"additional-ti-id"}]}}]}]}}`
sec.StringData["action-policy.json"] = `{"interface":{"rules":[{"interface":{"path":"cap.interface.dummy","revision":null},"oneOf":[{"implementationConstraints":{"requires":null,"attributes":null,"path":"cap.implementation.dummy"},"inject":{"requiredTypeInstances":[{"id":"policy-ti-id","description":"Sample description"}],"additionalParameters":[{"name":"additional-parameters","value":{"snapshot":true}}],"additionalTypeInstances":[{"name":"additional-ti","id":"additional-ti-id"}]}}]}]},"typeInstance":{"rules":[{"typeRef":{"path":"cap.type.aws.auth.credentials","revision":"0.1.0"},"backend":{"id":"00fd161c-01bd-47a6-9872-47490e11f996","description":"Vault TI"}},{"typeRef":{"path":"cap.type.aws.*","revision":null},"backend":{"id":"31bb8355-10d7-49ce-a739-4554d8a40b63","description":null}},{"typeRef":{"path":"cap.*","revision":null},"backend":{"id":"a36ed738-dfe7-45ec-acd1-8e44e8db893b","description":"Default Capact PostgreSQL backend"}}]}}`
}

return sec
Expand Down
16 changes: 4 additions & 12 deletions pkg/engine/k8s/policy/type_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"capact.io/capact/internal/ptr"

"capact.io/capact/pkg/sdk/apis/0.0.1/types"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func (t *TypeInstanceBackendCollection) SetByTypeRef(ref types.ManifestRefWithOp
if t.byTypeRef == nil {
t.byTypeRef = map[string]TypeInstanceBackend{}
}
t.byTypeRef[t.key(ref)] = backend
t.byTypeRef[ref.String()] = backend
}

// GetByTypeRef returns storage backend for a given TypeRef.
Expand All @@ -56,13 +55,13 @@ func (t *TypeInstanceBackendCollection) SetByTypeRef(ref types.ManifestRefWithOp
// - cap.*:0.1.0
// - cap.*
//
// If both methods fail, default backend is returned.
func (t TypeInstanceBackendCollection) GetByTypeRef(typeRef types.TypeRef) (TypeInstanceBackend, bool) {
// 1. Try the explicit TypeRef
backend, found := t.byTypeRef[t.key(types.ManifestRefWithOptRevision{
key := types.ManifestRefWithOptRevision{
Path: typeRef.Path,
Revision: ptr.String(typeRef.Revision),
})]
}
backend, found := t.byTypeRef[key.String()]
if found {
return backend, true
}
Expand Down Expand Up @@ -120,10 +119,3 @@ func (t *TypeInstanceBackendCollection) GetAll() map[string]TypeInstanceBackend
}
return out
}

func (t TypeInstanceBackendCollection) key(typeRef types.ManifestRefWithOptRevision) string {
if typeRef.Revision != nil && *typeRef.Revision != "" {
return fmt.Sprintf("%s:%s", typeRef.Path, *typeRef.Revision)
}
return typeRef.Path
}
1 change: 0 additions & 1 deletion pkg/hub/client/policy_enforced_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (e *PolicyEnforcedClient) ListTypeInstancesBackendsBasedOnPolicy(_ context.

// 1. Global Defaults based on TypeRefs
for _, rule := range e.mergedPolicy.TypeInstance.Rules {
// ensure that rule.TypeRef.Revision is resolved!
out.SetByTypeRef(rule.TypeRef, rule.Backend)
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/hub/client/public/facade/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package facade groups dedicated function per kind.
// The purpose of it is to hide more complex underlying logic that can be reused across codebase.
package facade
3 changes: 1 addition & 2 deletions pkg/sdk/renderer/argo/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func TestRenderHappyPath(t *testing.T) {
interfaceIOValidator := actionvalidation.NewValidator(fakeCli)
policyIOValidator := policyvalidation.NewValidator(fakeCli)
wfValidator := renderer.NewWorkflowInputValidator(interfaceIOValidator, policyIOValidator)
l, _ := logger.New(logger.Config{DevMode: true})
argoRenderer := NewRenderer(l, renderer.Config{
argoRenderer := NewRenderer(logger.Noop(), renderer.Config{
RenderTimeout: time.Second,
MaxDepth: 20,
}, fakeCli, typeInstanceHandler, wfValidator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,41 +1117,47 @@ args:
typeInstances:
- alias: mattermost-config
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.productivity.mattermost.config
revision: 0.1.0
value: null
- alias: mattermost-install-install-db-postgresql
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.database.postgresql.config
revision: 0.1.0
value: null
- alias: mattermost-install-install-db-postgres-install-helm-install-helm-release
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.helm.chart.release
revision: 0.1.0
value: null
- alias: mattermost-install-create-user-user
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.database.postgresql.user
revision: 0.1.0
value: null
- alias: mattermost-install-create-db-database
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.database.postgresql.database
revision: 0.1.0
value: null
- alias: mattermost-install-helm-install-helm-release
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.helm.chart.release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,15 @@ args:
typeInstances:
- alias: postgresql
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.database.postgresql.config
revision: 0.1.0
value: null
- alias: postgres-install-helm-install-helm-release
attributes: []
backend: null
createdBy: default/action
typeRef:
path: cap.type.helm.chart.release
Expand Down
Loading

0 comments on commit 7066856

Please sign in to comment.