Skip to content

Commit

Permalink
chore: adapt cluster connect command with 1.0 api (#437)
Browse files Browse the repository at this point in the history
Co-authored-by: wangyelei <[email protected]>
  • Loading branch information
wangyelei and wangyelei authored Sep 3, 2024
1 parent 4874b3a commit 55f8f45
Show file tree
Hide file tree
Showing 12 changed files with 771 additions and 836 deletions.
25 changes: 5 additions & 20 deletions docs/user_docs/cli/kbcli_cluster_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,26 @@ kbcli cluster connect (NAME | -i INSTANCE-NAME) [flags]
### Examples

```
# connect to a specified cluster, default connect to the leader/primary instance
# connect to a specified cluster
kbcli cluster connect mycluster
# connect to cluster as user
kbcli cluster connect mycluster --as-user myuser
# connect to a specified instance
kbcli cluster connect -i mycluster-instance-0
# connect to a specified component
kbcli cluster connect mycluster --component mycomponent
# show cli connection example with password mask
kbcli cluster connect mycluster --show-example --client=cli
# show java connection example with password mask
kbcli cluster connect mycluster --show-example --client=java
# show all connection examples with password mask
kbcli cluster connect mycluster --show-example
# show cli connection examples with real password
kbcli cluster connect mycluster --show-example --client=cli --show-password
# show cli connection example, supported client: [cli, java, python, rust, php, node.js, go, .net, django] and more.
kbcli cluster connect mycluster --client=cli
```

### Options

```
--as-user string Connect to cluster as user
--client string Which client connection example should be output, only valid if --show-example is true.
--component string The component to connect. If not specified, pick up the first one.
--client string Which client connection example should be output.
--component string The component to connect. If not specified and no any cluster scope services, pick up the first one.
-h, --help help for connect
-i, --instance string The instance name to connect.
--show-example Show how to connect to cluster/instance from different clients.
--show-password Show password in example.
```

### Options inherited from parent commands
Expand Down
199 changes: 101 additions & 98 deletions go.mod

Large diffs are not rendered by default.

465 changes: 242 additions & 223 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkg/cluster/cluster_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ package cluster
import (
"compress/gzip"
"fmt"
"slices"
"strings"

"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
Expand Down Expand Up @@ -219,8 +219,8 @@ func loadHelmChart(ci *ChartInfo, t ClusterType) error {

func SupportedTypes() []ClusterType {
types := maps.Keys(ClusterTypeCharts)
slices.SortFunc(types, func(i, j ClusterType) bool {
return i.String() < j.String()
slices.SortFunc(types, func(i, j ClusterType) int {
return strings.Compare(i.String(), j.String())
})
return types
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/cluster/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"

"github.com/apecloud/kbcli/pkg/testing"
Expand Down Expand Up @@ -377,6 +378,9 @@ func GetPodComponentName(pod *corev1.Pod) string {
if pod.Labels == nil {
return ""
}
if compName, ok := pod.Labels[constant.KBAppShardingNameLabelKey]; ok {
return compName
}
return pod.Labels[constant.KBAppComponentLabelKey]
}

Expand All @@ -396,6 +400,24 @@ func GetConfigConstraintByName(dynamic dynamic.Interface, name string) (*appsv1b
return ccObj, nil
}

func ListShardingComponents(dynamic dynamic.Interface, clusterName, clusterNamespace, componentName string) ([]*appsv1alpha1.Component, error) {
unstructuredObjList, err := dynamic.Resource(types.ComponentGVR()).Namespace(clusterNamespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s,%s=%s", constant.AppInstanceLabelKey, clusterName, constant.KBAppShardingNameLabelKey, componentName),
})
if err != nil {
return nil, nil
}
var components []*appsv1alpha1.Component
for i := range unstructuredObjList.Items {
comp := &appsv1alpha1.Component{}
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObjList.Items[i].UnstructuredContent(), comp); err != nil {
return nil, err
}
components = append(components, comp)
}
return components, nil
}

func GetServiceRefs(cd *appsv1alpha1.ClusterDefinition) []string {
var serviceRefs []string
for _, compDef := range cd.Spec.ComponentDefs {
Expand Down Expand Up @@ -441,3 +463,27 @@ func GetDefaultVersionByCompDefs(dynamic dynamic.Interface, compDefs []string) (
}
return cv, nil
}

func GetReadyNodeForNodePort(client *kubernetes.Clientset) (*corev1.Node, error) {
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
Limit: 20,
})
if err != nil {
return nil, err
}
var readyNode *corev1.Node
for _, node := range nodes.Items {
var nodeIsReady bool
for _, con := range node.Status.Conditions {
if con.Type == corev1.NodeReady {
nodeIsReady = con.Status == corev1.ConditionTrue
break
}
}
if nodeIsReady {
readyNode = &node
break
}
}
return readyNode, nil
}
Loading

0 comments on commit 55f8f45

Please sign in to comment.