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 package infrastructure/kubernetes #1259

Merged
merged 4 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/cmd/egctl/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/envoyproxy/gateway/internal/envoygateway"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/gatewayapi"
infra "github.com/envoyproxy/gateway/internal/infrastructure/kubernetes"
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/ratelimit"
"github.com/envoyproxy/gateway/internal/status"
"github.com/envoyproxy/gateway/internal/xds/bootstrap"
"github.com/envoyproxy/gateway/internal/xds/translator"
Expand Down Expand Up @@ -297,7 +297,7 @@ func translateGatewayAPIToXds(resourceType string, resources *gatewayapi.Resourc
xTranslator := &translator.Translator{
// Set some default settings for translation
GlobalRateLimit: &translator.GlobalRateLimitSettings{
ServiceURL: infra.GetRateLimitServiceURL("envoy-gateway"),
ServiceURL: ratelimit.GetServiceURL("envoy-gateway"),
},
}
xRes, err := xTranslator.Translate(val)
Expand Down
187 changes: 187 additions & 0 deletions internal/infrastructure/kubernetes/applier/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can just pass the ns and name to delete a specific resource, or we can use a single function to delete all kinds of resources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this PR is too large to review, let's do more refactor later.
Applier absolute can be more generic.

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
}
79 changes: 0 additions & 79 deletions internal/infrastructure/kubernetes/configmap.go

This file was deleted.

55 changes: 0 additions & 55 deletions internal/infrastructure/kubernetes/deployment.go

This file was deleted.

Loading