-
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.
feat: name uniqueness validation (#79)
Signed-off-by: Attila Mészáros <[email protected]>
- Loading branch information
Showing
10 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/io/csviri/operator/glue/customresource/AbstractStatus.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,17 @@ | ||
package io.csviri.operator.glue.customresource; | ||
|
||
import io.javaoperatorsdk.operator.api.ObservedGenerationAwareStatus; | ||
|
||
public class AbstractStatus extends ObservedGenerationAwareStatus { | ||
|
||
private String errorMessage; | ||
|
||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
public void setErrorMessage(String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
src/main/java/io/csviri/operator/glue/customresource/glue/GlueStatus.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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package io.csviri.operator.glue.customresource.glue; | ||
|
||
import io.javaoperatorsdk.operator.api.ObservedGenerationAwareStatus; | ||
import io.csviri.operator.glue.customresource.AbstractStatus; | ||
|
||
public class GlueStatus extends ObservedGenerationAwareStatus { | ||
public class GlueStatus extends AbstractStatus { | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
...main/java/io/csviri/operator/glue/customresource/operator/ResourceFlowOperatorStatus.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package io.csviri.operator.glue.customresource.operator; | ||
|
||
import io.javaoperatorsdk.operator.api.ObservedGenerationAwareStatus; | ||
import io.csviri.operator.glue.customresource.AbstractStatus; | ||
|
||
public class ResourceFlowOperatorStatus extends AbstractStatus { | ||
|
||
public class ResourceFlowOperatorStatus extends ObservedGenerationAwareStatus { | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/io/csviri/operator/glue/reconciler/ValidationAndErrorHandler.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,77 @@ | ||
package io.csviri.operator.glue.reconciler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.csviri.operator.glue.GlueException; | ||
import io.csviri.operator.glue.customresource.AbstractStatus; | ||
import io.csviri.operator.glue.customresource.glue.DependentResourceSpec; | ||
import io.csviri.operator.glue.customresource.glue.GlueSpec; | ||
import io.csviri.operator.glue.customresource.glue.RelatedResourceSpec; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.javaoperatorsdk.operator.api.reconciler.ErrorStatusUpdateControl; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
public class ValidationAndErrorHandler { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ValidationAndErrorHandler.class); | ||
|
||
public static final String NON_UNIQUE_NAMES_FOUND_PREFIX = "Non unique names found: "; | ||
|
||
public <T extends CustomResource<?, ? extends AbstractStatus>> ErrorStatusUpdateControl<T> updateStatusErrorMessage( | ||
Exception e, | ||
T resource) { | ||
log.error("Error during reconciliation of resource. Name: {} namespace: {}, Kind: {}", | ||
resource.getMetadata().getName(), resource.getMetadata().getNamespace(), resource.getKind(), | ||
e); | ||
if (e instanceof ValidationAndErrorHandler.NonUniqueNameException ex) { | ||
resource.getStatus() | ||
.setErrorMessage(NON_UNIQUE_NAMES_FOUND_PREFIX + String.join(",", ex.getDuplicates())); | ||
return ErrorStatusUpdateControl.updateStatus(resource).withNoRetry(); | ||
} else { | ||
resource.getStatus().setErrorMessage("Error during reconciliation"); | ||
return ErrorStatusUpdateControl.updateStatus(resource); | ||
} | ||
} | ||
|
||
public void checkIfNamesAreUnique(GlueSpec glueSpec) { | ||
Set<String> seen = new HashSet<>(); | ||
List<String> duplicates = new ArrayList<>(); | ||
|
||
Consumer<String> deduplicate = n -> { | ||
if (seen.contains(n)) { | ||
duplicates.add(n); | ||
} else { | ||
seen.add(n); | ||
} | ||
}; | ||
glueSpec.getResources().stream().map(DependentResourceSpec::getName).forEach(deduplicate); | ||
glueSpec.getRelatedResources().stream().map(RelatedResourceSpec::getName).forEach(deduplicate); | ||
|
||
if (!duplicates.isEmpty()) { | ||
throw new NonUniqueNameException(duplicates); | ||
} | ||
} | ||
|
||
public static class NonUniqueNameException extends GlueException { | ||
|
||
private final List<String> duplicates; | ||
|
||
public NonUniqueNameException(List<String> duplicates) { | ||
this.duplicates = duplicates; | ||
} | ||
|
||
public List<String> getDuplicates() { | ||
return duplicates; | ||
} | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Invalid GLUE, presents resources with non-unique name | ||
apiVersion: io.csviri.operator.glue/v1beta1 | ||
kind: Glue | ||
metadata: | ||
name: templating-sample | ||
spec: | ||
resources: | ||
- name: configMap1 | ||
resource: | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: cm1 | ||
data: | ||
key: "value1" | ||
- name: configMap1 | ||
resource: | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: cm2 | ||
data: | ||
key: "value1" |
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,25 @@ | ||
apiVersion: io.csviri.operator.glue/v1beta1 | ||
kind: GlueOperator | ||
metadata: | ||
name: non-unique-name | ||
spec: | ||
parent: | ||
apiVersion: io.csviri.operator.glue/v1 | ||
kind: TestCustomResource | ||
resources: | ||
- name: configMap1 | ||
resource: | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: "{parent.metadata.name}" | ||
data: | ||
key: "{parent.spec.value}" | ||
- name: configMap1 | ||
resource: | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: "{parent.metadata.name}" | ||
data: | ||
key: "{parent.spec.value}" |