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
cc04abca
Unverified
Commit
cc04abca
authored
Sep 25, 2018
by
k8s-ci-robot
Committed by
GitHub
Sep 25, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #68681 from dlespiau/instrument-kubectl
Add go profile instrumentation to kubectl
parents
c8f0af2a
5d634e7d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
0 deletions
+99
-0
BUILD
pkg/kubectl/cmd/BUILD
+1
-0
cmd.go
pkg/kubectl/cmd/cmd.go
+10
-0
profiling.go
pkg/kubectl/cmd/profiling.go
+88
-0
No files found.
pkg/kubectl/cmd/BUILD
View file @
cc04abca
...
@@ -40,6 +40,7 @@ go_library(
...
@@ -40,6 +40,7 @@ go_library(
"patch.go",
"patch.go",
"plugin.go",
"plugin.go",
"portforward.go",
"portforward.go",
"profiling.go",
"proxy.go",
"proxy.go",
"replace.go",
"replace.go",
"rollingupdate.go",
"rollingupdate.go",
...
...
pkg/kubectl/cmd/cmd.go
View file @
cc04abca
...
@@ -370,6 +370,14 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
...
@@ -370,6 +370,14 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
Find more information at:
Find more information at:
https://kubernetes.io/docs/reference/kubectl/overview/`
),
https://kubernetes.io/docs/reference/kubectl/overview/`
),
Run
:
runHelp
,
Run
:
runHelp
,
// Hook before and after Run initialize and write profiles to disk,
// respectively.
PersistentPreRunE
:
func
(
*
cobra
.
Command
,
[]
string
)
error
{
return
initProfiling
()
},
PersistentPostRunE
:
func
(
*
cobra
.
Command
,
[]
string
)
error
{
return
flushProfiling
()
},
BashCompletionFunction
:
bashCompletionFunc
,
BashCompletionFunction
:
bashCompletionFunc
,
}
}
...
@@ -380,6 +388,8 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
...
@@ -380,6 +388,8 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
// a.k.a. change all "_" to "-". e.g. glog package
// a.k.a. change all "_" to "-". e.g. glog package
flags
.
SetNormalizeFunc
(
utilflag
.
WordSepNormalizeFunc
)
flags
.
SetNormalizeFunc
(
utilflag
.
WordSepNormalizeFunc
)
addProfilingFlags
(
flags
)
kubeConfigFlags
:=
genericclioptions
.
NewConfigFlags
()
kubeConfigFlags
:=
genericclioptions
.
NewConfigFlags
()
kubeConfigFlags
.
AddFlags
(
flags
)
kubeConfigFlags
.
AddFlags
(
flags
)
matchVersionKubeConfigFlags
:=
cmdutil
.
NewMatchVersionFlags
(
kubeConfigFlags
)
matchVersionKubeConfigFlags
:=
cmdutil
.
NewMatchVersionFlags
(
kubeConfigFlags
)
...
...
pkg/kubectl/cmd/profiling.go
0 → 100644
View file @
cc04abca
/*
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
cmd
import
(
"fmt"
"os"
"runtime"
"runtime/pprof"
"github.com/spf13/pflag"
)
var
(
profileName
string
profileOutput
string
)
func
addProfilingFlags
(
flags
*
pflag
.
FlagSet
)
{
flags
.
StringVar
(
&
profileName
,
"profile"
,
"none"
,
"Name of profile to capture. One of (none|cpu|heap|goroutine|threadcreate|block|mutex)"
)
flags
.
StringVar
(
&
profileOutput
,
"profile-output"
,
"profile.pprof"
,
"Name of the file to write the profile to"
)
}
func
initProfiling
()
error
{
switch
profileName
{
case
"none"
:
return
nil
case
"cpu"
:
f
,
err
:=
os
.
Create
(
profileOutput
)
if
err
!=
nil
{
return
err
}
return
pprof
.
StartCPUProfile
(
f
)
// Block and mutex profiles need a call to Set{Block,Mutex}ProfileRate to
// output anything. We choose to sample all events.
case
"block"
:
runtime
.
SetBlockProfileRate
(
1
)
return
nil
case
"mutex"
:
runtime
.
SetMutexProfileFraction
(
1
)
return
nil
default
:
// Check the profile name is valid.
if
profile
:=
pprof
.
Lookup
(
profileName
);
profile
==
nil
{
return
fmt
.
Errorf
(
"unknown profile '%s'"
,
profileName
)
}
}
return
nil
}
func
flushProfiling
()
error
{
switch
profileName
{
case
"none"
:
return
nil
case
"cpu"
:
pprof
.
StopCPUProfile
()
case
"heap"
:
runtime
.
GC
()
fallthrough
default
:
profile
:=
pprof
.
Lookup
(
profileName
)
if
profile
==
nil
{
return
nil
}
f
,
err
:=
os
.
Create
(
profileOutput
)
if
err
!=
nil
{
return
err
}
profile
.
WriteTo
(
f
,
0
)
}
return
nil
}
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