Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: followup kubernetes/infrastruture refactoring. #1337

Merged
merged 11 commits into from
Apr 25, 2023
187 changes: 0 additions & 187 deletions internal/infrastructure/kubernetes/applier/apply.go

This file was deleted.

129 changes: 16 additions & 113 deletions internal/infrastructure/kubernetes/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ package kubernetes
import (
"context"

"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

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

var (
_ ResourceRender = &proxy.ResourceRender{}
_ ResourceRender = &ratelimit.ResourceRender{}
)

// ResourceRender renders Kubernetes infrastructure resources
Expand All @@ -38,153 +30,64 @@ type ResourceRender interface {
// Infra manages the creation and deletion of Kubernetes infrastructure
// based on Infra IR resources.
type Infra struct {
Client client.Client

// Namespace is the Namespace used for managed infra.
Namespace string

// EnvoyGateway is the configuration used to startup Envoy Gateway.
EnvoyGateway *v1alpha1.EnvoyGateway
applier *applier.Instance

// Client wrap k8s client.
Client *InfraClient
}

// NewInfra returns a new Infra.
func NewInfra(cli client.Client, cfg *config.Server) *Infra {
return &Infra{
Client: cli,
Namespace: cfg.Namespace,
EnvoyGateway: cfg.EnvoyGateway,
applier: applier.New(cli),
Client: New(cli),
}
}

// createOrUpdate creates a ServiceAccount/ConfigMap/Deployment/Service in the kube api server based on the
// provided ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdate(ctx context.Context, r ResourceRender) error {
if err := i.createOrUpdateServiceAccount(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to create or update serviceaccount %s/%s", i.Namespace, r.Name())
}

if err := i.createOrUpdateConfigMap(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to create or update configmap %s/%s", i.Namespace, r.Name())
}

if err := i.createOrUpdateDeployment(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to create or update deployment %s/%s", i.Namespace, r.Name())
}

if err := i.createOrUpdateService(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to create or update service %s/%s", i.Namespace, r.Name())
}

return nil
}

// createOrUpdateServiceAccount creates a ServiceAccount in the kube api server based on the
// provided ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdateServiceAccount(ctx context.Context, r ResourceRender) error {
sa, err := r.ServiceAccount()
if err != nil {
return err
}
return i.applier.CreateOrUpdateServiceAccount(ctx, sa)
}

// createOrUpdateConfigMap creates a ConfigMap in the Kube api server based on the provided
// ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdateConfigMap(ctx context.Context, r ResourceRender) error {
cm, err := r.ConfigMap()
if err != nil {
return err
}

return i.applier.CreateOrUpdateConfigMap(ctx, cm)
}

// createOrUpdateDeployment creates a Deployment in the kube api server based on the provided
// ResourceRender, if it doesn't exist and updates it if it does.
func (i *Infra) createOrUpdateDeployment(ctx context.Context, r ResourceRender) error {
deployment, err := r.Deployment()
if err != nil {
return err
}
return i.applier.CreateOrUpdateDeployment(ctx, deployment)
}

// createOrUpdateRateLimitService creates a Service in the kube api server based on the provided ResourceRender,
// if it doesn't exist or updates it if it does.
func (i *Infra) createOrUpdateService(ctx context.Context, r ResourceRender) error {
svc, err := r.Service()
if err != nil {
return err
}

return i.applier.CreateOrUpdateService(ctx, svc)
}

// delete deletes the ServiceAccount/ConfigMap/Deployment/Service in the kube api server, if it exists.
func (i *Infra) delete(ctx context.Context, r ResourceRender) error {
if err := i.deleteServiceAccount(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to delete serviceaccount %s/%s", i.Namespace, r.Name())
}

if err := i.deleteConfigMap(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to delete configmap %s/%s", i.Namespace, r.Name())
}

if err := i.deleteDeployment(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to delete deployment %s/%s", i.Namespace, r.Name())
}

if err := i.deleteService(ctx, r); err != nil {
return err
return errors.Wrapf(err, "failed to delete service %s/%s", i.Namespace, r.Name())
}

return nil
}

// deleteServiceAccount deletes the ServiceAccount in the kube api server,
// if it exists.
func (i *Infra) deleteServiceAccount(ctx context.Context, r ResourceRender) error {
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}

return i.applier.DeleteServiceAccount(ctx, sa)
}

// deleteDeployment deletes the Envoy Deployment in the kube api server, if it exists.
func (i *Infra) deleteDeployment(ctx context.Context, r ResourceRender) error {
deploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}

return i.applier.DeleteDeployment(ctx, deploy)
}

// deleteConfigMap deletes the ConfigMap in the kube api server, if it exists.
func (i *Infra) deleteConfigMap(ctx context.Context, r ResourceRender) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}

return i.applier.DeleteConfigMap(ctx, cm)
}

// deleteService deletes the Service in the kube api server, if it exists.
func (i *Infra) deleteService(ctx context.Context, r ResourceRender) error {
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: i.Namespace,
Name: r.Name(),
},
}

return i.applier.DeleteService(ctx, svc)
}
Loading