Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Multi-cluster service and log query #262

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJ
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d h1:105gxyaGwCFad8crR9dcMQWvV9Hvulu6hwUh4tWPJnM=
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZMPRZwes7CROmyNKgQzC3XPs6L/G2EJLHddWejkmf4=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
Expand Down
101 changes: 89 additions & 12 deletions pkg/kosmosctl/get/get.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package get

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
ctlget "k8s.io/kubectl/pkg/cmd/get"
ctlutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/utils/pointer"

"github.com/kosmos.io/kosmos/pkg/generated/clientset/versioned"
"github.com/kosmos.io/kosmos/pkg/kosmosctl/manifest"
"github.com/kosmos.io/kosmos/pkg/kosmosctl/util"
"github.com/kosmos.io/kosmos/pkg/utils"
)

const (
ClustersGroupResource = "clusters.kosmos.io"
ClusterNodesGroupResource = "clusternodes.kosmos.io"
KnodesGroupResource = "knodes.kosmos.io"
NodeConfigsGroupResource = "nodeconfigs.kosmos.io"
)

type CommandGetOptions struct {
Expand All @@ -28,6 +37,8 @@ type CommandGetOptions struct {
GetOptions *ctlget.GetOptions
}

var newF ctlutil.Factory

// NewCmdGet Display resources from the Kosmos control plane.
func NewCmdGet(f ctlutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := NewCommandGetOptions(streams)
Expand All @@ -47,9 +58,10 @@ func NewCmdGet(f ctlutil.Factory, streams genericclioptions.IOStreams) *cobra.Co
},
}

o.GetOptions.PrintFlags.AddFlags(cmd)
flags := cmd.Flags()
flags.StringVarP(&o.Namespace, "namespace", "n", utils.DefaultNamespace, "If present, the namespace scope for this CLI request.")
flags.StringVar(&o.Cluster, "cluster", utils.DefaultClusterName, "Specify a cluster, the default is the control cluster.")
o.GetOptions.PrintFlags.AddFlags(cmd)

return cmd
}
Expand All @@ -63,13 +75,71 @@ func NewCommandGetOptions(streams genericclioptions.IOStreams) *CommandGetOption
}

