Skip to content

Commit

Permalink
Add version info
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinvaneyk committed Jan 17, 2020
1 parent 5d2795e commit 7186ffa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
14 changes: 13 additions & 1 deletion cmd/kubectl-tap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ package main

import (
"github.com/erwinvaneyk/kubectl-tap/pkg/cmd"
versionpkg "github.com/erwinvaneyk/kubectl-tap/pkg/version"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
)

// Variables filled with ldflags:
var (
version string
commit string
date string
)

func main() {
flags := pflag.NewFlagSet("kubectl-tap", pflag.ExitOnError)
pflag.CommandLine = flags

root := cmd.NewCmdTap(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
root := cmd.NewCmdTap(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}, versionpkg.Info{
Version: version,
Commit: commit,
BuildDate: date,
})
if err := root.Execute(); err != nil {
os.Exit(1)
}
Expand Down
26 changes: 7 additions & 19 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/erwinvaneyk/kubectl-tap/pkg/version"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -23,8 +24,6 @@ var (
defaultTapKey = "tapped"
)

// TapOptions provides information required to update
// the current context on a user's KUBECONFIG
type TapOptions struct {
genericclioptions.IOStreams
configFlags *genericclioptions.ConfigFlags
Expand All @@ -48,12 +47,12 @@ func NewTapOptions(streams genericclioptions.IOStreams) *TapOptions {
}

// NewCmdTap provides a cobra command wrapping TapOptions
func NewCmdTap(streams genericclioptions.IOStreams) *cobra.Command {
func NewCmdTap(streams genericclioptions.IOStreams, version version.Info) *cobra.Command {
o := NewTapOptions(streams)

cmd := &cobra.Command{
Use: "tap TYPE[.VERSION][.GROUP]/NAME [flags]",
Short: "Trigger immediate reevaluation of a resource.",
Short: "Trigger watching controller to reevaluate the resource(s).",
Example: fmt.Sprintf(tapExample, "kubectl"),
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
Expand All @@ -69,9 +68,10 @@ func NewCmdTap(streams genericclioptions.IOStreams) *cobra.Command {

return nil
},
Version: version.String(),
}

cmd.Flags().StringVar(&o.tapKey, "key", o.tapKey, "The annotation key to use to update the resource.")
cmd.Flags().StringVar(&o.tapKey, "key", o.tapKey, "The key of annotation to update for the resource.")
cmd.Flags().StringVarP(&o.labelSelector, "selector", "l", o.labelSelector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
cmd.Flags().BoolVar(&o.allResources, "all", o.allResources, "Tap all resources in the namespace of the specified resource types.")

Expand Down Expand Up @@ -99,8 +99,6 @@ func (o *TapOptions) Validate() error {
return nil
}

// Run lists all available namespaces on a user's KUBECONFIG or updates the
// current context based on a provided namespace.
func (o *TapOptions) Run() error {
restCfg, err := o.configFlags.ToRESTConfig()
if err != nil {
Expand All @@ -124,26 +122,16 @@ func (o *TapOptions) Run() error {
Flatten().
Do()

// Convert to unstructured object.
obj, err := resp.Object()
if err != nil {
return err
}
unstructuredObj := &unstructured.Unstructured{}
unstructuredObj.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return err
}

return resp.Visit(func(info *resource.Info, err error) error {
unstructuredObj := &unstructured.Unstructured{}
unstructuredObj.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(info.Object)
if err != nil {
return err
}
// Update annotations

updatedObj := o.tap(unstructuredObj)

// Update the tapped object
updateResp, err := dynamicClient.Resource(info.ResourceMapping().Resource).
Namespace(o.targetNamespace).
Update(updatedObj, metav1.UpdateOptions{})
Expand Down
18 changes: 18 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package version

import "encoding/json"

type Info struct {
Version string `json:"version"`
Commit string `json:"commit"`
BuildDate string `json:"buildDate"`
}

func (i Info) String() string {
bs, err := json.Marshal(i)
if err != nil {
panic(err)
}

return string(bs)
}

0 comments on commit 7186ffa

Please sign in to comment.