Skip to content

Commit

Permalink
Update e fix dipendenze
Browse files Browse the repository at this point in the history
Added stitched schemas

Fixed stitched schemas k8s-harbor

fix package.json

Fix Harbor token and deleted comments

Fix up

Fix watch.js
  • Loading branch information
AnnaNarra committed Oct 23, 2024
1 parent f9a1b72 commit 5258f74
Show file tree
Hide file tree
Showing 18 changed files with 2,029 additions and 3,026 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ jobs:
- name: Setup nodeJS
uses: actions/setup-node@v4
with:
node-version: 14
node-version: 18

- name: Install the packages necessary for lint checking
working-directory: ./qlkube
Expand Down
1 change: 1 addition & 0 deletions deploy/crownlabs/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ qlkube:
rbacResourcesName: crownlabs-qlkube
ingress:
hostname: qlkube.crownlabs.example.com
harborToken: {}
configuration:
exposedAPIs:
apis:
Expand Down
3 changes: 2 additions & 1 deletion qlkube/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.env
.harbor_token.json
4 changes: 2 additions & 2 deletions qlkube/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:16.3-alpine
FROM node:18-alpine

RUN mkdir --parent /qlkube
RUN chown node:node /qlkube && chmod 770 /qlkube
Expand All @@ -13,6 +13,6 @@ RUN yarn install --production=true --frozen-lockfile