func (o *CommandGetOptions) Complete(f ctlutil.Factory, cmd *cobra.Command, args []string) error {
err := o.GetOptions.Complete(f, cmd, args)
if err != nil {
return fmt.Errorf("kosmosctl get complete error, options failed: %s", err)
if o.Cluster != utils.DefaultClusterName {
controlConfig, err := f.ToRESTConfig()
if err != nil {
return err
}

rootClient, err := versioned.NewForConfig(controlConfig)
if err != nil {
return err
}
cluster, err := rootClient.KosmosV1alpha1().Clusters().Get(context.TODO(), o.Cluster, metav1.GetOptions{})
if err != nil {
return err
}

leafConfig, err := clientcmd.RESTConfigFromKubeConfig(cluster.Spec.Kubeconfig)
if err != nil {
return fmt.Errorf("kosmosctl get complete error, load leaf cluster kubeconfig failed: %s", err)
}

leafClient, err := kubernetes.NewForConfig(leafConfig)
if err != nil {
return fmt.Errorf("kosmosctl get complete error, generate leaf cluster client failed: %s", err)
}

kosmosControlSA, err := util.GenerateServiceAccount(manifest.KosmosControlServiceAccount, manifest.ServiceAccountReplace{
Namespace: o.Namespace,
})
if err != nil {
return fmt.Errorf("kosmosctl get complete error, generate kosmos serviceaccount failed: %s", err)
}
expirationSeconds := int64(600)
leafToken, err := leafClient.CoreV1().ServiceAccounts(kosmosControlSA.Namespace).CreateToken(
context.TODO(), kosmosControlSA.Name, &authenticationv1.TokenRequest{
Spec: authenticationv1.TokenRequestSpec{
ExpirationSeconds: &expirationSeconds,
},
}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("kosmosctl get complete error, list leaf cluster secret failed: %s", err)
}

configFlags := genericclioptions.NewConfigFlags(false)
configFlags.APIServer = &leafConfig.Host
configFlags.BearerToken = &leafToken.Status.Token
configFlags.Insecure = pointer.Bool(true)
configFlags.Namespace = &o.Namespace

newF = ctlutil.NewFactory(configFlags)

err = o.GetOptions.Complete(newF, cmd, args)
if err != nil {
return fmt.Errorf("kosmosctl get complete error, options failed: %s", err)
}

o.GetOptions.Namespace = o.Namespace
} else {
err := o.GetOptions.Complete(f, cmd, args)
if err != nil {
return fmt.Errorf("kosmosctl get complete error, options failed: %s", err)
}

o.GetOptions.Namespace = o.Namespace
}

o.GetOptions.Namespace = o.Namespace

return nil
}

Expand All @@ -88,13 +158,20 @@ func (o *CommandGetOptions) Run(f ctlutil.Factory, cmd *cobra.Command, args []st
args[0] = ClustersGroupResource
case "clusternode", "clusternodes":
args[0] = ClusterNodesGroupResource
case "knode", "knodes":
args[0] = KnodesGroupResource
case "nodeconfig", "nodeconfigs":
args[0] = NodeConfigsGroupResource
}

err := o.GetOptions.Run(f, cmd, args)
if err != nil {
return fmt.Errorf("kosmosctl get run error, options failed: %s", err)
if o.Cluster != utils.DefaultClusterName {
err := o.GetOptions.Run(newF, cmd, args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job! Calling kubectl's function is a good method

if err != nil {
return fmt.Errorf("kosmosctl get run error, options failed: %s", err)
}
} else {
err := o.GetOptions.Run(f, cmd, args)
if err != nil {
return fmt.Errorf("kosmosctl get run error, options failed: %s", err)
}
}

return nil
Expand Down
37 changes: 13 additions & 24 deletions pkg/kosmosctl/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
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/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -32,21 +31,20 @@ import (
)

var installExample = templates.Examples(i18n.T(`
# Install all module to Kosmos control plane, e.g:
kosmosctl install --cni cni-name --default-nic nic-name

# Install all module to Kosmos control plane, e.g:
kosmosctl install --cni cni-name --default-nic nic-name
# Install Kosmos control plane, if you need to specify a special control plane cluster kubeconfig, e.g:
kosmosctl install --kubeconfig ~/kubeconfig/cluster-kubeconfig

# Install clusterlink module to Kosmos control plane, e.g:
kosmosctl install -m clusterlink --cni cni-name --default-nic nic-name

kosmosctl install --kubeconfig ~/kubeconfig/cluster-kubeconfig

# Install clustertree module to Kosmos control plane, e.g:
kosmosctl install -m clustertree

kosmosctl install -m clustertree

# Install clusterlink module to Kosmos control plane and set the necessary parameters, e.g:
kosmosctl install -m clusterlink --cni cni-name --default-nic nic-name

# Install coredns module to Kosmos control plane, e.g:
kosmosctl install -m coredns
`))
kosmosctl install -m coredns`))

type CommandInstallOptions struct {
Namespace string
Expand All @@ -65,7 +63,6 @@ type CommandInstallOptions struct {

KosmosClient versioned.Interface
K8sClient kubernetes.Interface
K8sDynamicClient *dynamic.DynamicClient
K8sExtensionsClient extensionsclient.Interface
}

Expand Down Expand Up @@ -137,11 +134,6 @@ func (o *CommandInstallOptions) Complete(f ctlutil.Factory) error {
return fmt.Errorf("kosmosctl install complete error, generate K8s basic client failed: %v", err)
}

o.K8sDynamicClient, err = dynamic.NewForConfig(config)
if err != nil {
return fmt.Errorf("kosmosctl install complete error, generate K8s dynamic client failed: %s", err)
}

o.K8sExtensionsClient, err = extensionsclient.NewForConfig(config)
if err != nil {
return fmt.Errorf("kosmosctl install complete error, generate K8s extensions client failed: %v", err)
Expand Down Expand Up @@ -312,7 +304,7 @@ func (o *CommandInstallOptions) runClusterlink() error {
}

operatorDeploy, err := util.GenerateDeployment(manifest.KosmosOperatorDeployment, manifest.DeploymentReplace{
Namespace: utils.DefaultNamespace,
Namespace: o.Namespace,
Version: version.GetReleaseVersion().PatchRelease(),
UseProxy: o.UseProxy,
ImageRepository: o.ImageRegistry,
Expand Down Expand Up @@ -470,7 +462,7 @@ func (o *CommandInstallOptions) runClustertree() error {
}

operatorDeploy, err := util.GenerateDeployment(manifest.KosmosOperatorDeployment, manifest.DeploymentReplace{
Namespace: utils.DefaultNamespace,
Namespace: o.Namespace,
Version: version.GetReleaseVersion().PatchRelease(),
UseProxy: o.UseProxy,
ImageRepository: o.ImageRegistry,
Expand Down Expand Up @@ -580,7 +572,6 @@ func (o *CommandInstallOptions) createControlCluster() error {
WaitTime: o.WaitTime,
KosmosClient: o.KosmosClient,
K8sClient: o.K8sClient,
K8sDynamicClient: o.K8sDynamicClient,
RootFlag: true,
EnableLink: true,
CNI: o.CNI,
Expand Down Expand Up @@ -637,7 +628,6 @@ func (o *CommandInstallOptions) createControlCluster() error {
WaitTime: o.WaitTime,
KosmosClient: o.KosmosClient,
K8sClient: o.K8sClient,
K8sDynamicClient: o.K8sDynamicClient,
RootFlag: true,
EnableTree: true,
}
Expand Down Expand Up @@ -674,7 +664,6 @@ func (o *CommandInstallOptions) createControlCluster() error {
WaitTime: o.WaitTime,
KosmosClient: o.KosmosClient,
K8sClient: o.K8sClient,
K8sDynamicClient: o.K8sDynamicClient,
RootFlag: true,
EnableLink: true,
CNI: o.CNI,
Expand Down
Loading
Loading