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

Extract http flags from consul/command pkg #259

Merged
merged 2 commits into from
Jul 7, 2020
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
16 changes: 15 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
module github.com/hashicorp/consul-k8s

require (
github.com/Azure/go-autorest v10.15.3+incompatible // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/cenkalti/backoff v2.1.1+incompatible
github.com/deckarep/golang-set v1.7.1
github.com/digitalocean/godo v1.10.0 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 // indirect
github.com/googleapis/gnostic v0.3.1 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/consul v1.7.1
github.com/hashicorp/consul/api v1.4.0
github.com/hashicorp/consul/sdk v0.4.0
github.com/hashicorp/go-discover v0.0.0-20191202160150-7ec2cfbda7a2
github.com/hashicorp/go-hclog v0.12.0
github.com/hashicorp/go-immutable-radix v1.1.0 // indirect
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/hashicorp/mdns v1.0.1 // indirect
github.com/hashicorp/memberlist v0.1.6 // indirect
github.com/hashicorp/serf v0.8.5 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/json-iterator/go v1.1.8 // indirect
github.com/kr/text v0.1.0
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a
github.com/miekg/dns v1.1.26 // indirect
github.com/mitchellh/cli v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/prometheus/common v0.0.0-20181126121408-4724e9255275
ishustava marked this conversation as resolved.
Show resolved Hide resolved
github.com/radovskyb/watcher v1.0.2
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20191106202628-ed6320f186d4 // indirect
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
Expand Down
129 changes: 0 additions & 129 deletions go.sum

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions subcommand/acl-init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"time"

"github.com/hashicorp/consul-k8s/subcommand"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/mitchellh/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -23,7 +22,7 @@ type Command struct {
UI cli.Ui

flags *flag.FlagSet
k8s *k8sflags.K8SFlags
k8s *flags.K8SFlags
flagSecretName string
flagInitType string
flagNamespace string
Expand All @@ -49,7 +48,7 @@ func (c *Command) init() {
c.flags.StringVar(&c.flagTokenSinkFile, "token-sink-file", "",
"Optional filepath to write acl token")

c.k8s = &k8sflags.K8SFlags{}
c.k8s = &flags.K8SFlags{}
flags.Merge(c.flags, c.k8s.Flags())
c.help = flags.Usage(help, c.flags)
}
Expand Down
12 changes: 5 additions & 7 deletions subcommand/create-federation-secret/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/cenkalti/backoff"
"github.com/hashicorp/consul-k8s/subcommand"
"github.com/hashicorp/consul-k8s/subcommand/common"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
corev1 "k8s.io/api/core/v1"
Expand All @@ -38,7 +37,7 @@ var retryInterval = 1 * time.Second
type Command struct {
UI cli.Ui
flags *flag.FlagSet
k8s *k8sflags.K8SFlags
k8s *flags.K8SFlags
http *flags.HTTPFlags

// flagExportReplicationToken controls whether we include the acl replication
Expand Down Expand Up @@ -90,12 +89,11 @@ func (c *Command) init() {
"Log verbosity level. Supported values (in order of detail) are \"trace\", "+
"\"debug\", \"info\", \"warn\", and \"error\".")

c.help = flags.Usage(help, c.flags)
c.http = &flags.HTTPFlags{}
c.k8s = &k8sflags.K8SFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.k8s = &flags.K8SFlags{}
flags.Merge(c.flags, c.http.Flags())
flags.Merge(c.flags, c.k8s.Flags())
c.help = flags.Usage(help, c.flags)
}

// Run creates a Kubernetes secret with data needed by secondary datacenters
Expand Down
7 changes: 3 additions & 4 deletions subcommand/delete-completed-job/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"flag"
"fmt"
"github.com/hashicorp/consul-k8s/subcommand"
k8sflags "github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul-k8s/subcommand/flags"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
v1 "k8s.io/api/batch/v1"
Expand All @@ -24,7 +23,7 @@ type Command struct {
UI cli.Ui

flags *flag.FlagSet
k8s *k8sflags.K8SFlags
k8s *flags.K8SFlags
flagNamespace string
flagTimeout string

Expand All @@ -39,7 +38,7 @@ type Command struct {
func (c *Command) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)

c.k8s = &k8sflags.K8SFlags{}
c.k8s = &flags.K8SFlags{}
c.flags.StringVar(&c.flagNamespace, "k8s-namespace", "",
"Name of Kubernetes namespace where the job is deployed")
c.flags.StringVar(&c.flagTimeout, "timeout", "30m",
Expand Down
23 changes: 23 additions & 0 deletions subcommand/flags/flag_slice_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package flags

import "strings"

// Taken from https://github.com/hashicorp/consul/blob/b5b9c8d953cd3c79c6b795946839f4cf5012f507/command/flags/flag_slice_value.go
// This was done so we don't depend on internal Consul implementation.

// AppendSliceValue implements the flag.Value interface and allows multiple
// calls to the same variable to append a list.
type AppendSliceValue []string

func (s *AppendSliceValue) String() string {
return strings.Join(*s, ",")
}

func (s *AppendSliceValue) Set(value string) error {
if *s == nil {
*s = make([]string, 0, 1)
}

*s = append(*s, value)
return nil
}
38 changes: 38 additions & 0 deletions subcommand/flags/flag_slice_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package flags

import (
"flag"
"reflect"
"testing"
)

// Taken from https://github.com/hashicorp/consul/blob/b5b9c8d953cd3c79c6b795946839f4cf5012f507/command/flags/flag_slice_value_test.go
// This was done so we don't depend on internal Consul implementation.

func TestAppendSliceValue_implements(t *testing.T) {
t.Parallel()
var raw interface{}
raw = new(AppendSliceValue)
if _, ok := raw.(flag.Value); !ok {
t.Fatalf("AppendSliceValue should be a Value")
}
}

func TestAppendSliceValueSet(t *testing.T) {
t.Parallel()
sv := new(AppendSliceValue)
err := sv.Set("foo")
if err != nil {
t.Fatalf("err: %s", err)
}

err = sv.Set("bar")
if err != nil {
t.Fatalf("err: %s", err)
}

expected := []string{"foo", "bar"}
if !reflect.DeepEqual([]string(*sv), expected) {
t.Fatalf("Bad: %#v", sv)
}
}
2 changes: 2 additions & 0 deletions subcommand/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package flags holds common flags that are shared between our commands.
package flags
154 changes: 154 additions & 0 deletions subcommand/flags/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package flags

import (
"flag"
"io/ioutil"
"strings"

"github.com/hashicorp/consul/api"
)

// Taken from https://github.com/hashicorp/consul/blob/b5b9c8d953cd3c79c6b795946839f4cf5012f507/command/flags/http.go
// with flags we don't use removed. This was done so we don't depend on internal
// Consul implementation.

// HTTPFlags are flags used to configure communication with a Consul agent.
type HTTPFlags struct {
address StringValue
token StringValue
tokenFile StringValue
caFile StringValue
caPath StringValue
certFile StringValue
keyFile StringValue
tlsServerName StringValue
ishustava marked this conversation as resolved.
Show resolved Hide resolved
}

func (f *HTTPFlags) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Var(&f.address, "http-addr",
"The `address` and port of the Consul HTTP agent. The value can be an IP "+
"address or DNS address, but it must also include the port. This can "+
"also be specified via the CONSUL_HTTP_ADDR environment variable. The "+
"default value is http://127.0.0.1:8500. The scheme can also be set to "+
"HTTPS by setting the environment variable CONSUL_HTTP_SSL=true.")
fs.Var(&f.token, "token",
"ACL token to use in the request. This can also be specified via the "+
"CONSUL_HTTP_TOKEN environment variable. If unspecified, the query will "+
"default to the token of the Consul agent at the HTTP address.")
fs.Var(&f.tokenFile, "token-file",
"File containing the ACL token to use in the request instead of one specified "+
"via the -token argument or CONSUL_HTTP_TOKEN environment variable. "+
"This can also be specified via the CONSUL_HTTP_TOKEN_FILE environment variable.")
fs.Var(&f.caFile, "ca-file",
"Path to a CA file to use for TLS when communicating with Consul. This "+
"can also be specified via the CONSUL_CACERT environment variable.")
fs.Var(&f.caPath, "ca-path",
"Path to a directory of CA certificates to use for TLS when communicating "+
"with Consul. This can also be specified via the CONSUL_CAPATH environment variable.")
fs.Var(&f.certFile, "client-cert",
"Path to a client cert file to use for TLS when 'verify_incoming' is enabled. This "+
"can also be specified via the CONSUL_CLIENT_CERT environment variable.")
fs.Var(&f.keyFile, "client-key",
"Path to a client key file to use for TLS when 'verify_incoming' is enabled. This "+
"can also be specified via the CONSUL_CLIENT_KEY environment variable.")
fs.Var(&f.tlsServerName, "tls-server-name",
"The server name to use as the SNI host when connecting via TLS. This "+
"can also be specified via the CONSUL_TLS_SERVER_NAME environment variable.")
return fs
}

func (f *HTTPFlags) Addr() string {
return f.address.String()
}

func (f *HTTPFlags) Token() string {
return f.token.String()
}

func (f *HTTPFlags) SetToken(v string) error {
return f.token.Set(v)
}

func (f *HTTPFlags) TokenFile() string {
return f.tokenFile.String()
}

func (f *HTTPFlags) SetTokenFile(v string) error {
return f.tokenFile.Set(v)
}

func (f *HTTPFlags) ReadTokenFile() (string, error) {
tokenFile := f.tokenFile.String()
if tokenFile == "" {
return "", nil
}

data, err := ioutil.ReadFile(tokenFile)
if err != nil {
return "", err
}

return strings.TrimSpace(string(data)), nil
}

func (f *HTTPFlags) APIClient() (*api.Client, error) {
c := api.DefaultConfig()

f.MergeOntoConfig(c)

return api.NewClient(c)
}

func (f *HTTPFlags) MergeOntoConfig(c *api.Config) {
f.address.Merge(&c.Address)
f.token.Merge(&c.Token)
f.tokenFile.Merge(&c.TokenFile)
f.caFile.Merge(&c.TLSConfig.CAFile)
f.caPath.Merge(&c.TLSConfig.CAPath)
f.certFile.Merge(&c.TLSConfig.CertFile)
f.keyFile.Merge(&c.TLSConfig.KeyFile)
f.tlsServerName.Merge(&c.TLSConfig.Address)
}

func Merge(dst, src *flag.FlagSet) {
if dst == nil {
panic("dst cannot be nil")
}
if src == nil {
return
}
src.VisitAll(func(f *flag.Flag) {
dst.Var(f.Value, f.Name, f.Usage)
})
}

// StringValue provides a flag value that's aware if it has been set.
type StringValue struct {
v *string
}

// Merge will overlay this value if it has been set.
func (s *StringValue) Merge(onto *string) {
if s.v != nil {
*onto = *(s.v)
}
}

// Set implements the flag.Value interface.
func (s *StringValue) Set(v string) error {
if s.v == nil {
s.v = new(string)
}
*(s.v) = v
return nil
}

// String implements the flag.Value interface.
func (s *StringValue) String() string {
var current string
if s.v != nil {
current = *(s.v)
}
return current
}
18 changes: 18 additions & 0 deletions subcommand/flags/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package flags

import (
"testing"

"github.com/stretchr/testify/require"
)

// Taken from https://github.com/hashicorp/consul/blob/b5b9c8d953cd3c79c6b795946839f4cf5012f507/command/flags/http_test.go
// This was done so we don't depend on internal Consul implementation.

func TestHTTPFlagsSetToken(t *testing.T) {
var f HTTPFlags
require := require.New(t)
require.Empty(f.Token())
require.NoError(f.SetToken("foo"))
require.Equal("foo", f.Token())
}
4 changes: 1 addition & 3 deletions subcommand/flags/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package flags

import (
"flag"

"github.com/hashicorp/consul/command/flags"
)

type K8SFlags struct {
kubeconfig flags.StringValue
kubeconfig StringValue
}

func (f *K8SFlags) Flags() *flag.FlagSet {
Expand Down
Loading