Skip to content

Commit 2df9bdd

Browse files
committed
e2e test skeleton
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 8c61be2 commit 2df9bdd

File tree

9 files changed

+465
-81
lines changed

9 files changed

+465
-81
lines changed

sample-operators/metrics-processing/k8s/operator.yaml

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,84 +17,55 @@
1717
apiVersion: v1
1818
kind: ServiceAccount
1919
metadata:
20-
name: webpage-operator
20+
name: metrics-processing-operator
2121

2222
---
2323
apiVersion: apps/v1
2424
kind: Deployment
2525
metadata:
26-
name: webpage-operator
26+
name: metrics-processing-operator
2727
spec:
2828
selector:
2929
matchLabels:
30-
app: webpage-operator
30+
app: metrics-processing-operator
3131
replicas: 1
3232
template:
3333
metadata:
3434
labels:
35-
app: webpage-operator
35+
app: metrics-processing-operator
3636
spec:
37-
serviceAccountName: webpage-operator
37+
serviceAccountName: metrics-processing-operator
3838
containers:
3939
- name: operator
40-
image: webpage-operator
40+
image: metrics-processing-operator
4141
imagePullPolicy: Never
42-
ports:
43-
- containerPort: 80
44-
startupProbe:
45-
httpGet:
46-
path: /startup
47-
port: 8080
48-
initialDelaySeconds: 1
49-
periodSeconds: 2
50-
timeoutSeconds: 1
51-
failureThreshold: 10
52-
livenessProbe:
53-
httpGet:
54-
path: /healthz
55-
port: 8080
56-
initialDelaySeconds: 5
57-
timeoutSeconds: 1
58-
periodSeconds: 2
59-
failureThreshold: 3
6042

6143
---
6244
apiVersion: rbac.authorization.k8s.io/v1
6345
kind: ClusterRoleBinding
6446
metadata:
65-
name: operator-admin
47+
name: metrics-processing-operator-admin
6648
subjects:
6749
- kind: ServiceAccount
68-
name: webpage-operator
50+
name: metrics-processing-operator
6951
namespace: default
7052
roleRef:
7153
kind: ClusterRole
72-
name: webpage-operator
73-
apiGroup: ""
54+
name: metrics-processing-operator
55+
apiGroup: rbac.authorization.k8s.io
7456

7557
---
7658
apiVersion: rbac.authorization.k8s.io/v1
7759
kind: ClusterRole
7860
metadata:
79-
name: webpage-operator
61+
name: metrics-processing-operator
8062
rules:
8163
- apiGroups:
8264
- ""
8365
resources:
84-
- deployments
85-
- services
86-
- configmaps
8766
- pods
8867
verbs:
8968
- '*'
90-
- apiGroups:
91-
- "apps"
92-
resources:
93-
- deployments
94-
- services
95-
- configmaps
96-
verbs:
97-
- '*'
9869
- apiGroups:
9970
- "apiextensions.k8s.io"
10071
resources:
@@ -104,14 +75,10 @@ rules:
10475
- apiGroups:
10576
- "sample.javaoperatorsdk"
10677
resources:
107-
- webpages
108-
- webpages/status
78+
- metricshandlingcustomresource1s
79+
- metricshandlingcustomresource1s/status
80+
- metricshandlingcustomresource2s
81+
- metricshandlingcustomresource2s/status
10982
verbs:
11083
- '*'
111-
- apiGroups:
112-
- "networking.k8s.io"
113-
resources:
114-
- ingresses
115-
verbs:
116-
- '*'
11784

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright Java Operator SDK Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
apiVersion: sample.javaoperatorsdk/v1
18+
kind: MetricsHandlingCustomResource1
19+
metadata:
20+
name: test-fail-1
21+
spec:
22+
number: 100
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright Java Operator SDK Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
apiVersion: sample.javaoperatorsdk/v1
18+
kind: MetricsHandlingCustomResource1
19+
metadata:
20+
name: test-success-1
21+
spec:
22+
number: 42
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright Java Operator SDK Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
apiVersion: sample.javaoperatorsdk/v1
18+
kind: MetricsHandlingCustomResource2
19+
metadata:
20+
name: test-error-2
21+
spec:
22+
number: 200
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright Java Operator SDK Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
apiVersion: sample.javaoperatorsdk/v1
18+
kind: MetricsHandlingCustomResource2
19+
metadata:
20+
name: test-success-2
21+
spec:
22+
number: 77

sample-operators/metrics-processing/src/main/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingReconciler1.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,55 @@
2424
import io.javaoperatorsdk.operator.api.reconciler.Context;
2525
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
2626
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource1;
27+
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingStatus;
2728

