Skip to content

Commit

Permalink
Reduce release docker image size (#19)
Browse files Browse the repository at this point in the history
* feat: use multi-stage docker build and node slim as final docker base to reduce the docker image size

* feat: add a dockerfile with alpine base

* chore: remove all auto generated files

* fix: add make generate to ci/cd flow

* feat: use alpine as the final base image
  • Loading branch information
HJ-Fan authored Jul 31, 2024
1 parent fa88620 commit beb92b1
Show file tree
Hide file tree
Showing 471 changed files with 106 additions and 75,143 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
with:
go-version: "1.22.1"

- name: Generate support files
run: make generate

- name: Format Golang
run: if [ "$(gofmt -l . | wc -l)" -gt 0 ]; then exit 1; fi

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
with:
node-version: 20

- name: Generate support files
working-directory: ./
run: make generate

- name: Clean Install Node.js
run: npm ci

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

# Folders
_obj
pkg/client
pkg/openapi
pkg/redfishapi
webui/src/axios

# Ignore the accounts file created during cxl-host execution
/cxl-host-accounts.json
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ generate-redfish:
docker run --rm -v ${PWD}:/local golang:$(GO_VERSION) $(GOFMT_OPTS)
@echo "Apply local patch for xml response fix"
git apply api/patch/Apply-xml-workaround-to-fix-metadata-response.patch
@echo "Apply local patch for missing import"
git apply api/patch/fix-missing-import.patch

generate-axios:
@echo "Generating $(OPENAPI_YAML) axios server using openapi-generator-cli"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ NOTE: If uncertain of which install option to use, the “install using the apt

- [DOCKER](docs/DOCKER.md) - Information on running the CFM Software Suite from within a Docker container

## Build locally

- User must run 'make generate' to generate all supporting files from the api yaml before building local binaries.

# Additional Project Documentation

- [README-SERVICE](docs/SERVICE.md) - README file for the **cfm-service** component
Expand Down
25 changes: 25 additions & 0 deletions api/patch/fix-missing-import.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From 5dcdc9a0e93a05efe3cf0eb3fae7669bf0975354 Mon Sep 17 00:00:00 2001
From: Hongjian Fan <[email protected]>
Date: Tue, 9 Jul 2024 15:28:25 -0500
Subject: [PATCH] fix missing import

---
pkg/redfishapi/model_resource_status_conditions_inner.go | 2 ++
1 file changed, 2 insertions(+)

diff --git a/pkg/redfishapi/model_resource_status_conditions_inner.go b/pkg/redfishapi/model_resource_status_conditions_inner.go
index 94ba4c9..a543d69 100644
--- a/pkg/redfishapi/model_resource_status_conditions_inner.go
+++ b/pkg/redfishapi/model_resource_status_conditions_inner.go
@@ -8,6 +8,8 @@

package redfishapi

+import "time"
+
type ResourceStatusConditionsInner struct {
LogEntry OdataV4IdRef `json:"LogEntry,omitempty"`

--
2.25.1

77 changes: 64 additions & 13 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,75 @@
FROM golang:1.22.1
FROM openapitools/openapi-generator-cli:v7.0.0 AS api-gen

LABEL org.opencontainers.image.source https://github.com/seagate/cfm

RUN apt update
RUN apt install -y nodejs npm
ENV OPENAPI_YAML=api/cfm-openapi.yaml
ENV OPENAPI_REDFISH_YAML=api/redfish-openapi.yaml
ENV BASEPATH=/cfm

# Copy whole repo into the image
COPY . /cfm
COPY . ${BASEPATH}

# Validating ${OPENAPI_YAML} using openapi-generator-cli
RUN docker-entrypoint.sh validate -i ${BASEPATH}/${OPENAPI_YAML}
RUN docker-entrypoint.sh validate -i ${BASEPATH}/${OPENAPI_REDFISH_YAML}

# Generating ${OPENAPI_YAML} go server using openapi-generator-cli
RUN docker-entrypoint.sh generate -i ${BASEPATH}/${OPENAPI_YAML} -g go-server -o ${BASEPATH}/pkg/openapi --additional-properties=sourceFolder=,outputAsLibrary=true,router=mux,serverPort=8080,enumClassPrefix=true -t ${BASEPATH}/api/templates/go-server --skip-validate-spec

# Generating ${OPENAPI_YAML} go client using openapi-generator-cli
RUN docker-entrypoint.sh generate -i ${BASEPATH}/${OPENAPI_YAML} -g go -o ${BASEPATH}/pkg/client -p isGoSubmodule=true,withGoMod=false --package-name client --ignore-file-override ${BASEPATH}/api/ignore/.openapi-generator-ignore-client -t ${BASEPATH}/api/templates/go

# workaround for withGoMod=false not functioning with openapi-generator
RUN rm ${BASEPATH}/pkg/client/go.mod
RUN rm ${BASEPATH}/pkg/client/go.sum

# Generating ${OPENAPI_YAML} redfish server using openapi-generator-cli
RUN docker-entrypoint.sh generate -i ${BASEPATH}/${OPENAPI_REDFISH_YAML} -g go-server -o ${BASEPATH}/pkg/redfishapi --package-name redfishapi --additional-properties=sourceFolder=,outputAsLibrary=true,router=mux,serverPort=8080,enumClassPrefix=true -t ${BASEPATH}/api/templates/go-server --skip-validate-spec

# Generating ${OPENAPI_YAML} axios server using openapi-generator-cli
RUN docker-entrypoint.sh generate -i ${BASEPATH}/${OPENAPI_YAML} -g typescript-axios -o ${BASEPATH}/webui/src/axios --skip-validate-spec


FROM golang:1.22.1-alpine AS go
ENV BASEPATH=/cfm

RUN apk add git make
# copy source code with generated files to go image
COPY --from=api-gen ${BASEPATH} ${BASEPATH}

WORKDIR ${BASEPATH}

# Format files to conform to project standard
RUN gofmt -w ${BASEPATH}

# apply local patch for xml response fix
RUN git apply api/patch/Apply-xml-workaround-to-fix-metadata-response.patch
RUN git apply api/patch/fix-missing-import.patch

# build the excutable
RUN make build-go

FROM node:alpine as npm
ENV BASEPATH=/cfm

# copy source code with generated files to go image
COPY --from=go ${BASEPATH}/webui ${BASEPATH}/webui

# setup web UI and build the dist package
WORKDIR /cfm/webui
WORKDIR ${BASEPATH}/webui
RUN npm install
RUN npm run build

# Build CFM SERVICE & CLI
WORKDIR /cfm
RUN rm cfmdatastore.json
RUN ln -s /local/cfmdatastore.json cfmdatastore.json
RUN make build-go
FROM alpine:latest
ENV BASEPATH=/cfm

# copy source code with generated files to go image
COPY --from=go ${BASEPATH}/cfm-service ${BASEPATH}/cfm-service
COPY --from=go ${BASEPATH}/cfm-cli ${BASEPATH}/cfm-cli
COPY --from=npm ${BASEPATH}/webui/dist ${BASEPATH}/webui/dist

RUN ln -s /local/cfmdatastore.json ${BASEPATH}/cfmdatastore.json

LABEL org.opencontainers.image.source https://github.com/seagate/cfm

WORKDIR ${BASEPATH}
# Start the service
ENTRYPOINT [ "/cfm/cfm-service" ]
ENTRYPOINT [ "/cfm/cfm-service" ]
24 changes: 0 additions & 24 deletions pkg/client/.gitignore

This file was deleted.

23 changes: 0 additions & 23 deletions pkg/client/.openapi-generator-ignore

This file was deleted.

55 changes: 0 additions & 55 deletions pkg/client/.openapi-generator/FILES

This file was deleted.

1 change: 0 additions & 1 deletion pkg/client/.openapi-generator/VERSION

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/client/.travis.yml

This file was deleted.

Loading

0 comments on commit beb92b1

Please sign in to comment.