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

Restart option for DaemonSets & StatefulSets #9866

Open
wants to merge 4 commits into
base: master
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
52 changes: 52 additions & 0 deletions modules/api/pkg/handler/apihandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ func CreateHTTPAPIHandler(iManager integration.Manager) (*restful.Container, err
Param(apiV1Ws.PathParameter("daemonSet", "name of the DaemonSet")).
Writes(common.EventList{}).
Returns(http.StatusOK, "OK", common.EventList{}))
apiV1Ws.Route(
apiV1Ws.PUT("/daemonset/{namespace}/{daemonSet}/restart").To(apiHandler.handleDaemonSetRestart).
// docs
Doc("rollout restart of the Daemon Set").
Param(apiV1Ws.PathParameter("namespace", "namespace of the Daemon Set")).
Param(apiV1Ws.PathParameter("daemonSet", "name of the Daemon Set")).
Writes(deployment.RolloutSpec{}).
Returns(http.StatusOK, "OK", daemonset.DaemonSetDetail{}),
)

// HorizontalPodAutoscaler
apiV1Ws.Route(
Expand Down Expand Up @@ -846,6 +855,15 @@ func CreateHTTPAPIHandler(iManager integration.Manager) (*restful.Container, err
Param(apiV1Ws.PathParameter("statefulset", "name of the StatefulSet")).
Writes(common.EventList{}).
Returns(http.StatusOK, "OK", common.EventList{}))
apiV1Ws.Route(
apiV1Ws.PUT("/statefulset/{namespace}/{statefulset}/restart").To(apiHandler.handleStatefulSetRestart).
// docs
Doc("rollout restart of the Daemon Set").
Param(apiV1Ws.PathParameter("namespace", "namespace of the StatefulSet")).
Param(apiV1Ws.PathParameter("statefulset", "name of the StatefulSet")).
Writes(deployment.RolloutSpec{}).
Returns(http.StatusOK, "OK", statefulset.StatefulSetDetail{}),
)

// Node
apiV1Ws.Route(
Expand Down Expand Up @@ -2801,6 +2819,40 @@ func (apiHandler *APIHandler) handleGetDaemonSetEvents(request *restful.Request,
_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (apiHandle *APIHandler) handleDaemonSetRestart(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("daemonSet")
result, err := daemonset.RestartDaemonSet(k8sClient, namespace, name)
if err != nil {
errors.HandleInternalError(response, err)
return
}
_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (apiHandle *APIHandler) handleStatefulSetRestart(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("daemonSet")
result, err := statefulset.RestartStatefulSet(k8sClient, namespace, name)
if err != nil {
errors.HandleInternalError(response, err)
return
}
_ = response.WriteHeaderAndEntity(http.StatusOK, result)
}

func (apiHandler *APIHandler) handleGetHorizontalPodAutoscalerList(request *restful.Request,
response *restful.Response) {
k8sClient, err := client.Client(request.Request)
Expand Down
42 changes: 42 additions & 0 deletions modules/api/pkg/resource/daemonset/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 daemonset

import (
"context"
v1 "k8s.io/api/apps/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
client "k8s.io/client-go/kubernetes"
"time"
)

const (
// RestartedAtAnnotationKey is an annotation key for rollout restart
RestartedAtAnnotationKey = "kubectl.kubernetes.io/restartedAt"
)

// RestartDaemonSet restarts a daemon set in the manner of `kubectl rollout restart`.
func RestartDaemonSet(client client.Interface, namespace, name string) (*v1.DaemonSet, error) {
daemonSet, err := client.AppsV1().DaemonSets(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}

if daemonSet.Spec.Template.ObjectMeta.Annotations == nil {
daemonSet.Spec.Template.ObjectMeta.Annotations = map[string]string{}
}
daemonSet.Spec.Template.ObjectMeta.Annotations[RestartedAtAnnotationKey] = time.Now().Format(time.RFC3339)
return client.AppsV1().DaemonSets(namespace).Update(context.TODO(), daemonSet, metaV1.UpdateOptions{})
}
28 changes: 28 additions & 0 deletions modules/api/pkg/resource/statefulset/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package statefulset

import (
"context"
v1 "k8s.io/api/apps/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
client "k8s.io/client-go/kubernetes"
"time"
)

const (
// RestartedAtAnnotationKey is an annotation key for rollout restart
RestartedAtAnnotationKey = "kubectl.kubernetes.io/restartedAt"
)

// RestartStatefulSet restarts a daemon set in the manner of `kubectl rollout restart`.
func RestartStatefulSet(client client.Interface, namespace, name string) (*v1.StatefulSet, error) {
statefulSet, err := client.AppsV1().StatefulSets(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}

if statefulSet.Spec.Template.ObjectMeta.Annotations == nil {
statefulSet.Spec.Template.ObjectMeta.Annotations = map[string]string{}
}
statefulSet.Spec.Template.ObjectMeta.Annotations[RestartedAtAnnotationKey] = time.Now().Format(time.RFC3339)
return client.AppsV1().StatefulSets(namespace).Update(context.TODO(), statefulSet, metaV1.UpdateOptions{})
}
2 changes: 2 additions & 0 deletions modules/common/types/resourcekind.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (k ResourceKind) Scalable() bool {
func (k ResourceKind) Restartable() bool {
restartable := []ResourceKind{
ResourceKindDeployment,
ResourceKindDaemonSet,
ResourceKindStatefulSet,
}

for _, kind := range restartable {
Expand Down