Skip to content

Commit 07b4ac2

Browse files
authored
Merge pull request #209 from anchore/account-routing
2 parents f714781 + 9f7883d commit 07b4ac2

File tree

12 files changed

+476
-54
lines changed

12 files changed

+476
-54
lines changed

anchore-k8s-inventory.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ namespace-selectors:
4343

4444
ignore-empty: false
4545

46+
account-routes:
47+
# Example
48+
# account:
49+
# user: username
50+
# password: password
51+
# namespaces:
52+
# - default
53+
# - ^kube-*
54+
4655
# Kubernetes API configuration parameters (should not need tuning)
4756
kubernetes:
4857
# Sets the request timeout for kubernetes API requests

cmd/root.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ var rootCmd = &cobra.Command{
4747
case mode.PeriodicPolling:
4848
pkg.PeriodicallyGetInventoryReport(appConfig)
4949
default:
50-
report, err := pkg.GetInventoryReport(appConfig)
50+
reports, err := pkg.GetInventoryReports(appConfig)
5151
if appConfig.Dev.ProfileCPU {
5252
pprof.StopCPUProfile()
5353
}
5454
if err != nil {
5555
log.Errorf("Failed to get Image Results: %+v", err)
5656
os.Exit(1)
5757
}
58-
err = pkg.HandleReport(report, appConfig)
59-
if err != nil {
60-
log.Errorf("Failed to handle Image Results: %+v", err)
61-
os.Exit(1)
58+
for account, report := range reports {
59+
err = pkg.HandleReport(report, appConfig, account)
60+
if err != nil {
61+
log.Errorf("Failed to handle Image Results: %+v", err)
62+
os.Exit(1)
63+
}
6264
}
6365
}
6466
},

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Application struct {
4646
Namespaces []string `mapstructure:"namespaces"`
4747
KubernetesRequestTimeoutSeconds int64 `mapstructure:"kubernetes-request-timeout-seconds"`
4848
NamespaceSelectors NamespaceSelector `mapstructure:"namespace-selectors"`
49+
AccountRoutes AccountRoutes `mapstructure:"account-routes"`
4950
MissingRegistryOverride string `mapstructure:"missing-registry-override"`
5051
MissingTagPolicy MissingTagConf `mapstructure:"missing-tag-policy"`
5152
RunMode mode.Mode
@@ -69,6 +70,14 @@ type NamespaceSelector struct {
6970
IgnoreEmpty bool `mapstructure:"ignore-empty"`
7071
}
7172

73+
type AccountRoutes map[string]AccountRouteDetails
74+
75+
type AccountRouteDetails struct {
76+
User string `mapstructure:"user"`
77+
Password string `mapstructure:"password"`
78+
Namespaces []string `mapstructure:"namespaces"`
79+
}
80+
7281
// KubernetesAPI details the configuration for interacting with the k8s api server
7382
type KubernetesAPI struct {
7483
RequestTimeoutSeconds int64 `mapstructure:"request-timeout-seconds"`
@@ -128,6 +137,7 @@ func setNonCliDefaultValues(v *viper.Viper) {
128137
v.SetDefault("missing-registry-override", "")
129138
v.SetDefault("missing-tag-policy.policy", "digest")
130139
v.SetDefault("missing-tag-policy.tag", "UNKNOWN")
140+
v.SetDefault("account-routes", AccountRoutes{})
131141
v.SetDefault("namespaces", []string{})
132142
v.SetDefault("namespace-selectors.include", []string{})
133143
v.SetDefault("namespace-selectors.exclude", []string{})

internal/config/config_test.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"flag"
55
"testing"
66

7-
"github.com/spf13/viper"
8-
97
"github.com/anchore/go-testutils"
8+
"github.com/spf13/viper"
109
)
1110

1211
var update = flag.Bool("update", false, "update the *.golden files for config string output")
@@ -68,3 +67,55 @@ func TestSensitiveConfigString(t *testing.T) {
6867
t.Errorf("Config string does not match expected\nactual: %s\nexpected: %s", actual, expected)
6968
}
7069
}
70+
71+
func TestAnchoreInfo_IsValid(t *testing.T) {
72+
type fields struct {
73+
URL string
74+
User string
75+
Password string
76+
Account string
77+
HTTP HTTPConfig
78+
}
79+
tests := []struct {
80+
name string
81+
fields fields
82+
want bool
83+
}{
84+
{
85+
name: "valid",
86+
fields: fields{
87+
URL: "http://anchore.example.com",
88+
User: "admin",
89+
Password: "foobar",
90+
Account: "admin",
91+
HTTP: HTTPConfig{},
92+
},
93+
want: true,
94+
},
95+
{
96+
name: "invalid",
97+
fields: fields{
98+
URL: "http://anchore.example.com",
99+
User: "",
100+
Password: "foobar",
101+
Account: "admin",
102+
HTTP: HTTPConfig{},
103+
},
104+
want: false,
105+
},
106+
}
107+
for _, tt := range tests {
108+
t.Run(tt.name, func(t *testing.T) {
109+
anchore := &AnchoreInfo{
110+
URL: tt.fields.URL,
111+
User: tt.fields.User,
112+
Password: tt.fields.Password,
113+
Account: tt.fields.Account,
114+
HTTP: tt.fields.HTTP,
115+
}
116+
if got := anchore.IsValid(); got != tt.want {
117+
t.Errorf("AnchoreInfo.IsValid() = %v, want %v", got, tt.want)
118+
}
119+
})
120+
}
121+
}

internal/config/test-fixtures/snapshot/TestDefaultConfigString.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespaceselectors:
3131
include: []
3232
exclude: []
3333
ignoreempty: false
34+
accountroutes: {}
3435
missingregistryoverride: ""
3536
missingtagpolicy:
3637
policy: digest

internal/config/test-fixtures/snapshot/TestEmptyConfigString.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespaceselectors:
3131
include: []
3232
exclude: []
3333
ignoreempty: false
34+
accountroutes: {}
3435
missingregistryoverride: ""
3536
missingtagpolicy:
3637
policy: ""

internal/config/test-fixtures/snapshot/TestSensitiveConfigString.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespaceselectors:
3131
include: []
3232
exclude: []
3333
ignoreempty: false
34+
accountroutes: {}
3435
missingregistryoverride: ""
3536
missingtagpolicy:
3637
policy: digest

pkg/client/client_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Wraps some of the initialization details for the k8s clientset
2+
package client
3+
4+
import (
5+
"testing"
6+
7+
"github.com/anchore/k8s-inventory/internal/config"
8+
"github.com/stretchr/testify/assert"
9+
"k8s.io/client-go/rest"
10+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
11+
)
12+
13+
func TestGetClientSet(t *testing.T) {
14+
type args struct {
15+
kubeConfig *rest.Config
16+
}
17+
tests := []struct {
18+
name string
19+
args args
20+
wantErr bool
21+
}{
22+
{
23+
name: "happy path",
24+
args: args{
25+
kubeConfig: &rest.Config{},
26+
},
27+
wantErr: false,
28+
},
29+
{
30+
name: "sad path",
31+
args: args{
32+
kubeConfig: &rest.Config{
33+
AuthProvider: &clientcmdapi.AuthProviderConfig{},
34+
ExecProvider: &clientcmdapi.ExecConfig{},
35+
},
36+
},
37+
wantErr: true,
38+
},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
_, err := GetClientSet(tt.args.kubeConfig)
43+
if tt.wantErr {
44+
assert.Error(t, err)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestGetKubeConfig(t *testing.T) {
51+
type args struct {
52+
appConfig *config.Application
53+
}
54+
tests := []struct {
55+
name string
56+
args args
57+
wantErr bool
58+
}{
59+
{
60+
name: "use default",
61+
args: args{
62+
appConfig: &config.Application{},
63+
},
64+
wantErr: false,
65+
},
66+
{
67+
name: "use in-cluster",
68+
args: args{
69+
appConfig: &config.Application{
70+
KubeConfig: config.KubeConf{
71+
Path: "use-in-cluster",
72+
},
73+
},
74+
},
75+
wantErr: false,
76+
},
77+
}
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
_, err := GetKubeConfig(tt.args.appConfig)
81+
if tt.wantErr {
82+
assert.Error(t, err)
83+
}
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)