Skip to content

Commit

Permalink
Fix fabric8io#2662: Allow option to containerize Go Model Schema gene…
Browse files Browse the repository at this point in the history
…ration builds

Added a bash script for doing Go Model generation Builds inside a docker
container. Go Build and schema generation are done inside a container
and copied to host folders afterwards
  • Loading branch information
rohanKanojia committed Feb 5, 2021
1 parent edd9b94 commit d84ab20
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 3 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/gobuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: Go Build

on:
push:
branches:
- master
pull_request:

jobs:
build:
name: Kubernetes Model Go to JSON Schema Generator Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build Project
run: |
ls
cd kuberntes-model-generator/
sh generateModelDocker.sh all
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fix #2748: Pass custom headers in kubernetes-client to watch api by modify WatchConnectionManager

#### Improvements
* Fix #2662: Allow option to containerize Go Model Schema generation builds
* Fix #2717: Remove edit() methods from RawCustomResourceOperationsImpl taking InputStream arguments

#### Dependency Upgrade
Expand Down
23 changes: 20 additions & 3 deletions kubernetes-model-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,31 @@ API resources in Kubernetes and OpenShift.

- Navigate into the cloned repo:

```
```bash
cd $GOPATH/src/github.com/fabric8io/kubernetes-client/kubernetes-model-generator
```

- Run:
```
If you have already have [GoLang installed](https://golang.org/doc/install). You can use this script to generate model for all the modules:
```bash
sh generateModel.sh
```
If you want to generate model for a single module, let's say `kubernetes-model-apps/`. You would need to go to that module and issue a `make`:
```bash
cd kubernetes-model-apps/
make
```

If you don't have GoLang setup on your system. You can run this whole Go Build process in a docker container using this script:

Building a Single Module(e.g. `kubernetes-model-apps`
```bash
sh generateModelDocker.sh kubernetes-model-apps
```
Building all modules
```bash
sh generateModelDocker.sh all
```

You should now be able to view the generated schema in `kube-schema.json`

Expand All @@ -91,7 +108,7 @@ tags/references from these repos:
- [Kubernetes Client Go](https://github.com/kubernetes/client-go)

After modifying just run:
```
```bash
sh generateModel.sh
```

Expand Down
176 changes: 176 additions & 0 deletions kubernetes-model-generator/generateModelDocker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#!/bin/bash
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


set -e

# Array for all existing modules
declare -a modules=(
"kubernetes-model-core"
"kubernetes-model-rbac"
"kubernetes-model-admissionregistration"
"kubernetes-model-apiextensions"
"kubernetes-model-apps"
"kubernetes-model-autoscaling"
"kubernetes-model-batch"
"kubernetes-model-certificates"
"kubernetes-model-coordination"
"kubernetes-model-discovery"
"kubernetes-model-events"
"kubernetes-model-extensions"
"kubernetes-model-networking"
"kubernetes-model-metrics"
"kubernetes-model-node"
"kubernetes-model-policy"
"kubernetes-model-scheduling"
"kubernetes-model-storageclass"
"openshift-model"
"openshift-model-operator"
"openshift-model-operatorhub"
"openshift-model-console"
"openshift-model-monitoring"
)

# Build Docker Image using Dockerfile and run a container
# Parameter: module name
doDockerBuildBasedOnDockerFileAndRunContainer() {
echo "Starting Docker Build for Dockerfile";
docker build -t $1-model-generator-image .
echo "Deleting container named $1 if exists";
docker rm -f $1 || true
echo "Running container named $1 based on image $1-generator-image";
docker run --name $1 $1-model-generator-image
}

# Once Docker container is running, copy schema files from container to host
# Parameter: container id, module name
copyGeneratedJsonSchemaFromContainerToHost() {
echo "Copying $1:/home/generator/generated-schema/$2/kube-schema.json -> ./$2/src/main/resources/schema/kube-schema.json";
docker cp $1:/home/generator/generated-schema/$2/kube-schema.json ./$2/src/main/resources/schema/kube-schema.json
echo "Copying $1:/home/generator/generated-schema/$2/validation-schema.json -> ./$2/src/main/resources/schema/validation-schema.json";
docker cp $1:/home/generator/generated-schema/$2/validation-schema.json ./$2/src/main/resources/schema/validation-schema.json
}

# Builds up a Dockerfile and docker image, then spins up a docker container
# based on the image created. Finally the generated schema files are copied to
# specified module's src/main/resources/schema directory
# Parameter: module name
generateKubernetesModelSchemaForModule() {
echo "Generating Dockerfile for module: $1";
generateDockerFileForModule $1
echo "Dockerfile generated. Starting docker build now...";
doDockerBuildBasedOnDockerFileAndRunContainer $1;
local containerId=`docker ps -aqf "name=$1"`;
echo "Checking container: $containerId";

copyGeneratedJsonSchemaFromContainerToHost $containerId $1
rm Dockerfile
echo "[Success] You can check updated JSON schema files in $1/src/main/resources/schema";
}

# Builds up a Dockerfile for all modules and docker image, then spings up a docker container
# based on the image created. Final all the generated schema files are copied to
# respective modules' src/main/resources/schema directories
generateKubernetesModelSchemaForAllModules() {
moduleName="kubernetes-model-complete"
echo "Generating Dockerfile for ALL modules";
generateDockerFileForAllModules
doDockerBuildBasedOnDockerFileAndRunContainer $moduleName;
local containerId=`docker ps -aqf "name=$moduleName"`;
for module in ${modules[*]}
do
copyGeneratedJsonSchemaFromContainerToHost $containerId $module
echo "[Success] You can check updated JSON schema files in $module/src/main/resources/schema";
done
rm Dockerfile
}

# Generate Dockerfile for a docker based build for a specific module
# Parameter: module name
generateDockerFileForModule() {
writeFromAndWorkdirToDockerfile;
writeModelGenerationRunsToDockerFile $1;
writeEntrypointToDockerFile;
}

# Write Dockerfile Header statements like FROM, ADD etc
writeFromAndWorkdirToDockerfile() {
cat << EOF > Dockerfile
FROM golang:1.15.6
ADD . /home/generator/
WORKDIR /home/generator/
RUN mkdir generated-schema
EOF
}

# Write Go Build and schema generation statements for a particular module
# Parameter: module name
writeModelGenerationRunsToDockerFile() {
printf "RUN CGO_ENABLED=0 GO111MODULE=on GO15VENDOREXPERIMENT=1 go build -a ./$1/cmd/generate/generate.go\n" >> Dockerfile;
printf "RUN sh -c 'mkdir -p ./generated-schema/$1'\n" >> Dockerfile;
printf "RUN sh -c './generate > ./generated-schema/$1/kube-schema.json'\n" >> Dockerfile;
printf "RUN sh -c './generate validation > ./generated-schema/$1/validation-schema.json'\n" >> Dockerfile
}

# Write an entrypoint which just lists contents of generated-schema folder
writeEntrypointToDockerFile() {
printf "ENTRYPOINT [\"ls\", \"generated-schema\"]" >> Dockerfile;
}

# Generate a Dockerfile for Schema generation for all modules
generateDockerFileForAllModules() {
echo "generateDockerFileForAllModules";
writeFromAndWorkdirToDockerfile;
for module in ${modules[*]}
do
writeModelGenerationRunsToDockerFile $module;
done
writeEntrypointToDockerFile;
}

# Generate Kubernetes Model Schema for ALL modules by calling generateKubernetesModelSchemaForModule for each
# element of modules array
generateKubernetesModelSchemasForAllModules() {
generateKubernetesModelSchemaForAllModules
}

# Check whether Docker is running by running a basic process list command
checkWhetherDockerRunning() {
docker ps > /dev/null;
}

# Validate whether first argument is not null
checkWhetherCommandLineArgumentProvided() {
if [ -z $1 ];
then
echo "Invalid argument; Sample Usage: sh generateModelDocker.sh [module-name]";
exit 1;
fi;
}

#####################################################################################
## Main
#####################################################################################
checkWhetherCommandLineArgumentProvided $1
checkWhetherDockerRunning
if [ $1 == "all" ];
then
echo "Building Model for ALL modules";
generateKubernetesModelSchemasForAllModules
exit 0;
fi;
generateKubernetesModelSchemaForModule $1

0 comments on commit d84ab20

Please sign in to comment.