From 1e760b0069460d7c52bdb7761913d87581e8cf5a Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Mon, 10 Jan 2022 14:51:38 +0800 Subject: [PATCH] Cleanup cluster controller and remove unused code --- pkg/controller/cluster/cluster_controller.go | 65 +++------- pkg/controller/cluster/util.go | 118 ------------------- 2 files changed, 17 insertions(+), 166 deletions(-) diff --git a/pkg/controller/cluster/cluster_controller.go b/pkg/controller/cluster/cluster_controller.go index 2b97602ed..f1d96f2a2 100644 --- a/pkg/controller/cluster/cluster_controller.go +++ b/pkg/controller/cluster/cluster_controller.go @@ -309,24 +309,6 @@ func buildClusterData(kubeconfig []byte) (*clusterData, error) { }, nil } -func (c *clusterController) syncStatus() error { - clusters, err := c.clusterLister.List(labels.Everything()) - if err != nil { - return err - } - - for _, cluster := range clusters { - key, err := cache.MetaNamespaceKeyFunc(cluster) - if err != nil { - return err - } - - c.queue.AddRateLimited(key) - } - - return nil -} - // reconcileHostCluster will create a host cluster if there are no clusters labeled 'cluster-role.kubesphere.io/host' func (c *clusterController) reconcileHostCluster() error { clusters, err := c.clusterLister.List(labels.SelectorFromSet(labels.Set{clusterv1alpha1.HostCluster: ""})) @@ -350,7 +332,7 @@ func (c *clusterController) reconcileHostCluster() error { } // only deal with cluster managed by kubesphere - cluster := clusters[0] + cluster := clusters[0].DeepCopy() managedByKubesphere, ok := cluster.Labels[kubesphereManaged] if !ok || managedByKubesphere != "true" { return nil @@ -406,45 +388,34 @@ func (c *clusterController) probeClusters() error { continue } - var con clusterv1alpha1.ClusterCondition - _, err = clientSet.Discovery().ServerVersion() - if err == nil { - con = clusterv1alpha1.ClusterCondition{ - Type: clusterv1alpha1.ClusterReady, - Status: v1.ConditionTrue, - LastUpdateTime: metav1.Now(), - LastTransitionTime: metav1.Now(), - Reason: string(clusterv1alpha1.ClusterReady), - Message: "Cluster is available now", - } - } else { - con = clusterv1alpha1.ClusterCondition{ - Type: clusterv1alpha1.ClusterReady, - Status: v1.ConditionFalse, - LastUpdateTime: metav1.Now(), - LastTransitionTime: metav1.Now(), - Reason: "failed to connect get kubernetes version", - Message: "Cluster is not available now", - } + condition := clusterv1alpha1.ClusterCondition{ + Type: clusterv1alpha1.ClusterReady, + Status: v1.ConditionTrue, + LastUpdateTime: metav1.Now(), + LastTransitionTime: metav1.Now(), + Reason: string(clusterv1alpha1.ClusterReady), + Message: "Cluster is available now", + } + if _, err = clientSet.Discovery().ServerVersion(); err != nil { + condition.Status = v1.ConditionFalse + condition.Reason = "failed to connect get kubernetes version" + condition.Message = "Cluster is not available now" } - c.updateClusterCondition(cluster, con) err = retry.RetryOnConflict(retry.DefaultBackoff, func() error { - ct, err := c.clusterClient.Get(context.TODO(), cluster.Name, metav1.GetOptions{}) + newCluster, err := c.clusterClient.Get(context.TODO(), cluster.Name, metav1.GetOptions{}) if err != nil { return err } - - ct.Status.Conditions = cluster.Status.Conditions - ct, err = c.clusterClient.Update(context.TODO(), ct, metav1.UpdateOptions{}) + c.updateClusterCondition(newCluster, condition) + _, err = c.clusterClient.Update(context.TODO(), newCluster, metav1.UpdateOptions{}) return err }) if err != nil { klog.Errorf("failed to update cluster %s status, err: %v", cluster.Name, err) } else { - klog.V(4).Infof("successfully updated cluster %s to status %v", cluster.Name, con) + klog.V(4).Infof("successfully updated cluster %s to status %v", cluster.Name, condition) } - } return nil @@ -465,7 +436,6 @@ func (c *clusterController) syncCluster(key string) error { }() cluster, err := c.clusterLister.Get(name) - if err != nil { // cluster not found, possibly been deleted // need to do the cleanup @@ -759,7 +729,6 @@ func (c *clusterController) tryToFetchKubeSphereComponents(host string, transpor return configz, nil } -// func (c *clusterController) tryFetchKubeSphereVersion(host string, transport http.RoundTripper) (string, error) { client := http.Client{ Transport: transport, diff --git a/pkg/controller/cluster/util.go b/pkg/controller/cluster/util.go index 279368e34..e7f624cbe 100644 --- a/pkg/controller/cluster/util.go +++ b/pkg/controller/cluster/util.go @@ -18,84 +18,14 @@ package cluster import ( "fmt" - "strings" - "github.com/pkg/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" pkgruntime "k8s.io/apimachinery/pkg/runtime" kubeclient "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" ) -// Default values for the federated group and version used by -// the enable and disable subcommands of `kubefedctl`. -const ( - DefaultFederatedGroup = "types.kubefed.io" - DefaultFederatedVersion = "v1beta1" - - FederatedKindPrefix = "Federated" -) - -// FedConfig provides a rest config based on the filesystem kubeconfig (via -// pathOptions) and context in order to talk to the host kubernetes cluster -// and the joining kubernetes cluster. -type FedConfig interface { - HostConfig(context, kubeconfigPath string) (*rest.Config, error) - ClusterConfig(context, kubeconfigPath string) (*rest.Config, error) - GetClientConfig(ontext, kubeconfigPath string) clientcmd.ClientConfig -} - -// fedConfig implements the FedConfig interface. -type fedConfig struct { - pathOptions *clientcmd.PathOptions -} - -// NewFedConfig creates a fedConfig for `kubefedctl` commands. -func NewFedConfig(pathOptions *clientcmd.PathOptions) FedConfig { - return &fedConfig{ - pathOptions: pathOptions, - } -} - -// HostConfig provides a rest config to talk to the host kubernetes cluster -// based on the context and kubeconfig passed in. -func (a *fedConfig) HostConfig(context, kubeconfigPath string) (*rest.Config, error) { - hostConfig := a.GetClientConfig(context, kubeconfigPath) - hostClientConfig, err := hostConfig.ClientConfig() - if err != nil { - return nil, err - } - - return hostClientConfig, nil -} - -// ClusterConfig provides a rest config to talk to the joining kubernetes -// cluster based on the context and kubeconfig passed in. -func (a *fedConfig) ClusterConfig(context, kubeconfigPath string) (*rest.Config, error) { - clusterConfig := a.GetClientConfig(context, kubeconfigPath) - clusterClientConfig, err := clusterConfig.ClientConfig() - if err != nil { - return nil, err - } - - return clusterClientConfig, nil -} - -// getClientConfig is a helper method to create a client config from the -// context and kubeconfig passed as arguments. -func (a *fedConfig) GetClientConfig(context, kubeconfigPath string) clientcmd.ClientConfig { - loadingRules := *a.pathOptions.LoadingRules - loadingRules.Precedence = a.pathOptions.GetLoadingPrecedence() - loadingRules.ExplicitPath = kubeconfigPath - overrides := &clientcmd.ConfigOverrides{ - CurrentContext: context, - } - - return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&loadingRules, overrides) -} - // HostClientset provides a kubernetes API compliant clientset to // communicate with the host cluster's kubernetes API server. func HostClientset(config *rest.Config) (*kubeclient.Clientset, error) { @@ -114,54 +44,6 @@ func ClusterServiceAccountName(joiningClusterName, hostClusterName string) strin return fmt.Sprintf("%s-%s", joiningClusterName, hostClusterName) } -// RoleName returns the name of a Role or ClusterRole and its -// associated RoleBinding or ClusterRoleBinding that are used to allow -// the service account to access necessary resources on the cluster. -func RoleName(serviceAccountName string) string { - return fmt.Sprintf("kubefed-controller-manager:%s", serviceAccountName) -} - -// HealthCheckRoleName returns the name of a ClusterRole and its -// associated ClusterRoleBinding that is used to allow the service -// account to check the health of the cluster and list nodes. -func HealthCheckRoleName(serviceAccountName, namespace string) string { - return fmt.Sprintf("kubefed-controller-manager:%s:healthcheck-%s", namespace, serviceAccountName) -} - -// IsFederatedAPIResource checks if a resource with the given Kind and group is a Federated one -func IsFederatedAPIResource(kind, group string) bool { - return strings.HasPrefix(kind, FederatedKindPrefix) && group == DefaultFederatedGroup -} - -// GetNamespace returns namespace of the current context -func GetNamespace(hostClusterContext string, kubeconfig string, config FedConfig) (string, error) { - clientConfig := config.GetClientConfig(hostClusterContext, kubeconfig) - currentContext, err := CurrentContext(clientConfig) - if err != nil { - return "", err - } - - ns, _, err := clientConfig.Namespace() - if err != nil { - return "", errors.Wrapf(err, "Failed to get ClientConfig for host cluster context %q and kubeconfig %q", - currentContext, kubeconfig) - } - - if len(ns) == 0 { - ns = "default" - } - return ns, nil -} - -// CurrentContext retrieves the current context from the provided config. -func CurrentContext(config clientcmd.ClientConfig) (string, error) { - rawConfig, err := config.RawConfig() - if err != nil { - return "", errors.Wrap(err, "Failed to get current context from config") - } - return rawConfig.CurrentContext, nil -} - // IsPrimaryCluster checks if the caller is working with objects for the // primary cluster by checking if the UIDs match for both ObjectMetas passed // in.