-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,013 additions
and
2 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
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,32 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "ConfigMap", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "${workspaceFolder}/main.go", | ||
"env": {}, | ||
"args": [ | ||
"configmap", | ||
"--name", | ||
"redis-config-ver-1" | ||
] | ||
}, | ||
{ | ||
"name": "Secret", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "${workspaceFolder}/main.go", | ||
"env": {}, | ||
"args": [ | ||
"secret" | ||
] | ||
} | ||
] | ||
} |
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,4 @@ | ||
{ | ||
"editor.formatOnPaste": true, | ||
"editor.formatOnSave": true | ||
} |
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,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/bmaynard/kubevol/pkg/core" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/jedib0t/go-pretty/v6/table" | ||
) | ||
|
||
func NewConfigMapCommand(k core.KubeData) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "configmap", | ||
Short: "Find all pods that have a specific ConfigMap attached", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
pods := k.GetPods() | ||
fmt.Printf(color.GreenString("There are %d pods in the cluster\n", len(pods.Items))) | ||
|
||
if name == "" { | ||
fmt.Printf(color.GreenString("Searching for pods that have a ConfigMap attached\n\n")) | ||
} else { | ||
fmt.Printf(color.GreenString("Searching for pods that have \"%s\" ConfigMap attached\n\n", name)) | ||
} | ||
|
||
t := table.NewWriter() | ||
t.SetOutputMirror(os.Stdout) | ||
t.AppendHeader(table.Row{"Namespace", "Pod Name", "ConfigMap Name", "Volume Name", "Out of Date"}) | ||
|
||
for _, pod := range pods.Items { | ||
podName := pod.ObjectMeta.Name | ||
namespace := pod.ObjectMeta.Namespace | ||
_, err := k.GetPod(podName, namespace) | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
podCreationTime := pod.ObjectMeta.CreationTimestamp.Time | ||
|
||
for _, volume := range pod.Spec.Volumes { | ||
if volume.ConfigMap != nil { | ||
if name == "" || (volume.ConfigMap != nil && volume.ConfigMap.LocalObjectReference.Name == name) { | ||
configMap := k.GetConfigMap(volume.ConfigMap.LocalObjectReference.Name, namespace) | ||
outOfDate := color.YellowString("Unknown") | ||
|
||
if configMap.ObjectMeta.CreationTimestamp.Time.After(podCreationTime) { | ||
outOfDate = color.RedString("Yes") | ||
} | ||
|
||
t.AppendRows([]table.Row{ | ||
{color.BlueString(namespace), podName, volume.ConfigMap.LocalObjectReference.Name, volume.Name, outOfDate}, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
t.Render() | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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,92 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/bmaynard/kubevol/pkg/core" | ||
"github.com/spf13/cobra" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
var name string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "kubevol", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kubevol.yaml)") | ||
rootCmd.PersistentFlags().StringVar(&name, "name", "", "Name of the object you wish to filter by") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
|
||
factory := core.NewDepsFactory() | ||
coreClient, err := factory.CoreClient() | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
kubeData := core.NewKubeData("", coreClient) | ||
|
||
rootCmd.AddCommand(NewConfigMapCommand(*kubeData)) | ||
rootCmd.AddCommand(NewSecretCommand(*kubeData)) | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Search config in home directory with name ".kubevol" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigName(".kubevol") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
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,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/bmaynard/kubevol/pkg/core" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/jedib0t/go-pretty/v6/table" | ||
) | ||
|
||
func NewSecretCommand(k core.KubeData) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "secret", | ||
Short: "Find all pods that have a specific Secret attached", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
pods := k.GetPods() | ||
fmt.Printf(color.GreenString("There are %d pods in the cluster\n", len(pods.Items))) | ||
|
||
if name == "" { | ||
fmt.Printf(color.GreenString("Searching for pods that have a Secret attached\n\n")) | ||
} else { | ||
fmt.Printf(color.GreenString("Searching for pods that have \"%s\" Secret attached\n\n", name)) | ||
} | ||
|
||
t := table.NewWriter() | ||
t.SetOutputMirror(os.Stdout) | ||
t.AppendHeader(table.Row{"Namespace", "Pod Name", "Secret Name", "Volume Name", "Out of Date"}) | ||
|
||
for _, pod := range pods.Items { | ||
podName := pod.ObjectMeta.Name | ||
namespace := pod.ObjectMeta.Namespace | ||
_, err := k.GetPod(podName, namespace) | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
podCreationTime := pod.ObjectMeta.CreationTimestamp.Time | ||
|
||
for _, volume := range pod.Spec.Volumes { | ||
if volume.Secret != nil { | ||
if name == "" || (volume.Secret != nil && volume.Secret.SecretName == name) { | ||
configMap := k.GetSecret(volume.Secret.SecretName, namespace) | ||
outOfDate := color.YellowString("Unknown") | ||
|
||
if configMap.ObjectMeta.CreationTimestamp.Time.After(podCreationTime) { | ||
outOfDate = color.RedString("Yes") | ||
} | ||
|
||
t.AppendRows([]table.Row{ | ||
{color.BlueString(namespace), podName, volume.Secret.SecretName, volume.Name, outOfDate}, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
t.Render() | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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,15 @@ | ||
module github.com/bmaynard/kubevol | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/fatih/color v1.9.0 | ||
github.com/go-delve/delve v1.4.1 // indirect | ||
github.com/jedib0t/go-pretty/v6 v6.0.3 | ||
github.com/mitchellh/go-homedir v1.1.0 | ||
github.com/spf13/cobra v1.0.0 | ||
github.com/spf13/viper v1.7.0 | ||
k8s.io/api v0.17.0 | ||
k8s.io/apimachinery v0.17.0 | ||
k8s.io/client-go v0.17.0 | ||
) |
Oops, something went wrong.