Skip to content

Commit

Permalink
Rename and refactor
Browse files Browse the repository at this point in the history
Signed-off-by: danehans <[email protected]>
  • Loading branch information
danehans committed Jul 28, 2022
1 parent 21423a4 commit a5712ee
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 91 deletions.
38 changes: 0 additions & 38 deletions internal/infrastructure/context.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const (

type Kind string

// Context holds all the translated Infra IR resources and provides
// Infra holds all the translated Infra IR resources and provides
// the scaffolding for the managing Kubernetes infrastructure.
type Context struct {
type Infra struct {
Client client.Client
Log logr.Logger
Resources *Resources
Expand All @@ -32,9 +32,9 @@ type Resources struct {
ServiceAccount *corev1.ServiceAccount
}

// NewContext returns a new Context.
func NewContext(cli client.Client, cfg *config.Server) *Context {
return &Context{
// NewInfra returns a new Infra.
func NewInfra(cli client.Client, cfg *config.Server) *Infra {
return &Infra{
Client: cli,
Log: cfg.Logger,
Resources: &Resources{
Expand All @@ -45,9 +45,9 @@ func NewContext(cli client.Client, cfg *config.Server) *Context {

// addResource adds the resource to the context resources, using kind to
// identify the object kind to add.
func (c *Context) addResource(kind Kind, obj client.Object) error {
if c.Resources == nil {
c.Resources = new(Resources)
func (i *Infra) addResource(kind Kind, obj client.Object) error {
if i.Resources == nil {
i.Resources = new(Resources)
}

switch kind {
Expand All @@ -56,16 +56,16 @@ func (c *Context) addResource(kind Kind, obj client.Object) error {
if !ok {
return fmt.Errorf("unexpected object kind %s", obj.GetObjectKind())
}
c.Resources.ServiceAccount = sa
i.Resources.ServiceAccount = sa
default:
return fmt.Errorf("unexpected object kind %s", obj.GetObjectKind())
}

return nil
}

// CreateIfNeeded creates the managed kube infra if it doesn't exist.
func (c *Context) CreateIfNeeded(ctx context.Context, infra *ir.Infra) error {
// CreateInfra creates the managed kube infra if it doesn't exist.
func (i *Infra) CreateInfra(ctx context.Context, infra *ir.Infra) error {
if infra == nil {
return errors.New("infra ir is nil")
}
Expand All @@ -74,13 +74,13 @@ func (c *Context) CreateIfNeeded(ctx context.Context, infra *ir.Infra) error {
return errors.New("infra proxy ir is nil")
}

if c.Resources == nil {
c.Resources = &Resources{
if i.Resources == nil {
i.Resources = &Resources{
ServiceAccount: new(corev1.ServiceAccount),
}
}

if err := c.createServiceAccountIfNeeded(ctx, infra); err != nil {
if err := i.createServiceAccountIfNeeded(ctx, infra); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCreateIfNeeded(t *testing.T) {
logger, err := log.NewLogger()
require.NoError(t, err)

kubeCtx := Context{Log: logger}
kube := Infra{Log: logger}

testCases := []struct {
name string
Expand Down Expand Up @@ -87,13 +87,13 @@ func TestCreateIfNeeded(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
kubeCtx.Client = fakeclient.NewClientBuilder().WithScheme(envoygateway.GetScheme()).WithObjects(tc.out.ServiceAccount).Build()
err := kubeCtx.CreateIfNeeded(context.Background(), tc.in)
kube.Client = fakeclient.NewClientBuilder().WithScheme(envoygateway.GetScheme()).WithObjects(tc.out.ServiceAccount).Build()
err := kube.CreateInfra(context.Background(), tc.in)
if !tc.expect {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tc.out, kubeCtx.Resources)
require.Equal(t, tc.out, kube.Resources)
}
})
}
Expand All @@ -103,7 +103,7 @@ func TestAddResource(t *testing.T) {
logger, err := log.NewLogger()
require.NoError(t, err)

kubeCtx := Context{Log: logger}
kube := Infra{Log: logger}

testCases := []struct {
name string
Expand Down Expand Up @@ -133,10 +133,10 @@ func TestAddResource(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
kubeCtx.Client = fakeclient.NewClientBuilder().WithScheme(envoygateway.GetScheme()).Build()
err := kubeCtx.addResource(tc.kind, tc.obj)
kube.Client = fakeclient.NewClientBuilder().WithScheme(envoygateway.GetScheme()).Build()
err := kube.addResource(tc.kind, tc.obj)
require.NoError(t, err)
require.Equal(t, tc.out, kubeCtx.Resources)
require.Equal(t, tc.out, kube.Resources)
})
}
}
22 changes: 11 additions & 11 deletions internal/infrastructure/kubernetes/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// createServiceAccountIfNeeded creates a serviceaccount based on the provided infra, if
// it doesn't exist in the kube api server.
func (c *Context) createServiceAccountIfNeeded(ctx context.Context, infra *ir.Infra) error {
func (i *Infra) createServiceAccountIfNeeded(ctx context.Context, infra *ir.Infra) error {
if infra == nil {
return errors.New("infra ir is nil")
}
Expand All @@ -24,46 +24,46 @@ func (c *Context) createServiceAccountIfNeeded(ctx context.Context, infra *ir.In
return errors.New("proxy infra ir is nil")
}

current, err := c.getServiceAccount(ctx, infra)
current, err := i.getServiceAccount(ctx, infra)
if err != nil {
if kerrors.IsNotFound(err) {
sa, err := c.createServiceAccount(ctx, infra)
sa, err := i.createServiceAccount(ctx, infra)
if err != nil {
return err
}
if err := c.addResource(KindServiceAccount, sa); err != nil {
if err := i.addResource(KindServiceAccount, sa); err != nil {
return err
}
return nil
}
return err
}

if err := c.addResource(KindServiceAccount, current); err != nil {
if err := i.addResource(KindServiceAccount, current); err != nil {
return err
}

return nil
}

// getServiceAccount gets the ServiceAccount from the kube api for the provided infra.
func (c *Context) getServiceAccount(ctx context.Context, infra *ir.Infra) (*corev1.ServiceAccount, error) {
func (i *Infra) getServiceAccount(ctx context.Context, infra *ir.Infra) (*corev1.ServiceAccount, error) {
ns := infra.Proxy.Namespace
name := infra.Proxy.Name
key := types.NamespacedName{
Namespace: ns,
Name: name,
}
sa := new(corev1.ServiceAccount)
if err := c.Client.Get(ctx, key, sa); err != nil {
if err := i.Client.Get(ctx, key, sa); err != nil {
return nil, fmt.Errorf("failed to get serviceaccount %s/%s: %w", ns, name, err)
}

return sa, nil
}

// expectedServiceAccount returns the expected proxy serviceAccount based on the provided infra.
func (c *Context) expectedServiceAccount(infra *ir.Infra) *corev1.ServiceAccount {
func (i *Infra) expectedServiceAccount(infra *ir.Infra) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
Expand All @@ -77,9 +77,9 @@ func (c *Context) expectedServiceAccount(infra *ir.Infra) *corev1.ServiceAccount
}

// createServiceAccount creates sa in the kube api server if it doesn't exist.
func (c *Context) createServiceAccount(ctx context.Context, infra *ir.Infra) (*corev1.ServiceAccount, error) {
expected := c.expectedServiceAccount(infra)
err := c.Client.Create(ctx, expected)
func (i *Infra) createServiceAccount(ctx context.Context, infra *ir.Infra) (*corev1.ServiceAccount, error) {
expected := i.expectedServiceAccount(infra)
err := i.Client.Create(ctx, expected)
if err != nil {
if kerrors.IsAlreadyExists(err) {
return expected, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/kubernetes/serviceaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCreateServiceAccountIfNeeded(t *testing.T) {
logger, err := log.NewLogger()
require.NoError(t, err)

kubeCtx := Context{Log: logger}
kubeCtx := Infra{Log: logger}

testCases := []struct {
name string
Expand Down
43 changes: 43 additions & 0 deletions internal/infrastructure/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package infrastructure

import (
"errors"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"
clicfg "sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/envoyproxy/gateway/api/config/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes"
)

// Manager provides the scaffolding for managing infrastructure.
type Manager struct {
Kubernetes *kubernetes.Infra
}

// NewManager returns a new infrastructure Manager.
func NewManager(cfg *config.Server) (*Manager, error) {
if cfg == nil {
return nil, errors.New("server config is nil")
}

mgr := new(Manager)

switch {
case cfg.EnvoyGateway == nil ||
cfg.EnvoyGateway.Provider == nil ||
cfg.EnvoyGateway.Provider.Type == v1alpha1.ProviderTypeKubernetes:
cli, err := client.New(clicfg.GetConfigOrDie(), client.Options{})
if err != nil {
return nil, err
}
mgr.Kubernetes = kubernetes.NewInfra(cli, cfg)
default:
// Unsupported provider type.
return nil, fmt.Errorf("unsupported provider type %v", cfg.EnvoyGateway.Provider.Type)
}

return mgr, nil
}
28 changes: 9 additions & 19 deletions internal/infrastructure/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,41 @@ import (
"errors"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"
clicfg "sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/envoyproxy/gateway/api/config/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes"
"github.com/envoyproxy/gateway/internal/ir"
)

// Translate translates the provided infra ir into managed infrastructure.
func Translate(ctx context.Context, cfg *config.Server, infra *ir.Infra) (*Context, error) {
func Translate(ctx context.Context, cfg *config.Server, infra *ir.Infra) (*Manager, error) {
if err := ir.ValidateInfra(infra); err != nil {
return nil, err
}

if cfg == nil {
return nil, errors.New("server config is nil")
}

infraCtx, err := NewContext(cfg)
mgr, err := NewManager(cfg)
if err != nil {
return nil, err
}

log := cfg.Logger

if *infraCtx.Provider == v1alpha1.ProviderTypeKubernetes {
if cfg.EnvoyGateway == nil ||
cfg.EnvoyGateway.Provider == nil ||
cfg.EnvoyGateway.Provider.Type == v1alpha1.ProviderTypeKubernetes {
log.Info("Using infra manager", "type", v1alpha1.ProviderTypeKubernetes)
cli, err := client.New(clicfg.GetConfigOrDie(), client.Options{})
if err != nil {
return nil, err
}
kubeCtx := kubernetes.NewContext(cli, cfg)
if err != nil {
return nil, fmt.Errorf("failed to create kube infra manager context: %v", err)
}
infraCtx.Kubernetes = kubeCtx

// A nil infra proxy ir means the proxy infra should be deleted, but metadata is
// required to know the ns/name of the resources to delete. Add support for deleting
// the infra when https://github.com/envoyproxy/gateway/issues/173 is resolved.

if err := infraCtx.Kubernetes.CreateIfNeeded(ctx, infra); err != nil {
return nil, fmt.Errorf("failed to create infra: %v", err)
if err := mgr.Kubernetes.CreateInfra(ctx, infra); err != nil {
return nil, fmt.Errorf("failed to create kube infra: %v", err)
}
return infraCtx, nil
return mgr, nil
}

return nil, fmt.Errorf("unsupported infra manager type %v", cfg.EnvoyGateway.Provider.Type)
Expand Down

0 comments on commit a5712ee

Please sign in to comment.