Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add native and container custom transformer variants for maven to helm #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions maven-helmchartgen-container/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM ubuntu
WORKDIR /m2k
COPY . .
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
description: A Helm Chart generated by Move2Kube for <~ .ServiceName ~>
keywords:
- <~ .ServiceName ~>
name: <~ .ServiceName ~>
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
move2kube.konveyor.io/service.expose: "true"
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
annotations:
move2kube.konveyor.io/service.expose: "true"
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
containers:
- image: {{ .Values.registryurl }}/{{ .Values.registrynamespace }}/<~ .ServiceName ~>:latest
imagePullPolicy: Always
name: <~ .ServiceName ~>
ports:
- containerPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: {}

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
rules:
- host: '{{ .Values.ingresshostdomain }}'
http:
paths:
- backend:
service:
name: <~ .ServiceName ~>
port:
name: port-8080
path: /gateway
pathType: Prefix
status:
loadBalancer: {}

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

---
apiVersion: v1
kind: Service
metadata:
annotations:
move2kube.konveyor.io/service.expose: "true"
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
ports:
- name: port-8080
port: 8080
protocol: TCP
targetPort: 8080
selector:
move2kube.konveyor.io/service: <~ .ServiceName ~>
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ingresshostdomain: put.the.hostdomain.of.ingress.here
registryurl: put.the.image.registry.url.here
registrynamespace: put.the.registry.namespace.here
imagetag: put.the.image.tag.here
151 changes: 151 additions & 0 deletions maven-helmchartgen-container/transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)

var artifactRegex = regexp.MustCompile(`<artifactId>(\w+)</artifactId>`)

const PomFile = "pom.xml"

var input []byte = make([]byte, 2048)

// DirectoryDetect runs detect in each sub directory
func DirectoryDetect(inputPath string) map[string]interface{} {
jsonFile, err := os.Open(inputPath)
if err != nil {
fmt.Println(err)

}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var data map[string]interface{}
err = json.Unmarshal(byteValue, &data)
dataFilePath := filepath.Join(data["InputDirectory"].(string), PomFile)

transform_return_data := make(map[string]interface{})
if _, err := os.Stat(dataFilePath); err == nil {
serviceName := getServiceName(dataFilePath)
transform_return_data = map[string]interface{}{
serviceName: []map[string]interface{}{
{
"paths": map[string]interface{}{
"ServiceDirectories": []string{data["InputDirectory"].(string)},
"ServiceRootDirectory": []string{data["InputDirectory"].(string)},
"pomFiles": []string{dataFilePath},
},
},
},
}
return transform_return_data
}
return transform_return_data
}

// Transform transforms
func Transform(inputPath string) map[string]interface{} {
jsonFile, err := os.Open(inputPath)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var data map[string]interface{}
err = json.Unmarshal(byteValue, &data)
if err != nil {
panic(err)
}
newArtifacts := data["newArtifacts"].([]interface{})

pathMappings := []map[string]interface{}{}
artifacts := []map[string]interface{}{}
pathTemplate := "{{ SourceRel .ServiceFsPath }}"
for _, v := range newArtifacts {
v := v.(map[string]interface{})
serviceName := v["configs"].(map[string]interface{})["Service"].(map[string]interface{})["ServiceName"].(string)
paths := v["paths"].(map[string]interface{})
dirs := paths["ServiceDirectories"].([]interface{})
dir := dirs[0].(string)

// Create a path template for the service
pathTemplateName := strings.ReplaceAll(serviceName, "-", "") + "path"
tplPathData := map[string]interface{}{
"ServiceFsPath": dir,
"PathTemplateName": pathTemplateName,
}
pathMappings = append(pathMappings, map[string]interface{}{
"type": "PathTemplate",
"sourcePath": pathTemplate,
"templateConfig": tplPathData,
})
// Since the helm chart uses the same templating character {{ }} as Golang templates,
// we use `SpecialTemplate` type here where the templating character is <~ ~>.
// The `Template` type can be used for all normal cases
pathMappings = append(pathMappings, map[string]interface{}{
"type": "SpecialTemplate",
"destinationPath": "{{ ." + pathTemplateName + " }}",
"templateConfig": map[string]interface{}{
"ServiceFsPath": dir,
"ServiceName": serviceName,
},
})
pathMappings = append(pathMappings, map[string]interface{}{
"type": "Source",
"sourcePath": "{{ ." + pathTemplateName + " }}",
"destinationPath": "{{ ." + pathTemplateName + " }}",
})
}
transform_return_data := map[string]interface{}{
"pathMappings": pathMappings,
"artifacts": artifacts,
}
return transform_return_data
}

// getServiceName extracts service name from pom file
func getServiceName(filePath string) string {
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
return ""
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
m := artifactRegex.FindStringSubmatch(line)
if len(m) != 0 {
return m[1]
}
}
return ""
}

func main() {
// ./transformer detect inputpath outputpath
// ./transformer transform inputpath outputpath
action := os.Args[1]
var data map[string]interface{}
inputPath := os.Getenv(os.Args[2])
// fmt.Println(inputPath)
if action == "detect" {
data = DirectoryDetect(inputPath)
} else {
data = Transform(inputPath)
}
outputPath := os.Getenv(os.Args[3])
// fmt.Println(outputPath)
file, err := json.Marshal(data)
if err != nil {
panic(err)
}
os.MkdirAll(filepath.Dir(outputPath), 0755)
err = ioutil.WriteFile(outputPath, file, 0644)
if err != nil {
panic(err)
}
}
30 changes: 30 additions & 0 deletions maven-helmchartgen-container/transformer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: move2kube.konveyor.io/v1alpha1
kind: Transformer
metadata:
name: HelmChartGeneratorContainer
labels:
move2kube.konveyor.io/built-in: false
spec:
mode: "Container"
class: "Executable"
isolated: true
override:
matchLabels:
move2kube.konveyor.io/built-in: "true"
directoryDetect:
levels: -1
consumes:
Service:
merge: false
config:
platforms:
- "darwin"
directoryDetectCMD: ["./transformer", "detect", "M2K_DETECT_INPUT_PATH", "M2K_DETECT_OUTPUT_PATH"]
transformCMD: ["./transformer", "transform", "M2K_TRANSFORM_INPUT_PATH", "M2K_TRANSFORM_OUTPUT_PATH"]
container:
image: containerizedtransformer:latest
workingDir: "/m2k/"
keepAliveCommand: ["tail", "-f", "/dev/null"]
build:
dockerfile: "Dockerfile"
context: "."
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
description: A Helm Chart generated by Move2Kube for <~ .ServiceName ~>
keywords:
- <~ .ServiceName ~>
name: <~ .ServiceName ~>
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
move2kube.konveyor.io/service.expose: "true"
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
annotations:
move2kube.konveyor.io/service.expose: "true"
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
containers:
- image: {{ .Values.registryurl }}/{{ .Values.registrynamespace }}/<~ .ServiceName ~>:latest
imagePullPolicy: Always
name: <~ .ServiceName ~>
ports:
- containerPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: {}

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
creationTimestamp: null
labels:
move2kube.konveyor.io/service: <~ .ServiceName ~>
name: <~ .ServiceName ~>
spec:
rules:
- host: '{{ .Values.ingresshostdomain }}'
http:
paths:
- backend:
service:
name: <~ .ServiceName ~>
port:
name: port-8080
path: /gateway
pathType: Prefix
status:
loadBalancer: {}

...
Loading
Loading