Skip to content

Commit

Permalink
refactor: rename plugin to vpa-recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Jan 25, 2022
1 parent 5635670 commit 1f46b6d
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ coverage.txt

# Build
dist/
kubectl-commitment
./kubectl-vpa-recommendation
21 changes: 11 additions & 10 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project_name: kubectl-commitment
project_name: kubectl-vpa-recommendation
release:
prerelease: auto
draft: true
Expand All @@ -7,8 +7,8 @@ before:
- go mod tidy
- go mod download
builds:
- binary: kubectl-commitment
main: ./cmd/kubectl-commitment
- binary: kubectl-vpa-recommendation
main: ./cmd/kubectl-vpa-recommendation
goos:
- darwin
- linux
Expand All @@ -26,14 +26,14 @@ builds:
ldflags:
- -s
- -w
- -X github.com/wI2L/kubectl-commitment/internal/version.buildDate={{ .Date }}
- -X github.com/wI2L/kubectl-commitment/internal/version.gitCommit={{ .FullCommit }}
- -X github.com/wI2L/kubectl-commitment/internal/version.gitVersion={{ .Version }}
- -X github.com/wI2L/kubectl-commitment/internal/version.gitTreeState={{ .Env.GIT_TREE_STATE }}
- -X github.com/wI2L/kubectl-commitment/internal/version.gitVersionMajor={{ .Major }}
- -X github.com/wI2L/kubectl-commitment/internal/version.gitVersionMinor={{ .Minor }}
- -X github.com/wI2L/kubectl-vpa-recommendation/internal/version.buildDate={{ .Date }}
- -X github.com/wI2L/kubectl-vpa-recommendation/internal/version.gitCommit={{ .FullCommit }}
- -X github.com/wI2L/kubectl-vpa-recommendation/internal/version.gitVersion={{ .Version }}
- -X github.com/wI2L/kubectl-vpa-recommendation/internal/version.gitTreeState={{ .Env.GIT_TREE_STATE }}
- -X github.com/wI2L/kubectl-vpa-recommendation/internal/version.gitVersionMajor={{ .Major }}
- -X github.com/wI2L/kubectl-vpa-recommendation/internal/version.gitVersionMinor={{ .Minor }}
archives:
- name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
- name_template: "{{ .ProjectName }}_{{ .Tag }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
files:
- README.md
- LICENSE
Expand All @@ -42,6 +42,7 @@ archives:
format: zip
checksum:
name_template: "{{ .ProjectName }}_checksums.txt"
algorithm: sha256
snapshot:
name_template: "{{ .Tag }}-snapshot"
changelog:
Expand Down
78 changes: 45 additions & 33 deletions .krew.yaml
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: commitment
name: vpa-recommendation
spec:
version: {{ .TagName }}
homepage: https://github.com/wI2L/kubectl-commitment
shortDescription: Compare VerticalPodAutoscaler recommendations to actual resources requests
homepage: https://github.com/wI2L/kubectl-vpa-recommendation
shortDescription: Compare VPA recommendations to actual resources requests
description: |
This plugin prints a table that show the percentage increase/decrease
of the selected VerticalPodAutoscaler recommendations compared to the
targeted controller's pod resource requests.
caveats: |
The plugin recognizes only some well-known controllers such as:
- CronJob
- DaemonSet
- Deployment
- Job
- ReplicaSet
- ReplicationController
- StatefulSet
The autoscaling.k8s.io/v1 API is required for the plugin to work:
https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler
platforms:
- selector:
matchLabels:
os: darwin
arch: amd64
{{addURIAndSha "https://github.com/wI2L/kubectl-commitment/releases/download/{{ .TagName }}/kubectl-commitment_darwin_amd64.tar.gz" .TagName }}
bin: kubectl-commitment
- selector:
matchLabels:
os: darwin
arch: arm64
{{addURIAndSha "https://github.com/wI2L/kubectl-commitment/releases/download/{{ .TagName }}/kubectl-commitment_darwin_arm64.tar.gz" .TagName }}
bin: kubectl-commitment
- selector:
matchLabels:
os: linux
arch: amd64
{{addURIAndSha "https://github.com/wI2L/kubectl-commitment/releases/download/{{ .TagName }}/kubectl-commitment_linux_amd64.tar.gz" .TagName }}
bin: kubectl-commitment
- selector:
matchLabels:
os: linux
arch: arm64
{{addURIAndSha "https://github.com/wI2L/kubectl-commitment/releases/download/{{ .TagName }}/kubectl-commitment_linux_arm64.tar.gz" .TagName }}
bin: kubectl-commitment
- selector:
matchLabels:
os: windows
arch: amd64
{{addURIAndSha "https://github.com/wI2L/kubectl-commitment/releases/download/{{ .TagName }}/kubectl-commitment_windows_amd64.zip" .TagName }}
bin: kubectl-commitment.exe
- selector:
matchLabels:
os: darwin
arch: amd64
{{addURIAndSha "https://github.com/wI2L/kubectl-vpa-recommendation/releases/download/{{ .TagName }}/kubectl-vpa-recommendation_{{ .TagName }}_darwin_amd64.tar.gz" .TagName }}
bin: kubectl-vpa-recommendation
- selector:
matchLabels:
os: darwin
arch: arm64
{{addURIAndSha "https://github.com/wI2L/kubectl-vpa-recommendation/releases/download/{{ .TagName }}/kubectl-vpa-recommendation_{{ .TagName }}_darwin_arm64.tar.gz" .TagName }}
bin: kubectl-vpa-recommendation
- selector:
matchLabels:
os: linux
arch: amd64
{{addURIAndSha "https://github.com/wI2L/kubectl-vpa-recommendation/releases/download/{{ .TagName }}/kubectl-vpa-recommendation_{{ .TagName }}_linux_amd64.tar.gz" .TagName }}
bin: kubectl-vpa-recommendation
- selector:
matchLabels:
os: linux
arch: arm64
{{addURIAndSha "https://github.com/wI2L/kubectl-vpa-recommendation/releases/download/{{ .TagName }}/kubectl-vpa-recommendation_{{ .TagName }}_linux_arm64.tar.gz" .TagName }}
bin: kubectl-vpa-recommendation
- selector:
matchLabels:
os: windows
arch: amd64
{{addURIAndSha "https://github.com/wI2L/kubectl-vpa-recommendation/releases/download/{{ .TagName }}/kubectl-vpa-recommendation_{{ .TagName }}_windows_amd64.zip" .TagName }}
bin: kubectl-vpa-recommendation.exe
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# kubectl commitment
# `kubectl vpa-recommendation`

