Skip to content

Commit

Permalink
feat(runtime): get rid off camel k runtime dependency - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jan 25, 2024
1 parent d8a72a5 commit ee7b96b
Show file tree
Hide file tree
Showing 23 changed files with 967 additions and 218 deletions.
2 changes: 1 addition & 1 deletion e2e/common/traits/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestBuilderTrait(t *testing.T) {

// Check logs
Eventually(Logs(integrationKitNamespace, builderKitName, corev1.PodLogOptions{Container: "custom1"})).Should(ContainSubstring(`generated-bytecode.jar`))
Eventually(Logs(integrationKitNamespace, builderKitName, corev1.PodLogOptions{Container: "custom2"})).Should(ContainSubstring(`<artifactId>camel-k-runtime-bom</artifactId>`))
Eventually(Logs(integrationKitNamespace, builderKitName, corev1.PodLogOptions{Container: "custom2"})).Should(ContainSubstring(`<artifactId>camel-quarkus-core</artifactId>`))

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/camel/v1/maven_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package v1

import (
"encoding/xml"
"errors"
"io"
)

func (in *MavenArtifact) GetDependencyID() string {
Expand All @@ -36,16 +38,19 @@ type propertiesEntry struct {
Value string `xml:",chardata"`
}

// AddAll --.
func (m Properties) AddAll(properties map[string]string) {
for k, v := range properties {
m.Add(k, v)
}
}

// Add --.
func (m Properties) Add(key string, value string) {
m[key] = value
}

// MarshalXML --.
func (m Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {
return nil
Expand All @@ -65,20 +70,48 @@ func (m Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeToken(start.End())
}

// nolint: musttag
type xmlMapEntry struct {
XMLName xml.Name
Value string `xml:",chardata"`
}

// UnmarshalXML --.
func (m *Properties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
*m = Properties{}
for {
var e xmlMapEntry

err := d.Decode(&e)
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return err
}

(*m)[e.XMLName.Local] = e.Value
}
return nil
}

// AddAll -- .
func (m PluginProperties) AddAll(properties map[string]string) {
for k, v := range properties {
m.Add(k, v)
}
}

// Add -- .
func (m PluginProperties) Add(key string, value string) {
m[key] = StringOrProperties{Value: value}
}

// AddProperties -- .
func (m PluginProperties) AddProperties(key string, properties map[string]string) {
m[key] = StringOrProperties{Properties: properties}
}

// MarshalXML -- .
func (m PluginProperties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {
return nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/camel/v1/maven_types_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ func TestMarshalProperties(t *testing.T) {
assert.Contains(t, result, "<v2>bar</v2>")
}

func TestUnMarshalProperties(t *testing.T) {
x := `<properties>
<p1>val</p1>
<p2>val2</p2>
</properties>`
var mp map[string]string
err := xml.Unmarshal([]byte(x), (*Properties)(&mp))
assert.Nil(t, err)
assert.Equal(t, "val", mp["p1"])
assert.Equal(t, "val2", mp["p2"])
}

func TestMarshalEmptyPluginProperties(t *testing.T) {
buf := new(bytes.Buffer)
e := xml.NewEncoder(buf)
Expand Down
95 changes: 41 additions & 54 deletions pkg/builder/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type quarkusSteps struct {
CommonSteps []Step
}

// Quarkus contains the steps required to build a Camel Quarkus Maven application.
var Quarkus = quarkusSteps{
LoadCamelQuarkusCatalog: NewStep(InitPhase, loadCamelQuarkusCatalog),
GenerateQuarkusProject: NewStep(ProjectGenerationPhase, generateQuarkusProject),
Expand Down Expand Up @@ -126,11 +127,13 @@ func loadCamelQuarkusCatalog(ctx *builderContext) error {
}

func generateQuarkusProject(ctx *builderContext) error {
p := GenerateQuarkusProjectCommon(
ctx.Build.Runtime.Version,
p, err := generateQuarkusProjectCommon(
ctx.Build.Runtime.Metadata["quarkus.group.id"],
ctx.Build.Runtime.Metadata["quarkus.version"],
ctx.Build.Maven.Properties)

if err != nil {
return err
}
// Add Maven build extensions
p.Build.Extensions = ctx.Build.Maven.Extension

Expand All @@ -143,41 +146,22 @@ func generateQuarkusProject(ctx *builderContext) error {
return nil
}

func GenerateQuarkusProjectCommon(runtimeVersion string, quarkusVersion string, buildTimeProperties map[string]string) maven.Project {
p := maven.NewProjectWithGAV("org.apache.camel.k.integration", "camel-k-integration", defaults.Version)
p.DependencyManagement = &maven.DependencyManagement{Dependencies: make([]maven.Dependency, 0)}
p.Dependencies = make([]maven.Dependency, 0)
p.Build = &maven.Build{Plugins: make([]maven.Plugin, 0)}
func generateQuarkusProjectCommon(quarkusPlatformGroupID string, quarkusPlatformVersion string, buildTimeProperties map[string]string) (maven.Project, error) {
p, err := maven.LoadProjectWithGAV("org.apache.camel.k.integration", "camel-k-integration", defaults.Version)
if err != nil {
return maven.Project{}, err
}

p.Properties["quarkus.platform.group-id"] = quarkusPlatformGroupID
p.Properties["quarkus.platform.version"] = quarkusPlatformVersion
// set fast-jar packaging by default, since it gives some startup time improvements
p.Properties.Add("quarkus.package.type", "fast-jar")

// DependencyManagement
p.DependencyManagement.Dependencies = append(p.DependencyManagement.Dependencies,
maven.Dependency{
GroupID: "org.apache.camel.k",
ArtifactID: "camel-k-runtime-bom",
Version: runtimeVersion,
Type: "pom",
Scope: "import",
},
)

// Add all the properties from the build configuration
p.Properties.AddAll(buildTimeProperties)

// Quarkus build time properties
buildProperties := make(map[string]string)

// disable quarkus banner
buildProperties["quarkus.banner.enabled"] = "false"

// camel-quarkus does route discovery at startup, but we don't want
// this to happen as routes are loaded at runtime and looking for
// routes at build time may try to load camel-k-runtime routes builder
// proxies which in some case may fail.
buildProperties["quarkus.camel.routes-discovery.enabled"] = "false"

// required for to resolve data type transformers at runtime with service discovery
// the different Camel runtimes use different resource paths for the service lookup
buildProperties["quarkus.camel.service.discovery.include-patterns"] = "META-INF/services/org/apache/camel/datatype/converter/*,META-INF/services/org/apache/camel/datatype/transformer/*,META-INF/services/org/apache/camel/transformer/*"
Expand All @@ -188,29 +172,30 @@ func GenerateQuarkusProjectCommon(runtimeVersion string, quarkusVersion string,
buildProperties[key] = value
}
}
if err = setBuildPluginExecutionConfiguration(p.Build, buildProperties); err != nil {
return p, err
}

return p, nil
}

// identify the build goal of quarkus-maven-plugin and set its configuration.
func setBuildPluginExecutionConfiguration(build *maven.Build, props map[string]string) error {
configuration := v1.PluginProperties{}
configuration.AddProperties("properties", buildProperties)

// Plugins
p.Build.Plugins = append(p.Build.Plugins,
maven.Plugin{
GroupID: "io.quarkus",
ArtifactID: "quarkus-maven-plugin",
Version: quarkusVersion,
Executions: []maven.Execution{
{
ID: "build-integration",
Goals: []string{
"build",
},
Configuration: configuration,
},
},
},
)

return p
configuration.AddProperties("properties", props)
for i := range build.Plugins {
if build.Plugins[i].ArtifactID == "quarkus-maven-plugin" {
for j := range build.Plugins[i].Executions {
for k := range build.Plugins[i].Executions[j].Goals {
if build.Plugins[i].Executions[j].Goals[k] == "build" {
build.Plugins[i].Executions[j].Configuration = configuration
return nil
}
}
}
}
}
return fmt.Errorf("no quarkus-maven-plugin build goal found")
}

func buildQuarkusRunner(ctx *builderContext) error {
Expand All @@ -228,15 +213,15 @@ func buildQuarkusRunner(ctx *builderContext) error {
)
}

err := BuildQuarkusRunnerCommon(ctx.C, mc, ctx.Maven.Project)
err := buildQuarkusRunnerCommon(ctx.C, mc, ctx.Maven.Project)
if err != nil {
return err
}

return nil
}

func BuildQuarkusRunnerCommon(ctx context.Context, mc maven.Context, project maven.Project) error {
func buildQuarkusRunnerCommon(ctx context.Context, mc maven.Context, project maven.Project) error {
resourcesPath := filepath.Join(mc.Path, "src", "main", "resources")
if err := os.MkdirAll(resourcesPath, os.ModePerm); err != nil {
return fmt.Errorf("failure while creating resource folder: %w", err)
Expand All @@ -261,6 +246,8 @@ func BuildQuarkusRunnerCommon(ctx context.Context, mc maven.Context, project mav
return nil
}

// computeQuarkusDependencies scans the executed maven project target and feed the context with the dependencies
// eventually required at runtime.
func computeQuarkusDependencies(ctx *builderContext) error {
mc := maven.NewContext(filepath.Join(ctx.Path, "maven"))
mc.GlobalSettings = ctx.Maven.GlobalSettings
Expand All @@ -270,7 +257,7 @@ func computeQuarkusDependencies(ctx *builderContext) error {
mc.AdditionalArguments = ctx.Build.Maven.CLIOptions

// Process artifacts list and add it to existing artifacts
artifacts, err := ProcessQuarkusTransitiveDependencies(mc)
artifacts, err := processQuarkusTransitiveDependencies(mc)
if err != nil {
return err
}
Expand All @@ -279,7 +266,7 @@ func computeQuarkusDependencies(ctx *builderContext) error {
return nil
}

func ProcessQuarkusTransitiveDependencies(mc maven.Context) ([]v1.Artifact, error) {
func processQuarkusTransitiveDependencies(mc maven.Context) ([]v1.Artifact, error) {
var artifacts []v1.Artifact

// Quarkus fast-jar format is split into various sub-directories in quarkus-app
Expand Down
Loading

0 comments on commit ee7b96b

Please sign in to comment.