Skip to content

Commit

Permalink
Add openapi compiling into build api step.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinath committed Dec 2, 2021
1 parent 683de9d commit d27254c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 44 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/turing-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ jobs:
name: turing-ui-dist
path: ui/build

- name: Download Swagger UI Dist
uses: actions/download-artifact@v2
with:
name: swagger-ui-dist
path: api/api/swagger-ui-dist

- name: Build and Publish Turing Docker image
env:
DOCKER_REGISTRY: ${{ inputs.container_registry }}/${{ github.repository_owner }}
Expand All @@ -89,4 +83,4 @@ jobs:
run: |
docker image load --input turing-api.${{ inputs.api_version }}.tar
make build-image
docker push ${{ env.DOCKER_REGISTRY }}/turing:${{ env.OVERWRITE_VERSION }}
docker push ${{ env.DOCKER_REGISTRY }}/turing:${{ env.OVERWRITE_VERSION }}
30 changes: 7 additions & 23 deletions .github/workflows/turing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -199,25 +199,6 @@ jobs:
path: ui/build/
retention-days: ${{ env.ARTIFACT_RETENTION_DAYS }}

build-swagger-ui:
runs-on: ubuntu-latest
defaults:
run:
working-directory: api/api
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Build static Swagger UI dist
run: make swagger-ui-dist

- name: Publish Artifact
uses: actions/upload-artifact@v2
with:
name: swagger-ui-dist
path: api/api/swagger-ui-dist/
retention-days: ${{ env.ARTIFACT_RETENTION_DAYS }}

build-api:
runs-on: ubuntu-latest
needs:
Expand All @@ -240,6 +221,10 @@ jobs:
# ${{ runner.os }}-vendor-${{ hashFiles('api/go.mod') }}
# restore-keys: ${{ runner.os }}-vendor-

- name: Build static Swagger UI dist
working-directory: api/api
run: make swagger-ui-dist

- name: Build Docker image
id: build-image
working-directory: api
Expand Down Expand Up @@ -382,17 +367,17 @@ jobs:
--port 80:80@loadbalancer
--k3s-server-arg "--no-deploy=traefik,metrics-server"
- name: Download Turing API Docker tar archieve
- name: Download Turing API Docker tar archive
uses: actions/download-artifact@v2
with:
name: turing-api.${{ env.TURING_API_VERSION }}.tar

- name: Download Turing Router Docker tar archieve
- name: Download Turing Router Docker tar archive
uses: actions/download-artifact@v2
with:
name: turing-router.${{ needs.build-router.outputs.router-version }}.tar

- name: Download Cluster Init Docker tar archieve
- name: Download Cluster Init Docker tar archive
uses: actions/download-artifact@v2
with:
name: cluster-init.${{ needs.build-cluster-init.outputs.cluster-init-version }}.tar
Expand Down Expand Up @@ -600,7 +585,6 @@ jobs:
- build-router
- build-api
- build-ui
- build-swagger-ui
- release-rules
- test-e2e
uses: gojek/turing/.github/workflows/turing-publish.yaml@main
Expand Down
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ RUN apk add --no-cache bash
USER ${TURING_USER}

ARG TURING_UI_DIST_PATH=ui/build
ARG SWAGGER_UI_DIST_PATH=api/api/swagger-ui-dist

ENV TURINGUICONFIG_SERVINGDIRECTORY "./turing-ui"
ENV OPENAPICONFIG_SWAGGERUICONFIG_SERVINGDIRECTORY "./swagger-ui"

COPY --chown=${TURING_USER}:${TURING_USER_GROUP} ${TURING_UI_DIST_PATH} ${TURINGUICONFIG_SERVINGDIRECTORY}/
COPY --chown=${TURING_USER}:${TURING_USER_GROUP} ${SWAGGER_UI_DIST_PATH} ${OPENAPICONFIG_SWAGGERUICONFIG_SERVINGDIRECTORY}/

COPY ./docker-entrypoint.sh ./

Expand Down
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ RUN addgroup -S ${TURING_USER_GROUP} \
&& chown -R ${TURING_USER}:${TURING_USER_GROUP} /app

COPY --chown=${TURING_USER}:${TURING_USER_GROUP} --from=api-builder /app/bin/* /app
COPY --chown=${TURING_USER}:${TURING_USER_GROUP} --from=api-builder /app/api /app/api
COPY --chown=${TURING_USER}:${TURING_USER_GROUP} --from=api-builder /app/db-migrations /app/db-migrations
COPY --chown=${TURING_USER}:${TURING_USER_GROUP} --from=api-builder /app/api/swagger-ui-dist /app/swagger-ui

USER ${TURING_USER}
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion api/turing/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func openapiValidationMiddleware(apiPath string, cfg *config.Config) (mux.Middle
}

openapiValidation, err := middleware.NewOpenAPIValidation(
cfg.OpenapiConfig.SpecFile,
specFile,
middleware.OpenAPIValidationOptions{
// Authentication is ignored because it is handled by another middleware
IgnoreAuthentication: true,
Expand Down
2 changes: 1 addition & 1 deletion api/turing/server/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Run() {
*cfg.OpenapiConfig.SwaggerUIConfigOverrideFile,
)
if err != nil {
log.Panicf("failed to merge openapi yamls.")
log.Panicf("failed to merge openapi yamls: %s", err)
}
}

Expand Down
22 changes: 15 additions & 7 deletions api/turing/utils/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"

Expand All @@ -14,36 +15,43 @@ import (
func MergeTwoYamls(originalYAMLFile, overrideYAMLFile string) error {
original, err := readYAML(originalYAMLFile)
if err != nil {
return err
return fmt.Errorf("error reading original yaml: %s", err)
}

override, err := readYAML(overrideYAMLFile)
if err != nil {
return err
return fmt.Errorf("error reading override yaml: %s", err)
}

merged, err := MergeMaps(original, override)
if err != nil {
return err
return fmt.Errorf("error merging maps: %s", err)
}

var output bytes.Buffer
yamlEncoder := yaml.NewEncoder(&output)
yamlEncoder.SetIndent(2)
err = yamlEncoder.Encode(merged)
if err != nil {
return err
return fmt.Errorf("error encoding: %s", err)
}

f, err := os.OpenFile(originalYAMLFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
return fmt.Errorf("error writing to file: %s", err)
}

w := bufio.NewWriter(f)
_, err = w.Write(output.Bytes())

return err
if _, err = w.Write(output.Bytes()); err != nil {
return fmt.Errorf("error writing: %s", err)
}

if err = w.Flush(); err != nil {
return fmt.Errorf("error flushing: %s", err)
}

return nil
}

// MergeMaps takes two maps with any value and merges it recursively.
Expand Down
2 changes: 1 addition & 1 deletion infra/charts/turing/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ OpenapiConfig:
ValidationEnabled: true
SwaggerUIConfigOverrideFile: /etc/openapi/override.yaml
SwaggerUIConfig:
ServingDirectory: ""
ServingDirectory: swagger-ui
ServingPath: /api-docs/
{{- end -}}

Expand Down

0 comments on commit d27254c

Please sign in to comment.