Skip to content

Commit

Permalink
Merge pull request #473 from zhouhaoA1/feature_components_deploy
Browse files Browse the repository at this point in the history
add component deployment functionality
  • Loading branch information
kosmos-robot authored Apr 23, 2024
2 parents f18f7e4 + 970ec4a commit 5a94d1b
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 51 deletions.
1 change: 1 addition & 0 deletions cmd/kubenest/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func run(ctx context.Context, opts *options.Options) error {
Config: mgr.GetConfig(),
EventRecorder: mgr.GetEventRecorderFor(constants.InitControllerName),
HostPortManager: hostPortManager,
RootClientSet: hostKubeClient,
}
if err = VirtualClusterInitController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting %s: %v", constants.InitControllerName, err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/apis/kosmos/v1alpha1/virtualcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ const (
Initialized Phase = "Initialized"
// Completed means everything is ready,kosmos is joined, and resource is promoted
Completed Phase = "Completed"
// ControllerPlaneCompleted means kubernetes control plane is ready,kosmos is joined, and resource is promoted
ControllerPlaneCompleted Phase = "ControllerPlaneCompleted"
// AllNodeReady means all nodes have joined the virtual control plane and are in the running state
AllNodeReady Phase = "AllNodeReady"
// Updating means that some changes are happening
Updating Phase = "Updating"
Pending Phase = "Pending"
)

