From b762a6429de820ea3234476899e9ca65d4c63526 Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Wed, 11 Jan 2023 11:29:44 -0500 Subject: [PATCH] feat: add support for Containerfile Containerfile is an alternate name for Dockerfile. They support the same syntax. See: https://github.com/containers/common/blob/main/docs/Containerfile.5.md Signed-off-by: Luiz Carvalho --- go/docs/port_detection.md | 2 +- go/pkg/apis/enricher/enricher.go | 11 ++++++---- go/test/apis/component_recognizer_test.go | 4 ++++ .../projectContainerFile/Containerfile | 16 ++++++++++++++ .../projectContainerFile/package.json | 17 +++++++++++++++ .../projectContainerFile/server.js | 21 +++++++++++++++++++ 6 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 resources/projectPortTesting/projectContainerFile/Containerfile create mode 100644 resources/projectPortTesting/projectContainerFile/package.json create mode 100644 resources/projectPortTesting/projectContainerFile/server.js diff --git a/go/docs/port_detection.md b/go/docs/port_detection.md index 97407ab6..7dd606f9 100644 --- a/go/docs/port_detection.md +++ b/go/docs/port_detection.md @@ -5,7 +5,7 @@ Because of the different nature of frameworks supported, Alizer tries to use cus Only ports with value > 0 and < 65535 are valid. There are three detection strategies currently available: -1) Docker file - Alizer looks for a Dockerfile in the root folder and tries to extract ports from it. +1) Docker file - Alizer looks for a Dockerfile, or Containerfile, in the root folder and tries to extract ports from it. 2) Compose file - Alizer searches for a docker-compose file in the root folder and tries to extract port of the service from it 3) Source - If a framework has been detected during component detection, a customized detection is performed. Below a detailed overview of the different strategies for each supported framework. diff --git a/go/pkg/apis/enricher/enricher.go b/go/pkg/apis/enricher/enricher.go index 2f116487..a40f2324 100644 --- a/go/pkg/apis/enricher/enricher.go +++ b/go/pkg/apis/enricher/enricher.go @@ -124,10 +124,13 @@ func GetDefaultProjectName(path string) string { } func GetPortsFromDockerFile(root string) []int { - file, err := os.Open(filepath.Join(root, "Dockerfile")) - if err == nil { - defer file.Close() - return getPortsFromReader(file) + locations := []string{"Dockerfile", "Containerfile"} + for _, location := range locations { + file, err := os.Open(filepath.Join(root, location)) + if err == nil { + defer file.Close() + return getPortsFromReader(file) + } } return []int{} } diff --git a/go/test/apis/component_recognizer_test.go b/go/test/apis/component_recognizer_test.go index e7d437c2..d543f698 100644 --- a/go/test/apis/component_recognizer_test.go +++ b/go/test/apis/component_recognizer_test.go @@ -209,6 +209,10 @@ func TestPortDetectionWithDockerFile(t *testing.T) { testPortDetectionInProject(t, "projectDockerFile", []int{8085}) } +func TestPortDetectionWithContainerFile(t *testing.T) { + testPortDetectionInProject(t, "projectContainerFile", []int{8085}) +} + func TestPortDetectionJavaMicronaut(t *testing.T) { testPortDetectionInProject(t, "projectMicronaut", []int{4444}) } diff --git a/resources/projectPortTesting/projectContainerFile/Containerfile b/resources/projectPortTesting/projectContainerFile/Containerfile new file mode 100644 index 00000000..d56dbd48 --- /dev/null +++ b/resources/projectPortTesting/projectContainerFile/Containerfile @@ -0,0 +1,16 @@ +FROM node:16 + +# Create app directory +WORKDIR /usr/src/app + +# Install app dependencies +# A wildcard is used to ensure both package.json AND package-lock.json are copied +COPY package*.json ./ + +RUN npm install + +# Bundle app source +COPY . . + +EXPOSE 8085 +CMD [ "node", "server.js" ] diff --git a/resources/projectPortTesting/projectContainerFile/package.json b/resources/projectPortTesting/projectContainerFile/package.json new file mode 100644 index 00000000..c0a041fd --- /dev/null +++ b/resources/projectPortTesting/projectContainerFile/package.json @@ -0,0 +1,17 @@ +{ + "name": "nodejs-starter", + "version": "1.0.0", + "description": "Simple Node.js application", + "license": "EPL-2.0", + "scripts": { + "start": "node server.js", + "debug": "node --inspect-brk=${DEBUG_PORT} server.js", + "test": "node test/test.js" + }, + "dependencies": { + "express": "^4.17.1", + "pino": "^6.2.1", + "pino-http": "^5.1.0", + "prom-client": "^12.0.0" + } +} diff --git a/resources/projectPortTesting/projectContainerFile/server.js b/resources/projectPortTesting/projectContainerFile/server.js new file mode 100644 index 00000000..0463ae7c --- /dev/null +++ b/resources/projectPortTesting/projectContainerFile/server.js @@ -0,0 +1,21 @@ +const express = require('express'); +const http = require('http'); + +const app = express(); +const server = http.createServer(app) + +app.get('/', (req, res) => { + // Use req.log (a `pino` instance) to log JSON: + req.log.info({message: 'Hello from my Node.js App!'}); + res.send('Hello from my Node.js App!'); +}); + +app.get('*', (req, res) => { + res.status(404).send("Not Found"); +}); + +// Listen and serve. +const PORT = process.env.PORT || 3000; +server.listen(PORT, () => { + console.log(`App started on PORT ${PORT}`); +});