-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-5 Switch to josdk managed dependent resource
- Loading branch information
1 parent
5efa965
commit 7bd8107
Showing
7 changed files
with
305 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/rcube/configmap/operator/config/SpringAwareBaseConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.rcube.configmap.operator.config; | ||
|
||
import io.javaoperatorsdk.operator.api.config.Version; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory; | ||
import io.javaoperatorsdk.operator.springboot.starter.OverridableBaseConfigService; | ||
|
||
public class SpringAwareBaseConfigService extends OverridableBaseConfigService { | ||
|
||
private DependentResourceFactory dependentResourceFactory = DependentResourceFactory.DEFAULT; | ||
|
||
public SpringAwareBaseConfigService(final Version version) { | ||
super(version); | ||
} | ||
|
||
public void setDependentResourceFactory(final DependentResourceFactory dependentResourceFactory) { | ||
this.dependentResourceFactory = dependentResourceFactory; | ||
} | ||
|
||
@Override | ||
public DependentResourceFactory dependentResourceFactory() { | ||
return dependentResourceFactory; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/rcube/configmap/operator/config/SpringAwareDependentResourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.rcube.configmap.operator.config; | ||
|
||
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceConfigurationResolver; | ||
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class SpringAwareDependentResourceFactory implements DependentResourceFactory<ControllerConfiguration<?>> { | ||
|
||
private final Map<String, DependentResource> dependentResourcesContainer; | ||
|
||
public SpringAwareDependentResourceFactory(final List<DependentResource> dependentResources) { | ||
this.dependentResourcesContainer = buildDependentResourcesContainer(dependentResources); | ||
} | ||
|
||
@Override | ||
public DependentResource createFrom(final DependentResourceSpec spec, final ControllerConfiguration<?> configuration) { | ||
final DependentResource dependentResourceFromSpringContext = dependentResourcesContainer.get(spec.getDependentResourceClass().getName()); | ||
if (dependentResourceFromSpringContext == null) { | ||
return DependentResourceFactory.DEFAULT.createFrom(spec, configuration); | ||
} | ||
DependentResourceConfigurationResolver.configure(dependentResourceFromSpringContext, spec, configuration); | ||
return dependentResourceFromSpringContext; | ||
} | ||
|
||
private Map<String, DependentResource> buildDependentResourcesContainer(List<DependentResource> dependentResources) { | ||
return dependentResources.stream() | ||
.collect(Collectors.toMap(dependentResource -> dependentResource.getClass().getName(), Function.identity())); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/rcube/configmap/operator/dependentresource/ConfigMapDependentResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.rcube.configmap.operator.dependentresource; | ||
|
||
import com.rcube.configmap.operator.ConfigMapCustomResource; | ||
import com.rcube.configmap.validators.SpecValidator; | ||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
import lombok.val; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ConfigMapDependentResource extends CRUDKubernetesDependentResource<ConfigMap, ConfigMapCustomResource> { | ||
|
||
@Autowired | ||
private SpecValidator specValidator; | ||
|
||
public ConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired(final ConfigMapCustomResource primary, final Context<ConfigMapCustomResource> context) { | ||
specValidator.validateResourceContent(primary); | ||
return createConfigMap(primary); | ||
} | ||
|
||
private ConfigMap createConfigMap(final ConfigMapCustomResource resource) { | ||
val configMapData = resource.getSpec().getConfig(); | ||
val configMap = buildConfigMap(resource); | ||
configMap.setData(configMapData.getData()); | ||
configMap.setAdditionalProperties(configMap.getAdditionalProperties()); | ||
configMap.setBinaryData(configMapData.getBinaryData()); | ||
configMap.setImmutable(configMapData.getImmutable()); | ||
return configMap; | ||
} | ||
private ConfigMap buildConfigMap(final ConfigMapCustomResource resource) { | ||
return new ConfigMapBuilder() | ||
.withNewMetadata() | ||
.withName(resource.getMetadata().getName()) | ||
.withNamespace(resource.getMetadata().getNamespace()) | ||
.withLabels(resource.getMetadata().getLabels()) | ||
.addNewOwnerReference() | ||
.withController(true) | ||
.withKind(resource.getKind()) | ||
.withApiVersion(resource.getApiVersion()) | ||
.withName(resource.getMetadata().getName()) | ||
.withUid(resource.getMetadata().getUid()) | ||
.endOwnerReference() | ||
.endMetadata() | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.