Skip to content

feat: Pod Disruption Budget API #10009

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

Merged
merged 11 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
61 changes: 61 additions & 0 deletions modules/api/pkg/handler/apihandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/dashboard/api/pkg/resource/poddisruptionbudget"
"k8s.io/klog/v2"

"k8s.io/dashboard/api/pkg/resource/networkpolicy"
Expand Down Expand Up @@ -1120,6 +1121,32 @@ func CreateHTTPAPIHandler(iManager integration.Manager) (*restful.Container, err
Writes(persistentvolumeclaim.PersistentVolumeClaimDetail{}).
Returns(http.StatusOK, "OK", persistentvolumeclaim.PersistentVolumeClaimDetail{}))

// PodDisruptionBudget
apiV1Ws.Route(
apiV1Ws.GET("/poddisruptionbudget/").
To(apiHandler.handleGetPodDisruptionBudgetList).
// docs
Doc("returns a list of PodDisruptionBudget").
Writes(poddisruptionbudget.PodDisruptionBudgetList{}).
Returns(http.StatusOK, "OK", poddisruptionbudget.PodDisruptionBudgetList{}))
apiV1Ws.Route(
apiV1Ws.GET("/poddisruptionbudget/{namespace}").
To(apiHandler.handleGetPodDisruptionBudgetList).
// docs
Doc("returns a list of PodDisruptionBudget from specified namespace").
Param(apiV1Ws.PathParameter("namespace", "namespace of the PodDisruptionBudget")).
Writes(poddisruptionbudget.PodDisruptionBudgetList{}).
Returns(http.StatusOK, "OK", poddisruptionbudget.PodDisruptionBudgetList{}))
apiV1Ws.Route(
apiV1Ws.GET("/poddisruptionbudget/{namespace}/{name}").
To(apiHandler.handleGetPodDisruptionBudgetDetail).
// docs
Doc("returns detailed information about PodDisruptionBudget").
Param(apiV1Ws.PathParameter("name", "name of the PodDisruptionBudget")).
Param(apiV1Ws.PathParameter("namespace", "namespace of the PodDisruptionBudget")).
Writes(poddisruptionbudget.PodDisruptionBudgetDetail{}).
Returns(http.StatusOK, "OK", poddisruptionbudget.PodDisruptionBudgetDetail{}))

// CRD
apiV1Ws.Route(
apiV1Ws.GET("/crd").
Expand Down Expand Up @@ -2674,6 +2701,40 @@ func (apiHandler *APIHandler) handleGetPersistentVolumeClaimDetail(request *rest
_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

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

namespace := parseNamespacePathParameter(request)
dataSelect := parser.ParseDataSelectPathParameter(request)
result, err := poddisruptionbudget.List(k8sClient, namespace, dataSelect)
if err != nil {
errors.HandleInternalError(response, err)
return
}
_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

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

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

func (apiHandler *APIHandler) handleGetPodContainers(request *restful.Request, response *restful.Response) {
k8sClient, err := client.Client(request.Request)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions modules/api/pkg/resource/common/resourcechannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
rbac "k8s.io/api/rbac/v1"
storage "k8s.io/api/storage/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -124,6 +125,8 @@ type ResourceChannels struct {

// List and error channels to ClusterRoleBindings
ClusterRoleBindingList ClusterRoleBindingListChannel

PodDisruptionBudget PodDisruptionBudgetListChannel
}

// ServiceListChannel is a list and error channels to Services.
Expand Down Expand Up @@ -841,6 +844,30 @@ func GetPersistentVolumeClaimListChannel(client client.Interface, nsQuery *Names
return channel
}

type PodDisruptionBudgetListChannel struct {
List chan *policyv1.PodDisruptionBudgetList
Error chan error
}

func GetPodDisruptionBudgetListChannel(client client.Interface, nsQuery *NamespaceQuery,
numReads int) PodDisruptionBudgetListChannel {

channel := PodDisruptionBudgetListChannel{
List: make(chan *policyv1.PodDisruptionBudgetList, numReads),
Error: make(chan error, numReads),
}

go func() {
list, err := client.PolicyV1().PodDisruptionBudgets(nsQuery.ToRequestParam()).List(context.TODO(), helpers.ListEverything)
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()

return channel
}

// CustomResourceDefinitionChannelV1 is a list and error channels to CustomResourceDefinition.
type CustomResourceDefinitionChannelV1 struct {
List chan *apiextensions.CustomResourceDefinitionList
Expand Down
53 changes: 53 additions & 0 deletions modules/api/pkg/resource/poddisruptionbudget/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 poddisruptionbudget

import (
policyv1 "k8s.io/api/policy/v1"

"k8s.io/dashboard/api/pkg/resource/dataselect"
)

type PodDisruptionBudgetCell policyv1.PodDisruptionBudget

func (self PodDisruptionBudgetCell) 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 []policyv1.PodDisruptionBudget) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = PodDisruptionBudgetCell(std[i])
}
return cells
}

func fromCells(cells []dataselect.DataCell) []policyv1.PodDisruptionBudget {
std := make([]policyv1.PodDisruptionBudget, len(cells))
for i := range std {
std[i] = policyv1.PodDisruptionBudget(cells[i].(PodDisruptionBudgetCell))
}
return std
}
45 changes: 45 additions & 0 deletions modules/api/pkg/resource/poddisruptionbudget/detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 poddisruptionbudget

import (
"context"

policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/client-go/kubernetes"
)

type PodDisruptionBudgetDetail struct {
PodDisruptionBudget `json:",inline"`
DisruptedPods map[string]metav1.Time `json:"disruptedPods"`
}

func Get(client kubernetes.Interface, namespace string, name string) (*PodDisruptionBudgetDetail, error) {
pdb, err := client.PolicyV1().PodDisruptionBudgets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}

return toDetails(*pdb), nil
}

