sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/kubeproxy/reconcile.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kubeproxy
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	appsv1 "k8s.io/api/apps/v1"
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"sigs.k8s.io/cluster-api-provider-aws/pkg/record"
    29  )
    30  
    31  const (
    32  	kubeProxyName      = "kube-proxy"
    33  	kubeProxyNamespace = "kube-system"
    34  )
    35  
    36  // ReconcileKubeProxy will reconcile kube-proxy.
    37  func (s *Service) ReconcileKubeProxy(ctx context.Context) error {
    38  	s.scope.Info("Reconciling kube-proxy DaemonSet in cluster", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
    39  
    40  	remoteClient, err := s.scope.RemoteClient()
    41  	if err != nil {
    42  		s.scope.Error(err, "getting client for remote cluster")
    43  		return fmt.Errorf("getting client for remote cluster: %w", err)
    44  	}
    45  
    46  	if s.scope.DisableKubeProxy() {
    47  		if err := s.deleteKubeProxy(ctx, remoteClient); err != nil {
    48  			return fmt.Errorf("disabling kube-proxy: %w", err)
    49  		}
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func (s *Service) deleteKubeProxy(ctx context.Context, remoteClient client.Client) error {
    56  	s.scope.Info("Ensuring the kube-proxy DaemonSet in cluster is deleted", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
    57  
    58  	ds := &appsv1.DaemonSet{}
    59  	if err := remoteClient.Get(ctx, types.NamespacedName{Namespace: kubeProxyNamespace, Name: kubeProxyName}, ds); err != nil {
    60  		if apierrors.IsNotFound(err) {
    61  			s.scope.V(2).Info("The kube-proxy DaemonSet is not found, no action")
    62  			return nil
    63  		}
    64  		return fmt.Errorf("getting kube-proxy daemonset: %w", err)
    65  	}
    66  
    67  	s.scope.V(2).Info("The kube-proxy DaemonSet found, deleting")
    68  	if err := remoteClient.Delete(ctx, ds, &client.DeleteOptions{}); err != nil {
    69  		if apierrors.IsNotFound(err) {
    70  			s.scope.V(2).Info("The kube-proxy DaemonSet is not found, not deleted")
    71  			return nil
    72  		}
    73  		return fmt.Errorf("deleting kube-proxy DaemonSet: %w", err)
    74  	}
    75  	record.Eventf(s.scope.InfraCluster(), "DeletedKubeProxy", "Kube-proxy has been removed from the cluster. Ensure you enable kube-proxy functionality via another mechanism")
    76  
    77  	return nil
    78  }