Skip to content

Commit

Permalink
Refactor build ... (#103)
Browse files Browse the repository at this point in the history
- split dev/test kustomize for faster builds
- scrape prometheus only once
- removed compose
  • Loading branch information
sschnabe committed May 2, 2023
1 parent 84f9365 commit a5d8705
Show file tree
Hide file tree
Showing 54 changed files with 79 additions and 47 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ jobs:
distribution: temurin
java-version: 17
cache: maven
- run: mvn -B -ntp dependency:go-offline
- run: mvn -B -ntp checkstyle:check

verify:
Expand All @@ -70,5 +69,7 @@ jobs:
distribution: temurin
java-version: 17
cache: maven
- run: mvn -B -ntp dependency:go-offline
- run: mvn -B -ntp verify -Dimage.tag.test=${{ matrix.image }} -Dcheckstyle.skip
- run: mvn -B -ntp verify -Dimage.tag.test=${{ matrix.image }} -Dcheckstyle.skip -Dk3s.skipRm
- name: Run kubectl logs -l app.kubernetes.io/name=keycloak
run: export KUBECONFIG=/home/runner/.kube/k3s-maven-plugin/mount/kubeconfig.yaml && kubectl logs -l app.kubernetes.io/name=keycloak --tail -1
if: ${{ failure() }}
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ Tags:

## Develop and testing

### k3s

Start k3s: `mvn pre-integration-test`

Open <http://help.127.0.0.1.nip.io:8080> or use `kubectl`:
Expand All @@ -69,9 +67,3 @@ Open <http://help.127.0.0.1.nip.io:8080> or use `kubectl`:
export KUBECONFIG=~/.kube/k3s-maven-plugin/mount/kubeconfig.yaml
kubectl get all --all-namespaces
```

### docker-compose

Start compose: `docker-compose -f src/test/compose/docker-compose.yaml up`

Open <http://keycloak.127.0.0.1.nip.io:8080>.
27 changes: 23 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,14 @@
</execution>
</executions>
<configuration>
<command>kubectl apply -k .</command>
<command>kubectl apply -k test</command>
<manifests>${project.build.directory}/k3s</manifests>
<portBindings>
<portBinding>8080:8080</portBinding>
</portBindings>
<ctrImages>
<ctrImage>${image.dashboard}</ctrImage>
<ctrImage>${image.grafana}</ctrImage>
<ctrImage>${image.mailhog}</ctrImage>
<ctrImage>${image.postgresql}</ctrImage>
<ctrImage>${image.prometheus}</ctrImage>
<ctrImage>${image.traefik}</ctrImage>
</ctrImages>
<dockerImages>
Expand Down Expand Up @@ -577,4 +574,26 @@

</plugins>
</build>

<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>!env.CI</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>io.kokuwa.maven</groupId>
<artifactId>k3s-maven-plugin</artifactId>
<configuration>
<command>kubectl apply -k dev</command>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
24 changes: 0 additions & 24 deletions src/test/compose/docker-compose.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions src/test/java/io/kokuwa/keycloak/K3sIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void metrics(Keycloak keycloak, Kubernetes kubernetes, OpenIDConnect oidc, Prome
// expect only one keycloak to get metrics from

kubernetes.scaleKeycloak(1);
prometheus.scrap();

// get state before test

Expand All @@ -72,6 +73,7 @@ void metrics(Keycloak keycloak, Kubernetes kubernetes, OpenIDConnect oidc, Prome

// check metrics count

prometheus.scrap();
assertAll("metrics",
() -> assertEquals(loginSuccessGrayc + 3, prometheus.logins(kokuwa), "kokuwa loginSuccess"),
() -> assertEquals(loginFailedGrayc + 2, prometheus.loginErrors(kokuwa), "kokuwa loginFailed"),
Expand Down
14 changes: 9 additions & 5 deletions src/test/java/io/kokuwa/keycloak/keycloak/Prometheus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.kokuwa.keycloak.keycloak;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -15,14 +16,15 @@
*/
public class Prometheus {

private final Set<PrometheusMetric> state = new HashSet<>();
private final PrometheusClient client;

public Prometheus(PrometheusClient client) {
this.client = client;
}

public int logins(RealmRepresentation realm) {
return scrap().stream()
return state.stream()
.filter(metric -> Objects.equals(metric.name(), "keycloak_event_user_total"))
.filter(metric -> Objects.equals(metric.tags().get("realm"), realm.getRealm()))
.filter(metric -> Objects.equals(metric.tags().get("type"), "LOGIN"))
Expand All @@ -31,17 +33,19 @@ public int logins(RealmRepresentation realm) {
}

public int loginErrors(RealmRepresentation realm) {
return scrap().stream()
return state.stream()
.filter(metric -> Objects.equals(metric.name(), "keycloak_event_user_total"))
.filter(metric -> Objects.equals(metric.tags().get("realm"), realm.getRealm()))
.filter(metric -> Objects.equals(metric.tags().get("type"), "LOGIN_ERROR"))
.mapToInt(metric -> metric.value().intValue())
.sum();
}

private Set<PrometheusMetric> scrap() {
return Stream.of(client.scrap().split("[\\r\\n]+"))
public void scrap() {
state.clear();
Stream.of(client.scrap().split("[\\r\\n]+"))
.filter(line -> !line.startsWith("#"))
.filter(line -> line.startsWith("keycloak"))
.map(line -> {
var name = line.substring(0, line.contains("{") ? line.indexOf("{") : line.lastIndexOf(" "));
var tags = line.contains("{")
Expand All @@ -53,6 +57,6 @@ private Set<PrometheusMetric> scrap() {
var value = Double.parseDouble(line.substring(line.lastIndexOf(" ")));
return new PrometheusMetric(name, tags, value);
})
.collect(Collectors.toSet());
.forEach(state::add);
}
}
34 changes: 34 additions & 0 deletions src/test/k3s/dev/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resources:
- ../test

patches:

# enable user deployments
- target:
kind: Deployment
name: help
patch: |-
- op: replace
path: /spec/replicas
value: 1
- target:
kind: Deployment
name: kubernetes-dashboard
patch: |-
- op: replace
path: /spec/replicas
value: 1
- target:
kind: Deployment
name: grafana
patch: |-
- op: replace
path: /spec/replicas
value: 1
- target:
kind: Deployment
name: prometheus
patch: |-
- op: replace
path: /spec/replicas
value: 1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: Deployment
metadata:
name: kubernetes-dashboard
spec:
replicas: 0
selector:
matchLabels:
app.kubernetes.io/name: kubernetes-dashboard
Expand All @@ -14,7 +15,7 @@ spec:
containers:
- name: dashboard
image: ${image.dashboard}
imagePullPolicy: Never
imagePullPolicy: IfNotPresent
args:
- --namespace=default
- --disable-settings-authorizer=true
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: Deployment
metadata:
name: grafana
spec:
replicas: 0
selector:
matchLabels:
app.kubernetes.io/name: grafana
Expand All @@ -18,7 +19,7 @@ spec:
initContainers:
- name: postgresql
image: ${image.postgresql}
imagePullPolicy: Never
imagePullPolicy: IfNotPresent
command: [sh, -c, until psql; do sleep 2; done]
env:
- name: PGHOST
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: Deployment
metadata:
name: help
spec:
replicas: 0
selector:
matchLabels:
app.kubernetes.io/name: help
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: Deployment
metadata:
name: prometheus
spec:
replicas: 0
strategy:
type: Recreate
selector:
Expand All @@ -16,7 +17,7 @@ spec:
containers:
- name: prometheus
image: ${image.prometheus}
imagePullPolicy: Never
imagePullPolicy: IfNotPresent
args:
- --storage.tsdb.path=/data
- --config.file=/etc/config/prometheus.yaml
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a5d8705

Please sign in to comment.