Refactor app.go and wire in autoregistration.

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer
2020-07-17 12:10:33 -05:00
parent a3bce5f42e
commit 092cc26789
7 changed files with 163 additions and 86 deletions
+86 -13
View File
@@ -20,10 +20,20 @@ import (
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
restclient "k8s.io/client-go/rest"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
aggregationv1client "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
"github.com/suzerain-io/placeholder-name-api/pkg/apis/placeholder"
"github.com/suzerain-io/placeholder-name/internal/autoregistration"
"github.com/suzerain-io/placeholder-name/internal/certauthority"
"github.com/suzerain-io/placeholder-name/internal/downward"
"github.com/suzerain-io/placeholder-name/pkg/config"
"github.com/suzerain-io/placeholder-name/pkg/handlers"
)
@@ -35,6 +45,10 @@ const shutdownGracePeriod = 5 * time.Second
type App struct {
cmd *cobra.Command
// CLI flags
configPath string
downwardAPIPath string
// listen address for healthz serve
healthAddr string
@@ -43,29 +57,39 @@ type App struct {
// webhook authenticates tokens
webhook authenticator.Token
// runFunc runs the actual program, after the parsing of flags has been done.
//
// It is mostly a field for the sake of testing.
runFunc func(ctx context.Context, configPath string) error
}
// New constructs a new App with command line args, stdout and stderr.
func New(args []string, stdout, stderr io.Writer) *App {
a := &App{
healthAddr: ":8080",
mainAddr: ":8443",
mainAddr: ":443",
}
a.runFunc = a.serve
var configPath string
cmd := &cobra.Command{
Use: `placeholder-name`,
Long: `placeholder-name provides a generic API for mapping an external
credential from somewhere to an internal credential to be used for
authenticating to the Kubernetes API.`,
RunE: func(cmd *cobra.Command, args []string) error {
return a.runFunc(context.Background(), configPath)
// Load the Kubernetes client configuration (kubeconfig),
kubeconfig, err := restclient.InClusterConfig()
if err != nil {
return fmt.Errorf("could not load in-cluster configuration: %w", err)
}
// Connect to the core Kubernetes API.
k8s, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
return fmt.Errorf("could not initialize Kubernetes client: %w", err)
}
// Connect to the Kubernetes aggregation API.
aggregation, err := aggregationv1client.NewForConfig(kubeconfig)
if err != nil {
return fmt.Errorf("could not initialize Kubernetes client: %w", err)
}
return a.serve(context.Background(), k8s.CoreV1(), aggregation)
},
Args: cobra.NoArgs,
}
@@ -75,13 +99,20 @@ authenticating to the Kubernetes API.`,
cmd.SetErr(stderr)
cmd.Flags().StringVarP(
&configPath,
&a.configPath,
"config",
"c",
"placeholder-name.yaml",
"path to configuration file",
)
cmd.Flags().StringVar(
&a.downwardAPIPath,
"downward-api-path",
"/etc/podinfo",
"path to Downward API volume mount",
)
a.cmd = cmd
return a
@@ -91,8 +122,8 @@ func (a *App) Run() error {
return a.cmd.Execute()
}
func (a *App) serve(ctx context.Context, configPath string) error {
cfg, err := config.FromPath(configPath)
func (a *App) serve(ctx context.Context, k8s corev1client.CoreV1Interface, aggregation aggregationv1client.Interface) error {
cfg, err := config.FromPath(a.configPath)
if err != nil {
return fmt.Errorf("could not load config: %w", err)
}
@@ -103,6 +134,11 @@ func (a *App) serve(ctx context.Context, configPath string) error {
}
a.webhook = webhook
podinfo, err := downward.Load(a.downwardAPIPath)
if err != nil {
return fmt.Errorf("could not read pod metadata: %w", err)
}
ca, err := certauthority.New(pkix.Name{CommonName: "Placeholder CA"})
if err != nil {
return fmt.Errorf("could not initialize CA: %w", err)
@@ -125,6 +161,43 @@ func (a *App) serve(ctx context.Context, configPath string) error {
// Start an errgroup to manage the lifetimes of the various listener goroutines.
eg, ctx := errgroup.WithContext(ctx)
// Dynamically register our v1alpha1 API service.
service := corev1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "placeholder-name-api"},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Protocol: corev1.ProtocolTCP,
Port: 443,
TargetPort: intstr.IntOrString{IntVal: 443}, //TODO: parse this out of mainAddr
},
},
Selector: podinfo.Labels,
Type: corev1.ServiceTypeClusterIP,
},
}
apiService := apiregistrationv1.APIService{
ObjectMeta: metav1.ObjectMeta{
Name: "v1alpha1." + placeholder.GroupName,
},
Spec: apiregistrationv1.APIServiceSpec{
Group: placeholder.GroupName,
Version: "v1alpha1",
CABundle: caBundle,
GroupPriorityMinimum: 2500,
VersionPriority: 10,
},
}
if err := autoregistration.Setup(ctx, autoregistration.SetupOptions{
CoreV1: k8s,
AggregationV1: aggregation,
Namespace: podinfo.Namespace,
ServiceTemplate: service,
APIServiceTemplate: apiService,
}); err != nil {
return fmt.Errorf("could not register API service: %w", err)
}
// Start healthz listener
eg.Go(func() error {
log.Printf("Starting healthz serve on %v", a.healthAddr)
+55 -38
View File
@@ -8,45 +8,60 @@ package app
import (
"bytes"
"context"
"strings"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1fake "k8s.io/client-go/kubernetes/fake"
aggregationv1fake "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset/fake"
)
const knownGoodUsage = `Usage:
const knownGoodUsage = `
placeholder-name provides a generic API for mapping an external
credential from somewhere to an internal credential to be used for
authenticating to the Kubernetes API.
Usage:
placeholder-name [flags]
Flags:
-c, --config string path to configuration file (default "placeholder-name.yaml")
-h, --help help for placeholder-name
-c, --config string path to configuration file (default "placeholder-name.yaml")
--downward-api-path string path to Downward API volume mount (default "/etc/podinfo")
-h, --help help for placeholder-name
`
func TestCommand(t *testing.T) {
tests := []struct {
name string
args []string
wantConfigPath string
name string
args []string
wantErr string
wantStdout string
}{
{
name: "NoArgsSucceeds",
args: []string{},
wantConfigPath: "placeholder-name.yaml",
name: "NoArgsSucceeds",
args: []string{},
},
{
name: "OneArgFails",
args: []string{"tuna"},
name: "Usage",
args: []string{"-h"},
wantStdout: knownGoodUsage,
},
{
name: "ShortConfigFlagSucceeds",
args: []string{"-c", "some/path/to/config.yaml"},
wantConfigPath: "some/path/to/config.yaml",
name: "OneArgFails",
args: []string{"tuna"},
wantErr: `unknown command "tuna" for "placeholder-name"`,
},
{
name: "LongConfigFlagSucceeds",
args: []string{"--config", "some/path/to/config.yaml"},
wantConfigPath: "some/path/to/config.yaml",
name: "ShortConfigFlagSucceeds",
args: []string{"-c", "some/path/to/config.yaml"},
},
{
name: "LongConfigFlagSucceeds",
args: []string{"--config", "some/path/to/config.yaml"},
},
{
name: "OneArgWithConfigFlagFails",
@@ -54,32 +69,27 @@ func TestCommand(t *testing.T) {
"--config", "some/path/to/config.yaml",
"tuna",
},
wantErr: `unknown command "tuna" for "placeholder-name"`,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
expect := require.New(t)
stdout := bytes.NewBuffer([]byte{})
stderr := bytes.NewBuffer([]byte{})
configPaths := make([]string, 0, 1)
runFunc := func(ctx context.Context, configPath string) error {
configPaths = append(configPaths, configPath)
a := New(test.args, stdout, stderr)
a.cmd.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
a := New(test.args, stdout, stderr)
a.runFunc = runFunc
err := a.Run()
if test.wantConfigPath != "" {
expect.Equal(1, len(configPaths))
expect.Equal(test.wantConfigPath, configPaths[0])
if test.wantErr != "" {
require.EqualError(t, err, test.wantErr)
} else {
expect.Error(err)
expect.Contains(stdout.String(), knownGoodUsage)
require.NoError(t, err)
}
if test.wantStdout != "" {
require.Equal(t, strings.TrimSpace(test.wantStdout), strings.TrimSpace(stdout.String()))
}
})
}
@@ -88,16 +98,21 @@ func TestCommand(t *testing.T) {
func TestServeApp(t *testing.T) {
t.Parallel()
fakev1 := corev1fake.NewSimpleClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-namespace"}})
fakeaggregationv1 := aggregationv1fake.NewSimpleClientset()
t.Run("success", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
cancel()
a := App{
healthAddr: "127.0.0.1:0",
mainAddr: "127.0.0.1:8443",
healthAddr: "127.0.0.1:0",
mainAddr: "127.0.0.1:8443",
configPath: "testdata/valid-config.yaml",
downwardAPIPath: "testdata/podinfo",
}
err := a.serve(ctx, "testdata/valid-config.yaml")
err := a.serve(ctx, fakev1.CoreV1(), fakeaggregationv1)
require.NoError(t, err)
})
@@ -107,10 +122,12 @@ func TestServeApp(t *testing.T) {
defer cancel()
a := App{
healthAddr: "127.0.0.1:8081",
mainAddr: "127.0.0.1:8081",
healthAddr: "127.0.0.1:8081",
mainAddr: "127.0.0.1:8081",
configPath: "testdata/valid-config.yaml",
downwardAPIPath: "testdata/podinfo",
}
err := a.serve(ctx, "testdata/valid-config.yaml")
err := a.serve(ctx, fakev1.CoreV1(), fakeaggregationv1)
require.EqualError(t, err, "listen tcp 127.0.0.1:8081: bind: address already in use")
})
}
+2
View File
@@ -0,0 +1,2 @@
foo="bar"
bat="baz"
+1
View File
@@ -0,0 +1 @@
test-namespace