Skip to content

Commit

Permalink
Merge pull request kosmos-io#280 from ONE7live/main
Browse files Browse the repository at this point in the history
fix: unjoin exception and init leaf model
  • Loading branch information
kosmos-robot authored Nov 23, 2023
2 parents 042d877 + 53036b3 commit 66f5d46
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 163 deletions.
24 changes: 0 additions & 24 deletions pkg/kosmosctl/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,30 +404,6 @@ func (o *CommandInstallOptions) runClustertree() error {
}
klog.Info("Create CRD " + clustertreeCluster.Name + " successful.")

serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil)
if err != nil {
return err
}
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clustertree run error, crd options failed: %v", err)
}
}
klog.Info("Create CRD " + serviceExport.Name + " successful.")

serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil)
if err != nil {
return err
}
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clustertree run error, crd options failed: %v", err)
}
}
klog.Info("Create CRD " + serviceImport.Name + " successful.")

klog.Info("Start creating kosmos-clustertree ConfigMap...")
clustertreeConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
75 changes: 68 additions & 7 deletions pkg/kosmosctl/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -60,9 +61,11 @@ type CommandJoinOptions struct {
UseProxy string

EnableTree bool
LeafModel string

KosmosClient versioned.Interface
K8sClient kubernetes.Interface
KosmosClient versioned.Interface
K8sClient kubernetes.Interface
K8sExtensionsClient extensionsclient.Interface
}

// NewCmdJoin join resource to Kosmos control plane.
Expand Down Expand Up @@ -99,6 +102,7 @@ func NewCmdJoin(f ctlutil.Factory) *cobra.Command {
flags.StringVar(&o.IpFamily, "ip-family", utils.DefaultIPv4, "Specify the IP protocol version used by network devices, common IP families include IPv4 and IPv6.")
flags.StringVar(&o.UseProxy, "use-proxy", "false", "Set whether to enable proxy.")
flags.BoolVar(&o.EnableTree, "enable-tree", false, "Turn on clustertree.")
flags.StringVar(&o.LeafModel, "leaf-model", "", "Set leaf cluster model, which supports one-to-one model.")
flags.IntVarP(&o.WaitTime, "wait-time", "", utils.DefaultWaitTime, "Wait the specified time for the Kosmos install ready.")

return cmd
Expand Down Expand Up @@ -147,7 +151,12 @@ func (o *CommandJoinOptions) Complete(f ctlutil.Factory) error {

o.K8sClient, err = kubernetes.NewForConfig(clusterConfig)
if err != nil {
return fmt.Errorf("kosmosctl join complete error, generate basic client failed: %v", err)
return fmt.Errorf("kosmosctl join complete error, generate K8s basic client failed: %v", err)
}

o.K8sExtensionsClient, err = extensionsclient.NewForConfig(clusterConfig)
if err != nil {
return fmt.Errorf("kosmosctl join complete error, generate K8s extensions client failed: %v", err)
}
} else {
return fmt.Errorf("kosmosctl join complete error, arg ClusterKubeConfig is required")
Expand Down Expand Up @@ -246,10 +255,62 @@ func (o *CommandJoinOptions) runCluster() error {
cluster.Spec.ClusterLinkOptions.CNI = o.CNI
}

// ToDo ClusterTree currently has no init parameters, can be expanded later.
//if o.EnableTree {
//
//}
if o.EnableTree {
serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil)
if err != nil {
return err
}
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err)
}
}
klog.Info("Create CRD " + serviceExport.Name + " successful.")

serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil)
if err != nil {
return err
}
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err)
}
}
klog.Info("Create CRD " + serviceImport.Name + " successful.")

