Skip to content

Commit

Permalink
Add support for IngressClass [kubernetes#5232]
Browse files Browse the repository at this point in the history
  • Loading branch information
andreadecorte committed Apr 21, 2022
1 parent 808077c commit 014a465
Show file tree
Hide file tree
Showing 34 changed files with 2,588 additions and 1,212 deletions.
129 changes: 101 additions & 28 deletions i18n/de/messages.de.xlf

Large diffs are not rendered by default.

116 changes: 88 additions & 28 deletions i18n/es/messages.es.xlf

Large diffs are not rendered by default.

104 changes: 76 additions & 28 deletions i18n/fr/messages.fr.xlf

Large diffs are not rendered by default.

104 changes: 76 additions & 28 deletions i18n/ja/messages.ja.xlf

Large diffs are not rendered by default.

104 changes: 76 additions & 28 deletions i18n/ko/messages.ko.xlf

Large diffs are not rendered by default.

2,019 changes: 1,032 additions & 987 deletions i18n/messages.xlf

Large diffs are not rendered by default.

104 changes: 76 additions & 28 deletions i18n/zh-Hans/messages.zh-Hans.xlf

Large diffs are not rendered by default.

104 changes: 76 additions & 28 deletions i18n/zh-Hant-HK/messages.zh-Hant-HK.xlf

Large diffs are not rendered by default.

104 changes: 76 additions & 28 deletions i18n/zh-Hant/messages.zh-Hant.xlf

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/app/backend/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const (
ResourceKindPlugin = "plugin"
ResourceKindEndpoint = "endpoint"
ResourceKindNetworkPolicy = "networkpolicy"
ResourceKindIngressClass = "ingressclass"
)

// Scalable method return whether ResourceKind is scalable.
Expand Down Expand Up @@ -223,6 +224,7 @@ var KindToAPIMapping = map[string]APIMapping{
ResourceKindEvent: {"events", ClientTypeDefault, true},
ResourceKindHorizontalPodAutoscaler: {"horizontalpodautoscalers", ClientTypeAutoscalingClient, true},
ResourceKindIngress: {"ingresses", ClientTypeNetworkingClient, true},
ResourceKindIngressClass: {"ingressclasses", ClientTypeNetworkingClient, false},
ResourceKindJob: {"jobs", ClientTypeBatchClient, true},
ResourceKindCronJob: {"cronjobs", ClientTypeBetaBatchClient, true},
ResourceKindLimitRange: {"limitrange", ClientTypeDefault, true},
Expand Down
42 changes: 42 additions & 0 deletions src/app/backend/handler/apihandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/kubernetes/dashboard/src/app/backend/resource/event"
"github.com/kubernetes/dashboard/src/app/backend/resource/horizontalpodautoscaler"
"github.com/kubernetes/dashboard/src/app/backend/resource/ingress"
"github.com/kubernetes/dashboard/src/app/backend/resource/ingressclass"
"github.com/kubernetes/dashboard/src/app/backend/resource/job"
"github.com/kubernetes/dashboard/src/app/backend/resource/logs"
ns "github.com/kubernetes/dashboard/src/app/backend/resource/namespace"
Expand Down Expand Up @@ -688,6 +689,15 @@ func CreateHTTPAPIHandler(iManager integration.IntegrationManager, cManager clie
To(apiHandler.handleGetStorageClassPersistentVolumes).
Writes(persistentvolume.PersistentVolumeList{}))

apiV1Ws.Route(
apiV1Ws.GET("/ingressclass").
To(apiHandler.handleGetIngressClassList).
Writes(ingressclass.IngressClassList{}))
apiV1Ws.Route(
apiV1Ws.GET("/ingressclass/{ingressclass}").
To(apiHandler.handleGetIngressClass).
Writes(ingressclass.IngressClass{}))

apiV1Ws.Route(
apiV1Ws.GET("/log/source/{namespace}/{resourceName}/{resourceType}").
To(apiHandler.handleLogSource).
Expand Down Expand Up @@ -2554,6 +2564,38 @@ func (apiHandler *APIHandler) handleGetStorageClassPersistentVolumes(request *re
response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (apiHandler *APIHandler) handleGetIngressClassList(request *restful.Request, response *restful.Response) {
k8sClient, err := apiHandler.cManager.Client(request)
if err != nil {
errors.HandleInternalError(response, err)
return
}

dataSelect := parser.ParseDataSelectPathParameter(request)
result, err := ingressclass.GetIngressClassList(k8sClient, dataSelect)
if err != nil {
errors.HandleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (apiHandler *APIHandler) handleGetIngressClass(request *restful.Request, response *restful.Response) {
k8sClient, err := apiHandler.cManager.Client(request)
if err != nil {
errors.HandleInternalError(response, err)
return
}

name := request.PathParameter("ingressclass")
result, err := ingressclass.GetIngressClass(k8sClient, name)
if err != nil {
errors.HandleInternalError(response, err)
return
}
response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (apiHandler *APIHandler) handleGetPodPersistentVolumeClaims(request *restful.Request,
response *restful.Response) {
k8sClient, err := apiHandler.cManager.Client(request)
Expand Down
28 changes: 28 additions & 0 deletions src/app/backend/resource/common/resourcechannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type ResourceChannels struct {
// List and error channels to StorageClasses
StorageClassList StorageClassListChannel

// List and error channels to IngressClasses
IngressClassList IngressClassListChannel

// List and error channels to Roles
RoleList RoleListChannel

Expand Down Expand Up @@ -943,3 +946,28 @@ func GetStorageClassListChannel(client client.Interface, numReads int) StorageCl

return channel
}

// IngressClassListChannel is a list and error channels to ingress classes.
type IngressClassListChannel struct {
List chan *networkingv1.IngressClassList
Error chan error
}

// GetIngressClassListChannel returns a pair of channels to a ingress class list and
// errors that both must be read numReads times.
func GetIngressClassListChannel(client client.Interface, numReads int) IngressClassListChannel {
channel := IngressClassListChannel{
List: make(chan *networkingv1.IngressClassList, numReads),
Error: make(chan error, numReads),
}

go func() {
list, err := client.NetworkingV1().IngressClasses().List(context.TODO(), api.ListEverything)
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()

return channel
}
54 changes: 54 additions & 0 deletions src/app/backend/resource/ingressclass/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2017 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 ingressclass

import (
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
networkingv1 "k8s.io/api/networking/v1"
)

// The code below allows to perform complex data section on []networkingv1.IngressClass

type IngressClassCell networkingv1.IngressClass

func (self IngressClassCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
switch name {
case dataselect.NameProperty:
return dataselect.StdComparableString(self.ObjectMeta.Name)
case dataselect.CreationTimestampProperty:
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
case dataselect.NamespaceProperty:
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
default:
// if name is not supported then just return a constant dummy value, sort will have no effect.
return nil
}
}

func toCells(std []networkingv1.IngressClass) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = IngressClassCell(std[i])
}
return cells
}

func fromCells(cells []dataselect.DataCell) []networkingv1.IngressClass {
std := make([]networkingv1.IngressClass, len(cells))
for i := range std {
std[i] = networkingv1.IngressClass(cells[i].(IngressClassCell))
}
return std
}
68 changes: 68 additions & 0 deletions src/app/backend/resource/ingressclass/detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2017 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 ingressclass

import (
"context"
"log"

networkingv1 "k8s.io/api/networking/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// IngressClassDetail provides the presentation layer view of Ingress Class resource.
type IngressClassDetail struct {
// Extends list item structure.
IngressClass `json:",inline"`
Parameters map[string]string `json:"parameters"`
}

// GetIngressClass returns Storage Class resource.
func GetIngressClass(client kubernetes.Interface, name string) (*IngressClassDetail, error) {
log.Printf("Getting details of %s ingress class", name)

ic, err := client.NetworkingV1().IngressClasses().Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}

ingressClass := toIngressClassDetail(ic)
return &ingressClass, err
}

func toIngressClassDetail(ingressClass *networkingv1.IngressClass) IngressClassDetail {
parametersMap := make(map[string]string)
parameters := ingressClass.Spec.Parameters
if parameters != nil {
// Mandatory parameters
parametersMap["Kind"] = parameters.Kind
parametersMap["Name"] = parameters.Name
// Optional parameters
if parameters.APIGroup != nil {
parametersMap["ApiGroup"] = *parameters.APIGroup
}
if parameters.Namespace != nil {
parametersMap["Namespace"] = *parameters.Namespace
}
if parameters.Scope != nil {
parametersMap["Scope"] = *parameters.Scope
}
}
return IngressClassDetail{
IngressClass: toIngressClass(ingressClass),
Parameters: parametersMap,
}
}
69 changes: 69 additions & 0 deletions src/app/backend/resource/ingressclass/detail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2017 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 ingressclass

import (
"reflect"
"testing"

"github.com/kubernetes/dashboard/src/app/backend/api"
networkingv1 "k8s.io/api/networking/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestToIngressClassDetails(t *testing.T) {
paramRef := networkingv1.IngressClassParametersReference{Kind: "pods", Name: "test"}
apiGroup := "k8s.example.com"
scope := "Namespace"
paramFullRef := networkingv1.IngressClassParametersReference{Kind: "pods", Name: "test", APIGroup: &apiGroup, Scope: &scope}
cases := []struct {
ingressClass *networkingv1.IngressClass
expected IngressClassDetail
}{
{
ingressClass: &networkingv1.IngressClass{
ObjectMeta: metaV1.ObjectMeta{Name: "test-ic"},
Spec: networkingv1.IngressClassSpec{Controller: "k8s.io/ingress-nginx", Parameters: &paramRef},
},
expected: IngressClassDetail{
IngressClass: IngressClass{
ObjectMeta: api.ObjectMeta{Name: "test-ic"},
TypeMeta: api.TypeMeta{Kind: "ingressclass"},
Controller: "k8s.io/ingress-nginx"},
Parameters: map[string]string{"Kind": "pods", "Name": "test"},
},
}, {
ingressClass: &networkingv1.IngressClass{
ObjectMeta: metaV1.ObjectMeta{Name: "test-ic"},
Spec: networkingv1.IngressClassSpec{Controller: "k8s.io/ingress-nginx", Parameters: &paramFullRef},
},
expected: IngressClassDetail{
IngressClass: IngressClass{
ObjectMeta: api.ObjectMeta{Name: "test-ic"},
TypeMeta: api.TypeMeta{Kind: "ingressclass"},
Controller: "k8s.io/ingress-nginx"},
Parameters: map[string]string{"ApiGroup": "k8s.example.com", "Kind": "pods", "Name": "test", "Scope": "Namespace"},
},
},
}

for _, c := range cases {
actual := toIngressClassDetail(c.ingressClass)

if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("toIngressClassDetails(%#v) == \ngot %#v, \nexpected %#v", c.ingressClass, actual, c.expected)
}
}
}
Loading

0 comments on commit 014a465

Please sign in to comment.