Search code examples
openshift

Use case of OpenShift + buildConfig + ConfigMaps


I am trying to create and run a buildconfig yml file.

C:\OpenShift>oc version
Client Version: 4.5.31
Kubernetes Version: v1.18.3+65bd32d
  • Background:-

I have multiple Springboot WebUI applications which i need to deploy on OpenShift

To have separate set of config yml files ( image stream, buildconfig, deployconfig, service, routes), for each and every application seems to be very inefficient.

Instead i would like to have a single set of parameterized yml files to which i can pass on custom parameters to setup each individual application

  • Solution so far:-

Version One

Dockerfile-

FROM org/rhelImage
USER root
# Install Yum Packages
RUN yum -y install\
net-tools\
&& yum -y install nmap-ncat\

RUN curl -s --create-dirs --insecure -L ${ARTIFACTURL} -o ${APPPATH}/${ARTIFACT}

# Add docker-entrypoint.sh to the image
ADD docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod -Rf 775 /app && chmod 775 /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN chmod -R g+rx /app


# Expose port
EXPOSE $MY_PORT

# Set working directory when container starts
WORKDIR $APPPATH

# Starting the applicaiton using ENTRYPOINT
#ENTRYPOINT ["sh","/docker-entrypoint.sh"]


$ oc create configmap myapp-configmap --from-env-file=MyApp.properties
configmap/myapp-configmap created

$ oc describe cm myapp-configmap
Name:         myapp-configmap
Namespace:    1234
Labels:       <none>
Annotations:  <none>

Data
====
APPPATH:
----
/app
ARTIFACT:
----
myapp.jar
ARTIFACTURL:
----
"https://myorg/1.2.3.4/myApp-1.2.3.4.jar"
MY_PORT:
----
12305
Events:  <none> 

buildconfig.yaml snippet

 strategy:
        dockerStrategy:
          env:
            - name: GIT_SSL_NO_VERIFY
              value: "true"
            - name: ARTIFACTURL
              valueFrom:
                configMapKeyRef:
                  name: "myapp-configmap"
                  key: ARTIFACTURL
            - name: ARTIFACT
              valueFrom:
                configMapKeyRef:
                  name: "myapp-configmap"
                  key: ARTIFACT

This works fine. However I somehow need to have those env: variables in file.

I am doing this to have greater flexibility, i.e. lets say I have a new variable introduced in docker file, I need NOT change the buildconfig.yml I just add the new key:value pair to the property file, rebuild and we are good to go

This is what I do next;

Version Two

Dockerfile

FROM org/rhelImage
USER root
# Install Yum Packages
RUN yum -y install\
net-tools\
&& yum -y install nmap-ncat\

#Intializing the variables file;
RUN ["sh", "-c", "source ./MyApp.properties"]

RUN curl -s --create-dirs --insecure -L ${ARTIFACTURL} -o ${APPPATH}/${ARTIFACT}

# Add docker-entrypoint.sh to the image
ADD docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod -Rf 775 /app && chmod 775 /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN chmod -R g+rx /app


# Expose port
EXPOSE $MY_PORT

# Set working directory when container starts
WORKDIR $APPPATH

# Starting the applicaiton using ENTRYPOINT
#ENTRYPOINT ["sh","/docker-entrypoint.sh"]


$ oc create configmap myapp-configmap --from-env-file=MyApp.properties=C:\MyRepo\MyTemplates\MyApp.properties
configmap/myapp-configmap created

C:\OpenShift>oc describe configmaps test-configmap
Name:         myapp-configmap
Namespace:    1234
Labels:       <none>
Annotations:  <none>

Data
====
MyApp.properties:
----
APPPATH=/app
ARTIFACTURL="https://myorg/1.2.3.4/myApp-1.2.3.4.jar"
ARTIFACT=myapp.jar
MY_PORT=12035
Events:  <none> 

buildconfig.yaml snippet

 source:
        contextDir: "${param_source_contextdir}"
        configMaps:
          - configMap:
              name: "${param_app_name}-configmap"

However the build fails

STEP 9: RUN ls ./MyApp.properties
ls: cannot access ./MyApp.properties: No such file or directory
error: build error: error building at STEP "RUN ls ./MyApp.properties": error while running runtime: exit status 2 
 

This means that the config map file didnt get copy to folder.

Can you please suggest what to do next?


Solution

  • I think you are misunderstanding Openshift a bit. The first thing you say is

    To have separate set of config yml files ( image stream, buildconfig, deployconfig, service, routes), for each and every application seems to be very inefficient.

    But that's how kubernetes/openshift works. If your resource files look the same, but only use a different git resource or image for example, then you probably are looking for Openshift Templates.

    Instead i would like to have a single set of parameterized yml files to which i can pass on custom parameters to setup each individual application

    Yep, I think Openshift Templates is what you are looking for. If you upload your template to the service catalog, whenever you have a new application to deploy, you can add some variables in a UI and click deploy.

    An Openshift Template is just a parameterised file for all of your openshift resources (configmap, service, buildconfig, etc.). If your application needs to be build from some git repo, using some credentials, you can parameterise those variables.

    But also take a look at Openshift's Source-to-Image solution (I'm not sure what version you are using, so you'll have to google some resources). It can build and deploy your application without you having to write your own Resource files.