|
| 1 | +package endpointcontroller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + v1 "k8s.io/api/core/v1" |
| 9 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes" |
| 12 | + "k8s.io/client-go/tools/record" |
| 13 | + "k8s.io/client-go/util/retry" |
| 14 | + "k8s.io/klog/v2" |
| 15 | + controllerruntime "sigs.k8s.io/controller-runtime" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 23 | + |
| 24 | + "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1" |
| 25 | + "github.com/kosmos.io/kosmos/pkg/kubenest/constants" |
| 26 | + "github.com/kosmos.io/kosmos/pkg/kubenest/util" |
| 27 | + "github.com/kosmos.io/kosmos/pkg/utils" |
| 28 | +) |
| 29 | + |
| 30 | +type ApiServerExternalSyncController struct { |
| 31 | + client.Client |
| 32 | + EventRecorder record.EventRecorder |
| 33 | +} |
| 34 | + |
| 35 | +const ApiServerExternalSyncControllerName string = "api-server-external-service-sync-controller" |
| 36 | + |
| 37 | +func (e *ApiServerExternalSyncController) SetupWithManager(mgr manager.Manager) error { |
| 38 | + skipEvent := func(obj client.Object) bool { |
| 39 | + return strings.Contains(obj.GetName(), "apiserver") && obj.GetNamespace() != "" |
| 40 | + } |
| 41 | + |
| 42 | + return controllerruntime.NewControllerManagedBy(mgr). |
| 43 | + Named(ApiServerExternalSyncControllerName). |
| 44 | + WithOptions(controller.Options{MaxConcurrentReconciles: 5}). |
| 45 | + For(&v1.Endpoints{}, |
| 46 | + builder.WithPredicates(predicate.Funcs{ |
| 47 | + CreateFunc: func(createEvent event.CreateEvent) bool { |
| 48 | + return skipEvent(createEvent.Object) |
| 49 | + }, |
| 50 | + UpdateFunc: func(updateEvent event.UpdateEvent) bool { return skipEvent(updateEvent.ObjectNew) }, |
| 51 | + DeleteFunc: func(deleteEvent event.DeleteEvent) bool { return false }, |
| 52 | + })). |
| 53 | + Complete(e) |
| 54 | +} |
| 55 | + |
| 56 | +func (e *ApiServerExternalSyncController) SyncApiServerExternalEPS(ctx context.Context, k8sClient kubernetes.Interface) error { |
| 57 | + kubeEndpoints, err := k8sClient.CoreV1().Endpoints(constants.DefaultNs).Get(ctx, "kubernetes", metav1.GetOptions{}) |
| 58 | + if err != nil { |
| 59 | + klog.Errorf("Error getting endpoints: %v", err) |
| 60 | + return fmt.Errorf("failed to get endpoints for kubernetes service: %v", err) |
| 61 | + } else { |
| 62 | + klog.Infof("Endpoints for service 'kubernetes': %v", kubeEndpoints) |
| 63 | + for _, subset := range kubeEndpoints.Subsets { |
| 64 | + for _, address := range subset.Addresses { |
| 65 | + klog.Infof("IP: %s", address.IP) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if len(kubeEndpoints.Subsets) != 1 { |
| 71 | + return fmt.Errorf("eps %s Subsets length is not 1", "kubernetes") |
| 72 | + } |
| 73 | + |
| 74 | + if kubeEndpoints.Subsets[0].Addresses == nil || len(kubeEndpoints.Subsets[0].Addresses) == 0 { |
| 75 | + return fmt.Errorf("eps %s Addresses length is nil", "kubernetes") |
| 76 | + } |
| 77 | + |
| 78 | + apiServerExternalEndpoints, err := k8sClient.CoreV1().Endpoints(constants.DefaultNs).Get(ctx, constants.ApiServerExternalService, metav1.GetOptions{}) |
| 79 | + if err != nil && !apierrors.IsNotFound(err) { |
| 80 | + return fmt.Errorf("failed to get endpoints for %s : %v", constants.ApiServerExternalService, err) |
| 81 | + } |
| 82 | + |
| 83 | + updateEPS := apiServerExternalEndpoints.DeepCopy() |
| 84 | + |
| 85 | + if apiServerExternalEndpoints != nil { |
| 86 | + klog.Infof("apiServerExternalEndpoints: %v", apiServerExternalEndpoints) |
| 87 | + } else { |
| 88 | + klog.Info("apiServerExternalEndpoints is nil") |
| 89 | + } |
| 90 | + |
| 91 | + if updateEPS != nil { |
| 92 | + klog.Infof("updateEPS: %v", updateEPS) |
| 93 | + } else { |
| 94 | + klog.Info("updateEPS is nil") |
| 95 | + } |
| 96 | + |
| 97 | + if len(updateEPS.Subsets) == 1 && len(updateEPS.Subsets[0].Addresses) == 1 { |
| 98 | + ip := kubeEndpoints.Subsets[0].Addresses[0].IP |
| 99 | + klog.Infof("IP address: %s", ip) |
| 100 | + updateEPS.Subsets[0].Addresses[0].IP = ip |
| 101 | + |
| 102 | + if _, err := k8sClient.CoreV1().Endpoints(constants.DefaultNs).Update(ctx, updateEPS, metav1.UpdateOptions{}); err != nil { |
| 103 | + return fmt.Errorf("failed to update endpoints for api-server-external-service: %v", err) |
| 104 | + } |
| 105 | + } else { |
| 106 | + klog.ErrorS(err, "Unexpected format of endpoints for api-server-external-service", "endpoint_data", updateEPS) |
| 107 | + return fmt.Errorf("unexpected format of endpoints for api-server-external-service") |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func (e *ApiServerExternalSyncController) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 114 | + klog.V(4).Infof("============ %s start to reconcile %s ============", ApiServerExternalSyncControllerName, request.NamespacedName) |
| 115 | + defer klog.V(4).Infof("============ %s finish to reconcile %s ============", ApiServerExternalSyncControllerName, request.NamespacedName) |
| 116 | + |
| 117 | + var virtualClusterList v1alpha1.VirtualClusterList |
| 118 | + if err := e.List(ctx, &virtualClusterList); err != nil { |
| 119 | + if apierrors.IsNotFound(err) { |
| 120 | + return reconcile.Result{}, nil |
| 121 | + } |
| 122 | + klog.V(4).Infof("query virtualcluster failed: %v", err) |
| 123 | + return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil |
| 124 | + } |
| 125 | + var targetVirtualCluster v1alpha1.VirtualCluster |
| 126 | + hasVirtualCluster := false |
| 127 | + for _, vc := range virtualClusterList.Items { |
| 128 | + if vc.Namespace == request.Namespace { |
| 129 | + targetVirtualCluster = vc |
| 130 | + klog.V(4).Infof("virtualcluster %s found", targetVirtualCluster.Name) |
| 131 | + hasVirtualCluster = true |
| 132 | + break |
| 133 | + } |
| 134 | + } |
| 135 | + if !hasVirtualCluster { |
| 136 | + klog.V(4).Infof("virtualcluster %s not found", request.Namespace) |
| 137 | + return reconcile.Result{}, nil |
| 138 | + } |
| 139 | + |
| 140 | + if targetVirtualCluster.Status.Phase != v1alpha1.AllNodeReady && targetVirtualCluster.Status.Phase != v1alpha1.Completed { |
| 141 | + return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil |
| 142 | + } |
| 143 | + |
| 144 | + k8sClient, err := util.GenerateKubeclient(&targetVirtualCluster) |
| 145 | + if err != nil { |
| 146 | + klog.Errorf("virtualcluster %s crd kubernetes client failed: %v", targetVirtualCluster.Name, err) |
| 147 | + return reconcile.Result{}, nil |
| 148 | + } |
| 149 | + |
| 150 | + if err := retry.RetryOnConflict(retry.DefaultRetry, func() error { |
| 151 | + return e.SyncApiServerExternalEPS(ctx, k8sClient) |
| 152 | + }); err != nil { |
| 153 | + klog.Errorf("virtualcluster %s sync apiserver external endpoints failed: %v", targetVirtualCluster.Name, err) |
| 154 | + return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil |
| 155 | + } |
| 156 | + |
| 157 | + return reconcile.Result{}, nil |
| 158 | +} |
0 commit comments