Skip to content

Commit 86b5c28

Browse files
authored
COH-24004 - The Operator runner should support the JIB JVM argument files (#507)
1 parent 8e03f50 commit 86b5c28

File tree

4 files changed

+285
-5
lines changed

4 files changed

+285
-5
lines changed

java/operator-test-helidon/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<version>${version.plugin.jib}</version>
6464
<configuration>
6565
<container>
66-
<mainClass>io.helidon.microprofile.cdi.com.oracle.coherence.k8s.testing.Main</mainClass>
66+
<mainClass>io.helidon.microprofile.server.Main</mainClass>
6767
</container>
6868
<!-- MUST use packaged mode for Helidon CDI application -->
6969
<containerizingMode>packaged</containerizingMode>

pkg/runner/cmd_server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ func server(details *RunDetails, _ *cobra.Command) {
4141
// If the main class environment variable is set then use that
4242
// otherwise run Coherence DCS.
4343
mc, found := details.lookupEnv(v1.EnvVarAppMainClass)
44+
appDir := details.getenvOrDefault(v1.EnvVarCohAppDir, "/app")
45+
jibMainClassFileName := appDir + "/jib-main-class-file"
46+
fi, err := os.Stat(jibMainClassFileName)
47+
mainCls := ""
48+
if err == nil && (fi.Size() != 0) {
49+
mainCls = readFirstLineFromFile(jibMainClassFileName, fi)
50+
}
51+
if !found && (len(mainCls) != 0) {
52+
mc = mainCls
53+
found = true
54+
}
4455
switch {
4556
case found && details.AppType != AppTypeSpring:
4657
// we have a main class specified, and we're not a Spring Boot app

pkg/runner/runner.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package runner
88

99
import (
10+
"bufio"
1011
"bytes"
1112
"context"
1213
"fmt"
@@ -273,10 +274,18 @@ func createCommand(details *RunDetails) (string, *exec.Cmd, error) {
273274
// are running a Spring Boot application.
274275
if !details.isBuildPacks() && details.AppType != AppTypeSpring && details.isEnvTrueOrBlank(v1.EnvVarJvmClasspathJib) {
275276
appDir := details.getenvOrDefault(v1.EnvVarCohAppDir, "/app")
276-
details.addClasspathIfExists(appDir + "/resources")
277-
details.addClasspathIfExists(appDir + "/classes")
278-
details.addJarsToClasspath(appDir + "/classpath")
279-
details.addJarsToClasspath(appDir + "/libs")
277+
fi, e := os.Stat(appDir + "/jib-classpath-file")
278+
if e == nil && (fi.Size() != 0) {
279+
clsPath := readFirstLineFromFile(appDir+"/jib-classpath-file", fi)
280+
if len(clsPath) != 0 {
281+
details.addClasspath(clsPath)
282+
}
283+
} else {
284+
details.addClasspathIfExists(appDir + "/resources")
285+
details.addClasspathIfExists(appDir + "/classes")
286+
details.addJarsToClasspath(appDir + "/classpath")
287+
details.addJarsToClasspath(appDir + "/libs")
288+
}
280289
}
281290

282291
// Add the Operator Utils jar to the classpath
@@ -557,6 +566,23 @@ func createJavaCommand(javaCmd string, details *RunDetails) (*exec.Cmd, error) {
557566
return _createJavaCommand(javaCmd, details, args)
558567
}
559568

569+
func readFirstLineFromFile(fqfn string, fi os.FileInfo) string {
570+
log.Info(fmt.Sprintf("%s size=%d", fi.Name(), fi.Size()))
571+
file, _ := os.Open(fqfn)
572+
scanner := bufio.NewScanner(file)
573+
scanner.Split(bufio.ScanLines)
574+
var text []string
575+
for scanner.Scan() {
576+
text = append(text, scanner.Text())
577+
}
578+
file.Close()
579+
if len(text) == 0 {
580+
return ""
581+
}
582+
log.Info(fmt.Sprintf("First Line of the %s:\n%s\n", fi.Name(), text[0]))
583+
return text[0]
584+
}
585+
560586
func createSpringBootCommand(javaCmd string, details *RunDetails) (*exec.Cmd, error) {
561587
if details.isBuildPacks() {
562588
return _createBuildPackCommand(details, SpringBootMain, details.getSpringBootArgs())
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at
4+
* http://oss.oracle.com/licenses/upl.
5+
*/
6+
7+
package runner
8+
9+
import (
10+
"bufio"
11+
"fmt"
12+
. "github.com/onsi/gomega"
13+
coh "github.com/oracle/coherence-operator/api/v1"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
"k8s.io/utils/pointer"
16+
"os"
17+
"testing"
18+
)
19+
20+
func TestJibClasspath(t *testing.T) {
21+
g := NewGomegaWithT(t)
22+
23+
d := &coh.Coherence{
24+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
25+
Spec: coh.CoherenceResourceSpec{
26+
JVM: &coh.JVMSpec{
27+
UseJibClasspath: pointer.BoolPtr(true),
28+
},
29+
},
30+
}
31+
32+
args := []string{"server", "--dry-run"}
33+
env := EnvVarsFromDeployment(d)
34+
35+
expectedCommand := GetJavaCommand()
36+
expectedArgs := GetMinimalExpectedArgs()
37+
38+
e, err := ExecuteWithArgs(env, args)
39+
g.Expect(err).NotTo(HaveOccurred())
40+
g.Expect(e).NotTo(BeNil())
41+
g.Expect(e.OsCmd).NotTo(BeNil())
42+
43+
g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir))
44+
g.Expect(e.OsCmd.Path).To(Equal(expectedCommand))
45+
g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs))
46+
}
47+
48+
func TestJibClasspathFile(t *testing.T) {
49+
g := NewGomegaWithT(t)
50+
51+
d := &coh.Coherence{
52+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
53+
Spec: coh.CoherenceResourceSpec{
54+
JVM: &coh.JVMSpec{
55+
UseJibClasspath: pointer.BoolPtr(true),
56+
},
57+
},
58+
}
59+
60+
args := []string{"server", "--dry-run"}
61+
env := EnvVarsFromDeployment(d)
62+
63+
expectedCommand := GetJavaCommand()
64+
f := createJibClasspathFile()
65+
defer os.Remove(f.Name())
66+
expectedArgs := GetMinimalExpectedArgsWithAppClasspathFile()
67+
68+
e, err := ExecuteWithArgs(env, args)
69+
g.Expect(err).NotTo(HaveOccurred())
70+
g.Expect(e).NotTo(BeNil())
71+
g.Expect(e.OsCmd).NotTo(BeNil())
72+
73+
g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir))
74+
g.Expect(e.OsCmd.Path).To(Equal(expectedCommand))
75+
g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs))
76+
}
77+
78+
func TestJibMainClassFile(t *testing.T) {
79+
g := NewGomegaWithT(t)
80+
81+
d := &coh.Coherence{
82+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
83+
Spec: coh.CoherenceResourceSpec{
84+
JVM: &coh.JVMSpec{
85+
UseJibClasspath: pointer.BoolPtr(true),
86+
},
87+
},
88+
}
89+
90+
args := []string{"server", "--dry-run"}
91+
env := EnvVarsFromDeployment(d)
92+
93+
expectedCommand := GetJavaCommand()
94+
f := createJibMainClassFile()
95+
defer os.Remove(f.Name())
96+
expectedArgs := GetMinimalExpectedArgsWithAppMainClassFile()
97+
98+
e, err := ExecuteWithArgs(env, args)
99+
g.Expect(err).NotTo(HaveOccurred())
100+
g.Expect(e).NotTo(BeNil())
101+
g.Expect(e.OsCmd).NotTo(BeNil())
102+
103+
g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir))
104+
g.Expect(e.OsCmd.Path).To(Equal(expectedCommand))
105+
g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs))
106+
}
107+
108+
func TestJibClasspathFileAndMainClassFile(t *testing.T) {
109+
g := NewGomegaWithT(t)
110+
111+
d := &coh.Coherence{
112+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
113+
Spec: coh.CoherenceResourceSpec{
114+
JVM: &coh.JVMSpec{
115+
UseJibClasspath: pointer.BoolPtr(true),
116+
},
117+
},
118+
}
119+
120+
args := []string{"server", "--dry-run"}
121+
env := EnvVarsFromDeployment(d)
122+
123+
expectedCommand := GetJavaCommand()
124+
f1 := createJibClasspathFile()
125+
defer os.Remove(f1.Name())
126+
f2 := createJibMainClassFile()
127+
defer os.Remove(f2.Name())
128+
expectedArgs := GetMinimalExpectedArgsWithAppClasspathFileAndMainClassFile()
129+
130+
e, err := ExecuteWithArgs(env, args)
131+
g.Expect(err).NotTo(HaveOccurred())
132+
g.Expect(e).NotTo(BeNil())
133+
g.Expect(e.OsCmd).NotTo(BeNil())
134+
135+
g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir))
136+
g.Expect(e.OsCmd.Path).To(Equal(expectedCommand))
137+
g.Expect(e.OsCmd.Args).To(ConsistOf(expectedArgs))
138+
}
139+
140+
func createJibClasspathFile() *os.File {
141+
f, err := os.Create(TestAppDir + string(os.PathSeparator) + "jib-classpath-file")
142+
if err != nil {
143+
fmt.Print(err)
144+
os.Exit(1)
145+
}
146+
147+
_, err = f.WriteString(fmt.Sprintf("%s/classpath/*:%s/libs/*", TestAppDir, TestAppDir))
148+
if err != nil {
149+
fmt.Print(err)
150+
os.Exit(1)
151+
}
152+
err = f.Close()
153+
if err != nil {
154+
fmt.Print(err)
155+
os.Exit(1)
156+
}
157+
158+
return f
159+
}
160+
161+
func createJibMainClassFile() *os.File {
162+
f, err := os.Create(TestAppDir + string(os.PathSeparator) + "jib-main-class-file")
163+
if err != nil {
164+
fmt.Print(err)
165+
os.Exit(1)
166+
}
167+
168+
_, err = f.WriteString("com.tangosol.net.DefaultCacheServer")
169+
if err != nil {
170+
fmt.Print(err)
171+
os.Exit(1)
172+
}
173+
err = f.Close()
174+
if err != nil {
175+
fmt.Print(err)
176+
os.Exit(1)
177+
}
178+
179+
return f
180+
}
181+
182+
func GetMinimalExpectedArgsWithAppClasspathFile() []string {
183+
fileName := fmt.Sprintf("%s/jib-classpath-file", TestAppDir)
184+
cp := readFirstLine(fileName)
185+
args := []string{
186+
GetJavaCommand(),
187+
"-cp",
188+
cp + ":/coherence-operator/utils/lib/coherence-operator.jar:/coherence-operator/utils/config",
189+
}
190+
191+
return append(AppendCommonExpectedArgs(args),
192+
"com.oracle.coherence.k8s.Main",
193+
"com.tangosol.net.DefaultCacheServer")
194+
}
195+
196+
func GetMinimalExpectedArgsWithAppMainClassFile() []string {
197+
cp := fmt.Sprintf("%s/resources:%s/classes:%s/classpath/bar2.JAR:%s/classpath/foo2.jar:%s/libs/bar1.JAR:%s/libs/foo1.jar",
198+
TestAppDir, TestAppDir, TestAppDir, TestAppDir, TestAppDir, TestAppDir)
199+
200+
args := []string{
201+
GetJavaCommand(),
202+
"-cp",
203+
cp + ":/coherence-operator/utils/lib/coherence-operator.jar:/coherence-operator/utils/config",
204+
}
205+
206+
fileName := fmt.Sprintf("%s/jib-main-class-file", TestAppDir)
207+
mainCls := readFirstLine(fileName)
208+
return append(AppendCommonExpectedArgs(args),
209+
"com.oracle.coherence.k8s.Main",
210+
mainCls)
211+
}
212+
213+
func GetMinimalExpectedArgsWithAppClasspathFileAndMainClassFile() []string {
214+
fileName := fmt.Sprintf("%s/jib-classpath-file", TestAppDir)
215+
cp := readFirstLine(fileName)
216+
217+
args := []string{
218+
GetJavaCommand(),
219+
"-cp",
220+
cp + ":/coherence-operator/utils/lib/coherence-operator.jar:/coherence-operator/utils/config",
221+
}
222+
223+
fileName = fmt.Sprintf("%s/jib-main-class-file", TestAppDir)
224+
mainCls := readFirstLine(fileName)
225+
return append(AppendCommonExpectedArgs(args),
226+
"com.oracle.coherence.k8s.Main",
227+
mainCls)
228+
}
229+
230+
func readFirstLine(fqfn string) string {
231+
file, _ := os.Open(fqfn)
232+
scanner := bufio.NewScanner(file)
233+
scanner.Split(bufio.ScanLines)
234+
var text []string
235+
for scanner.Scan() {
236+
text = append(text, scanner.Text())
237+
}
238+
file.Close()
239+
if len(text) == 0 {
240+
return ""
241+
}
242+
return text[0]
243+
}

0 commit comments

Comments
 (0)