Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
1bf99e2e
Commit
1bf99e2e
authored
Apr 02, 2019
by
Antoine Pelisse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create `kubectl rollout restart deployment/$deployment` to do a rolling restart
parent
b274fb35
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
282 additions
and
1 deletion
+282
-1
BUILD
pkg/kubectl/cmd/rollout/BUILD
+1
-0
rollout.go
pkg/kubectl/cmd/rollout/rollout.go
+1
-1
rollout_restart.go
pkg/kubectl/cmd/rollout/rollout_restart.go
+187
-0
BUILD
pkg/kubectl/polymorphichelpers/BUILD
+1
-0
interface.go
pkg/kubectl/polymorphichelpers/interface.go
+7
-0
objectrestarter.go
pkg/kubectl/polymorphichelpers/objectrestarter.go
+77
-0
apps.sh
test/cmd/apps.sh
+8
-0
No files found.
pkg/kubectl/cmd/rollout/BUILD
View file @
1bf99e2e
...
...
@@ -10,6 +10,7 @@ go_library(
"rollout.go",
"rollout_history.go",
"rollout_pause.go",
"rollout_restart.go",
"rollout_resume.go",
"rollout_status.go",
"rollout_undo.go",
...
...
pkg/kubectl/cmd/rollout/rollout.go
View file @
1bf99e2e
...
...
@@ -19,7 +19,6 @@ package rollout
import
(
"github.com/lithammer/dedent"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
...
...
@@ -62,6 +61,7 @@ func NewCmdRollout(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobr
cmd
.
AddCommand
(
NewCmdRolloutResume
(
f
,
streams
))
cmd
.
AddCommand
(
NewCmdRolloutUndo
(
f
,
streams
))
cmd
.
AddCommand
(
NewCmdRolloutStatus
(
f
,
streams
))
cmd
.
AddCommand
(
NewCmdRolloutRestart
(
f
,
streams
))
return
cmd
}
pkg/kubectl/cmd/rollout/rollout_restart.go
0 → 100644
View file @
1bf99e2e
/*
Copyright 2019 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
rollout
import
(
"fmt"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
utilerrors
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/kubernetes/pkg/kubectl/cmd/set"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
"k8s.io/kubernetes/pkg/kubectl/util/templates"
)
// RestartOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type
RestartOptions
struct
{
PrintFlags
*
genericclioptions
.
PrintFlags
ToPrinter
func
(
string
)
(
printers
.
ResourcePrinter
,
error
)
Resources
[]
string
Builder
func
()
*
resource
.
Builder
Restarter
polymorphichelpers
.
ObjectRestarterFunc
Namespace
string
EnforceNamespace
bool
resource
.
FilenameOptions
genericclioptions
.
IOStreams
}
var
(
restartLong
=
templates
.
LongDesc
(
`
Restart a resource.
A deployment with the "RolloutStrategy" will be rolling restarted.`
)
restartExample
=
templates
.
Examples
(
`
# Restart a deployment
kubectl rollout restart deployment/nginx`
)
)
// NewRolloutRestartOptions returns an initialized RestartOptions instance
func
NewRolloutRestartOptions
(
streams
genericclioptions
.
IOStreams
)
*
RestartOptions
{
return
&
RestartOptions
{
PrintFlags
:
genericclioptions
.
NewPrintFlags
(
"restarted"
)
.
WithTypeSetter
(
scheme
.
Scheme
),
IOStreams
:
streams
,
}
}
// NewCmdRolloutRestart returns a Command instance for 'rollout restart' sub command
func
NewCmdRolloutRestart
(
f
cmdutil
.
Factory
,
streams
genericclioptions
.
IOStreams
)
*
cobra
.
Command
{
o
:=
NewRolloutRestartOptions
(
streams
)
validArgs
:=
[]
string
{
"deployment"
}
cmd
:=
&
cobra
.
Command
{
Use
:
"restart RESOURCE"
,
DisableFlagsInUseLine
:
true
,
Short
:
i18n
.
T
(
"Restart a resource"
),
Long
:
restartLong
,
Example
:
restartExample
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
cmdutil
.
CheckErr
(
o
.
Complete
(
f
,
cmd
,
args
))
cmdutil
.
CheckErr
(
o
.
Validate
())
cmdutil
.
CheckErr
(
o
.
RunRestart
())
},
ValidArgs
:
validArgs
,
}
usage
:=
"identifying the resource to get from a server."
cmdutil
.
AddFilenameOptionFlags
(
cmd
,
&
o
.
FilenameOptions
,
usage
)
o
.
PrintFlags
.
AddFlags
(
cmd
)
return
cmd
}
// Complete completes all the required options
func
(
o
*
RestartOptions
)
Complete
(
f
cmdutil
.
Factory
,
cmd
*
cobra
.
Command
,
args
[]
string
)
error
{
o
.
Resources
=
args
o
.
Restarter
=
polymorphichelpers
.
ObjectRestarterFn
var
err
error
o
.
Namespace
,
o
.
EnforceNamespace
,
err
=
f
.
ToRawKubeConfigLoader
()
.
Namespace
()
if
err
!=
nil
{
return
err
}
o
.
ToPrinter
=
func
(
operation
string
)
(
printers
.
ResourcePrinter
,
error
)
{
o
.
PrintFlags
.
NamePrintFlags
.
Operation
=
operation
return
o
.
PrintFlags
.
ToPrinter
()
}
o
.
Builder
=
f
.
NewBuilder
return
nil
}
func
(
o
*
RestartOptions
)
Validate
()
error
{
if
len
(
o
.
Resources
)
==
0
&&
cmdutil
.
IsFilenameSliceEmpty
(
o
.
Filenames
,
o
.
Kustomize
)
{
return
fmt
.
Errorf
(
"required resource not specified"
)
}
return
nil
}
// RunRestart performs the execution of 'rollout restart' sub command
func
(
o
RestartOptions
)
RunRestart
()
error
{
r
:=
o
.
Builder
()
.
WithScheme
(
scheme
.
Scheme
,
scheme
.
Scheme
.
PrioritizedVersionsAllGroups
()
...
)
.
NamespaceParam
(
o
.
Namespace
)
.
DefaultNamespace
()
.
FilenameParam
(
o
.
EnforceNamespace
,
&
o
.
FilenameOptions
)
.
ResourceTypeOrNameArgs
(
true
,
o
.
Resources
...
)
.
ContinueOnError
()
.
Latest
()
.
Flatten
()
.
Do
()
if
err
:=
r
.
Err
();
err
!=
nil
{
return
err
}
allErrs
:=
[]
error
{}
infos
,
err
:=
r
.
Infos
()
if
err
!=
nil
{
// restore previous command behavior where
// an error caused by retrieving infos due to
// at least a single broken object did not result
// in an immediate return, but rather an overall
// aggregation of errors.
allErrs
=
append
(
allErrs
,
err
)
}
for
_
,
patch
:=
range
set
.
CalculatePatches
(
infos
,
scheme
.
DefaultJSONEncoder
(),
set
.
PatchFn
(
o
.
Restarter
))
{
info
:=
patch
.
Info
if
patch
.
Err
!=
nil
{
resourceString
:=
info
.
Mapping
.
Resource
.
Resource
if
len
(
info
.
Mapping
.
Resource
.
Group
)
>
0
{
resourceString
=
resourceString
+
"."
+
info
.
Mapping
.
Resource
.
Group
}
allErrs
=
append
(
allErrs
,
fmt
.
Errorf
(
"error: %s %q %v"
,
resourceString
,
info
.
Name
,
patch
.
Err
))
continue
}
if
string
(
patch
.
Patch
)
==
"{}"
||
len
(
patch
.
Patch
)
==
0
{
allErrs
=
append
(
allErrs
,
fmt
.
Errorf
(
"failed to create patch for %v: empty patch"
,
info
.
Name
))
}
obj
,
err
:=
resource
.
NewHelper
(
info
.
Client
,
info
.
Mapping
)
.
Patch
(
info
.
Namespace
,
info
.
Name
,
types
.
StrategicMergePatchType
,
patch
.
Patch
,
nil
)
if
err
!=
nil
{
allErrs
=
append
(
allErrs
,
fmt
.
Errorf
(
"failed to patch: %v"
,
err
))
continue
}
info
.
Refresh
(
obj
,
true
)
printer
,
err
:=
o
.
ToPrinter
(
"restarted"
)
if
err
!=
nil
{
allErrs
=
append
(
allErrs
,
err
)
continue
}
if
err
=
printer
.
PrintObj
(
info
.
Object
,
o
.
Out
);
err
!=
nil
{
allErrs
=
append
(
allErrs
,
err
)
}
}
return
utilerrors
.
NewAggregate
(
allErrs
)
}
pkg/kubectl/polymorphichelpers/BUILD
View file @
1bf99e2e
...
...
@@ -11,6 +11,7 @@ go_library(
"logsforobject.go",
"mapbasedselectorforobject.go",
"objectpauser.go",
"objectrestarter.go",
"objectresumer.go",
"portsforobject.go",
"protocolsforobject.go",
...
...
pkg/kubectl/polymorphichelpers/interface.go
View file @
1bf99e2e
...
...
@@ -106,3 +106,10 @@ type RollbackerFunc func(restClientGetter genericclioptions.RESTClientGetter, ma
// RollbackerFn gives a way to easily override the function for unit testing if needed
var
RollbackerFn
RollbackerFunc
=
rollbacker
// ObjectRestarterFunc is a function type that updates an annotation in a deployment to restart it..
type
ObjectRestarterFunc
func
(
runtime
.
Object
)
([]
byte
,
error
)
// ObjectRestarterFn gives a way to easily override the function for unit testing if needed.
// Returns the patched object in bytes and any error that occurred during the encoding.
var
ObjectRestarterFn
ObjectRestarterFunc
=
defaultObjectRestarter
pkg/kubectl/polymorphichelpers/objectrestarter.go
0 → 100644
View file @
1bf99e2e
/*
Copyright 2018 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
polymorphichelpers
import
(
"errors"
"fmt"
"time"
appsv1
"k8s.io/api/apps/v1"
appsv1beta1
"k8s.io/api/apps/v1beta1"
appsv1beta2
"k8s.io/api/apps/v1beta2"
extensionsv1beta1
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubectl/scheme"
)
func
defaultObjectRestarter
(
obj
runtime
.
Object
)
([]
byte
,
error
)
{
switch
obj
:=
obj
.
(
type
)
{
case
*
extensionsv1beta1
.
Deployment
:
if
obj
.
Spec
.
Paused
{
return
nil
,
errors
.
New
(
"can't restart paused deployment (run rollout resume first)"
)
}
if
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
==
nil
{
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
=
make
(
map
[
string
]
string
)
}
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
[
"kubectl.kubernetes.io/restartedAt"
]
=
time
.
Now
()
.
Format
(
time
.
RFC3339
)
return
runtime
.
Encode
(
scheme
.
Codecs
.
LegacyCodec
(
extensionsv1beta1
.
SchemeGroupVersion
),
obj
)
case
*
appsv1
.
Deployment
:
if
obj
.
Spec
.
Paused
{
return
nil
,
errors
.
New
(
"can't restart paused deployment (run rollout resume first)"
)
}
if
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
==
nil
{
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
=
make
(
map
[
string
]
string
)
}
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
[
"kubectl.kubernetes.io/restartedAt"
]
=
time
.
Now
()
.
Format
(
time
.
RFC3339
)
return
runtime
.
Encode
(
scheme
.
Codecs
.
LegacyCodec
(
appsv1
.
SchemeGroupVersion
),
obj
)
case
*
appsv1beta2
.
Deployment
:
if
obj
.
Spec
.
Paused
{
return
nil
,
errors
.
New
(
"can't restart paused deployment (run rollout resume first)"
)
}
if
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
==
nil
{
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
=
make
(
map
[
string
]
string
)
}
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
[
"kubectl.kubernetes.io/restartedAt"
]
=
time
.
Now
()
.
Format
(
time
.
RFC3339
)
return
runtime
.
Encode
(
scheme
.
Codecs
.
LegacyCodec
(
appsv1beta2
.
SchemeGroupVersion
),
obj
)
case
*
appsv1beta1
.
Deployment
:
if
obj
.
Spec
.
Paused
{
return
nil
,
errors
.
New
(
"can't restart paused deployment (run rollout resume first)"
)
}
if
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
==
nil
{
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
=
make
(
map
[
string
]
string
)
}
obj
.
Spec
.
Template
.
ObjectMeta
.
Annotations
[
"kubectl.kubernetes.io/restartedAt"
]
=
time
.
Now
()
.
Format
(
time
.
RFC3339
)
return
runtime
.
Encode
(
scheme
.
Codecs
.
LegacyCodec
(
appsv1beta1
.
SchemeGroupVersion
),
obj
)
default
:
return
nil
,
fmt
.
Errorf
(
"restarting is not supported"
)
}
}
test/cmd/apps.sh
View file @
1bf99e2e
...
...
@@ -309,6 +309,8 @@ run_deployment_tests() {
kubectl-with-retry rollout pause deployment nginx
"
${
kube_flags
[@]
}
"
# A paused deployment cannot be rolled back
!
kubectl rollout undo deployment nginx
"
${
kube_flags
[@]
}
"
# A paused deployment cannot be restarted
!
kubectl rollout restart deployment nginx
"
${
kube_flags
[@]
}
"
# Resume the deployment
kubectl-with-retry rollout resume deployment nginx
"
${
kube_flags
[@]
}
"
# The resumed deployment can now be rolled back
...
...
@@ -318,6 +320,12 @@ run_deployment_tests() {
kubectl get rs
"
${
newrs
}
"
-o
yaml |
grep
"deployment.kubernetes.io/revision-history: 1,3"
# Check that trying to watch the status of a superseded revision returns an error
!
kubectl rollout status deployment/nginx
--revision
=
3
# Restarting the deployment creates a new replicaset
kubectl rollout restart deployment/nginx
sleep
1
newrs
=
"
$(
kubectl describe deployment nginx |
grep
NewReplicaSet |
awk
'{print $2}'
)
"
rs
=
"
$(
kubectl get rs
"
${
newrs
}
"
-o
yaml
)
"
kube::test::if_has_string
"
${
rs
}
"
"deployment.kubernetes.io/revision:
\"
6
\"
"
cat
hack/testdata/deployment-revision1.yaml |
${
SED
}
"s/name: nginx
$/
name: nginx2/"
| kubectl create
-f
-
"
${
kube_flags
[@]
}
"
# Deletion of both deployments should not be blocked
kubectl delete deployment nginx2
"
${
kube_flags
[@]
}
"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment