forked from fabric8io/kubernetes-client
-
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(crd-generator): Add support for size constraints
- Loading branch information
Showing
10 changed files
with
510 additions
and
9 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
61 changes: 61 additions & 0 deletions
61
crd-generator/api/src/main/java/io/fabric8/crd/generator/SizeInfo.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,61 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.crd.generator; | ||
|
||
import io.sundr.model.AnnotationRef; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class SizeInfo { | ||
private final Long min; | ||
private final Long max; | ||
|
||
private SizeInfo(Long min, Long max) { | ||
this.min = min; | ||
this.max = max; | ||
} | ||
|
||
public Optional<Long> getMin() { | ||
return Optional.ofNullable(min); | ||
} | ||
|
||
public Optional<Long> getMax() { | ||
return Optional.ofNullable(max); | ||
} | ||
|
||
public static SizeInfo from(AnnotationRef annotationRef) { | ||
final Long min = (Long) annotationRef.getParameters().get("min"); | ||
final Long max = (Long) annotationRef.getParameters().get("max"); | ||
|
||
assertGreaterOrEqual(min, 0L, "min must be greater than 0"); | ||
assertGreaterOrEqual(max, 0L, "max must be greater than 0"); | ||
assertGreaterOrEqual(max, min, String.format("max (%s) must be greater or equal to min (%s)", max, min)); | ||
|
||
return new SizeInfo(mapIfNotDefault(min, 0L), mapIfNotDefault(max, Long.MAX_VALUE)); | ||
} | ||
|
||
private static void assertGreaterOrEqual(Long x, Long y, String message) { | ||
if (x >= y) | ||
return; | ||
throw new IllegalArgumentException(message); | ||
} | ||
|
||
private static Long mapIfNotDefault(Long value, Long defaultValue) { | ||
return Objects.equals(value, defaultValue) ? null : value; | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
crd-generator/api/src/test/java/io/fabric8/crd/example/size/SizeExample.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,25 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.crd.example.size; | ||
|
||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.fabric8.io") | ||
@Version("v1") | ||
public class SizeExample extends CustomResource<SizeExampleSpec, Void> { | ||
} |
63 changes: 63 additions & 0 deletions
63
crd-generator/api/src/test/java/io/fabric8/crd/example/size/SizeExampleSpec.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,63 @@ | ||
/* | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.crd.example.size; | ||
|
||
import io.fabric8.generator.annotation.Size; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class SizeExampleSpec { | ||
@Size(min = 1, max = 3) | ||
private String stringWithLowerAndUpperLimits; | ||
|
||
@Size(min = 1, max = 3) | ||
private List<String> listWithLowerAndUpperLimits; | ||
|
||
@Size(min = 1, max = 3) | ||
private String[] arrayWithLowerAndUpperLimits; | ||
|
||
@Size(min = 1, max = 3) | ||
private Map<String, String> mapWithLowerAndUpperLimits; | ||
|
||
@Size(min = 1) | ||
private String stringWithLowerLimit; | ||
|
||
@Size(min = 1) | ||
private List<String> listWithLowerLimit; | ||
|
||
@Size(min = 1) | ||
private String[] arrayWithLowerLimit; | ||
|
||
@Size(min = 1) | ||
private Map<String, String> mapWithLowerLimit; | ||
|
||
@Size(max = 3) | ||
private String stringWithUpperLimit; | ||
|
||
@Size(max = 3) | ||
private List<String> listWithUpperLimit; | ||
|
||
@Size(max = 3) | ||
private String[] arrayWithUpperLimit; | ||
|
||
@Size(max = 3) | ||
private Map<String, String> mapWithUpperLimit; | ||
|
||
@Size(min = 1, max = 3) // ignored, because int is not supported (use @Min and/or @Max) | ||
private int integerWithIgnoredSizeLimits; | ||
|
||
} |
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
Oops, something went wrong.