COPY src/*.js ./src/

ENV NODE_EXTRA_CA_CERTS /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
ENV NODE_EXTRA_CA_CERTS=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt

ENTRYPOINT [ "yarn", "start" ]
6 changes: 6 additions & 0 deletions qlkube/deploy/qlkube/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ spec:
- name: configuration
mountPath: "{{ .Values.configuration.mountPath }}/{{ .Values.configuration.wrappers.fileName }}"
subPath: "{{ .Values.configuration.wrappers.fileName }}"
- name: harbor-token
mountPath: "/var/run/secrets/harbor_token.json"
subPath: "harbor_token.json"
volumes:
- name: configuration
configMap:
name: "{{ include "qlkube.fullname" . }}"
- name: harbor-token
secret:
secretName: {{ include "qlkube.fullname" . }}-harbor-token
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
Expand Down
10 changes: 10 additions & 0 deletions qlkube/deploy/qlkube/templates/secret-harbor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ include "qlkube.fullname" . }}-harbor-token
labels:
{{- include "qlkube.labels" . | nindent 4 }}
type: Opaque
stringData:
harbor_token.json: |
{{ .Values.harborToken | toJson | indent 4 }}
2 changes: 2 additions & 0 deletions qlkube/deploy/qlkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ resources:
memory: 250Mi

rbacResourcesName: crownlabs-qlkube

harborToken: {}
34 changes: 21 additions & 13 deletions qlkube/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,33 @@
"homepage": "https://github.com/netgroup-polito/CrownLabs/qlkube#readme",
"devDependencies": {
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"lint-staged": "15.2.7",
"nodemon": "^2.0.7",
"prettier": "^2.2.1"
},
"dependencies": {
"@graphql-tools/schema": "^7.1.4",
"@kubernetes/client-node": "^0.15.1",
"apollo-server": "2.25.4",
"apollo-server-express": "^2.25.1",
"@apollo/server": "^4.10.4",
"@apollo/server-plugin-landing-page-graphql-playground": "^4.0.1",
"@graphql-tools/schema": "10.0.4",
"@graphql-tools/stitch": "^9.2.9",
"@graphql-tools/wrap": "10.0.5",
"@kubernetes/client-node": "^0.21.0",
"apollo-link-http": "^1.5.17",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
"dotenv": "^9.0.2",
"express": "^4.19.2",
"express-graphql": "^0.12.0",
"got": "^9.6.0",
"graphql": "^15.5.1",
"graphql-tools": "^8.1.0",
"oasgraph": "^0.14.3",
"pino": "^5.13.6",
"promise-any": "^0.2.0"
"got": "^12.6.1",
"graphql": "^16.9.0",
"graphql-http": "^1.22.1",
"graphql-subscriptions": "^2.0.0",
"graphql-tag": "^2.12.6",
"graphql-tools": "9.0.1",
"graphql-ws": "^5.16.0",
"node-fetch": "^3.3.2",
"openapi-to-graphql": "^3.0.7",
"pino": "^9.2.0",
"ws": "^8.17.1"
},
"prettier": {
"printWidth": 80,
Expand All @@ -56,7 +64,7 @@
"*.{ts,tsx,js,jsx,json,css}": "prettier --write"
},
"resolutions": {
"yargs-parser": "20.2.2",
"yargs-parser": "21.1.1",
"ansi-regex": "^5.0.1"
}
}
8 changes: 6 additions & 2 deletions qlkube/src/decorateBaseSchema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { gql } = require('apollo-server-core');
const { gql } = require('graphql-tag');
const { extendSchema } = require('graphql/utilities');
const { addResolversToSchema } = require('@graphql-tools/schema');
const { capitalizeType, graphqlLogger } = require('./utils.js');
Expand Down Expand Up @@ -67,7 +67,11 @@ function decorateBaseSchema(
};

const extendedSchema = extendSchema(baseSchema, extension);
const newSchema = addResolversToSchema(extendedSchema, resolvers);
const newSchema = addResolversToSchema({
schema: extendedSchema,
resolvers: resolvers,
});

return newSchema;
}

Expand Down
15 changes: 11 additions & 4 deletions qlkube/src/decorateSubscription.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { withFilter } = require('apollo-server');
const { gql, ForbiddenError } = require('apollo-server-core');
const { ForbiddenError } = require('@apollo/server');
const { withFilter } = require('graphql-subscriptions');
const { gql } = require('graphql-tag');
const { addResolversToSchema } = require('@graphql-tools/schema');
const { extendSchema } = require('graphql/utilities');
const { pubsubAsyncIterator } = require('./pubsub');
Expand Down Expand Up @@ -389,7 +390,10 @@ function decorateSubscription(baseSchema, targetType, enumType, kubeApiUrl) {
};

const extendedSchema = extendSchema(baseSchema, extension);
const newSchema = addResolversToSchema(extendedSchema, resolvers);
const newSchema = addResolversToSchema({
schema: extendedSchema,
resolvers: resolvers,
});
newSchema._subscriptionType = newSchema._typeMap.Subscription;
return newSchema;
}
Expand Down Expand Up @@ -598,7 +602,10 @@ function decorateLabelsSubscription(
};

const extendedSchema = extendSchema(baseSchema, extension);
const newSchema = addResolversToSchema(extendedSchema, resolvers);
const newSchema = addResolversToSchema({
schema: extendedSchema,
resolvers: resolvers,
});
newSchema._subscriptionType = newSchema._typeMap.Subscription;
return newSchema;
}
Expand Down
126 changes: 91 additions & 35 deletions qlkube/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
const { ApolloServer } = require('apollo-server-express');
const { ApolloServer } = require('@apollo/server');
const { expressMiddleware } = require('@apollo/server/express4');
const {
ApolloServerPluginDrainHttpServer,
} = require('@apollo/server/plugin/drainHttpServer');
const compression = require('compression');
const dotenv = require('dotenv');
const express = require('express');
const fs = require('fs').promises;
const { printSchema } = require('graphql');
const {
ApolloServerPluginLandingPageGraphQLPlayground,
} = require('@apollo/server-plugin-landing-page-graphql-playground');
const { createServer } = require('http');
const { WebSocketServer } = require('ws');
const bodyParser = require('body-parser');
const { useServer } = require('graphql-ws/lib/use/ws');
const logger = require('pino')({ useLevelLabels: true });
const { setupSubscriptions } = require('./decorateSubscription.js');
const getOpenApiSpec = require('./oas');
const { getOpenApiSpec } = require('./oas');
const { decorateOpenapi } = require('./decorateOpenapi');
const { createSchema } = require('./schema');
const { joinSchemas } = require('./joinSchemas.js');
const { createSchema, createHarborSchema } = require('./schema');
const { subscriptions } = require('./subscriptions.js');
const { kinformer } = require('./informer.js');
const {
getBearerToken,
graphqlQueryRegistry,
graphqlLogger,
} = require('./utils.js');
const { getBearerToken, graphqlLogger } = require('./utils.js');

const repl = require('repl');

dotenv.config();

Expand All @@ -30,6 +39,7 @@ async function main() {
const kubeApiUrl = inCluster
? 'https://kubernetes.default.svc'
: 'http://localhost:8001';
const harborApiUrl = 'https://harbor.crownlabs.polito.it';
const inClusterToken = inCluster
? await fs.readFile(
'/var/run/secrets/kubernetes.io/serviceaccount/token',
Expand All @@ -38,21 +48,70 @@ async function main() {
: '';
const oas = await getOpenApiSpec(kubeApiUrl, inClusterToken);
const targetOas = decorateOpenapi(oas);
let schema = await createSchema(targetOas, kubeApiUrl, inClusterToken);

const harborToken = JSON.parse(
await fs.readFile(
inCluster ? '/var/run/secrets/harbor_token.json' : './.harbor_token.json',
'utf8'
)
);

const harborOas = await getOpenApiSpec(harborApiUrl, harborToken);

let kubeSchema = await createSchema(targetOas, kubeApiUrl, inClusterToken);
let harborSchema = await createHarborSchema(
harborOas,
harborApiUrl,
harborToken
);

let schema = joinSchemas(kubeSchema, harborSchema);

try {
schema = setupSubscriptions(subscriptions, schema, kubeApiUrl);
} catch (e) {
console.error(e);
process.exit(1);
}

const app = express();
app.use(compression());
app.use(bodyParser.json());

app.get('/schema', (req, res) => {
res.setHeader('content-type', 'text/plain');
res.send(
printSchema(schema)
.split('\n')
.filter(l => l.trim() != '_') // remove empty values from enums
.join('\n')
);
});
app.get('/healthz', (req, res) => {
res.sendStatus(200);
});

const httpServer = createServer(app);

const server = new ApolloServer({
schema,
playground: true,
plugins: [graphqlQueryRegistry],
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer: httpServer }),
ApolloServerPluginLandingPageGraphQLPlayground(),
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
},
};
},
},
],
formatError: error => {
try {
const msgs = [...error.message.match(/(\d+)\s*\-(.*)$/)];
const msgs = [...(error.message.match(/(\d+)\s*\-(.*)$/) || [])];
if (msgs.length != 3) return error;
let msg = JSON.parse(msgs[2]);
try {
Expand Down Expand Up @@ -84,7 +143,7 @@ async function main() {
},
},

context: ({ req, connection }) => {
context: async ({ req, connection }) => {
if (connection) {
const { token } = connection.context;
return { token };
Expand All @@ -96,36 +155,33 @@ async function main() {
},
});

const app = express();
app.use(compression());
app.get('/schema', (req, res) => {
res.setHeader('content-type', 'text/plain');
res.send(
printSchema(schema)
.split('\n')
.filter(l => l.trim() != '_') // remove empty values from enums
.join('\n')
);
});
app.get('/healthz', (req, res) => {
res.sendStatus(200);
});
server.applyMiddleware({
app,
path: '/',
await server.start();

app.use(
'/',
expressMiddleware(server, {
context: async ({ req }) => {
const token = getBearerToken(req.headers);
return { token };
},
})
);

const wsServer = new WebSocketServer({
server: httpServer,
path: '/subscriptions',
});
const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);
const serverCleanup = useServer({ schema }, wsServer);

const PORT = process.env.CROWNLABS_QLKUBE_PORT || 8080;

httpServer.listen({ port: PORT }, () => {
console.log(`πŸš€ Server ready at http://localhost:${PORT}/`);
console.log(
`πŸš€ Server ready at http://localhost:${PORT}${server.graphqlPath}`
);
console.log(
`πŸš€ Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`
`πŸš€ Subscriptions ready at ws://localhost:${PORT}/subscriptions`
);
global.harborSchema = harborSchema;
repl.start('> ');
});

/**
Expand Down
21 changes: 21 additions & 0 deletions qlkube/src/joinSchemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { stitchSchemas } = require('@graphql-tools/stitch');
const { RenameRootFields } = require('@graphql-tools/wrap');

module.exports.joinSchemas = (kubeSchema, harborSchema) => {
let schema = stitchSchemas({
subschemas: [
{
schema: kubeSchema,
},
{
schema: harborSchema,
transforms: [
new RenameRootFields((operation, name, field) => {
return 'reg_' + name;
}),
],
},
],
});
return schema;
};
Loading

0 comments on commit 5258f74

Please sign in to comment.