Skip to content

Commit

Permalink
Initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaynard committed Jun 5, 2020
1 parent a0c0a5e commit bd58f4a
Show file tree
Hide file tree
Showing 13 changed files with 1,013 additions and 2 deletions.
91 changes: 89 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

# Created by https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,windows,linux
# Edit at https://www.toptal.com/developers/gitignore?templates=go,visualstudiocode,macos,windows,linux

### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
Expand All @@ -11,5 +16,87 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
### Go Patch ###
/vendor/
/Godeps/

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,windows,linux
32 changes: 32 additions & 0 deletions .vscode/launch.json
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"
]
}
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnPaste": true,
"editor.formatOnSave": true
}
66 changes: 66 additions & 0 deletions cmd/configmap.go
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
}
92 changes: 92 additions & 0 deletions cmd/root.go
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())
}
}
66 changes: 66 additions & 0 deletions cmd/secret.go
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
}
15 changes: 15 additions & 0 deletions go.mod
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
)
Loading

0 comments on commit bd58f4a

Please sign in to comment.