Skip to content

Commit

Permalink
feat: support to install or upgrade crds for 0.8 version (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: wangyelei <[email protected]>
  • Loading branch information
wangyelei and wangyelei authored Dec 11, 2023
1 parent 7f2fbab commit dc2fcd5
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 0 deletions.
Binary file modified pkg/cluster/charts/apecloud-mysql-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/clickhouse-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/kafka-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/llm-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/milvus-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/mongodb-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/neon-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/postgresql-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/qdrant-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/redis-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/weaviate-cluster.tgz
Binary file not shown.
Binary file modified pkg/cluster/charts/xinference-cluster.tgz
Binary file not shown.
4 changes: 4 additions & 0 deletions pkg/cmd/kubeblocks/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ func (o *InstallOptions) CompleteInstallOptions() error {

func (o *InstallOptions) Install() error {
var err error
// create or update crds
if err = createOrUpdateCRDS(o.Dynamic, o.Version); err != nil {
return fmt.Errorf("install crds failed: %s", err.Error())
}
// add helm repo
s := spinner.New(o.Out, spinnerMsg("Add and update repo "+types.KubeBlocksRepoName))
defer s.Fail()
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/kubeblocks/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func (o *InstallOptions) Upgrade() error {

msg = "to " + o.Version
}
// create or update crds
if err = createOrUpdateCRDS(o.Dynamic, o.Version); err != nil {
return fmt.Errorf("upgrade crds failed: %s", err.Error())
}
s = spinner.New(o.Out, spinnerMsg("Upgrading KubeBlocks "+msg))
defer s.Fail()
// upgrade KubeBlocks chart
Expand Down
49 changes: 49 additions & 0 deletions pkg/cmd/kubeblocks/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"sort"
"strings"

Expand All @@ -34,6 +35,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"

extensionsv1alpha1 "github.com/apecloud/kubeblocks/apis/extensions/v1alpha1"
Expand All @@ -44,6 +47,7 @@ import (
"github.com/apecloud/kbcli/pkg/util"
"github.com/apecloud/kbcli/pkg/util/helm"
"github.com/apecloud/kbcli/pkg/util/prompt"
"github.com/apecloud/kbcli/version"
)

func getGVRByCRD(crd *unstructured.Unstructured) (*schema.GroupVersionResource, error) {
Expand Down Expand Up @@ -277,3 +281,48 @@ func newHelmRepoEntry() *repo.Entry {
URL: util.GetHelmChartRepoURL(),
}
}

// createOrUpdateCRDS creates or updates the kubeBlocks crds.
func createOrUpdateCRDS(dynamic dynamic.Interface, kbVersion string) error {
if kbVersion == "" {
kbVersion = version.GetVersion()
}
crdsUrl := util.GetKubeBlocksCRDsUrl(kbVersion)
resp, err := http.Get(crdsUrl)
if err != nil {
return err
}
if resp.StatusCode == http.StatusNotFound {
return nil
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("can not download %s", crdsUrl)
}
defer resp.Body.Close()
d := yaml.NewYAMLToJSONDecoder(resp.Body)
var objs []unstructured.Unstructured
for {
var obj unstructured.Unstructured
if err = d.Decode(&obj); err != nil {
if err == io.EOF {
break
}
return err
}
objs = append(objs, obj)
}
ctx := context.Background()
for _, obj := range objs {
if _, err = dynamic.Resource(types.CustomResourceDefinitionGVR()).Get(ctx, obj.GetName(), metav1.GetOptions{}); err != nil {
// update crd
if _, err = dynamic.Resource(types.CustomResourceDefinitionGVR()).Create(ctx, &obj, metav1.CreateOptions{}); err != nil {
return err
}
} else {
// create crd
if _, err = dynamic.Resource(types.CustomResourceDefinitionGVR()).Update(ctx, &obj, metav1.UpdateOptions{}); err != nil {
return err
}
}
}
return nil
}
12 changes: 12 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,18 @@ func GetKubeBlocksNamespace(client kubernetes.Interface) (string, error) {
return "", errors.New("failed to get KubeBlocks installation namespace")
}

// GetKubeBlocksCRDsUrl gets the crds url by IP region.
func GetKubeBlocksCRDsUrl(kbVersion string) string {
kbVersion = TrimVersionPrefix(kbVersion)
location, _ := GetIPLocation()
crdsUrl := fmt.Sprintf("https://github.com/apecloud/kubeblocks/releases/download/v%s/kubeblocks_crds.yaml", kbVersion)
if location == "CN" || location == "" {
crdsUrl = fmt.Sprintf("https://jihulab.com/api/v4/projects/85948/packages/generic/kubeblocks/v%s/kubeblocks_crds.yaml", kbVersion)
}
klog.V(1).Infof("CRDs url: %s", crdsUrl)
return crdsUrl
}

// GetKubeBlocksNamespaceByDynamic gets namespace of KubeBlocks installation, infer namespace from helm secrets
func GetKubeBlocksNamespaceByDynamic(dynamic dynamic.Interface) (string, error) {
list, err := dynamic.Resource(types.SecretGVR()).List(context.TODO(), metav1.ListOptions{LabelSelector: types.KubeBlocksHelmLabel})
Expand Down

0 comments on commit dc2fcd5

Please sign in to comment.