Skip to content

Commit 66f5d46

Browse files
authored
Merge pull request kosmos-io#280 from ONE7live/main
fix: unjoin exception and init leaf model
2 parents 042d877 + 53036b3 commit 66f5d46

File tree

7 files changed

+236
-163
lines changed

7 files changed

+236
-163
lines changed

pkg/kosmosctl/install/install.go

-24
Original file line numberDiff line numberDiff line change
@@ -404,30 +404,6 @@ func (o *CommandInstallOptions) runClustertree() error {
404404
}
405405
klog.Info("Create CRD " + clustertreeCluster.Name + " successful.")
406406

407-
serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil)
408-
if err != nil {
409-
return err
410-
}
411-
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{})
412-
if err != nil {
413-
if !apierrors.IsAlreadyExists(err) {
414-
return fmt.Errorf("kosmosctl install clustertree run error, crd options failed: %v", err)
415-
}
416-
}
417-
klog.Info("Create CRD " + serviceExport.Name + " successful.")
418-
419-
serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil)
420-
if err != nil {
421-
return err
422-
}
423-
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{})
424-
if err != nil {
425-
if !apierrors.IsAlreadyExists(err) {
426-
return fmt.Errorf("kosmosctl install clustertree run error, crd options failed: %v", err)
427-
}
428-
}
429-
klog.Info("Create CRD " + serviceImport.Name + " successful.")
430-
431407
klog.Info("Start creating kosmos-clustertree ConfigMap...")
432408
clustertreeConfigMap := &corev1.ConfigMap{
433409
ObjectMeta: metav1.ObjectMeta{

pkg/kosmosctl/join/join.go

+68-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

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

6263
EnableTree bool
64+
LeafModel string
6365

64-
KosmosClient versioned.Interface
65-
K8sClient kubernetes.Interface
66+
KosmosClient versioned.Interface
67+
K8sClient kubernetes.Interface
68+
K8sExtensionsClient extensionsclient.Interface
6669
}
6770

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

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

148152
o.K8sClient, err = kubernetes.NewForConfig(clusterConfig)
149153
if err != nil {
150-
return fmt.Errorf("kosmosctl join complete error, generate basic client failed: %v", err)
154+
return fmt.Errorf("kosmosctl join complete error, generate K8s basic client failed: %v", err)
155+
}
156+
157+
o.K8sExtensionsClient, err = extensionsclient.NewForConfig(clusterConfig)
158+
if err != nil {
159+
return fmt.Errorf("kosmosctl join complete error, generate K8s extensions client failed: %v", err)
151160
}
152161
} else {
153162
return fmt.Errorf("kosmosctl join complete error, arg ClusterKubeConfig is required")
@@ -246,10 +255,62 @@ func (o *CommandJoinOptions) runCluster() error {
246255
cluster.Spec.ClusterLinkOptions.CNI = o.CNI
247256
}
248257

249-
// ToDo ClusterTree currently has no init parameters, can be expanded later.
250-
//if o.EnableTree {
251-
//
252-
//}
258+
if o.EnableTree {
259+
serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil)
260+
if err != nil {
261+
return err
262+
}
263+
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceExport, metav1.CreateOptions{})
264+
if err != nil {
265+
if !apierrors.IsAlreadyExists(err) {
266+
return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err)
267+
}
268+
}
269+
klog.Info("Create CRD " + serviceExport.Name + " successful.")
270+
271+
serviceImport, err := util.GenerateCustomResourceDefinition(manifest.ServiceImport, nil)
272+
if err != nil {
273+
return err
274+
}
275+
_, err = o.K8sExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), serviceImport, metav1.CreateOptions{})
276+
if err != nil {
277+
if !apierrors.IsAlreadyExists(err) {
278+
return fmt.Errorf("kosmosctl join run error, crd options failed: %v", err)
279+
}
280+
}
281+
klog.Info("Create CRD " + serviceImport.Name + " successful.")
282+
283+
if len(o.LeafModel) > 0 {
284+
switch o.LeafModel {
285+
case "one-to-one":
286+
// ToDo Perform follow-up query based on the leaf cluster label
287+
nodes, err := o.K8sClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{
288+
LabelSelector: utils.KosmosNodeJoinLabel + "=" + utils.KosmosNodeJoinValue,
289+
})
290+
if err != nil {
291+
return fmt.Errorf("kosmosctl join run error, list cluster node failed: %v", err)
292+
}
293+
var leafModels []v1alpha1.LeafModel
294+
for _, n := range nodes.Items {
295+
leafModel := v1alpha1.LeafModel{
296+
LeafNodeName: n.Name,
297+
Taints: []corev1.Taint{
298+
{
299+
Effect: utils.KosmosNodeTaintEffect,
300+
Key: utils.KosmosNodeTaintKey,
301+
Value: utils.KosmosNodeValue,
302+
},
303+
},
304+
NodeSelector: v1alpha1.NodeSelector{
305+
NodeName: n.Name,
306+
},
307+
}
308+
leafModels = append(leafModels, leafModel)
309+
}
310+
cluster.Spec.ClusterTreeOptions.LeafModels = leafModels
311+
}
312+
}
313+
}
253314

