Skip to content

Commit 11f2e13

Browse files
committed
fix: init playgroud bug (#604)
Co-authored-by: wangyelei <wangyelei@users.noreply.github.com> (cherry picked from commit f11c22d)
1 parent 95cf175 commit 11f2e13

9 files changed

Lines changed: 188 additions & 7 deletions

File tree

docs/user_docs/cli/kbcli_cluster_describe-backup.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ kbcli cluster describe-backup BACKUP-NAME [flags]
1111
### Examples
1212

1313
```
14+
# describe backups of the cluster
15+
kbcli cluster describe-backup <clusterName>
16+
1417
# describe a backup
15-
kbcli cluster describe-backup backup-default-mycluster-20230616190023
18+
kbcli cluster describe-backup --names <backupName>
1619
```
1720

1821
### Options

docs/user_docs/cli/kbcli_playground_init.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ kbcli playground init [flags]
3131
kbcli cluster describe mycluster
3232
3333
# connect to database
34-
kbcli cluster connect mycluster
34+
kbcli exec -it mycluster-mysql-0 bash
35+
mysql -h 127.1 -u root -p$MYSQL_ROOT_PASSWORD
3536
3637
# view the Grafana
3738
kbcli dashboard open kubeblocks-grafana
@@ -45,7 +46,7 @@ kbcli playground init [flags]
4546
```
4647
--auto-approve Skip interactive approval during the initialization of playground
4748
--cloud-provider string Cloud provider type, one of [local aws] (default "local")
48-
--cluster-type string Specify the cluster type to create, use 'kbcli cluster create --help' to get the available cluster type. (default "apecloud-mysql")
49+
--cluster-type string Specify the cluster type to create, use 'kbcli cluster create --help' to get the available cluster type. (default "mysql")
4950
-h, --help help for init
5051
--k3d-proxy-image string Specify k3d proxy image if you want to init playground locally (default "docker.io/apecloud/k3d-proxy:5.4.4")
5152
--k3s-image string Specify k3s image that you want to use for the nodes if you want to init playground locally (default "rancher/k3s:v1.23.8-k3s1")

pkg/cloudprovider/k3d.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func buildClusterConfig(clusterName string, opts k3d.ClusterCreateOpts, k3sImage
282282
Role: k3d.ServerRole,
283283
Image: k3sImage,
284284
ServerOpts: k3d.ServerOpts{},
285-
Args: []string{"--disable=metrics-server", "--disable=traefik", "--disable=local-storage"},
285+
Args: []string{"--disable=metrics-server", "--disable=traefik"},
286286
}
287287

288288
nodes = append(nodes, &serverNode)

pkg/cmd/cluster/create_subcmds.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ func (o *CreateSubCmdsOptions) Complete(cmd *cobra.Command) error {
160160
}
161161
}
162162

163+
if o.Tenancy == "" {
164+
o.Tenancy = "SharedNode"
165+
}
166+
167+
if o.PodAntiAffinity == "" {
168+
o.PodAntiAffinity = "Preferred"
169+
}
170+
163171
// get values from flags
164172
if cmd != nil {
165173
o.Values = getValuesFromFlags(cmd.LocalNonPersistentFlags())

pkg/cmd/cluster/dataprotection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ var (
9393
kbcli cluster list-restores --names r1,r2
9494
`)
9595
describeBackupExample = templates.Examples(`
96+
# describe backups of the cluster
97+
kbcli cluster describe-backup <clusterName>
98+
9699
# describe a backup
97-
kbcli cluster describe-backup backup-default-mycluster-20230616190023
100+
kbcli cluster describe-backup --names <backupName>
98101
`)
99102
describeBackupPolicyExample = templates.Examples(`
100103
# describe the default backup policy of the cluster

pkg/cmd/playground/init.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@ import (
2323
"fmt"
2424
"os"
2525
"path/filepath"
26+
"runtime"
2627
"strings"
2728
"time"
2829

2930
gv "github.com/hashicorp/go-version"
3031
"github.com/pkg/errors"
3132
"github.com/spf13/cobra"
3233
"golang.org/x/exp/slices"
34+
"golang.org/x/net/context"
3335
apierrors "k8s.io/apimachinery/pkg/api/errors"
36+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3438
"k8s.io/apimachinery/pkg/util/rand"
3539
"k8s.io/cli-runtime/pkg/genericiooptions"
3640
"k8s.io/klog/v2"
3741
cmdutil "k8s.io/kubectl/pkg/cmd/util"
3842
"k8s.io/kubectl/pkg/util/templates"
43+
"sigs.k8s.io/yaml"
3944

4045
cp "github.com/apecloud/kbcli/pkg/cloudprovider"
4146
"github.com/apecloud/kbcli/pkg/cluster"
@@ -72,7 +77,8 @@ on the created kubernetes cluster, and an apecloud-mysql cluster named mycluster
7277
kbcli cluster describe mycluster
7378
7479
# connect to database
75-
kbcli cluster connect mycluster
80+
kbcli exec -it mycluster-mysql-0 bash
81+
mysql -h 127.1 -u root -p$MYSQL_ROOT_PASSWORD
7682
7783
# view the Grafana
7884
kbcli dashboard open kubeblocks-grafana
@@ -207,6 +213,18 @@ func (o *initOptions) local() error {
207213
}
208214
}
209215

216+
if clusterInfo.K3sImage == "" {
217+
if o.prevCluster != nil {
218+
playgrouddir, err := initPlaygroundDir()
219+
if err != nil {
220+
return err
221+
}
222+
return fmt.Errorf(fmt.Sprintf("k3s image not specified, you can run `rm -rf %s ` and retry", playgrouddir))
223+
}
224+
clusterInfo.K3sImage = cp.K3sImageDefault
225+
clusterInfo.K3dProxyImage = cp.K3dProxyImageDefault
226+
}
227+
210228
if err = writeClusterInfo(o.stateFilePath, clusterInfo); err != nil {
211229
return errors.Wrapf(err, "failed to write kubernetes cluster info to state file %s:\n %v", o.stateFilePath, clusterInfo)
212230
}
@@ -410,6 +428,10 @@ func (o *initOptions) installKBAndCluster(info *cp.K8sClusterInfo) error {
410428
return errors.Wrap(err, "failed to install KubeBlocks")
411429
}
412430
klog.V(1).Info("KubeBlocks installed successfully")
431+
if err = o.createSnapshotController(); err != nil {
432+
return errors.Wrap(err, "failed to install snapshot controller")
433+
}
434+
klog.V(1).Info("create snapshot controller addon successfully")
413435
// install database cluster
414436
clusterInfo := "ClusterType: " + o.clusterType
415437
s := spinner.New(o.Out, spinnerMsg("Create cluster %s (%s)", kbClusterName, clusterInfo))
@@ -494,6 +516,46 @@ func (o *initOptions) installKubeBlocks(k8sClusterName string) error {
494516
return insOpts.Install()
495517
}
496518

519+
func (o *initOptions) createSnapshotController() error {
520+
if o.cloudProvider != cp.Local {
521+
return nil
522+
}
523+
cli, err := util.NewFactory().DynamicClient()
524+
if err != nil {
525+
return err
526+
}
527+
_, currentFile, _, _ := runtime.Caller(1)
528+
baseDir := filepath.Dir(currentFile)
529+
getUnstructured := func(fileName string) (*unstructured.Unstructured, error) {
530+
cmBytes, err := os.ReadFile(fileName)
531+
if err != nil {
532+
return nil, err
533+
}
534+
cm := &unstructured.Unstructured{}
535+
if err := yaml.Unmarshal(cmBytes, cm); err != nil {
536+
return nil, err
537+
}
538+
return cm, err
539+
}
540+
snapshotControllerCM, err := getUnstructured(baseDir + "/snapshot-controller/snapshot-controller-cm.yaml")
541+
if err != nil {
542+
return err
543+
}
544+
snapshotControllerCM.SetNamespace(defaultNamespace)
545+
if _, err = cli.Resource(types.ConfigmapGVR()).Namespace(defaultNamespace).Create(context.TODO(), snapshotControllerCM, metav1.CreateOptions{}); err != nil {
546+
return err
547+
}
548+
549+
snapshotControllerAddon, err := getUnstructured(baseDir + "/snapshot-controller/snapshot-controller-addon.yaml")
550+
if err != nil {
551+
return err
552+
}
553+
if _, err = cli.Resource(types.AddonGVR()).Namespace("").Create(context.TODO(), snapshotControllerAddon, metav1.CreateOptions{}); err != nil {
554+
return err
555+
}
556+
return nil
557+
}
558+
497559
// createCluster constructs a cluster create options and run
498560
func (o *initOptions) createCluster() error {
499561
c, err := cmdcluster.NewSubCmdsOptions(&cmdcluster.NewCreateOptions(util.NewFactory(), genericiooptions.NewTestIOStreamsDiscard()).CreateOptions, cluster.ClusterType(o.clusterType))
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
apiVersion: extensions.kubeblocks.io/v1alpha1
2+
kind: Addon
3+
metadata:
4+
name: snapshot-controller
5+
spec:
6+
defaultInstallValues:
7+
- enabled: true
8+
resources: {}
9+
tolerations: '[{"effect":"NoSchedule","key":"kb-controller","operator":"Equal","value":"true"}]'
10+
- resources: {}
11+
selectors:
12+
- key: KubeGitVersion
13+
operator: Contains
14+
values:
15+
- eks
16+
storageClass: ebs.csi.aws.com
17+
tolerations: '[{"effect":"NoSchedule","key":"kb-controller","operator":"Equal","value":"true"}]'
18+
- resources: {}
19+
selectors:
20+
- key: KubeGitVersion
21+
operator: Contains
22+
values:
23+
- gke
24+
storageClass: pd.csi.storage.gke.io
25+
tolerations: '[{"effect":"NoSchedule","key":"kb-controller","operator":"Equal","value":"true"}]'
26+
- resources: {}
27+
selectors:
28+
- key: KubeGitVersion
29+
operator: Contains
30+
values:
31+
- aks
32+
storageClass: disk.csi.azure.com
33+
tolerations: '[{"effect":"NoSchedule","key":"kb-controller","operator":"Equal","value":"true"}]'
34+
description: 'Deploys a Snapshot Controller in a cluster. Snapshot Controllers are
35+
often bundled with the Kubernetes distribution, this chart is meant for cases
36+
where it is not. '
37+
helm:
38+
chartLocationURL: file:///snapshot-controller-1.7.2.tgz
39+
chartsImage: apecloud-registry.cn-zhangjiakou.cr.aliyuncs.com/apecloud/kubeblocks-charts:0.9.3
40+
chartsPathInImage: /charts
41+
installValues:
42+
configMapRefs:
43+
- key: values-kubeblocks-override.yaml
44+
name: snapshot-controller-chart-kubeblocks-values
45+
valuesMapping:
46+
jsonMap:
47+
tolerations: tolerations
48+
resources:
49+
cpu:
50+
limits: resources.limits.cpu
51+
requests: resources.requests.cpu
52+
memory:
53+
limits: resources.limits.memory
54+
requests: resources.requests.memory
55+
valueMap:
56+
replicaCount: replicaCount
57+
storageClass: volumeSnapshotClasses[0].driver
58+
installable:
59+
autoInstall: true
60+
selectors:
61+
- key: KubeGitVersion
62+
operator: DoesNotContain
63+
values:
64+
- tke
65+
- aliyun.
66+
- key: KubeProvider
67+
operator: DoesNotContain
68+
values:
69+
- huaweiCloud
70+
- azure
71+
type: Helm
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
data:
3+
values-kubeblocks-override.yaml: |-
4+
affinity:
5+
nodeAffinity:
6+
preferredDuringSchedulingIgnoredDuringExecution:
7+
- preference:
8+
matchExpressions:
9+
- key: kb-controller
10+
operator: In
11+
values:
12+
- "true"
13+
weight: 100
14+
enabled: true
15+
image:
16+
repository: apecloud-registry.cn-zhangjiakou.cr.aliyuncs.com/apecloud/snapshot-controller
17+
tag: v6.2.1
18+
replicaCount: 1
19+
tolerations:
20+
- effect: NoSchedule
21+
key: kb-controller
22+
operator: Equal
23+
value: "true"
24+
volumeSnapshotClasses:
25+
- deletionPolicy: Delete
26+
driver: hostpath.csi.k8s.io
27+
name: default-vsc
28+
kind: ConfigMap
29+
metadata:
30+
labels:
31+
app.kubernetes.io/instance: kubeblocks
32+
app.kubernetes.io/name: kubeblocks
33+
name: snapshot-controller-chart-kubeblocks-values

pkg/cmd/playground/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const (
3232

3333
const (
3434
defaultCloudProvider = cloudprovider.Local
35-
defaultClusterType = "apecloud-mysql"
35+
defaultClusterType = "mysql"
3636

3737
// defaultNamespace is the namespace of playground cluster
3838
defaultNamespace = "default"

0 commit comments

Comments
 (0)