if len(o.LeafModel) > 0 {
switch o.LeafModel {
case "one-to-one":
// ToDo Perform follow-up query based on the leaf cluster label
nodes, err := o.K8sClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{
LabelSelector: utils.KosmosNodeJoinLabel + "=" + utils.KosmosNodeJoinValue,
})
if err != nil {
return fmt.Errorf("kosmosctl join run error, list cluster node failed: %v", err)
}
var leafModels []v1alpha1.LeafModel
for _, n := range nodes.Items {
leafModel := v1alpha1.LeafModel{
LeafNodeName: n.Name,
Taints: []corev1.Taint{
{
Effect: utils.KosmosNodeTaintEffect,
Key: utils.KosmosNodeTaintKey,
Value: utils.KosmosNodeValue,
},
},
NodeSelector: v1alpha1.NodeSelector{
NodeName: n.Name,
},
}
leafModels = append(leafModels, leafModel)
}
cluster.Spec.ClusterTreeOptions.LeafModels = leafModels
}
}
}

if o.RootFlag {
cluster.Annotations = map[string]string{
Expand Down
112 changes: 61 additions & 51 deletions pkg/kosmosctl/manifest/manifest_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,60 +491,11 @@ spec:
enable:
default: true
type: boolean
leafModel:
description: LeafModel provide an api to arrange the member cluster
leafModels:
description: LeafModels provide an api to arrange the member cluster
with some rules to pretend one or more leaf node
items:
properties:
labelSelector:
description: LabelSelector is a filter to select member
cluster nodes to pretend a leaf node in clusterTree by
labels. If nil or empty, the hole member cluster nodes
will pretend one leaf node.
properties:
matchExpressions:
description: matchExpressions is a list of label selector
requirements. The requirements are ANDed.
items:
description: A label selector requirement is a selector
that contains values, a key, and an operator that
relates the key and values.
properties:
key:
description: key is the label key that the selector
applies to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are In,
NotIn, Exists and DoesNotExist.
type: string
values:
description: values is an array of string values.
If the operator is In or NotIn, the values array
must be non-empty. If the operator is Exists
or DoesNotExist, the values array must be empty.
This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value} pairs.
A single {key,value} in the matchLabels map is equivalent
to an element of matchExpressions, whose key field
is "key", the operator is "In", and the values array
contains only "value". The requirements are ANDed.
type: object
type: object
x-kubernetes-map-type: atomic
labels:
additionalProperties:
type: string
Expand All @@ -556,6 +507,65 @@ spec:
the leaf node name will generate by controller and fill
in cluster link status
type: string
nodeSelector:
description: NodeSelector is a selector to select member
cluster nodes to pretend a leaf node in clusterTree.
properties:
labelSelector:
description: LabelSelector is a filter to select member
cluster nodes to pretend a leaf node in clusterTree
by labels. It will work on second level schedule on
pod create in member clusters.
properties:
matchExpressions:
description: matchExpressions is a list of label
selector requirements. The requirements are ANDed.
items:
description: A label selector requirement is a
selector that contains values, a key, and an
operator that relates the key and values.
properties:
key:
description: key is the label key that the
selector applies to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are
In, NotIn, Exists and DoesNotExist.
type: string
values:
description: values is an array of string
values. If the operator is In or NotIn,
the values array must be non-empty. If the
operator is Exists or DoesNotExist, the
values array must be empty. This array is
replaced during a strategic merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
matchLabels:
additionalProperties:
type: string
description: matchLabels is a map of {key,value}
pairs. A single {key,value} in the matchLabels
map is equivalent to an element of matchExpressions,
whose key field is "key", the operator is "In",
and the values array contains only "value". The
requirements are ANDed.
type: object
type: object
x-kubernetes-map-type: atomic
nodeName:
description: NodeName is Member cluster origin node
Name
type: string
type: object
taints:
description: Taints attached to the leaf pretended Node.
If nil or empty, controller will set the default no-schedule
Expand Down
2 changes: 1 addition & 1 deletion pkg/kosmosctl/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (o *CommandUninstallOptions) runClustertree() error {
}
} else {
klog.Info("Deployment " + clustertreeDeploy.Name + " is deleted.")
clustertreeSecret, err := util.GenerateService(manifest.ClusterTreeClusterManagerSecret, manifest.SecretReplace{
clustertreeSecret, err := util.GenerateSecret(manifest.ClusterTreeClusterManagerSecret, manifest.SecretReplace{
Namespace: o.Namespace,
Cert: "",
Key: "",
Expand Down
Loading

0 comments on commit 66f5d46

Please sign in to comment.