2020-07-08 17:06:44 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-07-27 20:32:14 +00:00
|
|
|
// Package server is the command line entry point for placeholder-name-server.
|
|
|
|
package server
|
2020-07-08 17:06:44 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-13 19:30:16 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-07-08 17:06:44 +00:00
|
|
|
"io"
|
|
|
|
|
2020-07-27 12:55:33 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-07-23 15:05:21 +00:00
|
|
|
genericapiserver "k8s.io/apiserver/pkg/server"
|
|
|
|
genericoptions "k8s.io/apiserver/pkg/server/options"
|
|
|
|
"k8s.io/apiserver/plugin/pkg/authenticator/token/webhook"
|
2020-07-16 19:24:30 +00:00
|
|
|
|
2020-07-28 15:28:34 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/apiserver"
|
2020-07-27 12:55:33 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/certauthority"
|
2020-08-09 17:04:05 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/controllermanager"
|
2020-07-27 12:55:33 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/downward"
|
2020-08-09 17:04:05 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/provider"
|
2020-08-05 20:58:03 +00:00
|
|
|
placeholderv1alpha1 "github.com/suzerain-io/placeholder-name/kubernetes/1.19/api/apis/placeholder/v1alpha1"
|
2020-07-14 15:50:14 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/pkg/config"
|
2020-07-08 17:06:44 +00:00
|
|
|
)
|
|
|
|
|
2020-07-27 20:32:14 +00:00
|
|
|
// App is an object that represents the placeholder-name-server application.
|
2020-07-08 17:06:44 +00:00
|
|
|
type App struct {
|
2020-08-07 21:49:04 +00:00
|
|
|
serverCommand *cobra.Command
|
2020-07-08 17:06:44 +00:00
|
|
|
|
2020-07-16 19:24:30 +00:00
|
|
|
// CLI flags
|
2020-07-24 20:41:51 +00:00
|
|
|
configPath string
|
|
|
|
downwardAPIPath string
|
|
|
|
clusterSigningCertFilePath string
|
|
|
|
clusterSigningKeyFilePath string
|
2020-07-08 17:06:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:05:21 +00:00
|
|
|
// This is ignored for now because we turn off etcd storage below, but this is
|
|
|
|
// the right prefix in case we turn it back on.
|
|
|
|
const defaultEtcdPathPrefix = "/registry/" + placeholderv1alpha1.GroupName
|
|
|
|
|
2020-07-08 17:06:44 +00:00
|
|
|
// New constructs a new App with command line args, stdout and stderr.
|
2020-07-23 15:05:21 +00:00
|
|
|
func New(ctx context.Context, args []string, stdout, stderr io.Writer) *App {
|
2020-08-07 21:49:04 +00:00
|
|
|
app := &App{}
|
|
|
|
app.addServerCommand(ctx, args, stdout, stderr)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the server.
|
|
|
|
func (app *App) Run() error {
|
|
|
|
return app.serverCommand.Execute()
|
|
|
|
}
|
2020-07-08 17:06:44 +00:00
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Create the server command and save it into the App.
|
|
|
|
func (app *App) addServerCommand(ctx context.Context, args []string, stdout, stderr io.Writer) {
|
2020-07-08 17:06:44 +00:00
|
|
|
cmd := &cobra.Command{
|
2020-07-27 20:32:14 +00:00
|
|
|
Use: `placeholder-name-server`,
|
2020-08-07 21:49:04 +00:00
|
|
|
Long: "placeholder-name-server provides a generic API for mapping an external\n" +
|
|
|
|
"credential from somewhere to an internal credential to be used for\n" +
|
|
|
|
"authenticating to the Kubernetes API.",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error { return app.runServer(ctx) },
|
2020-07-08 17:06:44 +00:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
cmd.SetOut(stdout)
|
|
|
|
cmd.SetErr(stderr)
|
2020-08-07 21:49:04 +00:00
|
|
|
addCommandlineFlagsToCommand(cmd, app)
|
2020-07-08 17:06:44 +00:00
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
app.serverCommand = cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the app's commandline flags.
|
|
|
|
func addCommandlineFlagsToCommand(cmd *cobra.Command, app *App) {
|
2020-07-08 17:06:44 +00:00
|
|
|
cmd.Flags().StringVarP(
|
2020-08-07 21:49:04 +00:00
|
|
|
&app.configPath,
|
2020-07-08 17:06:44 +00:00
|
|
|
"config",
|
|
|
|
"c",
|
|
|
|
"placeholder-name.yaml",
|
|
|
|
"path to configuration file",
|
|
|
|
)
|
|
|
|
|
2020-07-16 19:24:30 +00:00
|
|
|
cmd.Flags().StringVar(
|
2020-08-07 21:49:04 +00:00
|
|
|
&app.downwardAPIPath,
|
2020-07-16 19:24:30 +00:00
|
|
|
"downward-api-path",
|
|
|
|
"/etc/podinfo",
|
|
|
|
"path to Downward API volume mount",
|
|
|
|
)
|
|
|
|
|
2020-07-24 20:41:51 +00:00
|
|
|
cmd.Flags().StringVar(
|
2020-08-07 21:49:04 +00:00
|
|
|
&app.clusterSigningCertFilePath,
|
2020-07-24 20:41:51 +00:00
|
|
|
"cluster-signing-cert-file",
|
|
|
|
"",
|
|
|
|
"path to cluster signing certificate",
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Flags().StringVar(
|
2020-08-07 21:49:04 +00:00
|
|
|
&app.clusterSigningKeyFilePath,
|
2020-07-24 20:41:51 +00:00
|
|
|
"cluster-signing-key-file",
|
|
|
|
"",
|
|
|
|
"path to cluster signing private key",
|
|
|
|
)
|
2020-07-08 17:06:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Boot the aggregated API server, which will in turn boot the controllers.
|
|
|
|
func (app *App) runServer(ctx context.Context) error {
|
|
|
|
// Read the server config file.
|
|
|
|
cfg, err := config.FromPath(app.configPath)
|
2020-07-14 15:50:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load config: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:55:33 +00:00
|
|
|
// Load the Kubernetes cluster signing CA.
|
2020-08-07 21:49:04 +00:00
|
|
|
k8sClusterCA, err := certauthority.Load(app.clusterSigningCertFilePath, app.clusterSigningKeyFilePath)
|
2020-07-27 12:55:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load cluster signing CA: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Create a WebhookTokenAuthenticator.
|
2020-07-23 15:05:21 +00:00
|
|
|
webhookTokenAuthenticator, err := config.NewWebhook(cfg.WebhookConfig)
|
2020-07-14 15:50:14 +00:00
|
|
|
if err != nil {
|
2020-07-27 12:55:33 +00:00
|
|
|
return fmt.Errorf("could not create webhook client: %w", err)
|
2020-07-14 15:50:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Discover in which namespace we are installed.
|
|
|
|
podInfo, err := downward.Load(app.downwardAPIPath)
|
2020-07-16 19:24:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read pod metadata: %w", err)
|
|
|
|
}
|
2020-08-07 21:49:04 +00:00
|
|
|
serverInstallationNamespace := podInfo.Namespace
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-08-09 17:04:05 +00:00
|
|
|
// This cert provider will provide certs to the API server and will
|
|
|
|
// be mutated by a controller to keep the certs up to date with what
|
|
|
|
// is stored in a k8s Secret. Therefore it also effectively acting as
|
|
|
|
// an in-memory cache of what is stored in the k8s Secret, helping to
|
|
|
|
// keep incoming requests fast.
|
|
|
|
dynamicCertProvider := &provider.DynamicTLSServingCertProvider{}
|
2020-07-13 19:30:16 +00:00
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Prepare to start the controllers, but defer actually starting them until the
|
|
|
|
// post start hook of the aggregated API server.
|
2020-08-09 17:04:05 +00:00
|
|
|
startControllersFunc, err := controllermanager.PrepareControllers(
|
2020-08-03 14:17:11 +00:00
|
|
|
serverInstallationNamespace,
|
|
|
|
cfg.DiscoveryConfig.URL,
|
2020-08-09 17:04:05 +00:00
|
|
|
dynamicCertProvider,
|
2020-08-03 14:17:11 +00:00
|
|
|
)
|
2020-08-07 21:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not prepare controllers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the aggregated API server config.
|
|
|
|
aggregatedAPIServerConfig, err := getAggregatedAPIServerConfig(
|
2020-08-09 17:04:05 +00:00
|
|
|
dynamicCertProvider,
|
2020-07-31 16:08:07 +00:00
|
|
|
webhookTokenAuthenticator,
|
2020-08-07 21:49:04 +00:00
|
|
|
k8sClusterCA,
|
|
|
|
startControllersFunc,
|
2020-07-31 16:08:07 +00:00
|
|
|
)
|
2020-07-23 15:05:21 +00:00
|
|
|
if err != nil {
|
2020-08-07 21:49:04 +00:00
|
|
|
return fmt.Errorf("could not configure aggregated API server: %w", err)
|
2020-07-13 19:30:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Complete the aggregated API server config and make a server instance.
|
|
|
|
server, err := aggregatedAPIServerConfig.Complete().New()
|
2020-07-23 15:05:21 +00:00
|
|
|
if err != nil {
|
2020-08-07 21:49:04 +00:00
|
|
|
return fmt.Errorf("could not create aggregated API server: %w", err)
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
2020-07-14 15:50:14 +00:00
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Run the server. Its post-start hook will start the controllers.
|
2020-07-23 15:05:21 +00:00
|
|
|
return server.GenericAPIServer.PrepareRun().Run(ctx.Done())
|
2020-07-13 19:30:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
// Create a configuration for the aggregated API server.
|
|
|
|
func getAggregatedAPIServerConfig(
|
2020-08-09 17:04:05 +00:00
|
|
|
dynamicCertProvider *provider.DynamicTLSServingCertProvider,
|
2020-07-31 16:08:07 +00:00
|
|
|
webhookTokenAuthenticator *webhook.WebhookTokenAuthenticator,
|
|
|
|
ca *certauthority.CA,
|
|
|
|
startControllersPostStartHook func(context.Context),
|
|
|
|
) (*apiserver.Config, error) {
|
2020-08-07 21:49:04 +00:00
|
|
|
recommendedOptions := genericoptions.NewRecommendedOptions(
|
|
|
|
defaultEtcdPathPrefix,
|
|
|
|
apiserver.Codecs.LegacyCodec(placeholderv1alpha1.SchemeGroupVersion),
|
|
|
|
// TODO we should check to see if all the other default settings are acceptable for us
|
|
|
|
)
|
|
|
|
recommendedOptions.Etcd = nil // turn off etcd storage because we don't need it yet
|
2020-08-09 17:04:05 +00:00
|
|
|
recommendedOptions.SecureServing.ServerCert.GeneratedCert = dynamicCertProvider
|
2020-07-13 19:30:16 +00:00
|
|
|
|
2020-07-23 15:05:21 +00:00
|
|
|
serverConfig := genericapiserver.NewRecommendedConfig(apiserver.Codecs)
|
2020-08-09 17:04:05 +00:00
|
|
|
// Note that among other things, this ApplyTo() function copies
|
|
|
|
// `recommendedOptions.SecureServing.ServerCert.GeneratedCert` into
|
|
|
|
// `serverConfig.SecureServing.Cert` thus making `dynamicCertProvider`
|
|
|
|
// the cert provider for the running server. The provider will be called
|
|
|
|
// by the API machinery periodically. When the provider returns nil certs,
|
|
|
|
// the API server will return "the server is currently unable to
|
|
|
|
// handle the request" error responses for all incoming requests.
|
|
|
|
// If the provider later starts returning certs, then the API server
|
|
|
|
// will use them to handle the incoming requests successfully.
|
2020-08-07 21:49:04 +00:00
|
|
|
if err := recommendedOptions.ApplyTo(serverConfig); err != nil {
|
2020-07-23 15:05:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 19:30:16 +00:00
|
|
|
|
2020-07-23 15:05:21 +00:00
|
|
|
apiServerConfig := &apiserver.Config{
|
|
|
|
GenericConfig: serverConfig,
|
|
|
|
ExtraConfig: apiserver.ExtraConfig{
|
2020-07-31 16:08:07 +00:00
|
|
|
Webhook: webhookTokenAuthenticator,
|
|
|
|
Issuer: ca,
|
|
|
|
StartControllersPostStartHook: startControllersPostStartHook,
|
2020-07-23 15:05:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return apiServerConfig, nil
|
2020-07-13 19:30:16 +00:00
|
|
|
}
|