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

feat(cli) implement --kong-admin-filter-tag flag #440

Merged
merged 1 commit into from
Oct 30, 2019
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
56 changes: 56 additions & 0 deletions cli/ingress-controller/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestDefaults(t *testing.T) {

KongAdminURL: "http://localhost:8001",
KongWorkspace: "",
KongAdminFilterTags: []string{"managed-by-ingress-controller"},
KongAdminHeaders: []string{},
KongAdminTLSSkipVerify: false,
KongAdminTLSServerName: "",
Expand Down Expand Up @@ -94,6 +95,7 @@ func TestOverrideViaCLIFlags(t *testing.T) {

"--kong-url", "https://kong.example.com",
"--kong-workspace", "yolo",
"--kong-admin-filter-tag", "foo-tag",
"--admin-header", "foo:bar",
"--admin-tls-skip-verify",
"--admin-tls-server-name", "kong-admin.example.com",
Expand Down Expand Up @@ -126,6 +128,7 @@ func TestOverrideViaCLIFlags(t *testing.T) {

KongAdminURL: "https://kong.example.com",
KongWorkspace: "yolo",
KongAdminFilterTags: []string{"foo-tag"},
KongAdminHeaders: []string{"foo:bar"},
KongAdminTLSSkipVerify: true,
KongAdminTLSServerName: "kong-admin.example.com",
Expand Down Expand Up @@ -178,6 +181,7 @@ func TestOverrideViaEnvVars(t *testing.T) {
AdmissionWebhookCertPath: "/new-cert-path",
AdmissionWebhookKeyPath: "/new-key-path",

KongAdminFilterTags: []string{"managed-by-ingress-controller"},
KongAdminURL: "http://localhost:8001",
KongWorkspace: "",
KongAdminHeaders: []string{},
Expand Down Expand Up @@ -229,6 +233,7 @@ func TestDeprecatedFlags(t *testing.T) {
expectedConf := cliConfig{
KongAdminURL: "https://kong.example.com",
KongWorkspace: "yolo",
KongAdminFilterTags: []string{"managed-by-ingress-controller"},
KongAdminHeaders: []string{"foo:bar"},
KongAdminTLSSkipVerify: true,
KongAdminTLSServerName: "kong-admin.example.com",
Expand Down Expand Up @@ -286,6 +291,7 @@ func TestDeprecatedFlagPrecedences(t *testing.T) {
expectedConf := cliConfig{
KongAdminURL: "http://kong.yolo42.com",
KongWorkspace: "yolo",
KongAdminFilterTags: []string{"managed-by-ingress-controller"},
KongAdminHeaders: []string{"fuu:baz"},
KongAdminTLSSkipVerify: true,
KongAdminTLSServerName: "kong-admin-new.example.com",
Expand Down Expand Up @@ -352,3 +358,53 @@ func TestKongAdminHeadersEnvVar(t *testing.T) {

assert.Nil(err, "unexpected error parsing default flags")
}

func TestKongFilterTags(t *testing.T) {
resetForTesting(func() { t.Fatal("bad parse") })
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

assert := assert.New(t)

// comma-separated
os.Args = []string{
"cmd",
"--kong-admin-filter-tag", "foo,bar",
}
conf, err := parseFlags()
assert.Equal([]string{"foo", "bar"}, conf.KongAdminFilterTags)

assert.Nil(err, "unexpected error parsing default flags")

resetForTesting(func() { t.Fatal("bad parse") })

// differnt flags
os.Args = []string{
"cmd",
"--kong-admin-filter-tag", "foo",
"--kong-admin-filter-tag", "bar",
}
conf, err = parseFlags()
assert.Equal([]string{"foo", "bar"}, conf.KongAdminFilterTags)

assert.Nil(err, "unexpected error parsing default flags")
}

func TestKongAdminFilterTagEnvVar(t *testing.T) {
resetForTesting(func() { t.Fatal("bad parse") })
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

assert := assert.New(t)

k := "CONTROLLER_KONG_ADMIN_FILTER_TAG"
v := "tag1 tag2"
os.Setenv(k, v)
defer os.Unsetenv(k)

conf, err := parseFlags()
assert.Equal([]string{"tag1", "tag2"},
conf.KongAdminFilterTags)

assert.Nil(err, "unexpected error parsing default flags")
}
11 changes: 10 additions & 1 deletion cli/ingress-controller/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import (
"github.com/kong/kubernetes-ingress-controller/internal/ingress/annotations"
)

const defaultKongAdminURL = "http://localhost:8001"
const (
defaultKongAdminURL = "http://localhost:8001"
defaultKongFilterTag = "managed-by-ingress-controller"
)

type cliConfig struct {
// Admission controller server properties
Expand All @@ -42,6 +45,7 @@ type cliConfig struct {
// Kong connection details
KongAdminURL string
KongWorkspace string
KongAdminFilterTags []string
KongAdminHeaders []string
KongAdminTLSSkipVerify bool
KongAdminTLSServerName string
Expand Down Expand Up @@ -101,6 +105,10 @@ format of protocol://address:port`)
flags.String("kong-workspace", "",
"Workspace in Kong Enterprise to be configured")

flags.StringSlice("kong-admin-filter-tag", []string{defaultKongFilterTag},
`add a header (key:value) to every Admin API call,
this flag can be used multiple times to specify multiple tags`)

// deprecated
flags.StringSlice("admin-header", nil,
`DEPRECATED, use --kong-admin-header
Expand Down Expand Up @@ -227,6 +235,7 @@ func parseFlags() (cliConfig, error) {
config.KongAdminURL = kongAdminURL

config.KongWorkspace = viper.GetString("kong-workspace")
config.KongAdminFilterTags = viper.GetStringSlice("kong-admin-filter-tag")

config.KongAdminHeaders = viper.GetStringSlice("admin-header")
kongAdminHeaders := viper.GetStringSlice("kong-admin-header")
Expand Down
3 changes: 2 additions & 1 deletion cli/ingress-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ import (
func controllerConfigFromCLIConfig(cliConfig cliConfig) controller.Configuration {
return controller.Configuration{
Kong: controller.Kong{
URL: cliConfig.KongAdminURL,
URL: cliConfig.KongAdminURL,
FilterTags: cliConfig.KongAdminFilterTags,
},

ResyncPeriod: cliConfig.SyncPeriod,
Expand Down
3 changes: 2 additions & 1 deletion internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import (

// Kong Represents a Kong client and connection information
type Kong struct {
URL string
URL string
FilterTags []string
// Headers are injected into every request to Kong's Admin API
// to help with authorization/authentication.
Client *kong.Client
Expand Down
4 changes: 1 addition & 3 deletions internal/ingress/controller/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ import (

var count counter.Counter

const ingressControllerTag = "managed-by-ingress-controller"

var upstreamDefaults = kong.Upstream{
Slots: kong.Int(10000),
Healthchecks: &kong.Healthcheck{
Expand Down Expand Up @@ -166,7 +164,7 @@ func (n *KongController) onUpdateDBMode(state *parser.KongState) error {
func (n *KongController) getIngressControllerTags() []string {
var res []string
if n.cfg.Kong.HasTagSupport {
res = append(res, ingressControllerTag)
res = append(res, n.cfg.Kong.FilterTags...)
}
return res
}
Expand Down