Skip to content

Commit

Permalink
infra: use labels when deleting infra (envoyproxy#4430)
Browse files Browse the repository at this point in the history
  • Loading branch information
zirain authored Oct 16, 2024
1 parent 5880d6b commit 958df48
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type KubernetesDeploymentSpec struct {
// TODO: Expose config as use cases are better understood, e.g. labels.
}

// KubernetesDaemonsetSpec defines the desired state of the Kubernetes daemonset resource.
// KubernetesDaemonSetSpec defines the desired state of the Kubernetes daemonset resource.
type KubernetesDaemonSetSpec struct {
// Patch defines how to perform the patch operation to daemonset
//
Expand Down
2 changes: 2 additions & 0 deletions internal/infrastructure/kubernetes/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
Expand All @@ -29,6 +30,7 @@ var _ ResourceRender = &ratelimit.ResourceRender{}
// based on Infra IR resources.
type ResourceRender interface {
Name() string
LabelSelector() labels.Selector
ServiceAccount() (*corev1.ServiceAccount, error)
Service() (*corev1.Service, error)
ConfigMap() (*corev1.ConfigMap, error)
Expand Down
43 changes: 36 additions & 7 deletions internal/infrastructure/kubernetes/infra_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/envoyproxy/gateway/internal/metrics"
)
Expand Down Expand Up @@ -395,7 +396,11 @@ func (i *Infra) deleteServiceAccount(ctx context.Context, r ResourceRender) (err
}
}()

return i.Client.Delete(ctx, sa)
return i.Client.DeleteAllOf(ctx, sa, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}

// deleteDeployment deletes the Envoy Deployment in the kube api server, if it exists.
Expand Down Expand Up @@ -430,7 +435,11 @@ func (i *Infra) deleteDeployment(ctx context.Context, r ResourceRender) (err err
}
}()

return i.Client.Delete(ctx, deployment)
return i.Client.DeleteAllOf(ctx, deployment, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}

// deleteDaemonSet deletes the Envoy DaemonSet in the kube api server, if it exists.
Expand Down Expand Up @@ -465,7 +474,11 @@ func (i *Infra) deleteDaemonSet(ctx context.Context, r ResourceRender) (err erro
}
}()

return i.Client.Delete(ctx, daemonSet)
return i.Client.DeleteAllOf(ctx, daemonSet, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}

// deleteConfigMap deletes the ConfigMap in the kube api server, if it exists.
Expand Down Expand Up @@ -495,7 +508,11 @@ func (i *Infra) deleteConfigMap(ctx context.Context, r ResourceRender) (err erro
}
}()

return i.Client.Delete(ctx, cm)
return i.Client.DeleteAllOf(ctx, cm, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}

// deleteService deletes the Service in the kube api server, if it exists.
Expand Down Expand Up @@ -525,7 +542,11 @@ func (i *Infra) deleteService(ctx context.Context, r ResourceRender) (err error)
}
}()

return i.Client.Delete(ctx, svc)
return i.Client.DeleteAllOf(ctx, svc, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}

// deleteHpa deletes the Horizontal Pod Autoscaler associated to its renderer, if it exists.
Expand Down Expand Up @@ -560,7 +581,11 @@ func (i *Infra) deleteHPA(ctx context.Context, r ResourceRender) (err error) {
}
}()

return i.Client.Delete(ctx, hpa)
return i.Client.DeleteAllOf(ctx, hpa, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}

// deletePDB deletes the PodDistribution budget associated to its renderer, if it exists.
Expand Down Expand Up @@ -595,5 +620,9 @@ func (i *Infra) deletePDB(ctx context.Context, r ResourceRender) (err error) {
}
}()

return i.Client.Delete(ctx, pdb)
return i.Client.DeleteAllOf(ctx, pdb, &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
LabelSelector: r.LabelSelector(),
},
})
}
5 changes: 5 additions & 0 deletions internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"

Expand Down Expand Up @@ -46,6 +47,10 @@ func (r *ResourceRender) Name() string {
return ExpectedResourceHashedName(r.infra.Name)
}

func (r *ResourceRender) LabelSelector() labels.Selector {
return labels.SelectorFromSet(r.stableSelector().MatchLabels)
}

// ServiceAccount returns the expected proxy serviceAccount.
func (r *ResourceRender) ServiceAccount() (*corev1.ServiceAccount, error) {
// Set the labels based on the owning gateway name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -60,6 +61,10 @@ func (r *ResourceRender) Name() string {
return InfraName
}

func (r *ResourceRender) LabelSelector() labels.Selector {
return labels.SelectorFromSet(rateLimitLabels())
}

func enablePrometheus(rl *egv1a1.RateLimit) bool {
if rl != nil &&
rl.Telemetry != nil &&
Expand Down
4 changes: 0 additions & 4 deletions internal/infrastructure/kubernetes/ratelimit_infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ func (i *Infra) CreateOrUpdateRateLimitInfra(ctx context.Context) error {

// DeleteRateLimitInfra removes the managed kube infra, if it doesn't exist.
func (i *Infra) DeleteRateLimitInfra(ctx context.Context) error {
if err := ratelimit.Validate(ctx, i.Client.Client, i.EnvoyGateway, i.Namespace); err != nil {
return err
}

// Delete ratelimit infra do not require the uid of owner reference.
r := ratelimit.NewResourceRender(i.Namespace, i.EnvoyGateway, nil)
return i.delete(ctx, r)
Expand Down
2 changes: 1 addition & 1 deletion site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ _Appears in:_



KubernetesDaemonsetSpec defines the desired state of the Kubernetes daemonset resource.
KubernetesDaemonSetSpec defines the desired state of the Kubernetes daemonset resource.

_Appears in:_
- [EnvoyProxyKubernetesProvider](#envoyproxykubernetesprovider)
Expand Down
2 changes: 1 addition & 1 deletion site/content/zh/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ _Appears in:_



KubernetesDaemonsetSpec defines the desired state of the Kubernetes daemonset resource.
KubernetesDaemonSetSpec defines the desired state of the Kubernetes daemonset resource.

_Appears in:_
- [EnvoyProxyKubernetesProvider](#envoyproxykubernetesprovider)
Expand Down

0 comments on commit 958df48

Please sign in to comment.