// +genclient
Expand Down
21 changes: 15 additions & 6 deletions pkg/kubenest/constants/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ const (
InitControllerName = "virtual-cluster-init-controller"
NodeControllerName = "virtual-cluster-node-controller"
KosmosJoinControllerName = "kosmos-join-controller"
KosmosNs = "kosmos-system"
SystemNs = "kube-system"
DefauleImageRepositoryEnv = "IMAGE_REPOSITIRY"
DefauleImageVersionEnv = "IMAGE_VERSION"
VirtualClusterStatusCompleted = "Completed"
VirtualClusterStatusUpdating = "Updating"
VirtualClusterFinalizerName = "kosmos.io/virtual-cluster-finalizer"
ServiceType = "NodePort"
EtcdServiceType = "ClusterIP"
DisableCascadingDeletionLabel = "operator.virtualcluster.io/disable-cascading-deletion"
ControllerFinalizerName = "operator.virtualcluster.io/finalizer"
DefaultKubeconfigPath = "/etc/cluster-tree/cert"
Label = "virtualCluster-app"
ComponentBeReadyTimeout = 120 * time.Second
ComponentBeReadyTimeout = 300 * time.Second

// CertificateBlockType is a possible value for pem.Block.Type.
CertificateBlockType = "CERTIFICATE"
Expand Down Expand Up @@ -61,11 +60,13 @@ const (
VirtualClusterSchedulerReplicas = 1
VirtualClusterSchedulerComponent = "VirtualClusterScheduler"
VirtualClusterSchedulerComponentConfigMap = "scheduler-config"
VirtualClusterScheduler = "virtualCluster-scheduler"
VirtualClusterScheduler = "scheduler"
VirtualClusterKubeProxyComponent = "kube-proxy"

//controlplane auth
AdminConfig = "admin-config"
KubeConfig = "kubeconfig"
AdminConfig = "admin-config"
KubeConfig = "kubeconfig"
KubeProxyConfigmap = "kube-proxy"

//controlplane upload
VirtualClusterLabelKeyName = "app.kubernetes.io/managed-by"
Expand All @@ -77,6 +78,14 @@ const (
InitAction Action = "init"
// DeInitAction represents delete virtual cluster instance
DeInitAction Action = "deInit"

ManifestComponentsConfigmap = "components-manifest-cm"
NodePoolConfigmap = "node-pool"
NodeShareState = "share"
NodeVirtualclusterState = "virtualcluster"
NodeFreeState = "free"

WaitAllPodsRunningTimeoutSeconds = 1800
)

type Action string
4 changes: 2 additions & 2 deletions pkg/kubenest/controller/kosmos/kosmos_join_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *KosmosJoinController) InitNodeOwnerMap() {
return
}
for _, vc := range vcList.Items {
if vc.Status.Phase == constants.VirtualClusterStatusCompleted {
if vc.Status.Phase == v1alpha1.Completed {
kubeconfigStream, err := base64.StdEncoding.DecodeString(vc.Spec.Kubeconfig)
if err != nil {
klog.Errorf("virtualcluster %s decode target kubernetes kubeconfig %s err: %v", vc.Name, vc.Spec.Kubeconfig, err)
Expand Down Expand Up @@ -542,7 +542,7 @@ func (c *KosmosJoinController) Reconcile(ctx context.Context, request reconcile.
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
if vc.DeletionTimestamp.IsZero() {
if vc.Status.Phase != constants.VirtualClusterStatusCompleted {
if vc.Status.Phase != v1alpha1.Completed {
klog.Infof("cluster's status is %s, skip", vc.Status.Phase)
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (r *NodeController) Reconcile(ctx context.Context, request reconcile.Reques
return reconcile.Result{}, nil
}

if virtualCluster.Status.Phase != v1alpha1.ControllerPlaneCompleted {
if virtualCluster.Status.Phase != v1alpha1.Initialized {
klog.V(4).Infof("virtualcluster wait cluster ready, cluster name: %s", virtualCluster.Name)
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
Expand Down
44 changes: 19 additions & 25 deletions pkg/kubenest/controller/virtualcluster_execute_controller.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package controller

import (
"context"
"encoding/base64"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"github.com/pkg/errors"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -56,32 +53,29 @@ func (e *Executor) Execute() error {

if err := e.phase.Run(); err != nil {
klog.ErrorS(err, "failed to executed the workflow", "workflow", "virtual cluster", klog.KObj(e.virtualCluster))
}
//TODO modify status for virtualcluster
if err := e.afterRunPhase(); err != nil {
return err
return errors.Wrap(err, "failed to executed the workflow")
}
klog.InfoS("Successfully executed the workflow", "workflow", "virtual cluster", klog.KObj(e.virtualCluster))
return nil
}

func (e *Executor) afterRunPhase() error {
localClusterClient, err := clientset.NewForConfig(e.config)
if err != nil {
return fmt.Errorf("error when creating local cluster client, err: %w", err)
}
secret, err := localClusterClient.CoreV1().Secrets(e.virtualCluster.GetNamespace()).Get(context.TODO(),
fmt.Sprintf("%s-%s", e.virtualCluster.GetName(), constants.AdminConfig), metav1.GetOptions{})
if err != nil {
return err
}

kubeconfigBytes := secret.Data[constants.KubeConfig]
configString := base64.StdEncoding.EncodeToString(kubeconfigBytes)
e.virtualCluster.Spec.Kubeconfig = configString
e.virtualCluster.Status.Phase = v1alpha1.Completed
return e.Client.Update(context.TODO(), e.virtualCluster)
}
//func (e *Executor) afterRunPhase() error {
// localClusterClient, err := clientset.NewForConfig(e.config)
// if err != nil {
// return fmt.Errorf("error when creating local cluster client, err: %w", err)
// }
// secret, err := localClusterClient.CoreV1().Secrets(e.virtualCluster.GetNamespace()).Get(context.TODO(),
// fmt.Sprintf("%s-%s", e.virtualCluster.GetName(), constants.AdminConfig), metav1.GetOptions{})
// if err != nil {
// return err
// }
//
// kubeconfigBytes := secret.Data[constants.KubeConfig]
// configString := base64.StdEncoding.EncodeToString(kubeconfigBytes)
// e.virtualCluster.Spec.Kubeconfig = configString
// e.virtualCluster.Status.Phase = v1alpha1.Completed
// return e.Client.Update(context.TODO(), e.virtualCluster)
//}

func recognizeActionFor(virtualCluster *v1alpha1.VirtualCluster) constants.Action {
if !virtualCluster.DeletionTimestamp.IsZero() {
Expand Down
Loading

0 comments on commit 5a94d1b

Please sign in to comment.