Skip to content

Commit

Permalink
youngnick comments
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Sloka <slokas@vmware.com>
  • Loading branch information
stevesloka committed Apr 6, 2021
1 parent 0a3a8f9 commit 4885db6
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 34 deletions.
4 changes: 3 additions & 1 deletion cmd/contour/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"path/filepath"
"strconv"

"k8s.io/apimachinery/pkg/runtime"

"github.com/projectcontour/contour/internal/certgen"
"github.com/projectcontour/contour/internal/k8s"
"github.com/projectcontour/contour/pkg/certs"
Expand Down Expand Up @@ -136,7 +138,7 @@ func doCertgen(config *certgenConfig, log logrus.FieldLogger) {
log.WithError(err).Fatal("failed to generate certificates")
}

clients, err := k8s.NewClients(config.KubeConfig, config.InCluster)
clients, err := k8s.NewClients(config.KubeConfig, config.InCluster, runtime.NewScheme())
if err != nil {
log.WithError(err).Fatalf("failed to create Kubernetes client")
}
Expand Down
39 changes: 20 additions & 19 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,15 @@ func validateCRDs(dynamicClient dynamic.Interface, log logrus.FieldLogger) {

// doServe runs the contour serve subcommand.
func doServe(log logrus.FieldLogger, ctx *serveContext) error {

// Setup a Manager
mgr, err := manager.New(controller_config.GetConfigOrDie(), manager.Options{})
if err != nil {
log.Fatal(err, "unable to set up controller manager")
}

// Establish k8s core & dynamic client connections.
clients, err := k8s.NewClients(ctx.Config.Kubeconfig, ctx.Config.InCluster)
clients, err := k8s.NewClients(ctx.Config.Kubeconfig, ctx.Config.InCluster, mgr.GetScheme())
if err != nil {
return fmt.Errorf("failed to create Kubernetes clients: %w", err)
}
Expand Down Expand Up @@ -236,7 +243,7 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {

// Before we can build the event handler, we need to initialize the converter we'll
// use to convert from Unstructured.
converter, err := k8s.NewUnstructuredConverter()
converter, err := k8s.NewUnstructuredConverter(mgr.GetScheme())
if err != nil {
return err
}
Expand Down Expand Up @@ -369,12 +376,6 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
log.WithField("context", "envoy-client-certificate").Infof("enabled client certificate with secret: %q", clientCert)
}

// Setup a Manager
mgr, err := manager.New(controller_config.GetConfigOrDie(), manager.Options{})
if err != nil {
log.Fatal(err, "unable to set up controller manager")
}

// Build the core Kubernetes event handler.
eventHandler := &contour.EventHandler{
HoldoffDelay: 100 * time.Millisecond,
Expand All @@ -394,17 +395,17 @@ func doServe(log logrus.FieldLogger, ctx *serveContext) error {
Logger: log.WithField("context", "dynamicHandler"),
}

err = contour_api_v1.AddToScheme(mgr.GetScheme())
if err != nil {
log.Error(err, "unable to add Contour V1 API to scheme.")
os.Exit(1)
}

err = contour_api_v1alpha1.AddToScheme(mgr.GetScheme())
if err != nil {
log.Error(err, "unable to add Contour Alpha1 API to scheme.")
os.Exit(1)
}
//err = contour_api_v1.AddToScheme(mgr.GetScheme())
//if err != nil {
// log.Error(err, "unable to add Contour V1 API to scheme.")
// os.Exit(1)
//}
//
//err = contour_api_v1alpha1.AddToScheme(mgr.GetScheme())
//if err != nil {
// log.Error(err, "unable to add Contour Alpha1 API to scheme.")
// os.Exit(1)
//}

// Inform on DefaultResources by setting up each controller and registering the watch event
// with controller-runtime.
Expand Down
8 changes: 5 additions & 3 deletions internal/k8s/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package k8s
import (
"context"

"k8s.io/apimachinery/pkg/runtime"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
Expand All @@ -39,13 +41,13 @@ type Clients struct {
// NewClients returns a new set of the various API clients required
// by Contour using the supplied kubeconfig path, or the cluster
// environment variables if inCluster is true.
func NewClients(kubeconfig string, inCluster bool) (*Clients, error) {
func NewClients(kubeconfig string, inCluster bool, s *runtime.Scheme) (*Clients, error) {
config, err := newRestConfig(kubeconfig, inCluster)
if err != nil {
return nil, err
}

scheme, err := NewContourScheme()
err = NewContourScheme(s)
if err != nil {
return nil, err
}
Expand All @@ -67,7 +69,7 @@ func NewClients(kubeconfig string, inCluster bool) (*Clients, error) {
}

clients.cache, err = cache.New(config, cache.Options{
Scheme: scheme,
Scheme: s,
Mapper: clients.RESTMapper,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/k8s/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ type UnstructuredConverter struct {
}

// NewUnstructuredConverter returns a new UnstructuredConverter initialized
func NewUnstructuredConverter() (*UnstructuredConverter, error) {
s, err := NewContourScheme()
func NewUnstructuredConverter(s *runtime.Scheme) (*UnstructuredConverter, error) {
err := NewContourScheme(s)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/k8s/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"errors"
"testing"

"k8s.io/apimachinery/pkg/runtime"

contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
contour_api_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
"github.com/stretchr/testify/assert"
Expand All @@ -39,7 +41,7 @@ func TestConvertUnstructured(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Helper()

converter, err := NewUnstructuredConverter()
converter, err := NewUnstructuredConverter(runtime.NewScheme())
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 5 additions & 7 deletions internal/k8s/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
gatewayapi_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
)

// NewContourScheme returns a scheme that includes all the API types
// that Contour supports as well as the core Kubernetes API types from
// the default scheme.
func NewContourScheme() (*runtime.Scheme, error) {
s := runtime.NewScheme()
// NewContourScheme configures a scheme that includes all the API types
// that Contour supports as well as the core Kubernetes API types.
func NewContourScheme(s *runtime.Scheme) error {
b := runtime.SchemeBuilder{
contour_api_v1.AddToScheme,
contour_api_v1alpha1.AddToScheme,
Expand All @@ -36,8 +34,8 @@ func NewContourScheme() (*runtime.Scheme, error) {
}

if err := b.AddToScheme(s); err != nil {
return nil, err
return err
}

return s, nil
return nil
}
4 changes: 3 additions & 1 deletion internal/k8s/statusaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package k8s
import (
"testing"

"k8s.io/apimachinery/pkg/runtime"

contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/annotation"
"github.com/projectcontour/contour/internal/fixture"
Expand Down Expand Up @@ -182,7 +184,7 @@ func TestStatusAddressUpdater(t *testing.T) {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
emptyLBStatus := v1.LoadBalancerStatus{}
converter, err := NewUnstructuredConverter()
converter, err := NewUnstructuredConverter(runtime.NewScheme())
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 4885db6

Please sign in to comment.