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

Enable configuring of categories in CRD generation #6457

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -40,6 +40,7 @@ public class CustomResourceInfo {
private final String singular;
private final String plural;
private final String[] shortNames;
private final String[] categories;
private final boolean storage;
private final boolean served;
private final boolean deprecated;
Expand All @@ -56,7 +57,7 @@ public class CustomResourceInfo {
private final String[] labels;

public CustomResourceInfo(String group, String version, String kind, String singular,
String plural, String[] shortNames, boolean storage, boolean served, boolean deprecated, String deprecationWarning,
String plural, String[] shortNames, String[] categories, boolean storage, boolean served, boolean deprecated, String deprecationWarning,
Scope scope, Class<?> definition, String crClassName,
String specClassName, String statusClassName, String[] annotations, String[] labels) {
this.group = group;
Expand All @@ -65,6 +66,7 @@ public CustomResourceInfo(String group, String version, String kind, String sing
this.singular = singular;
this.plural = plural;
this.shortNames = shortNames;
this.categories = categories;
this.storage = storage;
this.served = served;
this.deprecated = deprecated;
Expand Down Expand Up @@ -112,6 +114,10 @@ public String[] shortNames() {
return shortNames;
}

public String[] categories() {
return categories;
}

public String singular() {
return singular;
}
Expand Down Expand Up @@ -161,6 +167,7 @@ public static CustomResourceInfo fromClass(Class<? extends HasMetadata> customRe
final HasMetadata instance = customResource.getDeclaredConstructor().newInstance();

final String[] shortNames = CustomResource.getShortNames(customResource);
final String[] categories = CustomResource.getCategories(customResource);

final Scope scope = Utils.isResourceNamespaced(customResource) ? Scope.NAMESPACED : Scope.CLUSTER;

Expand Down Expand Up @@ -189,7 +196,7 @@ public static CustomResourceInfo fromClass(Class<? extends HasMetadata> customRe
}

return new CustomResourceInfo(rdc.getGroup(), rdc.getVersion(), rdc.getKind(),
singular, rdc.getPlural(), shortNames, storage, served,
singular, rdc.getPlural(), shortNames, categories, storage, served,
deprecated, deprecationWarning,
scope, customResource,
customResource.getCanonicalName(), specAndStatus.getSpecClassName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void addPrinterColumn(String path, String column, String format, int prio
.withNewNames()
.withKind(config.kind())
.withShortNames(config.shortNames())
.withCategories(config.categories())
.withPlural(config.plural())
.withSingular(config.singular())
.endNames()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn.Type;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Categories;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.ShortNames;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("samples.javaoperatorsdk.io")
@Version("v1alpha1")
@ShortNames("jr")
@Categories({"cat1", "cat2"})
@AdditionalPrinterColumn(name = "Age", jsonPath = ".metadata.creationTimestamp", type = Type.DATE)
public class JokeRequest extends CustomResource<JokeRequestSpec, JokeRequestStatus> implements Namespaced {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ void jokerequestCRDShouldWork() {
"jokerequests", spec);
assertEquals(1, names.getShortNames().size());
assertTrue(names.getShortNames().contains("jr"));
assertEquals(2, names.getCategories().size());
assertTrue(names.getCategories().contains("cat1"));
assertTrue(names.getCategories().contains("cat2"));

final CustomResourceDefinitionVersion version = checkVersion(spec);
assertNotNull(version.getSubresources());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.Scope;
import io.fabric8.kubernetes.model.annotation.Categories;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.ShortNames;
import io.fabric8.kubernetes.model.annotation.Version;
Expand All @@ -42,6 +43,7 @@ public static class Status {
@Group(GROUP)
@Version(VERSION)
@ShortNames("s")
@Categories("cat")
public static class ClusteredCR extends CustomResource<Spec, Status> {
}

Expand Down Expand Up @@ -85,6 +87,7 @@ void shouldProperlyCreateCustomResourceInfo() {
assertEquals(HasMetadata.getPlural(ClusteredCR.class), info.plural());
assertEquals(CustomResource.getCRDName(ClusteredCR.class), info.crdName());
assertArrayEquals(CustomResource.getShortNames(ClusteredCR.class), info.shortNames());
assertArrayEquals(CustomResource.getCategories(ClusteredCR.class), info.categories());
assertTrue(info.served());
assertTrue(info.storage());
assertEquals(HasMetadata.getKind(ClusteredCR.class), info.kind());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.kubernetes.model.Scope;
import io.fabric8.kubernetes.model.annotation.Categories;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.ShortNames;
import io.fabric8.kubernetes.model.annotation.Version;
Expand Down Expand Up @@ -260,6 +261,18 @@ public static String[] getShortNames(Class<?> clazz) {
.orElse(new String[] {});
}

/**
* Retrieves the categories associated with this CustomResource or an empty array if none was provided
*
* @param clazz the CustomResource class for which the categories are to be retrieved
* @return the categories associated with this CustomResource or an empty array if none was provided
*/
public static String[] getCategories(Class<?> clazz) {
return Optional.ofNullable(clazz.getAnnotation(Categories.class))
.map(Categories::value)
.orElse(new String[] {});
}

/**
* Retrieves the scope that this CustomResource targets
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2024 Ericsson Software Technology
*
* 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.kubernetes.model.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Specifies the categories the annotated Custom Resource belongs to.
* See https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#categories for more details.
*/
@Target({ TYPE })
@Retention(RUNTIME)
public @interface Categories {

/**
* @return the categories this Custom Resource belongs to.
*/
String[] value() default {};
}