<p align="left">
<a href="https://github.com/wI2L/kubectl-commitment/actions"><img src="https://github.com/wI2L/kubectl-commitment/workflows/ci/badge.svg"></a>
<a href="https://goreportcard.com/report/github.com/wI2L/kubectl-commitment"><img src="https://goreportcard.com/badge/github.com/wI2L/kubectl-commitment"></a>
<a href="https://github.com/wI2L/kubectl-commitment/releases"><img src="https://img.shields.io/github/v/tag/wI2L/kubectl-commitment?color=blueviolet&label=release&sort=semver"></a>
<img src="https://codecov.io/gh/wI2L/kubectl-commitment/branch/master/graph/badge.svg?token=UTD8CW2ZS2"/>
<a href="https://github.com/wI2L/kubectl-vpa-recommendation/actions"><img src="https://github.com/wI2L/kubectl-vpa-recommendation/workflows/ci/badge.svg"></a>
<a href="https://goreportcard.com/report/github.com/wI2L/kubectl-vpa-recommendation"><img src="https://goreportcard.com/badge/github.com/wI2L/kubectl-vpa-recommendation"></a>
<a href="https://github.com/wI2L/kubectl-vpa-recommendation/releases"><img src="https://img.shields.io/github/v/tag/wI2L/kubectl-vpa-recommendation?color=blueviolet&label=release&sort=semver"></a>
<img src="https://codecov.io/gh/wI2L/kubectl-vpa-recommendation/branch/master/graph/badge.svg?token=UTD8CW2ZS2"/>
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue.svg"></a>
</p>

Expand All @@ -14,23 +14,30 @@ The plugin is compatible with Kubernetes servers starting from version 1.16, whi

