-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jian Zeng <zengjian.zj@bytedance.com>
- Loading branch information
0 parents
commit 775e347
Showing
10 changed files
with
1,419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Ignore all | ||
* | ||
|
||
# Unignore all with extensions | ||
!*.* | ||
|
||
# Unignore all dirs | ||
!*/ | ||
!Dockerfile | ||
!Makefile | ||
|
||
### Above combination will ignore all files without extension ### | ||
|
||
.idea/ | ||
|
||
*.swp | ||
*.[oa] | ||
*~ | ||
*.bac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Jian Zeng | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/resource" | ||
) | ||
|
||
type Options struct { | ||
namespace string | ||
args []string | ||
builder *resource.Builder | ||
} | ||
|
||
var ( | ||
blameLong = `Annotate each line in the given resource's YAML with information from the managedFields | ||
to show who last modified the field. | ||
As long as the field '.metadata.manageFields' of the given resource is set properly, this command | ||
is able to display the manager of each field.` | ||
|
||
blameExample = ` | ||
# Blame pod 'foo' in default namespace | ||
kubectl blame pods foo | ||
# Blame deployment 'bar' in 'ns1' namespace | ||
kubectl blame -n ns1 deploy bar | ||
` | ||
) | ||
|
||
func CheckErr(err error) { | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func NewCmdBlame() *cobra.Command { | ||
o := Options{} | ||
f := genericclioptions.NewConfigFlags(true) | ||
cmd := &cobra.Command{ | ||
Use: "blame TYPE[.VERSION][.GROUP] NAME", | ||
DisableFlagsInUseLine: true, | ||
Short: "Show the manager of each field of given resource", | ||
Long: blameLong, | ||
Example: blameExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
CheckErr(o.Complete(f, cmd, args)) | ||
CheckErr(o.Validate()) | ||
CheckErr(o.Run()) | ||
}, | ||
} | ||
f.AddFlags(cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
func (o *Options) Complete(f *genericclioptions.ConfigFlags, cmd *cobra.Command, args []string) error { | ||
var err error | ||
o.namespace, _, err = f.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
o.args = args | ||
o.builder = resource.NewBuilder(f) | ||
return nil | ||
} | ||
|
||
func (o *Options) Validate() error { | ||
if len(o.args) != 2 { | ||
return fmt.Errorf("wrong number of arguments") | ||
} | ||
return nil | ||
} | ||
|
||
func (o *Options) Run() error { | ||
result := o.builder.Unstructured(). | ||
NamespaceParam(o.namespace).DefaultNamespace(). | ||
ResourceNames(o.args[0], o.args[1]).SingleResourceType(). | ||
Latest(). | ||
Do() | ||
if err := result.Err(); err != nil { | ||
return err | ||
} | ||
|
||
infos, err := result.Infos() | ||
if err != nil { | ||
return err | ||
} | ||
if len(infos) < 1 { | ||
return fmt.Errorf("not found") | ||
} | ||
|
||
info := infos[0] | ||
obj, ok := info.Object.(metav1.Object) | ||
if !ok { | ||
return fmt.Errorf("unsupported object: %v: %s/%s", info.Mapping.Resource, info.Namespace, info.Name) | ||
} | ||
|
||
data, err := MarshalMetaObject(obj) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = os.Stdout.Write(data) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cmd | ||
|
||
type Context struct { | ||
Level int | ||
NewLine bool | ||
Node *Node | ||
} | ||
|
||
func (c Context) WithLevel(lvl int) Context { | ||
c.Level = lvl | ||
return c | ||
} | ||
|
||
func (c Context) WithNewLine(newLine bool) Context { | ||
c.NewLine = newLine | ||
return c | ||
} | ||
|
||
func (c Context) WithNode(node *Node) Context { | ||
c.Node = node | ||
return c | ||
} |
Oops, something went wrong.