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

matcher implementation #6

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Changes from 1 commit
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
42 changes: 36 additions & 6 deletions src/main/java/io/opdev/ExampleValuesReconciler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.processing.dependent.Matcher.Result;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.GenericKubernetesResourceMatcher;
import org.yaml.snakeyaml.Yaml;


Expand Down Expand Up @@ -90,17 +93,44 @@ private void createFromYaml(String pathToYaml, ExampleValues resource) throws Fi
log.info("Applying yaml " + pathToYaml + " to the namespace " + resource.getMetadata().getNamespace());
// Parse a yaml into a list of Kubernetes resources
List<HasMetadata> result = client.load(new FileInputStream(pathToYaml)).get();
for (HasMetadata object : result){
ObjectMeta meta = object.getMetadata();
for (HasMetadata desiredObject : result){
ObjectMeta meta = desiredObject.getMetadata();
// Patch all objects with owner references
meta.setOwnerReferences(buildOwnerReference(resource));
object.setMetadata(meta);
// Apply Kubernetes Resources
log.info("Creating resource kind: " + object.getKind() + " with name: " + meta.getName() );
client.resource(object).createOrReplace();
desiredObject.setMetadata(meta);
if (needToUpdateState(desiredObject, resource.getMetadata().getNamespace())){
log.info("Creating or updating resource kind: " + desiredObject.getKind() + " with name: " + meta.getName() );
client.resource(desiredObject).createOrReplace();
}
}
}

private boolean needToUpdateState(HasMetadata desiredObject, String namespace){
ResourceDefinitionContext metaContext = new ResourceDefinitionContext.Builder()
.withKind(desiredObject.getKind())
.withNamespaced(true)
.build();

// Get the existing actual kubernetes resource
HasMetadata actualObject = client.genericKubernetesResources(metaContext)
itroyano marked this conversation as resolved.
Show resolved Hide resolved
.inNamespace(namespace)
.withName(desiredObject.getMetadata().getName())
.get();

if (actualObject == null){
// Initial creation is needed
return true;
}

// Perform diff
Result<HasMetadata> matcherResult = GenericKubernetesResourceMatcher.match(desiredObject, actualObject, false);
//.match(desiredObject, actualObject, true, true);
//.match(actualObject, desiredObject, metaContext);

// return true if not matched, to indicate a need to update actual state
return !matcherResult.matched();
}

private List<OwnerReference> buildOwnerReference(ExampleValues resource) {
List<OwnerReference> refs = new ArrayList<>();
refs.add(new OwnerReference(
Expand Down