2829
@ControllerConfiguration
2930
public class MetricsHandlingReconciler1 implements Reconciler<MetricsHandlingCustomResource1> {
3031

31-
public static final String INDEX_HTML = "index.html";
32-
3332
private static final Logger log = LoggerFactory.getLogger(MetricsHandlingReconciler1.class);
3433

3534
public MetricsHandlingReconciler1() {}
3635

3736
@Override
3837
public List<EventSource<?, MetricsHandlingCustomResource1>> prepareEventSources(
3938
EventSourceContext<MetricsHandlingCustomResource1> context) {
40-
4139
return List.of();
4240
}
4341

4442
@Override
4543
public UpdateControl<MetricsHandlingCustomResource1> reconcile(
46-
MetricsHandlingCustomResource1 metricsHandlingCustomResource1,
47-
Context<MetricsHandlingCustomResource1> context) {
44+
MetricsHandlingCustomResource1 resource, Context<MetricsHandlingCustomResource1> context) {
45+
46+
String name = resource.getMetadata().getName();
47+
log.info("Reconciling resource: {}", name);
48+
49+
// Simulate some work
50+
try {
51+
Thread.sleep(100);
52+
} catch (InterruptedException e) {
53+
Thread.currentThread().interrupt();
54+
throw new RuntimeException("Interrupted during reconciliation", e);
55+
}
56+
57+
// Throw exception for resources with names containing "fail" or "error"
58+
if (name.toLowerCase().contains("fail") || name.toLowerCase().contains("error")) {
59+
log.error("Simulating failure for resource: {}", name);
60+
throw new IllegalStateException("Simulated reconciliation failure for resource: " + name);
61+
}
62+
63+
// Update status
64+
var status = resource.getStatus();
65+
if (status == null) {
66+
status = new MetricsHandlingStatus();
67+
resource.setStatus(status);
68+
}
69+
70+
var spec = resource.getSpec();
71+
if (spec != null) {
72+
status.setObservedNumber(spec.getObservedNumber());
73+
}
4874

49-
return UpdateControl.noUpdate();
75+
log.info("Successfully reconciled resource: {}", name);
76+
return UpdateControl.patchStatus(resource);
5077
}
5178
}

sample-operators/metrics-processing/src/main/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingReconciler2.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,57 @@
1515
*/
1616
package io.javaoperatorsdk.operator.sample.metrics;
1717

18-
import java.util.List;
19-
2018
import org.slf4j.Logger;
2119
import org.slf4j.LoggerFactory;
2220

2321
import io.javaoperatorsdk.operator.api.reconciler.Context;
2422
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
25-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
2623
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
2724
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
28-
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
29-
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource1;
25+
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingCustomResource2;
26+
import io.javaoperatorsdk.operator.sample.metrics.customresource.MetricsHandlingStatus;
3027

3128
@ControllerConfiguration
32-
public class MetricsHandlingReconciler2 implements Reconciler<MetricsHandlingCustomResource1> {
33-
34-
public static final String INDEX_HTML = "index.html";
29+
public class MetricsHandlingReconciler2 implements Reconciler<MetricsHandlingCustomResource2> {
3530

3631
private static final Logger log = LoggerFactory.getLogger(MetricsHandlingReconciler2.class);
3732

3833
public MetricsHandlingReconciler2() {}
3934

4035
@Override
41-
public List<EventSource<?, MetricsHandlingCustomResource1>> prepareEventSources(
42-
EventSourceContext<MetricsHandlingCustomResource1> context) {
36+
public UpdateControl<MetricsHandlingCustomResource2> reconcile(
37+
MetricsHandlingCustomResource2 resource, Context<MetricsHandlingCustomResource2> context) {
4338

44-
return List.of();
45-
}
39+
String name = resource.getMetadata().getName();
40+
log.info("Reconciling resource: {}", name);
4641

47-
@Override
48-
public UpdateControl<MetricsHandlingCustomResource1> reconcile(
49-
MetricsHandlingCustomResource1 metricsHandlingCustomResource1,
50-
Context<MetricsHandlingCustomResource1> context) {
42+
// Simulate some work (slightly different timing than reconciler1)
43+
try {
44+
Thread.sleep(150);
45+
} catch (InterruptedException e) {
46+
Thread.currentThread().interrupt();
47+
throw new RuntimeException("Interrupted during reconciliation", e);
48+
}
49+
50+
// Throw exception for resources with names containing "fail" or "error"
51+
if (name.toLowerCase().contains("fail") || name.toLowerCase().contains("error")) {
52+
log.error("Simulating failure for resource: {}", name);
53+
throw new IllegalStateException("Simulated reconciliation failure for resource: " + name);
54+
}
55+
56+
// Update status
57+
var status = resource.getStatus();
58+
if (status == null) {
59+
status = new MetricsHandlingStatus();
60+
resource.setStatus(status);
61+
}
62+
63+
var spec = resource.getSpec();
64+
if (spec != null) {
65+
status.setObservedNumber(spec.getObservedNumber());
66+
}
5167

52-
return UpdateControl.noUpdate();
68+
log.info("Successfully reconciled resource: {}", name);
69+
return UpdateControl.patchStatus(resource);
5370
}
5471
}

sample-operators/metrics-processing/src/main/java/io/javaoperatorsdk/operator/sample/metrics/MetricsHandlingSampleOperator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ public class MetricsHandlingSampleOperator {
5454
* can be implemented using the low level and higher level APIs.
5555
*/
5656
public static void main(String[] args) throws IOException {
57-
log.info("WebServer Operator starting!");
57+
log.info("Metrics Handling Sample Operator starting!");
5858

59-
// TODO add test for checking if there are metrics in prometheus
6059
// Load configuration from config.yaml
6160
Metrics metrics = initOTLPMetrics();
6261
Operator operator =
6362
new Operator(o -> o.withStopOnInformerErrorDuringStartup(false).withMetrics(metrics));
6463

64+
operator.register(new MetricsHandlingReconciler1());
65+
operator.register(new MetricsHandlingReconciler2());
66+
6567
operator.start();
6668
}
6769

0 commit comments

Comments
 (0)