Skip to content

Commit

Permalink
Merge pull request #507 from zhouhaoA1/feature_components_deploy
Browse files Browse the repository at this point in the history
add uninstall coredns feature and fix globalnode status bug
  • Loading branch information
kosmos-robot authored May 7, 2024
2 parents 0eb57c4 + 4d204e8 commit ffa2dfb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/kosmos/v1alpha1/global_node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:printcolumn:name="NODE_IP",type=string,JSONPath=`.spec.nodeIP`
// +kubebuilder:printcolumn:name="STATE",type=string,JSONPath=`.spec.state`
// +kubebuilder:printcolumn:name="VIRTUAL_CLUSTER",type=string,JSONPath=`.status.VirtualCluster`
// +kubebuilder:printcolumn:name="VIRTUAL_CLUSTER",type=string,JSONPath=`.status.virtualCluster`

type GlobalNode struct {
metav1.TypeMeta `json:",inline"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ func (r *NodeController) Reconcile(ctx context.Context, request reconcile.Reques

if err := r.DoNodeTask(ctx, virtualCluster); err != nil {
klog.Errorf("virtualcluster %s do node task failed: %v", virtualCluster.Name, err)
if err := r.UpdateVirtualClusterStatus(ctx, virtualCluster, v1alpha1.Pending, err.Error()); err != nil {
klog.Errorf("update virtualcluster %s status error: %v", request.NamespacedName, err)
}
//if err := r.UpdateVirtualClusterStatus(ctx, virtualCluster, v1alpha1.Pending, err.Error()); err != nil {
// klog.Errorf("update virtualcluster %s status error: %v", request.NamespacedName, err)
//}
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

Expand Down
17 changes: 11 additions & 6 deletions pkg/kubenest/controller/virtualcluster_init_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (c *VirtualClusterInitController) Reconcile(ctx context.Context, request re
func (c *VirtualClusterInitController) SetupWithManager(mgr manager.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).
Named(constants.InitControllerName).
WithOptions(controller.Options{}).
WithOptions(controller.Options{MaxConcurrentReconciles: 5}).
For(&v1alpha1.VirtualCluster{},
builder.WithPredicates(predicate.Funcs{
// UpdateFunc: c.onVirtualClusterUpdate,
Expand Down Expand Up @@ -332,11 +332,16 @@ func (c *VirtualClusterInitController) assignNodesByPolicy(virtualCluster *v1alp
for _, index := range newAssignNodes {
updated := globalNodes[index].DeepCopy()
updated.Spec.State = v1alpha1.NodeInUse
updated.Status.VirtualCluster = virtualCluster.Name
klog.V(2).Infof("Assign node %s for virtualcluster %s policy %s", updated.Name, virtualCluster.GetName(), policy.LabelSelector.String())
if _, err := c.KosmosClient.KosmosV1alpha1().GlobalNodes().Update(context.TODO(), updated, metav1.UpdateOptions{}); err != nil {
updated, err := c.KosmosClient.KosmosV1alpha1().GlobalNodes().Update(context.TODO(), updated, metav1.UpdateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Failed to update globalNode %s to InUse", updated.Name)
}
updated.Status.VirtualCluster = virtualCluster.Name
updated, err = c.KosmosClient.KosmosV1alpha1().GlobalNodes().UpdateStatus(context.TODO(), updated, metav1.UpdateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Failed to update globalNode %s status virtualcluster to %s", updated.Name, virtualCluster.Name)
}
globalNodes[index] = *updated
nodesAssigned = append(nodesAssigned, v1alpha1.NodeInfo{
NodeName: updated.Name,
Expand Down Expand Up @@ -395,7 +400,7 @@ func (c *VirtualClusterInitController) ensureAllPodsRunning(virtualCluster *v1al
}
klog.V(2).Infof("Check if all pods ready in namespace %s", namespace.Name)
err := wait.PollWithContext(context.TODO(), 5*time.Second, time.Duration(endTime-startTime)*time.Second, func(ctx context.Context) (done bool, err error) {
klog.V(2).Infof("Check if all deployments ready in namespace %s", namespace.Name)
klog.V(2).Infof("Check if virtualcluster %s all deployments ready in namespace %s", virtualCluster.Name, namespace.Name)
deployList, err := clientset.AppsV1().Deployments(namespace.Name).List(ctx, metav1.ListOptions{})
if err != nil {
return false, errors.Wrapf(err, "Get deployment list in namespace %s error", namespace.Name)
Expand All @@ -407,7 +412,7 @@ func (c *VirtualClusterInitController) ensureAllPodsRunning(virtualCluster *v1al
}
}

klog.V(2).Infof("Check if all statefulset ready in namespace %s", namespace.Name)
klog.V(2).Infof("Check if virtualcluster %s all statefulset ready in namespace %s", virtualCluster.Name, namespace.Name)
stsList, err := clientset.AppsV1().StatefulSets(namespace.Name).List(ctx, metav1.ListOptions{})
if err != nil {
return false, errors.Wrapf(err, "Get statefulset list in namespace %s error", namespace.Name)
Expand All @@ -419,7 +424,7 @@ func (c *VirtualClusterInitController) ensureAllPodsRunning(virtualCluster *v1al
}
}

klog.V(2).Infof("Check if all daemonset ready in namespace %s", namespace.Name)
klog.V(2).Infof("Check if virtualcluster %s all daemonset ready in namespace %s", virtualCluster.Name, namespace.Name)
damonsetList, err := clientset.AppsV1().DaemonSets(namespace.Name).List(ctx, metav1.ListOptions{})
if err != nil {
return false, errors.Wrapf(err, "Get daemonset list in namespace %s error", namespace.Name)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubenest/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewInitPhase(opts *InitOptions, hostPortManager *vcnodecontroller.HostPortM

func UninstallPhase(opts *InitOptions, hostPortManager *vcnodecontroller.HostPortManager) *workflow.Phase {
destroyPhase := workflow.NewPhase()

destroyPhase.AppendTask(tasks.UninstallCoreDNSTask())
destroyPhase.AppendTask(tasks.UninstallComponentTask())
destroyPhase.AppendTask(tasks.UninstallVirtualClusterApiserverTask())
destroyPhase.AppendTask(tasks.UninstallEtcdTask())
Expand Down
39 changes: 39 additions & 0 deletions pkg/kubenest/tasks/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ func runCoreDns(r workflow.RunData) error {
return nil
}

func UninstallCoreDNSTask() workflow.Task {
return workflow.Task{
Name: "coredns",
Run: runCoreDns,
RunSubTasks: true,
Tasks: []workflow.Task{
{
Name: "remove-core-dns-in-host-cluster",
Run: uninstallCorednsHostTask,
},
},
}
}

func getCoreDnsHostComponentsConfig(client clientset.Interface, keyName string) ([]ComponentConfig, error) {
cm, err := client.CoreV1().ConfigMaps(constants.KosmosNs).Get(context.Background(), constants.ManifestComponentsConfigMap, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -109,6 +123,31 @@ func runCoreDnsHostTask(r workflow.RunData) error {
return nil
}

func uninstallCorednsHostTask(r workflow.RunData) error {
data, ok := r.(InitData)
if !ok {
return errors.New("Virtual cluster manifests-components task invoked with an invalid data struct")
}

deployName := fmt.Sprintf("%s-%s", data.GetName(), "coredns")
if err := data.RemoteClient().AppsV1().Deployments(data.GetNamespace()).Delete(context.TODO(), deployName, metav1.DeleteOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "Failed to delete deployment %s/%s", deployName, data.GetNamespace())
}
}
if err := data.RemoteClient().CoreV1().ConfigMaps(data.GetNamespace()).Delete(context.TODO(), "coredns", metav1.DeleteOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "Failed to delete configmap %s/%s", "coredns", data.GetNamespace())
}
}
if err := data.RemoteClient().CoreV1().Services(data.GetNamespace()).Delete(context.TODO(), "kube-dns", metav1.DeleteOptions{}); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "Failed to delete service %s/%s", "kube-dns", data.GetNamespace())
}
}
return nil
}

// in host
func runCheckCoreDnsTask(r workflow.RunData) error {
data, ok := r.(InitData)
Expand Down

0 comments on commit ffa2dfb

Please sign in to comment.