## Installation

### With [Krew](https://krew.sigs.k8s.io/)

```shell
$ kubectl krew install vpa-recommendation
$ kubectl vpa-recommendation --help
```

### Manual download

Head to the [releases](https://github.com/wI2L/kubectl-commitment/releases) page to download an existing release for your OS/arch, and add the path to the binary to your `$PATH`.
Head to the [releases](https://github.com/wI2L/kubectl-vpa-recommendation/releases) page to download an existing release for your OS/arch, and add the path to the binary to your `$PATH`.

### Build from source

:warning: `kubectl-commitment` requires [Go 1.17+](https://golang.org/doc/install) to build.
:warning: `kubectl-vpa-recommendation` requires [Go 1.17+](https://golang.org/doc/install) to build.

To build the plugin yourself from the source, use the following commands:

```shell
$ git clone [email protected]:wI2L/kubectl-commitment.git
$ cd kubectl-commitment
$ git clone [email protected]:wI2L/kubectl-vpa-recommendation.git
$ cd kubectl-vpa-recommendation
$ make build
```

It creates a single binary file for the host machine platform/architecture in the `dist/` folder. Move it somewhere within your `$PATH`, such as `$GOPATH/bin`. You can then invoke it directly (`kubectl-commitment`), or via `kubectl` (`kubectl commitment`).
It creates a single binary file for the host machine platform/architecture in the `dist/` folder. Move it somewhere within your `$PATH`, such as `$GOPATH/bin`. You can then invoke it directly (`kubectl-vpa-recommendation`), or via `kubectl` (`kubectl vpa-recommendation`).

## Usage

Expand Down Expand Up @@ -63,12 +70,12 @@ Apart from the flags defined by the [`genericclioptions`](https://pkg.go.dev/k8s
To view the full list of available options, use the following command:

```shell
$ kubectl commitment --help
$ kubectl vpa-recommendation --help
```

## Limitations

- Unlike the [official VPA recommender](https://github.com/kubernetes/autoscaler/blob/master/vertical-pod-autoscaler/pkg/recommender/README.md), which is fully generic and handle any kind of "scalable" resources, the plugin only recognize the *well-known* controllers such as: `CronJob`, `DaemonSet`, `Deployment`, `Job`, `ReplicaSet`, `ReplicationController`, `StatefulSet`.
- Unlike the [official VPA recommender](https://github.com/kubernetes/autoscaler/blob/master/vertical-pod-autoscaler/pkg/recommender/README.md), which is fully generic and handle any kind of "scalable" resources, the plugin recognize only some *well-known* controllers such as: `CronJob`, `DaemonSet`, `Deployment`, `Job`, `ReplicaSet`, `ReplicationController`, `StatefulSet`.

## License

Expand Down
Binary file modified assets/example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/example2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"k8s.io/kubectl/pkg/util/templates"
"k8s.io/utils/pointer"

"github.com/wI2L/kubectl-commitment/client"
"github.com/wI2L/kubectl-commitment/internal/version"
"github.com/wI2L/kubectl-commitment/vpa"
"github.com/wI2L/kubectl-vpa-recommendation/client"
"github.com/wI2L/kubectl-vpa-recommendation/internal/version"
"github.com/wI2L/kubectl-vpa-recommendation/vpa"
)

const (
Expand Down Expand Up @@ -218,7 +218,7 @@ func (co *CommandOptions) bindRecommendationsAndRequests(list []*vpav1.VerticalP
childRow := &tableRow{
Name: fmt.Sprintf("%s %s", prefix, c.ContainerName),
Requests: rqs,
Recommendations: &rcs,
Recommendations: rcs,
CPUDifference: vpa.DiffQuantitiesAsPercent(rqs.CPU, rcs.CPU),
MemoryDifference: vpa.DiffQuantitiesAsPercent(rqs.Memory, rcs.Memory),
}
Expand Down
6 changes: 3 additions & 3 deletions cli/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/wI2L/kubectl-commitment/vpa"
"github.com/wI2L/kubectl-vpa-recommendation/vpa"
)

const (
Expand Down Expand Up @@ -54,8 +54,8 @@ type tableRow struct {
Mode string
TargetName string
TargetGVK schema.GroupVersionKind
Requests *vpa.ResourceQuantities
Recommendations *vpa.ResourceQuantities
Requests vpa.ResourceQuantities
Recommendations vpa.ResourceQuantities
CPUDifference *float64
MemoryDifference *float64
Children []*tableRow
Expand Down
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Interface interface {
HasGroupVersion(version schema.GroupVersion) (bool, error)
ListVPAResources(context.Context, ListOptions) ([]*vpav1.VerticalPodAutoscaler, error)
GetVPATarget(context.Context, *autoscalingv1.CrossVersionObjectReference, string) (*unstructuredv1.Unstructured, error)
ListDependentPods(ctx context.Context, targetMeta metav1.ObjectMeta) ([]*corev1.Pod, error)
ListDependentPods(ctx context.Context, targetMeta metav1.ObjectMeta, labelSelector string) ([]*corev1.Pod, error)
}

var _ Interface = (*client)(nil)
Expand Down Expand Up @@ -191,14 +191,15 @@ func (c *client) GetVPATarget(ctx context.Context, ref *autoscalingv1.CrossVersi

// ListDependentPods returns the list of pods that depends
// on the controller represented by its metadata.
func (c *client) ListDependentPods(ctx context.Context, targetMeta metav1.ObjectMeta) ([]*corev1.Pod, error) {
func (c *client) ListDependentPods(ctx context.Context, targetMeta metav1.ObjectMeta, labelSelector string) ([]*corev1.Pod, error) {
i := c.coreClient.Pods(targetMeta.Namespace)

p := pager.New(func(ctx context.Context, o metav1.ListOptions) (runtime.Object, error) {
return i.List(ctx, o)
})
obj, _, err := p.List(ctx, metav1.ListOptions{
Limit: 250,
LabelSelector: labelSelector,
Limit: 250,
})
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
// Initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/wI2L/kubectl-commitment/cli"
"github.com/wI2L/kubectl-vpa-recommendation/cli"
)

const pluginName = "kubectl-commitment"
const pluginName = "kubectl-vpa-recommendation"

func main() {
cmdName := pluginName
Expand All @@ -27,7 +27,7 @@ func main() {
// use a nicer format for the root command name in
// help outputs generated by cobra.
if strings.HasPrefix(filepath.Base(os.Args[0]), "kubectl-") {
cmdName = strings.ReplaceAll(cmdName, "-", "")
cmdName = strings.ReplaceAll(cmdName, "-", " ")
}
streams := genericclioptions.IOStreams{
In: os.Stdin,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/wI2L/kubectl-commitment
module github.com/wI2L/kubectl-vpa-recommendation

go 1.17

Expand Down
6 changes: 3 additions & 3 deletions vpa/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
// GetTotalRecommendations returns the total resource recommendations
// of the given VPA as the sum of all resource quantities recommended
// for each container.
func GetTotalRecommendations(vpa *vpav1.VerticalPodAutoscaler) *ResourceQuantities {
func GetTotalRecommendations(vpa *vpav1.VerticalPodAutoscaler) ResourceQuantities {
if vpa == nil || vpa.Status.Recommendation == nil {
return nil
return ResourceQuantities{}
}
var cpu, mem resource.Quantity
for _, cr := range vpa.Status.Recommendation.ContainerRecommendations {
Expand All @@ -21,7 +21,7 @@ func GetTotalRecommendations(vpa *vpav1.VerticalPodAutoscaler) *ResourceQuantiti
mem.Add(*m)
}
}
return &ResourceQuantities{
return ResourceQuantities{
CPU: &cpu,
Memory: &mem,
}
Expand Down
2 changes: 1 addition & 1 deletion vpa/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ResourceQuantities struct {
// two quantities expressed as a percentage of each other,
// where x is the request and y the recommendation.
func DiffQuantitiesAsPercent(x, y *resource.Quantity) *float64 {
if x.IsZero() || y.IsZero() {
if x == nil || y == nil || x.IsZero() || y.IsZero() {
return nil
}
ai, oka := x.AsInt64()
Expand Down
Loading

0 comments on commit 1f46b6d

Please sign in to comment.