|
| 1 | +package hub |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/openshift/library-go/pkg/controller/controllercmd" |
| 8 | + "github.com/spf13/pflag" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/client-go/dynamic" |
| 11 | + "k8s.io/client-go/dynamic/dynamicinformer" |
| 12 | + kubeinformers "k8s.io/client-go/informers" |
| 13 | + "k8s.io/client-go/kubernetes" |
| 14 | + "k8s.io/client-go/metadata" |
| 15 | + "k8s.io/client-go/rest" |
| 16 | + "k8s.io/klog/v2" |
| 17 | + cpclientset "sigs.k8s.io/cluster-inventory-api/client/clientset/versioned" |
| 18 | + cpinformerv1alpha1 "sigs.k8s.io/cluster-inventory-api/client/informers/externalversions" |
| 19 | + |
| 20 | + addonclient "open-cluster-management.io/api/client/addon/clientset/versioned" |
| 21 | + addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" |
| 22 | + clusterv1client "open-cluster-management.io/api/client/cluster/clientset/versioned" |
| 23 | + clusterv1informers "open-cluster-management.io/api/client/cluster/informers/externalversions" |
| 24 | + workv1client "open-cluster-management.io/api/client/work/clientset/versioned" |
| 25 | + workv1informers "open-cluster-management.io/api/client/work/informers/externalversions" |
| 26 | + clusterv1 "open-cluster-management.io/api/cluster/v1" |
| 27 | + ocmfeature "open-cluster-management.io/api/feature" |
| 28 | + |
| 29 | + "open-cluster-management.io/ocm/pkg/addon" |
| 30 | + commonoptions "open-cluster-management.io/ocm/pkg/common/options" |
| 31 | + "open-cluster-management.io/ocm/pkg/features" |
| 32 | + placementcontrollers "open-cluster-management.io/ocm/pkg/placement/controllers" |
| 33 | + registrationhub "open-cluster-management.io/ocm/pkg/registration/hub" |
| 34 | + workhub "open-cluster-management.io/ocm/pkg/work/hub" |
| 35 | +) |
| 36 | + |
| 37 | +type HubOption struct { |
| 38 | + CommonOption *commonoptions.Options |
| 39 | + registrationOption *registrationhub.HubManagerOptions |
| 40 | + workOption *workhub.WorkHubManagerOptions |
| 41 | +} |
| 42 | + |
| 43 | +func NewHubOption() *HubOption { |
| 44 | + return &HubOption{ |
| 45 | + CommonOption: commonoptions.NewOptions(), |
| 46 | + registrationOption: registrationhub.NewHubManagerOptions(), |
| 47 | + workOption: workhub.NewWorkHubManagerOptions(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (o *HubOption) AddFlags(fs *pflag.FlagSet) { |
| 52 | + o.CommonOption.AddFlags(fs) |
| 53 | + o.registrationOption.AddFlags(fs) |
| 54 | + o.workOption.AddFlags(fs) |
| 55 | +} |
| 56 | + |
| 57 | +func (o *HubOption) RunManager(ctx context.Context, controllerContext *controllercmd.ControllerContext) error { |
| 58 | + kubeClient, err := kubernetes.NewForConfig(controllerContext.KubeConfig) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + dynamicClient, err := dynamic.NewForConfig(controllerContext.KubeConfig) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + // copy a separate config for gc controller and increase the gc controller's throughput. |
| 69 | + metadataKubeConfig := rest.CopyConfig(controllerContext.KubeConfig) |
| 70 | + metadataKubeConfig.QPS = controllerContext.KubeConfig.QPS * 2 |
| 71 | + metadataKubeConfig.Burst = controllerContext.KubeConfig.Burst * 2 |
| 72 | + metadataClient, err := metadata.NewForConfig(metadataKubeConfig) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + clusterClient, err := clusterv1client.NewForConfig(controllerContext.KubeConfig) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + clusterProfileClient, err := cpclientset.NewForConfig(controllerContext.KubeConfig) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + workClient, err := workv1client.NewForConfig(controllerContext.KubeConfig) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + addOnClient, err := addonclient.NewForConfig(controllerContext.KubeConfig) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + clusterInformers := clusterv1informers.NewSharedInformerFactory(clusterClient, 30*time.Minute) |
| 98 | + clusterProfileInformers := cpinformerv1alpha1.NewSharedInformerFactory(clusterProfileClient, 30*time.Minute) |
| 99 | + workInformers := workv1informers.NewSharedInformerFactory(workClient, 30*time.Minute) |
| 100 | + replicaSetInformers := workv1informers.NewSharedInformerFactory(workClient, 30*time.Minute) |
| 101 | + kubeInfomers := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 30*time.Minute, kubeinformers.WithTweakListOptions( |
| 102 | + func(listOptions *metav1.ListOptions) { |
| 103 | + // Note all kube resources managed by registration should have the cluster label, and should not have |
| 104 | + // the addon label. |
| 105 | + selector := &metav1.LabelSelector{ |
| 106 | + MatchExpressions: []metav1.LabelSelectorRequirement{ |
| 107 | + { |
| 108 | + Key: clusterv1.ClusterNameLabelKey, |
| 109 | + Operator: metav1.LabelSelectorOpExists, |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + listOptions.LabelSelector = metav1.FormatLabelSelector(selector) |
| 114 | + })) |
| 115 | + addOnInformers := addoninformers.NewSharedInformerFactory(addOnClient, 30*time.Minute) |
| 116 | + dynamicInformers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 30*time.Minute) |
| 117 | + |
| 118 | + // Create an error channel to collect errors from controllers |
| 119 | + errCh := make(chan error, 4) |
| 120 | + |
| 121 | + // start registration controller |
| 122 | + go func() { |
| 123 | + err := o.registrationOption.RunControllerManagerWithInformers( |
| 124 | + ctx, controllerContext, |
| 125 | + kubeClient, metadataClient, clusterClient, clusterProfileClient, addOnClient, |
| 126 | + kubeInfomers, clusterInformers, clusterProfileInformers, workInformers, addOnInformers) |
| 127 | + if err != nil { |
| 128 | + klog.Errorf("failed to start registration controller: %v", err) |
| 129 | + errCh <- err |
| 130 | + } |
| 131 | + }() |
| 132 | + |
| 133 | + // start placement controller |
| 134 | + go func() { |
| 135 | + err := placementcontrollers.RunControllerManagerWithInformers( |
| 136 | + ctx, controllerContext, kubeClient, clusterClient, clusterInformers) |
| 137 | + if err != nil { |
| 138 | + klog.Errorf("failed to start placement controller: %v", err) |
| 139 | + errCh <- err |
| 140 | + } |
| 141 | + }() |
| 142 | + |
| 143 | + // start addon controller |
| 144 | + if features.HubMutableFeatureGate.Enabled(ocmfeature.AddonManagement) { |
| 145 | + go func() { |
| 146 | + err := addon.RunControllerManagerWithInformers( |
| 147 | + ctx, controllerContext, kubeClient, addOnClient, workClient, |
| 148 | + clusterInformers, addOnInformers, workInformers, dynamicInformers) |
| 149 | + if err != nil { |
| 150 | + klog.Errorf("failed to start addon controller: %v", err) |
| 151 | + errCh <- err |
| 152 | + } |
| 153 | + }() |
| 154 | + } |
| 155 | + |
| 156 | + // start work controller |
| 157 | + if features.HubMutableFeatureGate.Enabled(ocmfeature.ManifestWorkReplicaSet) { |
| 158 | + go func() { |
| 159 | + hubConfig := workhub.NewWorkHubManagerConfig(o.workOption) |
| 160 | + err := hubConfig.RunControllerManagerWithInformers( |
| 161 | + ctx, controllerContext, workClient, replicaSetInformers, workInformers, clusterInformers) |
| 162 | + if err != nil { |
| 163 | + klog.Errorf("failed to start work controller: %v", err) |
| 164 | + errCh <- err |
| 165 | + } |
| 166 | + }() |
| 167 | + } |
| 168 | + |
| 169 | + // Wait for context cancellation or first error |
| 170 | + select { |
| 171 | + case <-ctx.Done(): |
| 172 | + return nil |
| 173 | + case err := <-errCh: |
| 174 | + return err |
| 175 | + } |
| 176 | +} |
0 commit comments