Skip to content

Commit

Permalink
Merge pull request #750 from xlight05/fix-invalid-probe
Browse files Browse the repository at this point in the history
Fix missing port in probes
  • Loading branch information
xlight05 authored Sep 22, 2023
2 parents 3c85d2b + b8ab4dd commit e908fc1
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "cloud"
version = "2.11.0"
version = "2.11.1"
repository = "https://github.com/ballerina-platform/module-ballerina-c2c"
license = ["Apache-2.0"]
keywords = ["cloud", "kubernetes", "docker", "k8s", "c2c"]
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "code2cloud"
class = "io.ballerina.c2c.C2CCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-2.11.0.jar"
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-2.11.1-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.8.0"
[[package]]
org = "ballerina"
name = "cloud"
version = "2.11.0"
version = "2.11.1"
modules = [
{org = "ballerina", packageName = "cloud", moduleName = "cloud"}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public void testMissingPort() {
BuildProject project = BuildProject.load(projectPath);
Collection<Diagnostic> diagnostics =
getC2CDiagnostics(project.currentPackage().getCompilation().diagnosticResult().diagnostics());
Assert.assertEquals(diagnostics.size(), 2);
Assert.assertEquals(diagnostics.size(), 3);
Iterator<Diagnostic> iterator = diagnostics.iterator();
Assert.assertEquals(iterator.next().message(), "Missing Readiness Probe Port");
Assert.assertEquals(iterator.next().message(), "Invalid Liveness Probe Port");
Assert.assertEquals(iterator.next().message(), "Invalid Liveness Probe Path");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ max_cpu="0.75m"
[cloud.deployment.probes.liveness]
port = 9091
path = "/helloWorld/readyz"

[cloud.deployment.probes.readiness]
path = "/helloWorld/readyz"
2 changes: 1 addition & 1 deletion compiler-plugin-tests/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<class name="io.ballerina.c2c.test.samples.Sample10Test"/>
<class name="io.ballerina.c2c.test.samples.Sample11Test"/>
<class name="io.ballerina.c2c.test.samples.Sample12Test"/>
<!-- <class name="io.ballerina.c2c.test.samples.NativeTest"/>-->
<class name="io.ballerina.c2c.test.samples.NativeTest"/>
<class name="io.ballerina.c2c.test.samples.NativeArgsTest"/>
<class name="io.ballerina.c2c.test.samples.NativeBaseTest"/>
<class name="io.ballerina.c2c.test.samples.NativeBuilderConfigTest"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public class TomlDiagnosticChecker {
private final Project project;

public TomlDiagnosticChecker(Project project) {

this.project = project;
}

public List<Diagnostic> validateTomlWithSource(Toml toml) {

List<Diagnostic> diagnosticInfoList = new ArrayList<>();
if (toml == null) {
return Collections.emptyList();
Expand All @@ -65,9 +67,17 @@ public List<Diagnostic> validateTomlWithSource(Toml toml) {
}

private List<Diagnostic> validateProbe(ProjectServiceInfo projectServiceInfo, Toml probe, ProbeType type) {

List<Diagnostic> diagnosticInfos = new ArrayList<>();
if (probe.get("port").isEmpty() || probe.get("path").isEmpty()) {
return Collections.emptyList();
TomlNodeLocation tableLocation = probe.rootNode().location();
if (probe.get("port").isEmpty()) {
Diagnostic portDiag = getTomlDiagnostic(tableLocation, "C2C005", "missing.probe.port",
DiagnosticSeverity.ERROR, "Missing " + type.getValue() + " Port");
return Collections.singletonList(portDiag);
} else if (probe.get("path").isEmpty()) {
Diagnostic portDiag = getTomlDiagnostic(tableLocation, "C2C006", "missing.probe.path",
DiagnosticSeverity.ERROR, "Missing " + type.getValue() + " Path");
return Collections.singletonList(portDiag);
}
TomlValueNode portNode = probe.get("port").get();
TomlValueNode pathNode = probe.get("path").get();
Expand All @@ -85,7 +95,6 @@ private List<Diagnostic> validateProbe(ProjectServiceInfo projectServiceInfo, To
return diagnosticInfos;
}


for (ServiceInfo serviceInfo : serviceList) {
List<ListenerInfo> listeners = serviceInfo.getListeners();
for (ListenerInfo listener : listeners) {
Expand Down Expand Up @@ -119,13 +128,14 @@ private List<Diagnostic> validateProbe(ProjectServiceInfo projectServiceInfo, To
"Invalid " + type.getValue() + " Resource Path");
diagnosticInfos.add(diag);
}
}
}
}
}
return diagnosticInfos;
}

private static boolean isValidServicePath(String servicePath, String tomlPath) {

if (servicePath.equals("/")) {
return true;
}
Expand All @@ -139,6 +149,7 @@ private static boolean isValidServicePath(String servicePath, String tomlPath) {
}

private boolean isListenerPortValid(long port, List<ServiceInfo> serviceInfo) {

boolean isValid = false;
for (ServiceInfo service : serviceInfo) {
List<ListenerInfo> listeners = service.getListeners();
Expand All @@ -154,6 +165,7 @@ private boolean isListenerPortValid(long port, List<ServiceInfo> serviceInfo) {
}

private String trimResourcePath(String resourcePath) {

resourcePath = resourcePath.trim();
if (resourcePath.startsWith("/")) {
resourcePath = resourcePath.substring(1);
Expand All @@ -166,6 +178,7 @@ private String trimResourcePath(String resourcePath) {

private TomlDiagnostic getTomlDiagnostic(TomlNodeLocation location, String code, String template,
DiagnosticSeverity severity, String message) {

io.ballerina.tools.diagnostics.DiagnosticInfo
diagnosticInfo = new io.ballerina.tools.diagnostics.DiagnosticInfo(code, template, severity);
return new TomlDiagnostic(location, diagnosticInfo, message);
Expand All @@ -178,10 +191,12 @@ enum ProbeType {
private String value;

ProbeType(String value) {

this.value = value;
}

public String getValue() {

return value;
}
}
Expand Down

0 comments on commit e908fc1

Please sign in to comment.