Skip to content

Commit

Permalink
Watch changes to ConfigMaps and Secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaynard committed Jul 8, 2020
1 parent 3f15b8d commit 9550bd0
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 19 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/build.yml → .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
name: Build Binary
name: Build Binary + Unit Tests
on: [push]

jobs:
build:
name: Build
runs-on: ubuntu-latest
#strategy:
# matrix:
# build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/386, darwin/amd64
# goos: [linux, windows, darwin]
# goarch: ["386", amd64]
strategy:
matrix:
goos: [linux, windows, darwin]
goarch: ["386", amd64]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
Expand All @@ -23,14 +22,16 @@ jobs:
- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: go build -v .

env:
GOARCH: ${{ matrix.goarch }}
GOOS: ${{ matrix.goos }}
tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: engineerd/setup-kind@v0.4.0
- name: Testing
run: |
Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ func NewKubevolApp() *cobra.Command {
initConfig()

factory := core.NewDepsFactory()
coreClient, err := factory.CoreClient(viper.GetString("kubeconfig"))
coreClient, err := factory.CoreClient()

if err != nil {
panic(err.Error())
factory.Logger.Fatal(err)
}

kubeData := core.NewKubeData(coreClient)

rootCmd.AddCommand(NewConfigMapCommand(*kubeData))
rootCmd.AddCommand(NewSecretCommand(*kubeData))
rootCmd.AddCommand(NewWatchConfigmapCommand(*factory))
rootCmd.AddCommand(NewWatchSecretCommand(*factory))

return rootCmd
}
Expand Down
56 changes: 56 additions & 0 deletions cmd/watch_configmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"github.com/bmaynard/kubevol/pkg/core"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"

w "github.com/bmaynard/kubevol/pkg/watch"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func NewWatchConfigmapCommand(f core.Factory) *cobra.Command {
var cmd = &cobra.Command{
Use: "watch-configmap",
Short: "Watch for updates to ConfigMaps",
RunE: func(cmd *cobra.Command, args []string) error {
clientset, err := f.CoreClient()

if err != nil {
f.Logger.Fatal(err)
}

informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return clientset.CoreV1().ConfigMaps("").List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return clientset.CoreV1().ConfigMaps("").Watch(options)
},
},
&apiv1.ConfigMap{},
0, //Skip resync
cache.Indexers{},
)

watcher := w.NewWatch(&f)

informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: watcher.UpateConfigMapTracker,
DeleteFunc: watcher.DeleteConfigMapTracker,
})

stopCh := make(chan struct{})
defer close(stopCh)
informer.Run(stopCh)
return nil
},
}

return cmd
}
56 changes: 56 additions & 0 deletions cmd/watch_secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"github.com/bmaynard/kubevol/pkg/core"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"

w "github.com/bmaynard/kubevol/pkg/watch"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func NewWatchSecretCommand(f core.Factory) *cobra.Command {
var cmd = &cobra.Command{
Use: "watch-secret",
Short: "Watch for updates to Secrets",
RunE: func(cmd *cobra.Command, args []string) error {
clientset, err := f.CoreClient()

if err != nil {
f.Logger.Fatal(err)
}

informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return clientset.CoreV1().Secrets("").List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return clientset.CoreV1().Secrets("").Watch(options)
},
},
&apiv1.Secret{},
0, //Skip resync
cache.Indexers{},
)

watcher := w.NewWatch(&f)

informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: watcher.UpateSecretTracker,
DeleteFunc: watcher.DeleteSecretTracker,
})

stopCh := make(chan struct{})
defer close(stopCh)
informer.Run(stopCh)
return nil
},
}

return cmd
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/jedib0t/go-pretty/v6 v6.0.3
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.6.0
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
Expand Down
43 changes: 39 additions & 4 deletions pkg/core/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,53 @@ import (
"os"
"path/filepath"

"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

type Factory struct {
Logger *Logger
kubeclient kubernetes.Interface
}

func NewDepsFactory() *Factory {
return &Factory{}
return &Factory{
Logger: NewLogger(),
}
}

func (f *Factory) CoreClient() (kubernetes.Interface, error) {
if f.kubeclient == nil {
clientset, err := f.getKubernetesConfigClient(viper.GetString("kubeconfig"))

if err != nil {
return nil, err
}

f.kubeclient = clientset
}

return f.kubeclient, nil
}

func (f *Factory) getKubeconfigRESTClient(kubeconfig string) (kubernetes.Interface, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("Building Core clientset: %s", err)

}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("Building Core clientset: %s", err)
}

return clientset, nil
}

func (f *Factory) CoreClient(kubeconfig string) (kubernetes.Interface, error) {
func (f *Factory) getKubernetesConfigClient(kubeconfig string) (kubernetes.Interface, error) {

if kubeconfig == "" {
kubeconfig = filepath.Join(homeDir(), ".kube", "config")
Expand All @@ -25,13 +60,13 @@ func (f *Factory) CoreClient(kubeconfig string) (kubernetes.Interface, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)

if err != nil {
return nil, fmt.Errorf("Building Core clientset: %s", err)
return f.getKubeconfigRESTClient("")
}

clientset, err := kubernetes.NewForConfig(config)

if err != nil {
return nil, fmt.Errorf("Building Core clientset: %s", err)
return f.getKubeconfigRESTClient("")
}

return clientset, nil
Expand Down
72 changes: 72 additions & 0 deletions pkg/core/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package core

import (
"os"

"github.com/sirupsen/logrus"
)

type Logger struct {
logger *logrus.Logger
}

func NewLogger() *Logger {
return &Logger{
logger: &logrus.Logger{
Out: os.Stdout,
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
DisableLevelTruncation: true,
},
Level: logrus.DebugLevel,
},
}
}

func (l *Logger) Debugf(format string, args ...interface{}) {
l.logger.Debugf(format, args...)
}

func (l *Logger) Infof(format string, args ...interface{}) {
l.logger.Infof(format, args...)
}

func (l *Logger) Warnf(format string, args ...interface{}) {
l.logger.Warnf(format, args...)
}

func (l *Logger) Errorf(format string, args ...interface{}) {
l.logger.Errorf(format, args...)
}

func (l *Logger) Fatalf(format string, args ...interface{}) {
l.logger.Fatalf(format, args...)
}

func (l *Logger) Panicf(format string, args ...interface{}) {
l.logger.Fatalf(format, args...)
}

func (l *Logger) Debug(args ...interface{}) {
l.logger.Debug(args...)
}

func (l *Logger) Info(args ...interface{}) {
l.logger.Info(args...)
}

func (l *Logger) Warn(args ...interface{}) {
l.logger.Warn(args...)
}

func (l *Logger) Error(args ...interface{}) {
l.logger.Error(args...)
}

func (l *Logger) Fatal(args ...interface{}) {
l.logger.Fatal(args...)
}

func (l *Logger) Panic(args ...interface{}) {
l.logger.Fatal(args...)
}
Loading

0 comments on commit 9550bd0

Please sign in to comment.