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

Add flags for user and group impersonation #109

Merged
merged 1 commit into from
Jan 22, 2024
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ kube-capacity --pods --containers --util --output yaml

## Flags Supported
```
--as string user to impersonate command with
--as-group string group to impersonate command with
-c, --containers includes containers in output
--context string context to use for Kubernetes config
-h, --help help for kube-capacity
Expand Down
4 changes: 2 additions & 2 deletions pkg/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
)

// FetchAndPrint gathers cluster resource data and outputs it
func FetchAndPrint(showContainers, showPods, showUtil, showPodCount, excludeTainted, availableFormat bool, podLabels, nodeLabels, namespaceLabels, namespace, kubeContext, kubeConfig, output, sortBy string) {
clientset, err := kube.NewClientSet(kubeContext, kubeConfig)
func FetchAndPrint(showContainers, showPods, showUtil, showPodCount, excludeTainted, availableFormat bool, podLabels, nodeLabels, namespaceLabels, namespace, kubeContext, kubeConfig, impersonateUser string, impersonateGroup string, output, sortBy string) {
clientset, err := kube.NewClientSet(kubeContext, kubeConfig, impersonateUser, impersonateGroup)
if err != nil {
fmt.Printf("Error connecting to Kubernetes: %v\n", err)
os.Exit(1)
Expand Down
9 changes: 7 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var kubeConfig string
var outputFormat string
var sortBy string
var availableFormat bool
var impersonateUser string
var impersonateGroup string

var rootCmd = &cobra.Command{
Use: "kube-capacity",
Expand All @@ -52,7 +54,7 @@ var rootCmd = &cobra.Command{
}

capacity.FetchAndPrint(showContainers, showPods, showUtil, showPodCount, excludeTainted, availableFormat, podLabels,
nodeLabels, namespaceLabels, namespace, kubeContext, kubeConfig, outputFormat, sortBy)
nodeLabels, namespaceLabels, namespace, kubeContext, kubeConfig, impersonateUser, impersonateGroup, outputFormat, sortBy)
},
}

Expand Down Expand Up @@ -84,10 +86,13 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&sortBy,
"sort", "", "name",
fmt.Sprintf("attribute to sort results by (supports: %v)", capacity.SupportedSortAttributes))

rootCmd.PersistentFlags().StringVarP(&outputFormat,
"output", "o", capacity.TableOutput,
fmt.Sprintf("output format for information (supports: %v)", capacity.SupportedOutputs()))
rootCmd.PersistentFlags().StringVarP(&impersonateUser,
"as", "", "", "user to impersonate kube-capacity with")
rootCmd.PersistentFlags().StringVarP(&impersonateGroup,
"as-group", "", "", "group to impersonate kube-capacity with")
}

// Execute is the primary entrypoint for this CLI
Expand Down
12 changes: 11 additions & 1 deletion pkg/kube/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ import (
)

// NewClientSet returns a new Kubernetes clientset
func NewClientSet(kubeContext, kubeConfig string) (*kubernetes.Clientset, error) {
func NewClientSet(kubeContext, kubeConfig string, impersonateUser string, impersonateGroup string) (*kubernetes.Clientset, error) {
config, err := getKubeConfig(kubeContext, kubeConfig)
if err != nil {
return nil, err
}

if impersonateUser != "" || impersonateGroup != "" {
config.Impersonate = rest.ImpersonationConfig{}
if impersonateUser != "" {
config.Impersonate.UserName = impersonateUser
}
if impersonateGroup != "" {
config.Impersonate.Groups = []string{impersonateGroup}
}
}

return kubernetes.NewForConfig(config)
}

Expand Down
Loading