Skip to content

Commit

Permalink
Clean up CRD metrics types and add validation rule, tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Nov 2, 2024
1 parent 884fd47 commit 88130a2
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.streamshub.console.api.v1alpha1.spec.metrics.MetricsSource;

import io.fabric8.generator.annotation.Required;
import io.fabric8.kubernetes.api.model.EnvVar;
Expand All @@ -27,7 +28,7 @@ public class ConsoleSpec {

Images images;

List<Prometheus> metricsSources;
List<MetricsSource> metricsSources;

List<SchemaRegistry> schemaRegistries;

Expand All @@ -51,11 +52,11 @@ public void setImages(Images images) {
this.images = images;
}

public List<Prometheus> getMetricsSources() {
public List<MetricsSource> getMetricsSources() {
return metricsSources;
}

public void setMetricsSources(List<Prometheus> metricsSources) {
public void setMetricsSources(List<MetricsSource> metricsSources) {
this.metricsSources = metricsSources;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.github.streamshub.console.api.v1alpha1.spec;
package com.github.streamshub.console.api.v1alpha1.spec.metrics;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonValue;
import com.github.streamshub.console.config.Named;

Expand All @@ -13,14 +10,14 @@

@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Prometheus implements Named {
public class MetricsSource implements Named {

@Required
private String name;
@Required
private Type type;
private String url;
private Authentication authentication;
private MetricsSourceAuthentication authentication;

@Override
public String getName() {
Expand All @@ -47,11 +44,11 @@ public void setUrl(String url) {
this.url = url;
}

public Authentication getAuthentication() {
public MetricsSourceAuthentication getAuthentication() {
return authentication;
}

public void setAuthentication(Authentication authentication) {
public void setAuthentication(MetricsSourceAuthentication authentication) {
this.authentication = authentication;
}

Expand Down Expand Up @@ -86,45 +83,4 @@ public static Type fromValue(String value) {
throw new IllegalArgumentException("Invalid Prometheus type: " + value);
}
}

@JsonTypeInfo(use = Id.DEDUCTION)
@JsonSubTypes({ @JsonSubTypes.Type(Basic.class), @JsonSubTypes.Type(Bearer.class) })
abstract static class Authentication {
}

public static class Basic extends Authentication {
@Required
private String username;
@Required
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

public static class Bearer extends Authentication {
@Required
private String token;

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.streamshub.console.api.v1alpha1.spec.metrics;

import com.fasterxml.jackson.annotation.JsonInclude;

import io.fabric8.generator.annotation.ValidationRule;
import io.sundr.builder.annotations.Buildable;

@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder")
@JsonInclude(JsonInclude.Include.NON_NULL)
@ValidationRule(
value = "has(self.token) || (has(self.username) && has(self.password))",
message = "One of `token` or `username` + `password` must be provided")
public class MetricsSourceAuthentication {

private String username;
private String password;
private String token;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import jakarta.inject.Inject;

import com.github.streamshub.console.api.v1alpha1.Console;
import com.github.streamshub.console.api.v1alpha1.spec.Prometheus.Type;
import com.github.streamshub.console.api.v1alpha1.spec.metrics.MetricsSource.Type;

import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
import io.javaoperatorsdk.operator.api.reconciler.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import com.github.streamshub.console.api.v1alpha1.spec.ConfigVars;
import com.github.streamshub.console.api.v1alpha1.spec.Credentials;
import com.github.streamshub.console.api.v1alpha1.spec.KafkaCluster;
import com.github.streamshub.console.api.v1alpha1.spec.Prometheus;
import com.github.streamshub.console.api.v1alpha1.spec.Prometheus.Type;
import com.github.streamshub.console.api.v1alpha1.spec.SchemaRegistry;
import com.github.streamshub.console.api.v1alpha1.spec.metrics.MetricsSource;
import com.github.streamshub.console.api.v1alpha1.spec.metrics.MetricsSource.Type;
import com.github.streamshub.console.config.ConsoleConfig;
import com.github.streamshub.console.config.KafkaClusterConfig;
import com.github.streamshub.console.config.PrometheusConfig;
Expand Down Expand Up @@ -149,35 +149,37 @@ private void addMetricsSources(Console primary, ConsoleConfig config, Context<Co
return;
}

for (Prometheus prometheus : metricsSources) {
for (MetricsSource metricsSource : metricsSources) {
var prometheusConfig = new PrometheusConfig();
prometheusConfig.setName(prometheus.getName());
prometheusConfig.setName(metricsSource.getName());

if (prometheus.getType() == Type.OPENSHIFT_MONITORING) {
if (metricsSource.getType() == Type.OPENSHIFT_MONITORING) {
prometheusConfig.setType(PrometheusConfig.Type.OPENSHIFT_MONITORING);
prometheusConfig.setUrl(getOpenShiftMonitoringUrl(context));
} else {
// embedded Prometheus used like standalone by console
prometheusConfig.setType(PrometheusConfig.Type.STANDALONE);

if (prometheus.getType() == Type.EMBEDDED) {
if (metricsSource.getType() == Type.EMBEDDED) {
prometheusConfig.setUrl(prometheusService.getUrl(primary, context));
} else {
prometheusConfig.setUrl(prometheus.getUrl());
prometheusConfig.setUrl(metricsSource.getUrl());
}
}

var prometheusAuthn = prometheus.getAuthentication();

if (prometheusAuthn instanceof Prometheus.Basic basic) {
var basicConfig = new PrometheusConfig.Basic();
basicConfig.setUsername(basic.getUsername());
basicConfig.setPassword(basic.getPassword());
prometheusConfig.setAuthentication(basicConfig);
} else if (prometheusAuthn instanceof Prometheus.Bearer bearer) {
var bearerConfig = new PrometheusConfig.Bearer();
bearerConfig.setToken(bearer.getToken());
prometheusConfig.setAuthentication(bearerConfig);
var metricsAuthn = metricsSource.getAuthentication();

if (metricsAuthn != null) {
if (metricsAuthn.getToken() == null) {
var basicConfig = new PrometheusConfig.Basic();
basicConfig.setUsername(metricsAuthn.getUsername());
basicConfig.setPassword(metricsAuthn.getPassword());
prometheusConfig.setAuthentication(basicConfig);
} else {
var bearerConfig = new PrometheusConfig.Bearer();
bearerConfig.setToken(metricsAuthn.getToken());
prometheusConfig.setAuthentication(bearerConfig);
}
}

config.getMetricsSources().add(prometheusConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Optional;

import com.github.streamshub.console.api.v1alpha1.Console;
import com.github.streamshub.console.api.v1alpha1.spec.Prometheus.Type;
import com.github.streamshub.console.api.v1alpha1.spec.metrics.MetricsSource.Type;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
Expand Down
Loading

0 comments on commit 88130a2

Please sign in to comment.