254315
if o.RootFlag {
255316
cluster.Annotations = map[string]string{

pkg/kosmosctl/manifest/manifest_crds.go

+61-51
Original file line numberDiff line numberDiff line change
@@ -491,60 +491,11 @@ spec:
491491
enable:
492492
default: true
493493
type: boolean
494-
leafModel:
495-
description: LeafModel provide an api to arrange the member cluster
494+
leafModels:
495+
description: LeafModels provide an api to arrange the member cluster
496496
with some rules to pretend one or more leaf node
497497
items:
498498
properties:
499-
labelSelector:
500-
description: LabelSelector is a filter to select member
501-
cluster nodes to pretend a leaf node in clusterTree by
502-
labels. If nil or empty, the hole member cluster nodes
503-
will pretend one leaf node.
504-
properties:
505-
matchExpressions:
506-
description: matchExpressions is a list of label selector
507-
requirements. The requirements are ANDed.
508-
items:
509-
description: A label selector requirement is a selector
510-
that contains values, a key, and an operator that
511-
relates the key and values.
512-
properties:
513-
key:
514-
description: key is the label key that the selector
515-
applies to.
516-
type: string
517-
operator:
518-
description: operator represents a key's relationship
519-
to a set of values. Valid operators are In,
520-
NotIn, Exists and DoesNotExist.
521-
type: string
522-
values:
523-
description: values is an array of string values.
524-
If the operator is In or NotIn, the values array
525-
must be non-empty. If the operator is Exists
526-
or DoesNotExist, the values array must be empty.
527-
This array is replaced during a strategic merge
528-
patch.
529-
items:
530-
type: string
531-
type: array
532-
required:
533-
- key
534-
- operator
535-
type: object
536-
type: array
537-
matchLabels:
538-
additionalProperties:
539-
type: string
540-
description: matchLabels is a map of {key,value} pairs.
541-
A single {key,value} in the matchLabels map is equivalent
542-
to an element of matchExpressions, whose key field
543-
is "key", the operator is "In", and the values array
544-
contains only "value". The requirements are ANDed.
545-
type: object
546-
type: object
547-
x-kubernetes-map-type: atomic
548499
labels:
549500
additionalProperties:
550501
type: string
@@ -556,6 +507,65 @@ spec:
556507
the leaf node name will generate by controller and fill
557508
in cluster link status
558509
type: string
510+
nodeSelector:
511+
description: NodeSelector is a selector to select member
512+
cluster nodes to pretend a leaf node in clusterTree.
513+
properties:
514+
labelSelector:
515+
description: LabelSelector is a filter to select member
516+
cluster nodes to pretend a leaf node in clusterTree
517+
by labels. It will work on second level schedule on
518+
pod create in member clusters.
519+
properties:
520+
matchExpressions:
521+
description: matchExpressions is a list of label
522+
selector requirements. The requirements are ANDed.
523+
items:
524+
description: A label selector requirement is a
525+
selector that contains values, a key, and an
526+
operator that relates the key and values.
527+
properties:
528+
key:
529+
description: key is the label key that the
530+
selector applies to.
531+
type: string
532+
operator:
533+
description: operator represents a key's relationship
534+
to a set of values. Valid operators are
535+
In, NotIn, Exists and DoesNotExist.
536+
type: string
537+
values:
538+
description: values is an array of string
539+
values. If the operator is In or NotIn,
540+
the values array must be non-empty. If the
541+
operator is Exists or DoesNotExist, the
542+
values array must be empty. This array is
543+
replaced during a strategic merge patch.
544+
items:
545+
type: string
546+
type: array
547+
required:
548+
- key
549+
- operator
550+
type: object
551+
type: array
552+
matchLabels:
553+
additionalProperties:
554+
type: string
555+
description: matchLabels is a map of {key,value}
556+
pairs. A single {key,value} in the matchLabels
557+
map is equivalent to an element of matchExpressions,
558+
whose key field is "key", the operator is "In",
559+
and the values array contains only "value". The
560+
requirements are ANDed.
561+
type: object
562+
type: object
563+
x-kubernetes-map-type: atomic
564+
nodeName:
565+
description: NodeName is Member cluster origin node
566+
Name
567+
type: string
568+
type: object
559569
taints:
560570
description: Taints attached to the leaf pretended Node.
561571
If nil or empty, controller will set the default no-schedule

pkg/kosmosctl/uninstall/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (o *CommandUninstallOptions) runClustertree() error {
319319
}
320320
} else {
321321
klog.Info("Deployment " + clustertreeDeploy.Name + " is deleted.")
322-
clustertreeSecret, err := util.GenerateService(manifest.ClusterTreeClusterManagerSecret, manifest.SecretReplace{
322+
clustertreeSecret, err := util.GenerateSecret(manifest.ClusterTreeClusterManagerSecret, manifest.SecretReplace{
323323
Namespace: o.Namespace,
324324
Cert: "",
325325
Key: "",

0 commit comments

Comments
 (0)