Skip to content

Commit a88f912

Browse files
committed
deleting additional vpc cni resources when disabled
1 parent 9e71830 commit a88f912

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
sigs.k8s.io/cluster-api v1.2.0
4141
sigs.k8s.io/cluster-api/test v1.2.0
4242
sigs.k8s.io/controller-runtime v0.12.3
43+
sigs.k8s.io/kustomize/api v0.11.4
4344
sigs.k8s.io/yaml v1.3.0
4445
)
4546

@@ -73,6 +74,7 @@ require (
7374
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
7475
github.com/fatih/color v1.13.0 // indirect
7576
github.com/fsnotify/fsnotify v1.5.4 // indirect
77+
github.com/go-errors/errors v1.0.1 // indirect
7678
github.com/go-logr/zapr v1.2.0 // indirect
7779
github.com/go-openapi/jsonpointer v0.19.5 // indirect
7880
github.com/go-openapi/jsonreference v0.19.5 // indirect
@@ -158,5 +160,6 @@ require (
158160
k8s.io/kubectl v0.24.0 // indirect
159161
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
160162
sigs.k8s.io/kind v0.14.0 // indirect
163+
sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect
161164
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
162165
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ github.com/fvbommel/sortorder v1.0.1/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui72
220220
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
221221
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
222222
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
223+
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
223224
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
224225
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
225226
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -1269,9 +1270,11 @@ sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 h1:kDi4JBNAsJWfz1aEXhO8Jg87J
12691270
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2/go.mod h1:B+TnT182UBxE84DiCz4CVE26eOSDAeYCpfDnC2kdKMY=
12701271
sigs.k8s.io/kind v0.14.0 h1:cNmI3jGBvp7UegEGbC5we8plDtCUmaNRL+bod7JoSCE=
12711272
sigs.k8s.io/kind v0.14.0/go.mod h1:UrFRPHG+2a5j0Q7qiR4gtJ4rEyn8TuMQwuOPf+m4oHg=
1273+
sigs.k8s.io/kustomize/api v0.11.4 h1:/0Mr3kfBBNcNPOW5Qwk/3eb8zkswCwnqQxxKtmrTkRo=
12721274
sigs.k8s.io/kustomize/api v0.11.4/go.mod h1:k+8RsqYbgpkIrJ4p9jcdPqe8DprLxFUUO0yNOq8C+xI=
12731275
sigs.k8s.io/kustomize/cmd/config v0.10.6/go.mod h1:/S4A4nUANUa4bZJ/Edt7ZQTyKOY9WCER0uBS1SW2Rco=
12741276
sigs.k8s.io/kustomize/kustomize/v4 v4.5.4/go.mod h1:Zo/Xc5FKD6sHl0lilbrieeGeZHVYCA4BzxeAaLI05Bg=
1277+
sigs.k8s.io/kustomize/kyaml v0.13.6 h1:eF+wsn4J7GOAXlvajv6OknSunxpcOBQQqsnPxObtkGs=
12751278
sigs.k8s.io/kustomize/kyaml v0.13.6/go.mod h1:yHP031rn1QX1lr/Xd934Ri/xdVNG8BE2ECa78Ht/kEg=
12761279
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
12771280
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=

pkg/cloud/scope/managedcontrolplane.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/pkg/errors"
2828
appsv1 "k8s.io/api/apps/v1"
2929
corev1 "k8s.io/api/core/v1"
30+
rbacv1 "k8s.io/api/rbac/v1"
3031
"k8s.io/apimachinery/pkg/runtime"
3132
"k8s.io/klog/v2"
3233
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -48,6 +49,7 @@ func init() {
4849
_ = amazoncni.AddToScheme(scheme)
4950
_ = appsv1.AddToScheme(scheme)
5051
_ = corev1.AddToScheme(scheme)
52+
_ = rbacv1.AddToScheme(scheme)
5153
}
5254

5355
// ManagedControlPlaneScopeParams defines the input parameters used to create a new Scope.

pkg/cloud/services/awsnode/cni.go

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ import (
2323
amazoncni "github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
2424
appsv1 "k8s.io/api/apps/v1"
2525
corev1 "k8s.io/api/core/v1"
26+
rbacv1 "k8s.io/api/rbac/v1"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
28+
"k8s.io/apimachinery/pkg/api/meta"
2729
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2830
"k8s.io/apimachinery/pkg/labels"
2931
"k8s.io/apimachinery/pkg/types"
3032
"k8s.io/klog/v2"
3133
"sigs.k8s.io/controller-runtime/pkg/client"
34+
"sigs.k8s.io/kustomize/api/konfig"
3235

3336
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta2"
3437
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/awserrors"
@@ -239,26 +242,77 @@ func (s *Service) applyUserProvidedEnvironmentProperties(containerEnv []corev1.E
239242
}
240243

241244
func (s *Service) deleteCNI(ctx context.Context, remoteClient client.Client) error {
242-
s.scope.Info("Ensuring aws-node DaemonSet in cluster is deleted", "cluster", klog.KRef(s.scope.Namespace(), s.scope.Name()))
245+
// EKS has a tendency to pre-install the vpc-cni automagically even if you don't specify it as an addon
246+
// and looks like a kubectl apply from a script of a manifest that looks like this
247+
// https://github.com/aws/amazon-vpc-cni-k8s/blob/master/config/master/aws-k8s-cni.yaml
248+
// and removing these pieces will enable someone to install and alternative CNI. There is also another use
249+
// case where someone would want to remove the vpc-cni and reinstall it via the helm chart located here
250+
// https://github.com/aws/amazon-vpc-cni-k8s/tree/master/charts/aws-vpc-cni meaning we need to account for
251+
// managed-by: Helm label, or we will delete the helm chart resources every reconcile loop. EKS does make
252+
// a CRD for eniconfigs but the default env var on the vpc-cni pod is ENABLE_POD_ENI=false. We will make an
253+
// assumption no CRs are ever created and leave the CRD to reduce complexity of this operation.
254+
255+
s.scope.Info("Ensuring all resources for AWS VPC CNI in cluster are deleted", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
256+
257+
s.scope.Info("Trying to delete AWS VPC CNI DaemonSet", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
258+
if err := s.deleteResource(ctx, remoteClient, types.NamespacedName{
259+
Namespace: awsNodeNamespace,
260+
Name: awsNodeName,
261+
}, &appsv1.DaemonSet{}); err != nil {
262+
return err
263+
}
243264

244-
ds := &appsv1.DaemonSet{}
245-
if err := remoteClient.Get(ctx, types.NamespacedName{Namespace: awsNodeNamespace, Name: awsNodeName}, ds); err != nil {
246-
if apierrors.IsNotFound(err) {
247-
s.scope.V(2).Info("The aws-node DaemonSet is not found, not action")
248-
return nil
249-
}
250-
return fmt.Errorf("getting aws-node daemonset: %w", err)
265+
s.scope.Info("Trying to delete AWS VPC CNI ServiceAccount", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
266+
if err := s.deleteResource(ctx, remoteClient, types.NamespacedName{
267+
Namespace: awsNodeNamespace,
268+
Name: awsNodeName,
269+
}, &corev1.ServiceAccount{}); err != nil {
270+
return err
251271
}
252272

253-
s.scope.V(2).Info("The aws-node DaemonSet found, deleting")
254-
if err := remoteClient.Delete(ctx, ds, &client.DeleteOptions{}); err != nil {
255-
if apierrors.IsNotFound(err) {
256-
s.scope.V(2).Info("The aws-node DaemonSet is not found, not deleted")
257-
return nil
258-
}
259-
return fmt.Errorf("deleting aws-node DaemonSet: %w", err)
273+
s.scope.Info("Trying to delete AWS VPC CNI ClusterRoleBinding", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
274+
if err := s.deleteResource(ctx, remoteClient, types.NamespacedName{
275+
Namespace: string(meta.RESTScopeNameRoot),
276+
Name: awsNodeName,
277+
}, &rbacv1.ClusterRoleBinding{}); err != nil {
278+
return err
260279
}
280+
281+
s.scope.Info("Trying to delete AWS VPC CNI ClusterRole", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
282+
if err := s.deleteResource(ctx, remoteClient, types.NamespacedName{
283+
Namespace: string(meta.RESTScopeNameRoot),
284+
Name: awsNodeName,
285+
}, &rbacv1.ClusterRole{}); err != nil {
286+
return err
287+
}
288+
261289
record.Eventf(s.scope.InfraCluster(), "DeletedVPCCNI", "The AWS VPC CNI has been removed from the cluster. Ensure you enable a CNI via another mechanism")
262290

263291
return nil
264292
}
293+
294+
func (s *Service) deleteResource(ctx context.Context, remoteClient client.Client, key client.ObjectKey, obj client.Object) error {
295+
if err := remoteClient.Get(ctx, key, obj); err != nil {
296+
if !apierrors.IsNotFound(err) {
297+
return fmt.Errorf("deleting resource %s: %w", key, err)
298+
}
299+
s.scope.V(2).Info(fmt.Sprintf("resource %s was not found, no action", key))
300+
} else {
301+
// resource found, delete if no label or not managed by helm
302+
if val, ok := obj.GetLabels()[konfig.ManagedbyLabelKey]; !ok || val != "Helm" {
303+
if err := remoteClient.Delete(ctx, obj, &client.DeleteOptions{}); err != nil {
304+
if !apierrors.IsNotFound(err) {
305+
return fmt.Errorf("deleting %s: %w", key, err)
306+
}
307+
s.scope.V(2).Info(fmt.Sprintf(
308+
"resource %s was not found, not deleted", key))
309+
} else {
310+
s.scope.V(2).Info(fmt.Sprintf("resource %s was deleted", key))
311+
}
312+
} else {
313+
s.scope.V(2).Info(fmt.Sprintf("resource %s is managed by helm, not deleted", key))
314+
}
315+
}
316+
317+
return nil
318+
}

0 commit comments

Comments
 (0)