diff --git a/internal/infrastructure/kubernetes/applier/apply.go b/internal/infrastructure/kubernetes/applier/apply.go deleted file mode 100644 index 6aa7f751d6c..00000000000 --- a/internal/infrastructure/kubernetes/applier/apply.go +++ /dev/null @@ -1,187 +0,0 @@ -// Copyright Envoy Gateway Authors -// SPDX-License-Identifier: Apache-2.0 -// The full text of the Apache license is available in the LICENSE file at -// the root of the repo. - -package applier - -import ( - "context" - "fmt" - "reflect" - - appsv1 "k8s.io/api/apps/v1" - corev1 "k8s.io/api/core/v1" - kerrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -type Instance struct { - Client client.Client -} - -func New(cli client.Client) *Instance { - return &Instance{ - Client: cli, - } -} - -func (i *Instance) CreateOrUpdateConfigMap(ctx context.Context, cm *corev1.ConfigMap) error { - current := &corev1.ConfigMap{} - key := types.NamespacedName{ - Namespace: cm.Namespace, - Name: cm.Name, - } - - if err := i.Client.Get(ctx, key, current); err != nil { - // Create if not found. - if kerrors.IsNotFound(err) { - if err := i.Client.Create(ctx, cm); err != nil { - return fmt.Errorf("failed to create configmap %s/%s: %w", cm.Namespace, cm.Name, err) - } - } - } else { - // Update if current value is different. - if !reflect.DeepEqual(cm.Data, current.Data) { - cm.ResourceVersion = current.ResourceVersion - cm.UID = current.UID - if err := i.Client.Update(ctx, cm); err != nil { - return fmt.Errorf("failed to update configmap %s/%s: %w", cm.Namespace, cm.Name, err) - } - } - } - - return nil -} - -func (i *Instance) DeleteConfigMap(ctx context.Context, cm *corev1.ConfigMap) error { - if err := i.Client.Delete(ctx, cm); err != nil { - if kerrors.IsNotFound(err) { - return nil - } - return fmt.Errorf("failed to delete configmap %s/%s: %w", cm.Namespace, cm.Name, err) - } - - return nil -} - -func (i *Instance) CreateOrUpdateDeployment(ctx context.Context, deployment *appsv1.Deployment) error { - current := &appsv1.Deployment{} - key := types.NamespacedName{ - Namespace: deployment.Namespace, - Name: deployment.Name, - } - if err := i.Client.Get(ctx, key, current); err != nil { - // Create if not found. - if kerrors.IsNotFound(err) { - if err := i.Client.Create(ctx, deployment); err != nil { - return fmt.Errorf("failed to create deployment %s/%s: %w", - deployment.Namespace, deployment.Name, err) - } - } - } else { - // Update if current value is different. - if !reflect.DeepEqual(deployment.Spec, current.Spec) { - deployment.ResourceVersion = current.ResourceVersion - deployment.UID = current.UID - if err := i.Client.Update(ctx, deployment); err != nil { - return fmt.Errorf("failed to update deployment %s/%s: %w", - deployment.Namespace, deployment.Name, err) - } - } - } - - return nil -} - -func (i *Instance) DeleteDeployment(ctx context.Context, deploy *appsv1.Deployment) error { - if err := i.Client.Delete(ctx, deploy); err != nil { - if kerrors.IsNotFound(err) { - return nil - } - return fmt.Errorf("failed to delete deployment %s/%s: %w", deploy.Namespace, deploy.Name, err) - } - return nil -} - -func (i *Instance) CreateOrUpdateService(ctx context.Context, svc *corev1.Service) error { - current := &corev1.Service{} - key := types.NamespacedName{ - Namespace: svc.Namespace, - Name: svc.Name, - } - - if err := i.Client.Get(ctx, key, current); err != nil { - // Create if not found. - if kerrors.IsNotFound(err) { - if err := i.Client.Create(ctx, svc); err != nil { - return fmt.Errorf("failed to create service %s/%s: %w", - svc.Namespace, svc.Name, err) - } - } - } else { - // Update if current value is different. - if !reflect.DeepEqual(svc.Spec, current.Spec) { - svc.ResourceVersion = current.ResourceVersion - svc.UID = current.UID - if err := i.Client.Update(ctx, svc); err != nil { - return fmt.Errorf("failed to update service %s/%s: %w", - svc.Namespace, svc.Name, err) - } - } - } - - return nil -} - -func (i *Instance) DeleteService(ctx context.Context, svc *corev1.Service) error { - if err := i.Client.Delete(ctx, svc); err != nil { - if kerrors.IsNotFound(err) { - return nil - } - return fmt.Errorf("failed to delete service %s/%s: %w", svc.Namespace, svc.Name, err) - } - - return nil -} - -func (i *Instance) CreateOrUpdateServiceAccount(ctx context.Context, sa *corev1.ServiceAccount) error { - current := &corev1.ServiceAccount{} - key := types.NamespacedName{ - Namespace: sa.Namespace, - Name: sa.Name, - } - - if err := i.Client.Get(ctx, key, current); err != nil { - if kerrors.IsNotFound(err) { - // Create if it does not exist. - if err := i.Client.Create(ctx, sa); err != nil { - return fmt.Errorf("failed to create serviceaccount %s/%s: %w", - sa.Namespace, sa.Name, err) - } - } - } else { - // Since the ServiceAccount does not have a specific Spec field to compare - // just perform an update for now. - sa.ResourceVersion = current.ResourceVersion - sa.UID = current.UID - if err := i.Client.Update(ctx, sa); err != nil { - return fmt.Errorf("failed to update serviceaccount %s/%s: %w", - sa.Namespace, sa.Name, err) - } - } - - return nil -} - -func (i *Instance) DeleteServiceAccount(ctx context.Context, sa *corev1.ServiceAccount) error { - if err := i.Client.Delete(ctx, sa); err != nil { - if kerrors.IsNotFound(err) { - return nil - } - return fmt.Errorf("failed to delete serviceaccount %s/%s: %w", sa.Namespace, sa.Name, err) - } - - return nil -} diff --git a/internal/infrastructure/kubernetes/infra.go b/internal/infrastructure/kubernetes/infra.go index ee81eba89d5..632006af6aa 100644 --- a/internal/infrastructure/kubernetes/infra.go +++ b/internal/infrastructure/kubernetes/infra.go @@ -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 @@ -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) -} diff --git a/internal/infrastructure/kubernetes/infra_client.go b/internal/infrastructure/kubernetes/infra_client.go new file mode 100644 index 00000000000..a94757549e7 --- /dev/null +++ b/internal/infrastructure/kubernetes/infra_client.go @@ -0,0 +1,57 @@ +// Copyright Envoy Gateway Authors +// SPDX-License-Identifier: Apache-2.0 +// The full text of the Apache license is available in the LICENSE file at +// the root of the repo. + +package kubernetes + +import ( + "context" + + kerrors "k8s.io/apimachinery/pkg/api/errors" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type InfraClient struct { + client.Client +} + +func New(cli client.Client) *InfraClient { + return &InfraClient{ + Client: cli, + } +} + +func (cli *InfraClient) Create(ctx context.Context, key client.ObjectKey, current client.Object, specific client.Object, updateChecker func() bool) error { + if err := cli.Client.Get(ctx, key, current); err != nil { + if kerrors.IsNotFound(err) { + // Create if it does not exist. + if err := cli.Client.Create(ctx, specific); err != nil { + return err + } + } + } else { + // Since the client.Object does not have a specific Spec field to compare + // just perform an update for now. + if updateChecker() { + specific.SetResourceVersion(current.GetResourceVersion()) + specific.SetUID(current.GetUID()) + if err := cli.Client.Update(ctx, specific); err != nil { + return err + } + } + } + + return nil +} + +func (cli *InfraClient) Delete(ctx context.Context, object client.Object) error { + if err := cli.Client.Delete(ctx, object); err != nil { + if kerrors.IsNotFound(err) { + return nil + } + return err + } + + return nil +} diff --git a/internal/infrastructure/kubernetes/infra_resource.go b/internal/infrastructure/kubernetes/infra_resource.go new file mode 100644 index 00000000000..798d5650bd6 --- /dev/null +++ b/internal/infrastructure/kubernetes/infra_resource.go @@ -0,0 +1,140 @@ +// Copyright Envoy Gateway Authors +// SPDX-License-Identifier: Apache-2.0 +// The full text of the Apache license is available in the LICENSE file at +// the root of the repo. + +package kubernetes + +import ( + "context" + "reflect" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +// 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 + } + + current := &corev1.ServiceAccount{} + key := types.NamespacedName{ + Namespace: sa.Namespace, + Name: sa.Name, + } + + return i.Client.Create(ctx, key, current, sa, func() bool { + return true + }) +} + +// 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 + } + + current := &corev1.ConfigMap{} + key := types.NamespacedName{ + Namespace: cm.Namespace, + Name: cm.Name, + } + + return i.Client.Create(ctx, key, current, cm, func() bool { + return !reflect.DeepEqual(cm.Data, current.Data) + }) +} + +// 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 + } + + current := &appsv1.Deployment{} + key := types.NamespacedName{ + Namespace: deployment.Namespace, + Name: deployment.Name, + } + + return i.Client.Create(ctx, key, current, deployment, func() bool { + return !reflect.DeepEqual(deployment.Spec, current.Spec) + }) +} + +// 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 + } + + current := &corev1.Service{} + key := types.NamespacedName{ + Namespace: svc.Namespace, + Name: svc.Name, + } + + return i.Client.Create(ctx, key, current, svc, func() bool { + return !reflect.DeepEqual(svc.Spec, current.Spec) + }) +} + +// 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.Client.Delete(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 { + deployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: i.Namespace, + Name: r.Name(), + }, + } + + return i.Client.Delete(ctx, deployment) +} + +// 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.Client.Delete(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.Client.Delete(ctx, svc) +} diff --git a/internal/infrastructure/kubernetes/proxy/utils.go b/internal/infrastructure/kubernetes/proxy/resource.go similarity index 86% rename from internal/infrastructure/kubernetes/proxy/utils.go rename to internal/infrastructure/kubernetes/proxy/resource.go index 2ff967d56df..55198f4aa12 100644 --- a/internal/infrastructure/kubernetes/proxy/utils.go +++ b/internal/infrastructure/kubernetes/proxy/resource.go @@ -36,6 +36,7 @@ var ( `"private_key":{"filename":"%s"}}}]}`, XdsTLSCertFilename, XdsTLSKeyFilename) ) +// ExpectedResourceHashedName returns expected resource hashed name. func ExpectedResourceHashedName(name string) string { hashedName := providerutils.GetHashedName(name) return fmt.Sprintf("%s-%s", config.EnvoyPrefix, hashedName) @@ -48,12 +49,12 @@ func EnvoyAppLabel() map[string]string { } } -// EnvoyLabels returns the labels, including extraLbls, used for Envoy resources. -func EnvoyLabels(extraLbls map[string]string) map[string]string { - lbls := EnvoyAppLabel() - for k, v := range extraLbls { - lbls[k] = v +// envoyLabels returns the labels, including extraLabels, used for Envoy resources. +func envoyLabels(extraLabels map[string]string) map[string]string { + labels := EnvoyAppLabel() + for k, v := range extraLabels { + labels[k] = v } - return lbls + return labels } diff --git a/internal/infrastructure/kubernetes/proxy/resource_provider.go b/internal/infrastructure/kubernetes/proxy/resource_provider.go index 715d38713dd..54472bf1db0 100644 --- a/internal/infrastructure/kubernetes/proxy/resource_provider.go +++ b/internal/infrastructure/kubernetes/proxy/resource_provider.go @@ -16,7 +16,7 @@ import ( egcfgv1a1 "github.com/envoyproxy/gateway/api/config/v1alpha1" "github.com/envoyproxy/gateway/internal/gatewayapi" - "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/utils" + "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/resource" "github.com/envoyproxy/gateway/internal/ir" "github.com/envoyproxy/gateway/internal/xds/bootstrap" ) @@ -31,13 +31,13 @@ const ( ) type ResourceRender struct { - infra *ir.Infra + infra *ir.ProxyInfra // Namespace is the Namespace used for managed infra. Namespace string } -func NewResourceRender(ns string, infra *ir.Infra) *ResourceRender { +func NewResourceRender(ns string, infra *ir.ProxyInfra) *ResourceRender { return &ResourceRender{ Namespace: ns, infra: infra, @@ -45,13 +45,13 @@ func NewResourceRender(ns string, infra *ir.Infra) *ResourceRender { } func (r *ResourceRender) Name() string { - return ExpectedResourceHashedName(r.infra.Proxy.Name) + return ExpectedResourceHashedName(r.infra.Name) } // ServiceAccount returns the expected proxy serviceAccount. func (r *ResourceRender) ServiceAccount() (*corev1.ServiceAccount, error) { // Set the labels based on the owning gateway name. - labels := EnvoyLabels(r.infra.GetProxyInfra().GetProxyMetadata().Labels) + labels := envoyLabels(r.infra.GetProxyMetadata().Labels) if len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 { return nil, fmt.Errorf("missing owning gateway labels") } @@ -63,7 +63,7 @@ func (r *ResourceRender) ServiceAccount() (*corev1.ServiceAccount, error) { }, ObjectMeta: metav1.ObjectMeta{ Namespace: r.Namespace, - Name: ExpectedResourceHashedName(r.infra.Proxy.Name), + Name: ExpectedResourceHashedName(r.infra.Name), Labels: labels, }, }, nil @@ -72,7 +72,7 @@ func (r *ResourceRender) ServiceAccount() (*corev1.ServiceAccount, error) { // Service returns the expected Service based on the provided infra. func (r *ResourceRender) Service() (*corev1.Service, error) { var ports []corev1.ServicePort - for _, listener := range r.infra.Proxy.Listeners { + for _, listener := range r.infra.Listeners { for _, port := range listener.Ports { target := intstr.IntOrString{IntVal: port.ContainerPort} protocol := corev1.ProtocolTCP @@ -90,21 +90,21 @@ func (r *ResourceRender) Service() (*corev1.Service, error) { } // Set the labels based on the owning gatewayclass name. - labels := EnvoyLabels(r.infra.GetProxyInfra().GetProxyMetadata().Labels) + labels := envoyLabels(r.infra.GetProxyMetadata().Labels) if len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 { return nil, fmt.Errorf("missing owning gateway labels") } // Get annotations var annotations map[string]string - provider := r.infra.GetProxyInfra().GetProxyConfig().GetEnvoyProxyProvider() + provider := r.infra.GetProxyConfig().GetEnvoyProxyProvider() envoyServiceConfig := provider.GetEnvoyProxyKubeProvider().EnvoyService if envoyServiceConfig.Annotations != nil { annotations = envoyServiceConfig.Annotations } - serviceSpec := utils.ExpectedServiceSpec(envoyServiceConfig.Type) + serviceSpec := resource.ExpectedServiceSpec(envoyServiceConfig.Type) serviceSpec.Ports = ports - serviceSpec.Selector = utils.GetSelector(labels).MatchLabels + serviceSpec.Selector = resource.GetSelector(labels).MatchLabels svc := &corev1.Service{ TypeMeta: metav1.TypeMeta{ @@ -113,7 +113,7 @@ func (r *ResourceRender) Service() (*corev1.Service, error) { }, ObjectMeta: metav1.ObjectMeta{ Namespace: r.Namespace, - Name: ExpectedResourceHashedName(r.infra.Proxy.Name), + Name: ExpectedResourceHashedName(r.infra.Name), Labels: labels, Annotations: annotations, }, @@ -126,7 +126,7 @@ func (r *ResourceRender) Service() (*corev1.Service, error) { // ConfigMap returns the expected ConfigMap based on the provided infra. func (r *ResourceRender) ConfigMap() (*corev1.ConfigMap, error) { // Set the labels based on the owning gateway name. - labels := EnvoyLabels(r.infra.GetProxyInfra().GetProxyMetadata().Labels) + labels := envoyLabels(r.infra.GetProxyMetadata().Labels) if len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 { return nil, fmt.Errorf("missing owning gateway labels") } @@ -138,7 +138,7 @@ func (r *ResourceRender) ConfigMap() (*corev1.ConfigMap, error) { }, ObjectMeta: metav1.ObjectMeta{ Namespace: r.Namespace, - Name: ExpectedResourceHashedName(r.infra.Proxy.Name), + Name: ExpectedResourceHashedName(r.infra.Name), Labels: labels, }, Data: map[string]string{ @@ -148,10 +148,10 @@ func (r *ResourceRender) ConfigMap() (*corev1.ConfigMap, error) { }, nil } -// ExpectedDeployment returns the expected Deployment based on the provided infra. +// Deployment returns the expected Deployment based on the provided infra. func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { // Get the EnvoyProxy config to configure the deployment. - provider := r.infra.GetProxyInfra().GetProxyConfig().GetEnvoyProxyProvider() + provider := r.infra.GetProxyConfig().GetEnvoyProxyProvider() if provider.Type != egcfgv1a1.ProviderTypeKubernetes { return nil, fmt.Errorf("invalid provider type %v for Kubernetes infra manager", provider.Type) } @@ -164,12 +164,12 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { } // Set the labels based on the owning gateway name. - labels := EnvoyLabels(r.infra.GetProxyInfra().GetProxyMetadata().Labels) + labels := envoyLabels(r.infra.GetProxyMetadata().Labels) if len(labels[gatewayapi.OwningGatewayNamespaceLabel]) == 0 || len(labels[gatewayapi.OwningGatewayNameLabel]) == 0 { return nil, fmt.Errorf("missing owning gateway labels") } - selector := utils.GetSelector(labels) + selector := resource.GetSelector(labels) // Get annotations var annotations map[string]string @@ -184,7 +184,7 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { }, ObjectMeta: metav1.ObjectMeta{ Namespace: r.Namespace, - Name: ExpectedResourceHashedName(r.infra.Proxy.Name), + Name: ExpectedResourceHashedName(r.infra.Name), Labels: labels, }, Spec: appsv1.DeploymentSpec{ @@ -197,7 +197,7 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { }, Spec: corev1.PodSpec{ Containers: containers, - ServiceAccountName: ExpectedResourceHashedName(r.infra.Proxy.Name), + ServiceAccountName: ExpectedResourceHashedName(r.infra.Name), AutomountServiceAccountToken: pointer.Bool(false), TerminationGracePeriodSeconds: pointer.Int64(int64(300)), DNSPolicy: corev1.DNSClusterFirst, @@ -218,7 +218,7 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ - Name: ExpectedResourceHashedName(r.infra.Proxy.Name), + Name: ExpectedResourceHashedName(r.infra.Name), }, Items: []corev1.KeyToPath{ { @@ -244,12 +244,12 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { return deployment, nil } -func expectedProxyContainers(infra *ir.Infra, deploymentConfig *egcfgv1a1.KubernetesDeploymentSpec) ([]corev1.Container, error) { +func expectedProxyContainers(infra *ir.ProxyInfra, deploymentConfig *egcfgv1a1.KubernetesDeploymentSpec) ([]corev1.Container, error) { // Define slice to hold container ports var ports []corev1.ContainerPort // Iterate over listeners and ports to get container ports - for _, listener := range infra.Proxy.Listeners { + for _, listener := range infra.Listeners { for _, p := range listener.Ports { var protocol corev1.Protocol switch p.Protocol { @@ -272,9 +272,9 @@ func expectedProxyContainers(infra *ir.Infra, deploymentConfig *egcfgv1a1.Kubern var bootstrapConfigurations string // Get Bootstrap from EnvoyProxy API if set by the user // The config should have been validated already - if infra.Proxy.Config != nil && - infra.Proxy.Config.Spec.Bootstrap != nil { - bootstrapConfigurations = *infra.Proxy.Config.Spec.Bootstrap + if infra.Config != nil && + infra.Config.Spec.Bootstrap != nil { + bootstrapConfigurations = *infra.Config.Spec.Bootstrap } else { var err error // Use the default Bootstrap @@ -293,7 +293,7 @@ func expectedProxyContainers(infra *ir.Infra, deploymentConfig *egcfgv1a1.Kubern "envoy", }, Args: []string{ - fmt.Sprintf("--service-cluster %s", infra.Proxy.Name), + fmt.Sprintf("--service-cluster %s", infra.Name), fmt.Sprintf("--service-node $(%s)", envoyPodEnvVar), fmt.Sprintf("--config-yaml %s", bootstrapConfigurations), "--log-level info", diff --git a/internal/infrastructure/kubernetes/proxy/resource_provider_test.go b/internal/infrastructure/kubernetes/proxy/resource_provider_test.go index 2aea42e9ae1..d95444e9d68 100644 --- a/internal/infrastructure/kubernetes/proxy/resource_provider_test.go +++ b/internal/infrastructure/kubernetes/proxy/resource_provider_test.go @@ -121,7 +121,7 @@ func TestDeployment(t *testing.T) { tc.infra.Proxy.Config.Spec.Bootstrap = tc.bootstrap } - r := NewResourceRender(cfg.Namespace, tc.infra) + r := NewResourceRender(cfg.Namespace, tc.infra.GetProxyInfra()) dp, err := r.Deployment() require.NoError(t, err) @@ -176,7 +176,7 @@ func TestService(t *testing.T) { provider.EnvoyService = tc.service } - r := NewResourceRender(cfg.Namespace, tc.infra) + r := NewResourceRender(cfg.Namespace, tc.infra.GetProxyInfra()) svc, err := r.Service() require.NoError(t, err) @@ -204,7 +204,7 @@ func TestConfigMap(t *testing.T) { infra := newTestInfra() - r := NewResourceRender(cfg.Namespace, infra) + r := NewResourceRender(cfg.Namespace, infra.GetProxyInfra()) cm, err := r.ConfigMap() require.NoError(t, err) @@ -230,7 +230,7 @@ func TestServiceAccount(t *testing.T) { infra := newTestInfra() - r := NewResourceRender(cfg.Namespace, infra) + r := NewResourceRender(cfg.Namespace, infra.GetProxyInfra()) sa, err := r.ServiceAccount() require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/proxy/utils_test.go b/internal/infrastructure/kubernetes/proxy/resource_test.go similarity index 96% rename from internal/infrastructure/kubernetes/proxy/utils_test.go rename to internal/infrastructure/kubernetes/proxy/resource_test.go index e1def1a3821..a4a515394cb 100644 --- a/internal/infrastructure/kubernetes/proxy/utils_test.go +++ b/internal/infrastructure/kubernetes/proxy/resource_test.go @@ -30,7 +30,7 @@ func TestEnvoyPodSelector(t *testing.T) { for _, tc := range cases { tc := tc t.Run("", func(t *testing.T) { - got := EnvoyLabels(tc.in) + got := envoyLabels(tc.in) require.Equal(t, tc.expected, got) }) } diff --git a/internal/infrastructure/kubernetes/proxy_configmap_test.go b/internal/infrastructure/kubernetes/proxy_configmap_test.go index adc7044efae..b7c17eccc95 100644 --- a/internal/infrastructure/kubernetes/proxy_configmap_test.go +++ b/internal/infrastructure/kubernetes/proxy_configmap_test.go @@ -98,7 +98,7 @@ func TestCreateOrUpdateProxyConfigMap(t *testing.T) { cli = fakeclient.NewClientBuilder().WithScheme(envoygateway.GetScheme()).Build() } kube := NewInfra(cli, cfg) - r := proxy.NewResourceRender(kube.Namespace, infra) + r := proxy.NewResourceRender(kube.Namespace, infra.GetProxyInfra()) err := kube.createOrUpdateConfigMap(context.Background(), r) require.NoError(t, err) actual := &corev1.ConfigMap{ @@ -157,8 +157,14 @@ func TestDeleteConfigProxyMap(t *testing.T) { infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNamespaceLabel] = "default" infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNameLabel] = infra.Proxy.Name - r := proxy.NewResourceRender(kube.Namespace, infra) - err = kube.deleteConfigMap(context.Background(), r) + r := proxy.NewResourceRender(kube.Namespace, infra.GetProxyInfra()) + cm := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: kube.Namespace, + Name: r.Name(), + }, + } + err = kube.Client.Delete(context.Background(), cm) require.NoError(t, err) }) } diff --git a/internal/infrastructure/kubernetes/proxy_deployment_test.go b/internal/infrastructure/kubernetes/proxy_deployment_test.go index 4c171186344..b4f89cc9418 100644 --- a/internal/infrastructure/kubernetes/proxy_deployment_test.go +++ b/internal/infrastructure/kubernetes/proxy_deployment_test.go @@ -47,7 +47,7 @@ func TestCreateOrUpdateProxyDeployment(t *testing.T) { infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNamespaceLabel] = "default" infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNameLabel] = infra.Proxy.Name - r := proxy.NewResourceRender(cfg.Namespace, infra) + r := proxy.NewResourceRender(cfg.Namespace, infra.GetProxyInfra()) deploy, err := r.Deployment() require.NoError(t, err) @@ -112,7 +112,7 @@ func TestCreateOrUpdateProxyDeployment(t *testing.T) { } kube := NewInfra(cli, cfg) - r := proxy.NewResourceRender(kube.Namespace, tc.in) + r := proxy.NewResourceRender(kube.Namespace, tc.in.GetProxyInfra()) err := kube.createOrUpdateDeployment(context.Background(), r) require.NoError(t, err) @@ -151,12 +151,17 @@ func TestDeleteProxyDeployment(t *testing.T) { infra := ir.NewInfra() infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNamespaceLabel] = "default" infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNameLabel] = infra.Proxy.Name - r := proxy.NewResourceRender(kube.Namespace, infra) + r := proxy.NewResourceRender(kube.Namespace, infra.GetProxyInfra()) err := kube.createOrUpdateDeployment(context.Background(), r) require.NoError(t, err) - - err = kube.deleteDeployment(context.Background(), r) + deployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: kube.Namespace, + Name: r.Name(), + }, + } + err = kube.Client.Delete(context.Background(), deployment) require.NoError(t, err) }) } diff --git a/internal/infrastructure/kubernetes/proxy_infra.go b/internal/infrastructure/kubernetes/proxy_infra.go index 863d6368d3c..203c3fa4ef6 100644 --- a/internal/infrastructure/kubernetes/proxy_infra.go +++ b/internal/infrastructure/kubernetes/proxy_infra.go @@ -23,7 +23,7 @@ func (i *Infra) CreateOrUpdateProxyInfra(ctx context.Context, infra *ir.Infra) e return errors.New("infra proxy ir is nil") } - r := proxy.NewResourceRender(i.Namespace, infra) + r := proxy.NewResourceRender(i.Namespace, infra.GetProxyInfra()) return i.createOrUpdate(ctx, r) } @@ -33,6 +33,6 @@ func (i *Infra) DeleteProxyInfra(ctx context.Context, infra *ir.Infra) error { return errors.New("infra ir is nil") } - r := proxy.NewResourceRender(i.Namespace, infra) + r := proxy.NewResourceRender(i.Namespace, infra.GetProxyInfra()) return i.delete(ctx, r) } diff --git a/internal/infrastructure/kubernetes/proxy_service_test.go b/internal/infrastructure/kubernetes/proxy_service_test.go index 6b3197a7882..6aa221a4113 100644 --- a/internal/infrastructure/kubernetes/proxy_service_test.go +++ b/internal/infrastructure/kubernetes/proxy_service_test.go @@ -33,7 +33,7 @@ func TestDeleteProxyService(t *testing.T) { infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNamespaceLabel] = "default" infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNameLabel] = infra.Proxy.Name - r := proxy.NewResourceRender(kube.Namespace, infra) + r := proxy.NewResourceRender(kube.Namespace, infra.GetProxyInfra()) err := kube.createOrUpdateService(context.Background(), r) require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/proxy_serviceaccount_test.go b/internal/infrastructure/kubernetes/proxy_serviceaccount_test.go index e406f564e39..f5a7f5735f5 100644 --- a/internal/infrastructure/kubernetes/proxy_serviceaccount_test.go +++ b/internal/infrastructure/kubernetes/proxy_serviceaccount_test.go @@ -171,7 +171,7 @@ func TestCreateOrUpdateProxyServiceAccount(t *testing.T) { kube := NewInfra(cli, cfg) - r := proxy.NewResourceRender(kube.Namespace, tc.in) + r := proxy.NewResourceRender(kube.Namespace, tc.in.GetProxyInfra()) err = kube.createOrUpdateServiceAccount(context.Background(), r) require.NoError(t, err) @@ -204,7 +204,7 @@ func TestDeleteProxyServiceAccount(t *testing.T) { infra := ir.NewInfra() infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNamespaceLabel] = "default" infra.Proxy.GetProxyMetadata().Labels[gatewayapi.OwningGatewayNameLabel] = infra.Proxy.Name - r := proxy.NewResourceRender(kube.Namespace, infra) + r := proxy.NewResourceRender(kube.Namespace, infra.GetProxyInfra()) err := kube.createOrUpdateServiceAccount(context.Background(), r) require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/ratelimit/deployment.go b/internal/infrastructure/kubernetes/ratelimit/deployment.go index 9eb31794fa2..81b2f38790d 100644 --- a/internal/infrastructure/kubernetes/ratelimit/deployment.go +++ b/internal/infrastructure/kubernetes/ratelimit/deployment.go @@ -12,7 +12,7 @@ import ( "k8s.io/utils/pointer" egcfgv1a1 "github.com/envoyproxy/gateway/api/config/v1alpha1" - "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/utils" + "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/resource" ) const ( @@ -39,14 +39,14 @@ const ( ) // Deployment returns the expected rate limit Deployment based on the provided infra. -func (i *ResourceRender) Deployment() (*appsv1.Deployment, error) { - containers := expectedRateLimitContainers(i.ratelimit, i.rateLimitDeployment) +func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) { + containers := expectedRateLimitContainers(r.ratelimit, r.rateLimitDeployment) labels := rateLimitLabels() - selector := utils.GetSelector(labels) + selector := resource.GetSelector(labels) - var annos map[string]string - if i.rateLimitDeployment.Pod.Annotations != nil { - annos = i.rateLimitDeployment.Pod.Annotations + var annotations map[string]string + if r.rateLimitDeployment.Pod.Annotations != nil { + annotations = r.rateLimitDeployment.Pod.Annotations } deployment := &appsv1.Deployment{ @@ -55,17 +55,17 @@ func (i *ResourceRender) Deployment() (*appsv1.Deployment, error) { APIVersion: "apps/v1", }, ObjectMeta: metav1.ObjectMeta{ - Namespace: i.Namespace, + Namespace: r.Namespace, Name: InfraName, Labels: labels, }, Spec: appsv1.DeploymentSpec{ - Replicas: i.rateLimitDeployment.Replicas, + Replicas: r.rateLimitDeployment.Replicas, Selector: selector, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: selector.MatchLabels, - Annotations: annos, + Annotations: annotations, }, Spec: corev1.PodSpec{ Containers: containers, @@ -75,7 +75,7 @@ func (i *ResourceRender) Deployment() (*appsv1.Deployment, error) { DNSPolicy: corev1.DNSClusterFirst, RestartPolicy: corev1.RestartPolicyAlways, SchedulerName: "default-scheduler", - SecurityContext: i.rateLimitDeployment.Pod.SecurityContext, + SecurityContext: r.rateLimitDeployment.Pod.SecurityContext, Volumes: []corev1.Volume{ { Name: InfraName, diff --git a/internal/infrastructure/kubernetes/ratelimit/utils.go b/internal/infrastructure/kubernetes/ratelimit/resource.go similarity index 100% rename from internal/infrastructure/kubernetes/ratelimit/utils.go rename to internal/infrastructure/kubernetes/ratelimit/resource.go diff --git a/internal/infrastructure/kubernetes/ratelimit/resource_provider.go b/internal/infrastructure/kubernetes/ratelimit/resource_provider.go index 1caf96b4b8c..896c27a71d9 100644 --- a/internal/infrastructure/kubernetes/ratelimit/resource_provider.go +++ b/internal/infrastructure/kubernetes/ratelimit/resource_provider.go @@ -11,7 +11,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" egcfgv1a1 "github.com/envoyproxy/gateway/api/config/v1alpha1" - "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/utils" + "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/resource" "github.com/envoyproxy/gateway/internal/ir" ) @@ -25,12 +25,12 @@ type ResourceRender struct { } // NewResourceRender returns a new ResourceRender. -func NewResourceRender(ns string, infra *ir.RateLimitInfra, rl *egcfgv1a1.RateLimit, deploy *egcfgv1a1.KubernetesDeploymentSpec) *ResourceRender { +func NewResourceRender(ns string, infra *ir.RateLimitInfra, gateway *egcfgv1a1.EnvoyGateway) *ResourceRender { return &ResourceRender{ Namespace: ns, infra: infra, - ratelimit: rl, - rateLimitDeployment: deploy, + ratelimit: gateway.RateLimit, + rateLimitDeployment: gateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment, } } @@ -74,9 +74,9 @@ func (r *ResourceRender) Service() (*corev1.Service, error) { labels := rateLimitLabels() - serviceSpec := utils.ExpectedServiceSpec(egcfgv1a1.DefaultKubernetesServiceType()) + serviceSpec := resource.ExpectedServiceSpec(egcfgv1a1.DefaultKubernetesServiceType()) serviceSpec.Ports = ports - serviceSpec.Selector = utils.GetSelector(labels).MatchLabels + serviceSpec.Selector = resource.GetSelector(labels).MatchLabels svc := &corev1.Service{ TypeMeta: metav1.TypeMeta{ diff --git a/internal/infrastructure/kubernetes/ratelimit/resource_provider_test.go b/internal/infrastructure/kubernetes/ratelimit/resource_provider_test.go index 04874038020..edefcf46a74 100644 --- a/internal/infrastructure/kubernetes/ratelimit/resource_provider_test.go +++ b/internal/infrastructure/kubernetes/ratelimit/resource_provider_test.go @@ -68,7 +68,7 @@ func TestServiceAccount(t *testing.T) { require.NoError(t, err) rateLimitInfra := new(ir.RateLimitInfra) - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -76,7 +76,7 @@ func TestServiceAccount(t *testing.T) { }, }, } - r := NewResourceRender(cfg.Namespace, rateLimitInfra, rl, cfg.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := NewResourceRender(cfg.Namespace, rateLimitInfra, cfg.EnvoyGateway) sa, err := r.ServiceAccount() require.NoError(t, err) @@ -109,7 +109,7 @@ func TestConfigMap(t *testing.T) { }, }, } - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -118,7 +118,7 @@ func TestConfigMap(t *testing.T) { }, } - r := NewResourceRender(cfg.Namespace, rateLimitInfra, rl, cfg.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := NewResourceRender(cfg.Namespace, rateLimitInfra, cfg.EnvoyGateway) cm, err := r.ConfigMap() require.NoError(t, err) @@ -150,7 +150,7 @@ func TestService(t *testing.T) { }, }, } - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -158,7 +158,7 @@ func TestService(t *testing.T) { }, }, } - r := NewResourceRender(cfg.Namespace, rateLimitInfra, rl, cfg.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := NewResourceRender(cfg.Namespace, rateLimitInfra, cfg.EnvoyGateway) svc, err := r.Service() require.NoError(t, err) @@ -232,7 +232,7 @@ func TestDeployment(t *testing.T) { }, } - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -240,8 +240,12 @@ func TestDeployment(t *testing.T) { }, }, } - - r := NewResourceRender(cfg.Namespace, rateLimitInfra, rl, tc.deploy) + cfg.EnvoyGateway.Provider = &egcfgv1a1.EnvoyGatewayProvider{ + Type: egcfgv1a1.ProviderTypeKubernetes, + Kubernetes: &egcfgv1a1.EnvoyGatewayKubernetesProvider{ + RateLimitDeployment: tc.deploy, + }} + r := NewResourceRender(cfg.Namespace, rateLimitInfra, cfg.EnvoyGateway) dp, err := r.Deployment() require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/ratelimit_configmap_test.go b/internal/infrastructure/kubernetes/ratelimit_configmap_test.go index ac25f939f27..4898c3915c5 100644 --- a/internal/infrastructure/kubernetes/ratelimit_configmap_test.go +++ b/internal/infrastructure/kubernetes/ratelimit_configmap_test.go @@ -51,7 +51,7 @@ func TestCreateOrUpdateRateLimitConfigMap(t *testing.T) { Name: rateLimitListener, Config: rateLimitConfig, }) - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -59,7 +59,7 @@ func TestCreateOrUpdateRateLimitConfigMap(t *testing.T) { }, }, } - r := ratelimit.NewResourceRender(cfg.Namespace, rateLimitInfra, rl, cfg.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := ratelimit.NewResourceRender(cfg.Namespace, rateLimitInfra, cfg.EnvoyGateway) testCases := []struct { name string @@ -135,7 +135,7 @@ func TestDeleteRateLimitConfigMap(t *testing.T) { cfg, err := config.New() require.NoError(t, err) - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -178,12 +178,18 @@ func TestDeleteRateLimitConfigMap(t *testing.T) { kube := NewInfra(cli, cfg) rateLimitInfra := new(ir.RateLimitInfra) - r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, rl, cfg.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, cfg.EnvoyGateway) err := kube.createOrUpdateConfigMap(context.Background(), r) require.NoError(t, err) - err = kube.deleteConfigMap(context.Background(), r) + cm := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: kube.Namespace, + Name: r.Name(), + }, + } + err = kube.Client.Delete(context.Background(), cm) require.NoError(t, err) }) } diff --git a/internal/infrastructure/kubernetes/ratelimit_deployment_test.go b/internal/infrastructure/kubernetes/ratelimit_deployment_test.go index 4f692d7bb96..4c73d133133 100644 --- a/internal/infrastructure/kubernetes/ratelimit_deployment_test.go +++ b/internal/infrastructure/kubernetes/ratelimit_deployment_test.go @@ -27,7 +27,7 @@ func TestCreateOrUpdateRateLimitDeployment(t *testing.T) { require.NoError(t, err) rateLimitInfra := new(ir.RateLimitInfra) - rl := &egcfgv1a1.RateLimit{ + cfg.EnvoyGateway.RateLimit = &egcfgv1a1.RateLimit{ Backend: egcfgv1a1.RateLimitDatabaseBackend{ Type: egcfgv1a1.RedisBackendType, Redis: &egcfgv1a1.RateLimitRedisSettings{ @@ -36,7 +36,7 @@ func TestCreateOrUpdateRateLimitDeployment(t *testing.T) { }, } - r := ratelimit.NewResourceRender(cfg.Namespace, rateLimitInfra, rl, cfg.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := ratelimit.NewResourceRender(cfg.Namespace, rateLimitInfra, cfg.EnvoyGateway) deployment, err := r.Deployment() require.NoError(t, err) @@ -76,7 +76,8 @@ func TestCreateOrUpdateRateLimitDeployment(t *testing.T) { } kube := NewInfra(cli, cfg) - r := ratelimit.NewResourceRender(kube.Namespace, tc.in, rl, kube.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + kube.EnvoyGateway.RateLimit = cfg.EnvoyGateway.RateLimit + r := ratelimit.NewResourceRender(kube.Namespace, tc.in, kube.EnvoyGateway) err := kube.createOrUpdateDeployment(context.Background(), r) require.NoError(t, err) @@ -117,7 +118,8 @@ func TestDeleteRateLimitDeployment(t *testing.T) { t.Run(tc.name, func(t *testing.T) { kube := newTestInfra(t) rateLimitInfra := new(ir.RateLimitInfra) - r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, rl, kube.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + kube.EnvoyGateway.RateLimit = rl + r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, kube.EnvoyGateway) err := kube.createOrUpdateDeployment(context.Background(), r) require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/ratelimit_infra.go b/internal/infrastructure/kubernetes/ratelimit_infra.go index 9e5e0388cc4..fb4f21af9af 100644 --- a/internal/infrastructure/kubernetes/ratelimit_infra.go +++ b/internal/infrastructure/kubernetes/ratelimit_infra.go @@ -19,8 +19,7 @@ func (i *Infra) CreateOrUpdateRateLimitInfra(ctx context.Context, infra *ir.Rate return errors.New("ratelimit infra ir is nil") } - r := ratelimit.NewResourceRender(i.Namespace, infra, i.EnvoyGateway.RateLimit, i.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) - + r := ratelimit.NewResourceRender(i.Namespace, infra, i.EnvoyGateway) return i.createOrUpdate(ctx, r) } @@ -30,6 +29,6 @@ func (i *Infra) DeleteRateLimitInfra(ctx context.Context, infra *ir.RateLimitInf return errors.New("ratelimit infra ir is nil") } - r := ratelimit.NewResourceRender(i.Namespace, infra, i.EnvoyGateway.RateLimit, i.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := ratelimit.NewResourceRender(i.Namespace, infra, i.EnvoyGateway) return i.delete(ctx, r) } diff --git a/internal/infrastructure/kubernetes/ratelimit_service_test.go b/internal/infrastructure/kubernetes/ratelimit_service_test.go index 1de360deb7f..23cb66c089b 100644 --- a/internal/infrastructure/kubernetes/ratelimit_service_test.go +++ b/internal/infrastructure/kubernetes/ratelimit_service_test.go @@ -40,7 +40,8 @@ func TestDeleteRateLimitService(t *testing.T) { kube := newTestInfra(t) rateLimitInfra := new(ir.RateLimitInfra) - r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, rl, kube.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + kube.EnvoyGateway.RateLimit = rl + r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, kube.EnvoyGateway) err := kube.createOrUpdateService(context.Background(), r) require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/ratelimit_serviceaccount_test.go b/internal/infrastructure/kubernetes/ratelimit_serviceaccount_test.go index ea856ba6af0..16395e92a81 100644 --- a/internal/infrastructure/kubernetes/ratelimit_serviceaccount_test.go +++ b/internal/infrastructure/kubernetes/ratelimit_serviceaccount_test.go @@ -89,8 +89,9 @@ func TestCreateOrUpdateRateLimitServiceAccount(t *testing.T) { cfg.Namespace = tc.ns kube := NewInfra(cli, cfg) + kube.EnvoyGateway.RateLimit = rl - r := ratelimit.NewResourceRender(kube.Namespace, tc.in, rl, kube.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + r := ratelimit.NewResourceRender(kube.Namespace, tc.in, kube.EnvoyGateway) err = kube.createOrUpdateServiceAccount(context.Background(), r) require.NoError(t, err) @@ -133,7 +134,9 @@ func TestDeleteRateLimitServiceAccount(t *testing.T) { kube := newTestInfra(t) rateLimitInfra := new(ir.RateLimitInfra) - r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, rl, kube.EnvoyGateway.GetEnvoyGatewayProvider().GetEnvoyGatewayKubeProvider().RateLimitDeployment) + kube.EnvoyGateway.RateLimit = rl + + r := ratelimit.NewResourceRender(kube.Namespace, rateLimitInfra, kube.EnvoyGateway) err := kube.createOrUpdateServiceAccount(context.Background(), r) require.NoError(t, err) diff --git a/internal/infrastructure/kubernetes/utils/utils.go b/internal/infrastructure/kubernetes/resource/resource.go similarity index 94% rename from internal/infrastructure/kubernetes/utils/utils.go rename to internal/infrastructure/kubernetes/resource/resource.go index e7203d3ca89..8f71150fc65 100644 --- a/internal/infrastructure/kubernetes/utils/utils.go +++ b/internal/infrastructure/kubernetes/resource/resource.go @@ -3,7 +3,7 @@ // The full text of the Apache license is available in the LICENSE file at // the root of the repo. -package utils +package resource import ( corev1 "k8s.io/api/core/v1" @@ -20,6 +20,7 @@ func GetSelector(labels map[string]string) *metav1.LabelSelector { } } +// ExpectedServiceSpec returns service spec. func ExpectedServiceSpec(serviceType *egcfgv1a1.ServiceType) corev1.ServiceSpec { serviceSpec := corev1.ServiceSpec{} serviceSpec.Type = corev1.ServiceType(*serviceType) diff --git a/internal/infrastructure/kubernetes/utils/utils_test.go b/internal/infrastructure/kubernetes/resource/resource_test.go similarity index 99% rename from internal/infrastructure/kubernetes/utils/utils_test.go rename to internal/infrastructure/kubernetes/resource/resource_test.go index 1b5f88f71cb..f9fd40c5247 100644 --- a/internal/infrastructure/kubernetes/utils/utils_test.go +++ b/internal/infrastructure/kubernetes/resource/resource_test.go @@ -3,7 +3,7 @@ // The full text of the Apache license is available in the LICENSE file at // the root of the repo. -package utils +package resource import ( "testing"