ingressclass: show (default) marker for default IngressClass

Signed-off-by: jaehanbyun <awbrg789@naver.com>
This commit is contained in:
jaehanbyun 2025-10-23 08:35:22 +09:00
parent cafff6eade
commit 336ec22700
3 changed files with 50 additions and 1 deletions

View file

@ -0,0 +1,27 @@
/*
Copyright 2025 The Kubernetes Authors.
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 util
import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// HasDefaultAnnotation returns true if the object metadata has the default annotation set.
func HasDefaultAnnotation(obj metav1.ObjectMeta) bool {
return obj.Annotations[networkingv1.AnnotationIsDefaultIngressClass] == "true"
}

View file

@ -68,6 +68,7 @@ import (
"k8s.io/kubernetes/pkg/apis/flowcontrol"
apihelpers "k8s.io/kubernetes/pkg/apis/flowcontrol/util"
"k8s.io/kubernetes/pkg/apis/networking"
networkingutil "k8s.io/kubernetes/pkg/apis/networking/util"
nodeapi "k8s.io/kubernetes/pkg/apis/node"
"k8s.io/kubernetes/pkg/apis/policy"
"k8s.io/kubernetes/pkg/apis/rbac"
@ -1494,6 +1495,11 @@ func printIngressClass(obj *networking.IngressClass, options printers.GenerateOp
row := metav1.TableRow{
Object: runtime.RawExtension{Object: obj},
}
name := obj.Name
if networkingutil.HasDefaultAnnotation(obj.ObjectMeta) {
name += " (default)"
}
parameters := "<none>"
if obj.Spec.Parameters != nil {
parameters = obj.Spec.Parameters.Kind
@ -1503,7 +1509,7 @@ func printIngressClass(obj *networking.IngressClass, options printers.GenerateOp
parameters = parameters + "/" + obj.Spec.Parameters.Name
}
createTime := translateTimestampSince(obj.CreationTimestamp)
row.Cells = append(row.Cells, obj.Name, obj.Spec.Controller, parameters, createTime)
row.Cells = append(row.Cells, name, obj.Spec.Controller, parameters, createTime)
return []metav1.TableRow{row}, nil
}

View file

@ -26,6 +26,7 @@ import (
"github.com/google/go-cmp/cmp"
apiv1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
@ -1055,6 +1056,21 @@ func TestPrintIngressClass(t *testing.T) {
},
},
expected: []metav1.TableRow{{Cells: []interface{}{"test2", "example.com/controller2", "<none>", "11y"}}},
}, {
name: "example with default annotation",
ingressClass: &networking.IngressClass{
ObjectMeta: metav1.ObjectMeta{
Name: "test-default",
CreationTimestamp: metav1.Time{Time: time.Now().Add(time.Duration(-9 * 365 * 24 * time.Hour))},
Annotations: map[string]string{
networkingv1.AnnotationIsDefaultIngressClass: "true",
},
},
Spec: networking.IngressClassSpec{
Controller: "example.com/controller",
},
},
expected: []metav1.TableRow{{Cells: []interface{}{"test-default (default)", "example.com/controller", "<none>", "9y"}}},
}}
for _, testCase := range testCases {