Skip to Content
Technical Articles

Kyma Maven Plugin

Deploying an application powered via the SAP Marketing Cloud SDK to Kyma has been a tricky process. Unfortunately, it was automated only for Unix based systems. Even after automating the build and deploy process, there were some cumbersome tasks a developer had to perform to successfully execute the build script provided, which were as follows:

  • Having a Docker daemon set to be running on the system.
  • Installing the kubectl command line interface and mapping it to their cluster context.

Typical Java developers who are simply interested in their application  need not have to go through such trouble to deploy their application on Kyma. Hence, we developed a Maven plugin for general usage, to deploy any application container to Kyma via a Maven workflow. By the amalgamation of kyma-maven-plugin with the maven-jib-plugin, developers can have a best breed deployment solution for Maven application deployment to Kyma.

The maven-jib-plugin takes care of containerising your Maven application into Docker containers. The kyma-maven-plugin would create/apply a custom resource definition to a Kyma cluster and create a resource of type “Service” and “Deployment”.

Traditional Way of Creating Containers

To create Docker containers, the Continuous Integration (CI) system needs to ensure running of a Docker daemon, which must be authenticated to a docker registry for pushing the docker image. Having a perennial Docker daemon is an added CLI dependency to be managed separately along with your build process. To overcome this, we integrated our application archetype with the maven-jib-plugin.

# Authenticate to Docker Registry
docker login

# Build a Docker image using a Dockerfile
docker build -t ${dockerImageName} .

# Push the Docker image to a Docker registry
docker push ${dockerImageName}

Creating Containers Using maven-jib-plugin

Jib builds optimised Docker -images for your Java applications without the need for a Docker daemon or deep mastery of Docker best practices. It is available as plugins for Maven. To containerise your Java application during the maven lifecycle process, all you have to do is add the below snippet to your pom.xml ‘s “<build>” section and you’d be good to go.

<plugin>                          
  <groupId>com.google.cloud.tools</groupId>                            
  <artifactId>jib-maven-plugin</artifactId>                            
  <version>1.0.2</version>                            
  <configuration>                                    
    <to>                                            
      <image>registry.hub.docker.com/${dockerImageName}</image>                                          
      <credHelper>osxkeychain</credHelper>                                     
    </to>                            
  </configuration>                          
  <executions>                                    
    <execution>                                            
      <phase>package</phase>                                           
      <goals>                                                    
        <goal>build</goal>                                           
      </goals>                                 
    </execution>                           
  </executions>                 
</plugin>

For more information on jib, please visit https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin

Traditional Way of Deploying Containers

Marketing Cloud SDK archetype shipped a YAML specification known as CRD (Custom Resource Definition), to instruct a Kyma cluster on what container to deploy. But to apply this CRD, the build system needs to have a Kubernetes CLI installed with its context mapped to your Kubernetes cluster powered by Kyma.

It is an additional CLI dependency to add and manage it in your build environment. To overcome this, we developed a Maven plugin to apply these CRDs as a simple configuration to your build profile. This plugin is named as kyma-maven-plugin, which is a Beta offering from SAP Marketing Cloud SDK, available for evaluation from release 1.2.0

apiVersion: v1
kind: Service
metadata:
  name: ${artifactId}
  labels:
    application: ${artifactId}
  annotations:
    auth.istio.io/8080: NONE
spec:
  ports:
  - name: http
    port: 8080
  type: NodePort
  selector:
    application: ${artifactId}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ${artifactId}
spec:
  replicas: 1
  template:
    metadata:
      labels:
        application: ${artifactId}
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      containers:
      - image: ${dockerImageName}
        imagePullPolicy: Always
        name: ${artifactId}
        ports:
        - name: http
          containerPort: 8080

Deploying a container using kyma-maven-plugin

The kyma-maven-plugin has significantly reduced the amount of static code shipped as part of SAP Marketing Cloud SDK archetype’s build and deploy process. Using this plugin, you can deploy your containers and orchestrate them without Kubernetes CLI. Hence, no more “kubectl apply …”.

As a prerequisite to this plugin, ensure that you give the correct kubeconfig path. kubeconfig is a cluster configuration file that you can download from the Kyma Console UI. All you have to do is, navigate to Kyma Console UI > General Settings > Download config. 

To use kyma-maven-plugin, add the below snippet to your pom.xml ‘s “<build>” section and you should be good to go.

<plugin>                          
  <groupId>com.sap.cloud.mkt</groupId>                            
  <artifactId>kyma-maven-plugin</artifactId>                            
  <version>1.2.0</version>                            
  <configuration>                                      
    <kubeConfig>${KUBEPATH}/kubeconfig</kubeConfig>
    <!-- path to kubeconfig file -->                                    
    <artifactId>${artifactId}</artifactId>                                   
    <namespace>stage</namespace>
    <!-- kyma namespace to deploy -->                                 
    <dockerImageName>${dockerImageName}</dockerImageName>                               
    <replicas>1</replicas>
    <!-- Number of pods to be spawned -->                        
  </configuration>                     
  <executions>                               
    <execution>                                      
      <phase>package</phase>                                  
      <goals>                                                 
        <goal>deployToKyma</goal>                                          
      </goals>                                 
    </execution>                          
  </executions>                   
</plugin>

After adding these plugins, use the Maven command: mvn clean install to build your application.

You have now learnt how to use the Kyma Maven Plugin to deploy a container. Go ahead and give it a try!

Be the first to leave a comment
You must be Logged on to comment or reply to a post.