@@ -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
498560func (o * initOptions ) createCluster () error {
499561 c , err := cmdcluster .NewSubCmdsOptions (& cmdcluster .NewCreateOptions (util .NewFactory (), genericiooptions .NewTestIOStreamsDiscard ()).CreateOptions , cluster .ClusterType (o .clusterType ))
0 commit comments