func toDetails(pdb policyv1.PodDisruptionBudget) *PodDisruptionBudgetDetail {
return &PodDisruptionBudgetDetail{
PodDisruptionBudget: toListItem(pdb),
DisruptedPods: pdb.Status.DisruptedPods,
}
}
73 changes: 73 additions & 0 deletions modules/api/pkg/resource/poddisruptionbudget/detail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 poddisruptionbudget

import (
"reflect"
"testing"

"github.com/samber/lo"
policyv1 "k8s.io/api/policy/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/dashboard/types"
)

func TestToDetails(t *testing.T) {
cases := []struct {
resource *policyv1.PodDisruptionBudget
expected *PodDisruptionBudgetDetail
}{
{
&policyv1.PodDisruptionBudget{
ObjectMeta: metaV1.ObjectMeta{Name: "foo", Namespace: "bar"},
TypeMeta: metaV1.TypeMeta{Kind: types.ResourceKindPodDisruptionBudget},
Spec: policyv1.PodDisruptionBudgetSpec{
MinAvailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 1},
MaxUnavailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 3},
UnhealthyPodEvictionPolicy: lo.ToPtr(policyv1.IfHealthyBudget),
},
Status: policyv1.PodDisruptionBudgetStatus{
CurrentHealthy: 10,
DesiredHealthy: 10,
ExpectedPods: 10,
DisruptedPods: make(map[string]metaV1.Time),
DisruptionsAllowed: 0,
},
},
&PodDisruptionBudgetDetail{
PodDisruptionBudget: PodDisruptionBudget{
ObjectMeta: types.ObjectMeta{Name: "foo", Namespace: "bar"},
TypeMeta: types.TypeMeta{Kind: types.ResourceKindPodDisruptionBudget},
MinAvailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 1},
MaxUnavailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 3},
UnhealthyPodEvictionPolicy: lo.ToPtr(policyv1.IfHealthyBudget),
CurrentHealthy: 10,
DesiredHealthy: 10,
ExpectedPods: 10,
DisruptionsAllowed: 0,
},
DisruptedPods: make(map[string]metaV1.Time),
},
},
}
for _, c := range cases {
actual := toDetails(*c.resource)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("toDetails(%#v) == \n%#v\nexpected \n%#v\n",
c.resource, actual, c.expected)
}
}
}
Loading
Loading