Skip to content

Commit

Permalink
Merge pull request kosmos-io#151 from GreatLazyMan/installmcscrd
Browse files Browse the repository at this point in the history
title: create mcs releated crd
  • Loading branch information
kosmos-robot authored Oct 27, 2023
2 parents ad7c407 + 7c0938e commit 106a93d
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 3 deletions.
36 changes: 35 additions & 1 deletion hack/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ func main() {
fmt.Println("can not read file:", err)
return
}
serviceImportCRD, err := os.ReadFile(fmt.Sprintf("%s/deploy/crds/mcs/multicluster.x-k8s.io_serviceimports.yaml", projectpath.Root))
if err != nil {
fmt.Println("can not read file:", err)
return
}
serviceExportCRD, err := os.ReadFile(fmt.Sprintf("%s/deploy/crds/mcs/multicluster.x-k8s.io_serviceexports.yaml", projectpath.Root))
if err != nil {
fmt.Println("can not read file:", err)
return
}

filename := fmt.Sprintf("%s/pkg/clusterlinkctl/initmaster/ctlmaster/manifests_crd.go", projectpath.Root)
filename := fmt.Sprintf("%s/pkg/kosmosctl/manifest/manifest_crds.go", projectpath.Root)
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
Expand Down Expand Up @@ -73,6 +83,30 @@ func main() {
return true
})

ast.Inspect(node, func(n ast.Node) bool {
if ident, ok := n.(*ast.Ident); ok && ident.Obj != nil && ident.Obj.Kind == ast.Con && ident.Obj.Name == "ServiceImport" {
valueSpec := ident.Obj.Decl.(*ast.ValueSpec)
valueSpec.Values[0] = &ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf("`%s`", serviceImportCRD),
}
return false
}
return true
})

ast.Inspect(node, func(n ast.Node) bool {
if ident, ok := n.(*ast.Ident); ok && ident.Obj != nil && ident.Obj.Kind == ast.Con && ident.Obj.Name == "ServiceExport" {
valueSpec := ident.Obj.Decl.(*ast.ValueSpec)
valueSpec.Values[0] = &ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf("`%s`", serviceExportCRD),
}
return false
}
return true
})

var buf bytes.Buffer
err = format.Node(&buf, fset, node)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions pkg/kosmosctl/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,30 @@ func (o *CommandInstallOptions) runClustertree() error {
}
klog.Info("Create CRD " + clustertreeKnode.Name + " successful.")

serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil)
if err != nil {
return err
}
_, err = o.ExtensionsClient.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.ExtensionsClient.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
38 changes: 36 additions & 2 deletions pkg/kosmosctl/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -56,8 +57,9 @@ type CommandJoinOptions struct {
UseProxy string
WaitTime int

Client kubernetes.Interface
DynamicClient *dynamic.DynamicClient
Client kubernetes.Interface
DynamicClient *dynamic.DynamicClient
ExtensionsClient extensionsclient.Interface
}

// NewCmdJoin join resource to Kosmos control plane.
Expand Down Expand Up @@ -133,6 +135,12 @@ func (o *CommandJoinOptions) Complete(f ctlutil.Factory) error {
if err != nil {
return fmt.Errorf("kosmosctl join complete error, generate basic client failed: %v", err)
}
o.ExtensionsClient, err = extensionsclient.NewForConfig(clusterConfig)
if err != nil {
return fmt.Errorf("kosmosctl join complete error, generate extensions client failed: %v", err)
}
} else {
return fmt.Errorf("kosmosctl join complete error, arg ClusterKubeConfig is required")
}

return nil
Expand Down Expand Up @@ -316,6 +324,32 @@ func (o *CommandJoinOptions) runKnode() error {
return fmt.Errorf("kosmosctl join run error, create knode failed: %s", err)
}
klog.Info("Knode: " + obj.GetName() + " has been created.")

klog.Info("Attempting to create kosmos mcs CRDs...")
serviceExport, err := util.GenerateCustomResourceDefinition(manifest.ServiceExport, nil)
if err != nil {
return err
}
_, err = o.ExtensionsClient.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.ExtensionsClient.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.")

klog.Info("Knode [" + obj.GetName() + "] registration successful.")

return nil
Expand Down
Loading

0 comments on commit 106a93d

Please sign in to comment.