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

feat: merged ms operator #80

Merged
merged 3 commits into from
Nov 26, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_slot_v1_yaml
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_slot_v1_yaml.base-package=gen.org.tkit.onecx.product.store.slot.v1
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_slot_v1_yaml.return-response=true
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_slot_v1_yaml.enable-security-generation=false
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_ms_v1_yaml.config-key=product_store_client
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_ms_v1_yaml.base-package=gen.org.tkit.onecx.product.store.ms.v1
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_ms_v1_yaml.return-response=true
quarkus.openapi-generator.codegen.spec.onecx_product_store_operator_ms_v1_yaml.enable-security-generation=false
----
====

Expand Down Expand Up @@ -65,22 +69,7 @@ app:
enabled: true
spec:
kcConfig:
defaultClientScopes: [ ocx-ps-product:write ]

----


fieldRef:
fieldPath: metadata.namespace
serviceAccount:
enabled: true
operator:
keycloak:
client:
enabled: true
spec:
kcConfig:
defaultClientScopes: [ ocx-ps-product:write ]
defaultClientScopes: [ ocx-ps-product:write, ocx-ps-slot:write, ocx-ps-ms:write ]

----

13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@
<skipCache>true</skipCache>
</configuration>
</execution>
<execution>
<id>ms-operator-api</id>
<phase>generate-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://raw.githubusercontent.com/onecx/onecx-product-store-svc/main/src/main/openapi/onecx-product-store-operator-ms-v1.yaml</url>
<outputDirectory>target/tmp/openapi</outputDirectory>
<outputFileName>onecx-product-store-operator-ms-v1.yaml</outputFileName>
<skipCache>true</skipCache>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
3 changes: 3 additions & 0 deletions src/main/helm/templates/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ rules:
- slots
- slots/status
- slots/finalizers
- microservices
- microservices/status
- microservices/finalizers
verbs:
- get
- list
Expand Down
2 changes: 1 addition & 1 deletion src/main/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ app:
enabled: true
spec:
kcConfig:
defaultClientScopes: [ ocx-ps-product:write, ocx-ps-slot:write ]
defaultClientScopes: [ ocx-ps-product:write, ocx-ps-slot:write, ocx-ps-ms:write ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.tkit.onecx.product.store.operator;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.javaoperatorsdk.operator.api.ObservedGenerationAwareStatus;

public abstract class CustomResourceStatus extends ObservedGenerationAwareStatus {

@JsonProperty("responseCode")
private int responseCode;

@JsonProperty("status")
private Status status;

@JsonProperty("message")
private String message;

public enum Status {

ERROR,

CREATED,

UPDATED,

UNDEFINED;
}

public int getResponseCode() {
return responseCode;
}

public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.onecx.product.store.operator.client.mappers.ProductStoreMapper;
import org.tkit.onecx.product.store.operator.microservice.Microservice;
import org.tkit.onecx.product.store.operator.microservice.MicroserviceSpec;
import org.tkit.onecx.product.store.operator.product.Product;
import org.tkit.onecx.product.store.operator.product.ProductSpec;
import org.tkit.onecx.product.store.operator.slot.Slot;
import org.tkit.onecx.product.store.operator.slot.SlotSpec;

import gen.org.tkit.onecx.product.store.ms.v1.api.OperatorMsApi;
import gen.org.tkit.onecx.product.store.ms.v1.model.UpdateMsRequest;
import gen.org.tkit.onecx.product.store.product.v1.api.OperatorProductApi;
import gen.org.tkit.onecx.product.store.product.v1.model.UpdateProductRequest;
import gen.org.tkit.onecx.product.store.slot.v1.api.OperatorSlotApi;
Expand All @@ -33,6 +37,10 @@ public class ProductStoreService {
@RestClient
OperatorSlotApi slotClient;

@Inject
@RestClient
OperatorMsApi msClient;

public int updateProduct(Product product) {
ProductSpec spec = product.getSpec();
UpdateProductRequest dto = mapper.map(spec);
Expand All @@ -46,7 +54,16 @@ public int updateSlot(Slot slot) {
SlotSpec spec = slot.getSpec();
UpdateSlotRequest dto = mapper.map(spec);
try (var response = slotClient.createOrUpdateSlot(spec.getProductName(), spec.getAppId(), dto)) {
log.info("Update micro-fronted response {}", response.getStatus());
log.info("Update slot response {}", response.getStatus());
return response.getStatus();
}
}

public int updateMicroservice(Microservice microservice) {
MicroserviceSpec spec = microservice.getSpec();
UpdateMsRequest dto = mapper.map(spec);
try (var response = msClient.createOrUpdateMs(spec.getProductName(), spec.getAppId(), dto)) {
log.info("Update micro-service response {}", response.getStatus());
return response.getStatus();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.tkit.onecx.product.store.operator.microservice.MicroserviceSpec;
import org.tkit.onecx.product.store.operator.product.ProductSpec;
import org.tkit.onecx.product.store.operator.slot.SlotSpec;

import gen.org.tkit.onecx.product.store.ms.v1.model.UpdateMsRequest;
import gen.org.tkit.onecx.product.store.product.v1.model.UpdateProductRequest;
import gen.org.tkit.onecx.product.store.slot.v1.model.UpdateSlotRequest;

Expand All @@ -16,4 +18,7 @@ public interface ProductStoreMapper {

@Mapping(target = "undeployed", constant = "false")
UpdateSlotRequest map(SlotSpec spec);

@Mapping(target = "undeployed", constant = "false")
UpdateMsRequest map(MicroserviceSpec spec);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.tkit.onecx.product.store.operator.microservice;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Version("v1")
@Group("onecx.tkit.org")
public class Microservice extends CustomResource<MicroserviceSpec, MicroserviceStatus> implements Namespaced {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.tkit.onecx.product.store.operator.microservice;

import io.quarkus.runtime.annotations.ConfigDocFilename;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.quarkus.runtime.annotations.StaticInitSafe;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;

@StaticInitSafe
@ConfigDocFilename("onecx-product-store-ms-operator.adoc")
@ConfigMapping(prefix = "onecx.product-store.operator.ms")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public interface MicroserviceConfig {

/**
* Leader election configuration
*/
@WithName("leader-election")
LeaderElectionConfig leaderElectionConfig();

/**
* Leader election config
*/
interface LeaderElectionConfig {

/**
* Lease name
*/
@WithName("lease-name")
@WithDefault("onecx-product-store-ms-operator-lease")
String leaseName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.tkit.onecx.product.store.operator.microservice;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE;

import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.onecx.product.store.operator.CustomResourceStatus;
import org.tkit.onecx.product.store.operator.client.ProductStoreService;

import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;

@ControllerConfiguration(name = "microservice", namespaces = WATCH_CURRENT_NAMESPACE, onAddFilter = MicroserviceController.MicrofrontendAddFilter.class, onUpdateFilter = MicroserviceController.MicrofrontendUpdateFilter.class)
public class MicroserviceController implements Reconciler<Microservice>, ErrorStatusHandler<Microservice> {

private static final Logger log = LoggerFactory.getLogger(MicroserviceController.class);

@Inject
ProductStoreService service;

@Override
public UpdateControl<Microservice> reconcile(Microservice microservice, Context<Microservice> context)
throws Exception {

String appId = microservice.getSpec().getAppId();
String productName = microservice.getSpec().getProductName();

log.info("Reconcile microservice: {} for product: {}", appId, productName);
int responseCode = service.updateMicroservice(microservice);

updateStatusPojo(microservice, responseCode);
log.info("Microservice '{}' reconciled - updating status", microservice.getMetadata().getName());
return UpdateControl.updateStatus(microservice);

}

@Override
public ErrorStatusUpdateControl<Microservice> updateErrorStatus(Microservice microfrontend,
Context<Microservice> context, Exception e) {

int responseCode = -1;
if (e.getCause() instanceof WebApplicationException re) {
responseCode = re.getResponse().getStatus();
}

log.error("Error reconcile resource", e);
MicroserviceStatus status = new MicroserviceStatus();
status.setRequestProductName(null);
status.setRequestAppId(null);
status.setRequestAppName(null);
status.setRequestAppVersion(null);
status.setResponseCode(responseCode);
status.setStatus(CustomResourceStatus.Status.ERROR);
status.setMessage(e.getMessage());
microfrontend.setStatus(status);
return ErrorStatusUpdateControl.updateStatus(microfrontend);
}

private void updateStatusPojo(Microservice microfrontend, int responseCode) {
MicroserviceStatus result = new MicroserviceStatus();
MicroserviceSpec spec = microfrontend.getSpec();
result.setRequestProductName(spec.getProductName());
result.setRequestAppId(spec.getAppId());
result.setRequestAppName(spec.getName());
result.setRequestAppVersion(spec.getVersion());
result.setResponseCode(responseCode);
var status = switch (responseCode) {
case 201:
yield CustomResourceStatus.Status.CREATED;
case 200:
yield CustomResourceStatus.Status.UPDATED;
default:
yield CustomResourceStatus.Status.UNDEFINED;
};
result.setStatus(status);
microfrontend.setStatus(result);
}

public static class MicrofrontendAddFilter implements OnAddFilter<Microservice> {

@Override
public boolean accept(Microservice resource) {
return resource.getSpec() != null;
}
}

public static class MicrofrontendUpdateFilter implements OnUpdateFilter<Microservice> {

@Override
public boolean accept(Microservice newResource, Microservice oldResource) {
return newResource.getSpec() != null;
}
}

}
Loading
Loading