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

Added quickstart demonstrating dapr.io integration with jkube (#2035) #2076

Merged
merged 3 commits into from
Mar 27, 2023
Merged
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
62 changes: 62 additions & 0 deletions quickstarts/gradle/dapr-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
name: "Gradle :: dapr-hello-world"

description:

Dapr hello world application deployed in Kubernetes using Eclipse JKube.

The application contains a single controller HelloWorldController.java that prints 'Hello World'.

---

# Eclipse JKube Dapr Gradle Quickstart

This is a demo gradle application based on Dapr framework which can be deployed to
Kubernetes with the help of Kubernetes JKube Gradle Plugin.

## How to Build?
In order to build,you will need to install Dapr in kubernetes

- Install Dapr in kubernetes
```shell script
$ dapr init -k
```

- Verify installation
```shell script
$ dapr status -k
```

- If required, update installation
```shell script
$ dapr upgrade -k --runtime-version=x.x.x
```

## Deploying to Kubernetes
Kubernetes Gradle Plugin is already added to the project plugins section.
You can deploy it using the usual Kubernetes Gradle Plugin tasks, just make sure you are logged into
a Kubernetes cluster:

For the Dapr application to work in a Kubernetes cluster it needs following annotation in the `deployment.yaml` file under spec, template metadata section

```yaml
annotations:
dapr.io/enabled: "true"
dapr.io/app-id: "dapr-hello-world"
dapr.io/app-port: "8085"
dapr.io/config: "appconfig"
```

In order to deploy the application, you need to run the Kubernetes Gradle Plugin build, resource, and apply goals:

```shell script
# Kubernetes
$ gradle clean build k8sBuild k8sResource k8sApply
# OpenShift
$ gradle clean build ocBuild ocResource ocApply
```

If you are working with slow internet connection then readiness and liveness probe may timeout uncomment timeout and delay related annotation from above

If you need time observability in your application you can use zipkin and Dapr appconfig, installation instruction for zipkin with appconfig is given in zipkininstallation.sh

23 changes: 23 additions & 0 deletions quickstarts/gradle/dapr-hello-world/appconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: appconfig
spec:
tracing:
samplingRate: "1"
zipkin:
endpointAddress: "http://zipkin.default.svc.cluster.local:9411/api/v2/spans"
82 changes: 82 additions & 0 deletions quickstarts/gradle/dapr-hello-world/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'io.fabric8:kubernetes-client'
}
}

plugins {
id 'java'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'org.eclipse.jkube.kubernetes' version '1.11.0'
id 'org.eclipse.jkube.openshift' version '1.11.0'
}

group = 'org.eclipse.jkube.quickstart.gradle.dapr'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.dapr:dapr-sdk-springboot:1.7.1'
}

tasks.named('test') {
useJUnitPlatform()
}

tasks.register('zipkinInstall') {
doLast {
new io.fabric8.kubernetes.client.KubernetesClientBuilder().build().withCloseable { kc ->
kc.load(new FileInputStream(new File('./zipkin.yaml'))).withGracePeriod(0).delete()
kc.load(new FileInputStream(new File('./appconfig.yaml'))).withGracePeriod(0).delete()
kc.load(new FileInputStream(new File('./zipkin.yaml'))).create()
kc.load(new FileInputStream(new File('./appconfig.yaml'))).create()
}
}
}

tasks.register('zipkinUninstall') {
doLast {
new io.fabric8.kubernetes.client.KubernetesClientBuilder().build().withCloseable { kc ->
kc.load(new FileInputStream(new File('./zipkin.yaml'))).withGracePeriod(0).delete()
kc.load(new FileInputStream(new File('./appconfig.yaml'))).withGracePeriod(0).delete()
}
}
}

kubernetes {
resources {
annotations {
pod = [
'dapr.io/enabled' : true,
'dapr.io/app-id' : "dapr-hello-world",
'dapr.io/app-port': "8085",
// if you want to trace application then run gradle zipkinInstall
'dapr.io/config' : "appconfig"
]
}
}
}

openshift {
resources {
annotations {
pod = [
'dapr.io/enabled' : true,
'dapr.io/app-id' : "dapr-hello-world",
'dapr.io/app-port': "8085",
// if you want to trace application then run gradle zipkinInstall
'dapr.io/config' : "appconfig"
]
}
}
}
19 changes: 19 additions & 0 deletions quickstarts/gradle/dapr-hello-world/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

jkube.generator.name = jkube-quickstarts/%a:%l
jkube.image.name = dapr-hello-world
jkube.generator.build.imagePullPolicy = Always
jkube.enricher.jkube-controller.replicaCount = 1
jkube.enricher.jkube-service.type = LoadBalancer
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
91 changes: 91 additions & 0 deletions quickstarts/gradle/dapr-hello-world/gradlew.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
1 change: 1 addition & 0 deletions quickstarts/gradle/dapr-hello-world/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'dapr-hello-world'
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DaprHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(DaprHelloWorldApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
@GetMapping("/wish")
public String wish() {
return "Hello World";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

spring.application.name=dapr-hello-world
server.port=8085
Loading