openpitrix crd
Signed-off-by: LiHui <andrewli@yunify.com> delete helm repo, release and app Signed-off-by: LiHui <andrewli@yunify.com> Fix Dockerfile Signed-off-by: LiHui <andrewli@yunify.com> add unit test for category controller Signed-off-by: LiHui <andrewli@yunify.com> resource api Signed-off-by: LiHui <andrewli@yunify.com> miscellaneous Signed-off-by: LiHui <andrewli@yunify.com> resource api Signed-off-by: LiHui <andrewli@yunify.com> add s3 repo indx Signed-off-by: LiHui <andrewli@yunify.com> attachment api Signed-off-by: LiHui <andrewli@yunify.com> repo controller test Signed-off-by: LiHui <andrewli@yunify.com> application controller test Signed-off-by: LiHui <andrewli@yunify.com> release metric Signed-off-by: LiHui <andrewli@yunify.com> helm release controller test Signed-off-by: LiHui <andrewli@yunify.com> move constants to /pkg/apis/application Signed-off-by: LiHui <andrewli@yunify.com> remove unused code Signed-off-by: LiHui <andrewli@yunify.com> add license header Signed-off-by: LiHui <andrewli@yunify.com> Fix bugs Signed-off-by: LiHui <andrewli@yunify.com> cluster cluent Signed-off-by: LiHui <andrewli@yunify.com> format code Signed-off-by: LiHui <andrewli@yunify.com> move workspace,cluster from spec to labels Signed-off-by: LiHui <andrewli@yunify.com> add license header Signed-off-by: LiHui <andrewli@yunify.com> openpitrix test Signed-off-by: LiHui <andrewli@yunify.com> add worksapce labels for app in appstore Signed-off-by: LiHui <andrewli@yunify.com>
This commit is contained in:
207
pkg/utils/clusterclient/clusterclient.go
Normal file
207
pkg/utils/clusterclient/clusterclient.go
Normal file
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
Copyright 2020 KubeSphere Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package clusterclient
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/klog"
|
||||
clusterv1alpha1 "kubesphere.io/kubesphere/pkg/apis/cluster/v1alpha1"
|
||||
clusterinformer "kubesphere.io/kubesphere/pkg/client/informers/externalversions/cluster/v1alpha1"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
ClusterNotExistsFormat = "cluster %s not exists"
|
||||
)
|
||||
|
||||
type innerCluster struct {
|
||||
KubernetesURL *url.URL
|
||||
KubesphereURL *url.URL
|
||||
Transport http.RoundTripper
|
||||
}
|
||||
|
||||
type clusterClients struct {
|
||||
sync.RWMutex
|
||||
clusterMap map[string]*clusterv1alpha1.Cluster
|
||||
clusterKubeconfig map[string]string
|
||||
|
||||
// build a in memory cluster cache to speed things up
|
||||
innerClusters map[string]*innerCluster
|
||||
}
|
||||
|
||||
type ClusterClients interface {
|
||||
IsHostCluster(cluster *clusterv1alpha1.Cluster) bool
|
||||
IsClusterReady(cluster *clusterv1alpha1.Cluster) bool
|
||||
GetClusterKubeconfig(string) (string, error)
|
||||
Get(string) (*clusterv1alpha1.Cluster, error)
|
||||
GetInnerCluster(string) *innerCluster
|
||||
}
|
||||
|
||||
func (c *clusterClients) IsClusterReady(cluster *clusterv1alpha1.Cluster) bool {
|
||||
for _, condition := range cluster.Status.Conditions {
|
||||
if condition.Type == clusterv1alpha1.ClusterReady && condition.Status == corev1.ConditionTrue {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *clusterClients) IsHostCluster(cluster *clusterv1alpha1.Cluster) bool {
|
||||
if _, ok := cluster.Labels[clusterv1alpha1.HostCluster]; ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *clusterClients) GetInnerCluster(name string) *innerCluster {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
if cluster, ok := c.innerClusters[name]; ok {
|
||||
return cluster
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var c *clusterClients
|
||||
var lock sync.Mutex
|
||||
|
||||
func NewClusterClient(clusterInformer clusterinformer.ClusterInformer) ClusterClients {
|
||||
|
||||
if c == nil {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
if c != nil {
|
||||
return c
|
||||
}
|
||||
|
||||
c = &clusterClients{
|
||||
clusterMap: map[string]*clusterv1alpha1.Cluster{},
|
||||
clusterKubeconfig: map[string]string{},
|
||||
innerClusters: make(map[string]*innerCluster),
|
||||
}
|
||||
|
||||
clusterInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: func(obj interface{}) {
|
||||
c.addCluster(obj)
|
||||
},
|
||||
UpdateFunc: func(oldObj, newObj interface{}) {
|
||||
c.removeCluster(oldObj)
|
||||
c.addCluster(newObj)
|
||||
},
|
||||
DeleteFunc: func(obj interface{}) {
|
||||
c.removeCluster(obj)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *clusterClients) removeCluster(obj interface{}) {
|
||||
cluster := obj.(*clusterv1alpha1.Cluster)
|
||||
klog.V(4).Infof("remove cluster %s", cluster.Name)
|
||||
c.Lock()
|
||||
if _, ok := c.clusterMap[cluster.Name]; ok {
|
||||
delete(c.clusterMap, cluster.Name)
|
||||
delete(c.innerClusters, cluster.Name)
|
||||
delete(c.clusterKubeconfig, cluster.Name)
|
||||
}
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func newInnerCluster(cluster *clusterv1alpha1.Cluster) *innerCluster {
|
||||
kubernetesEndpoint, err := url.Parse(cluster.Spec.Connection.KubernetesAPIEndpoint)
|
||||
if err != nil {
|
||||
klog.Errorf("Parse kubernetes apiserver endpoint %s failed, %v", cluster.Spec.Connection.KubernetesAPIEndpoint, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
kubesphereEndpoint, err := url.Parse(cluster.Spec.Connection.KubeSphereAPIEndpoint)
|
||||
if err != nil {
|
||||
klog.Errorf("Parse kubesphere apiserver endpoint %s failed, %v", cluster.Spec.Connection.KubeSphereAPIEndpoint, err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepare for
|
||||
clientConfig, err := clientcmd.NewClientConfigFromBytes(cluster.Spec.Connection.KubeConfig)
|
||||
if err != nil {
|
||||
klog.Errorf("Unable to create client config from kubeconfig bytes, %#v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
clusterConfig, err := clientConfig.ClientConfig()
|
||||
if err != nil {
|
||||
klog.Errorf("Failed to get client config, %#v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
transport, err := rest.TransportFor(clusterConfig)
|
||||
if err != nil {
|
||||
klog.Errorf("Create transport failed, %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return &innerCluster{
|
||||
KubernetesURL: kubernetesEndpoint,
|
||||
KubesphereURL: kubesphereEndpoint,
|
||||
Transport: transport,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *clusterClients) addCluster(obj interface{}) {
|
||||
cluster := obj.(*clusterv1alpha1.Cluster)
|
||||
klog.V(4).Infof("add new cluster %s", cluster.Name)
|
||||
_, err := url.Parse(cluster.Spec.Connection.KubernetesAPIEndpoint)
|
||||
if err != nil {
|
||||
klog.Errorf("Parse kubernetes apiserver endpoint %s failed, %v", cluster.Spec.Connection.KubernetesAPIEndpoint, err)
|
||||
return
|
||||
}
|
||||
|
||||
innerCluster := newInnerCluster(cluster)
|
||||
c.Lock()
|
||||
c.clusterMap[cluster.Name] = cluster
|
||||
c.clusterKubeconfig[cluster.Name] = string(cluster.Spec.Connection.KubeConfig)
|
||||
c.innerClusters[cluster.Name] = innerCluster
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c *clusterClients) GetClusterKubeconfig(clusterName string) (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
if c, exists := c.clusterKubeconfig[clusterName]; exists {
|
||||
return c, nil
|
||||
} else {
|
||||
return "", fmt.Errorf(ClusterNotExistsFormat, clusterName)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *clusterClients) Get(clusterName string) (*clusterv1alpha1.Cluster, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
if cluster, exists := c.clusterMap[clusterName]; exists {
|
||||
return cluster, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf(ClusterNotExistsFormat, clusterName)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,20 @@
|
||||
// /*
|
||||
// Copyright 2020 The KubeSphere Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// */
|
||||
//
|
||||
|
||||
package metrics
|
||||
|
||||
import (
|
||||
@@ -22,13 +39,12 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
compbasemetrics.BuildVersion = versionGet
|
||||
|
||||
defaultRegistry = compbasemetrics.NewKubeRegistry()
|
||||
MustRegister = defaultRegistry.MustRegister
|
||||
Register = defaultRegistry.Register
|
||||
RawMustRegister = defaultRegistry.RawMustRegister
|
||||
|
||||
RawMustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
|
||||
RawMustRegister(prometheus.NewGoCollector())
|
||||
}
|
||||
|
||||
// DefaultMetrics installs the default prometheus metrics handler
|
||||
@@ -36,6 +52,9 @@ type DefaultMetrics struct{}
|
||||
|
||||
// Install adds the DefaultMetrics handler
|
||||
func (m DefaultMetrics) Install(c *restful.Container) {
|
||||
RawMustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
|
||||
RawMustRegister(prometheus.NewGoCollector())
|
||||
|
||||
c.Handle("/kapis/metrics", Handler())
|
||||
}
|
||||
|
||||
@@ -59,5 +78,5 @@ func versionGet() apimachineryversion.Info {
|
||||
// already instrumented with InstrumentHandler (using "prometheus" as handler
|
||||
// name).
|
||||
func Handler() http.Handler {
|
||||
return promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(defaultRegistry, promhttp.HandlerOpts{}))
|
||||
return promhttp.InstrumentMetricHandler(prometheus.NewRegistry(), promhttp.HandlerFor(defaultRegistry, promhttp.HandlerOpts{}))
|
||||
}
|
||||
|
||||
349
pkg/utils/reposcache/repo_cahes.go
Normal file
349
pkg/utils/reposcache/repo_cahes.go
Normal file
@@ -0,0 +1,349 @@
|
||||
// /*
|
||||
// Copyright 2020 The KubeSphere Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// */
|
||||
//
|
||||
|
||||
package reposcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/apis/application/v1alpha1"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix/helmrepoindex"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var WorkDir string
|
||||
|
||||
func NewReposCache() ReposCache {
|
||||
return &cachedRepos{
|
||||
chartsInRepo: map[workspace]map[string]int{},
|
||||
repos: map[string]*v1alpha1.HelmRepo{},
|
||||
apps: map[string]*v1alpha1.HelmApplication{},
|
||||
versions: map[string]*v1alpha1.HelmApplicationVersion{},
|
||||
repoCtgCounts: map[string]map[string]int{},
|
||||
}
|
||||
}
|
||||
|
||||
type ReposCache interface {
|
||||
AddRepo(repo *v1alpha1.HelmRepo) error
|
||||
DeleteRepo(repo *v1alpha1.HelmRepo) error
|
||||
|
||||
GetApplication(string) (*v1alpha1.HelmApplication, bool)
|
||||
GetAppVersion(string) (*v1alpha1.HelmApplicationVersion, bool, error)
|
||||
GetAppVersionWithData(string) (*v1alpha1.HelmApplicationVersion, bool, error)
|
||||
|
||||
ListAppVersionsByAppId(appId string) (ret []*v1alpha1.HelmApplicationVersion, exists bool)
|
||||
ListApplicationsByRepoId(repoId string) (ret []*v1alpha1.HelmApplication, exists bool)
|
||||
}
|
||||
|
||||
type workspace string
|
||||
type cachedRepos struct {
|
||||
sync.RWMutex
|
||||
|
||||
chartsInRepo map[workspace]map[string]int
|
||||
repoCtgCounts map[string]map[string]int
|
||||
|
||||
repos map[string]*v1alpha1.HelmRepo
|
||||
apps map[string]*v1alpha1.HelmApplication
|
||||
versions map[string]*v1alpha1.HelmApplicationVersion
|
||||
}
|
||||
|
||||
func (c *cachedRepos) deleteRepo(repo *v1alpha1.HelmRepo) {
|
||||
if len(repo.Status.Data) == 0 {
|
||||
return
|
||||
}
|
||||
index, err := helmrepoindex.ByteArrayToSavedIndex([]byte(repo.Status.Data))
|
||||
if err != nil {
|
||||
klog.Errorf("json unmarshal repo %s failed, error: %s", repo.Name, err)
|
||||
return
|
||||
}
|
||||
|
||||
klog.V(4).Infof("delete repo %s from cache", repo.Name)
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
repoId := repo.GetHelmRepoId()
|
||||
ws := workspace(repo.GetWorkspace())
|
||||
if _, exists := c.chartsInRepo[ws]; exists {
|
||||
delete(c.chartsInRepo[ws], repoId)
|
||||
}
|
||||
|
||||
delete(c.repoCtgCounts, repoId)
|
||||
delete(c.repos, repoId)
|
||||
|
||||
for _, app := range index.Applications {
|
||||
delete(c.apps, app.ApplicationId)
|
||||
for _, ver := range app.Charts {
|
||||
delete(c.versions, ver.ApplicationVersionId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadBuiltinChartData(name, version string) ([]byte, error) {
|
||||
fName := path.Join(WorkDir, "chart", fmt.Sprintf("%s-%s.tgz", name, version))
|
||||
f, err := os.Open(fName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
klog.Errorf("read index failed, error: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (c *cachedRepos) DeleteRepo(repo *v1alpha1.HelmRepo) error {
|
||||
c.deleteRepo(repo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *cachedRepos) GetApplication(appId string) (app *v1alpha1.HelmApplication, exists bool) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
if app, exists := c.apps[appId]; exists {
|
||||
return app, true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *cachedRepos) AddRepo(repo *v1alpha1.HelmRepo) error {
|
||||
return c.addRepo(repo, false)
|
||||
}
|
||||
|
||||
//Add new Repo to cachedRepos
|
||||
func (c *cachedRepos) addRepo(repo *v1alpha1.HelmRepo, builtin bool) error {
|
||||
if len(repo.Status.Data) == 0 {
|
||||
return nil
|
||||
}
|
||||
index, err := helmrepoindex.ByteArrayToSavedIndex([]byte(repo.Status.Data))
|
||||
if err != nil {
|
||||
klog.Errorf("json unmarshal repo %s failed, error: %s", repo.Name, err)
|
||||
return err
|
||||
}
|
||||
|
||||
klog.V(4).Infof("add repo %s to cache", repo.Name)
|
||||
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
ws := workspace(repo.GetWorkspace())
|
||||
if _, exists := c.chartsInRepo[ws]; !exists {
|
||||
c.chartsInRepo[ws] = make(map[string]int)
|
||||
}
|
||||
|
||||
repoId := repo.GetHelmRepoId()
|
||||
c.repos[repoId] = repo
|
||||
//c.repoCtgCounts[repo.GetHelmRepoId()] = make(map[string]int)
|
||||
if _, exists := c.repoCtgCounts[repoId]; !exists {
|
||||
c.repoCtgCounts[repoId] = map[string]int{}
|
||||
}
|
||||
var appName string
|
||||
|
||||
chartsCount := 0
|
||||
for key, app := range index.Applications {
|
||||
if builtin {
|
||||
appName = v1alpha1.HelmApplicationIdPrefix + app.Name
|
||||
} else {
|
||||
appName = app.ApplicationId
|
||||
}
|
||||
|
||||
HelmApp := v1alpha1.HelmApplication{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: appName,
|
||||
Annotations: map[string]string{
|
||||
constants.CreatorAnnotationKey: repo.GetCreator(),
|
||||
},
|
||||
Labels: map[string]string{
|
||||
constants.ChartRepoIdLabelKey: repo.GetHelmRepoId(),
|
||||
},
|
||||
},
|
||||
Spec: v1alpha1.HelmApplicationSpec{
|
||||
Name: key,
|
||||
Description: app.Description,
|
||||
Icon: app.Icon,
|
||||
},
|
||||
Status: v1alpha1.HelmApplicationStatus{
|
||||
State: v1alpha1.StateActive,
|
||||
},
|
||||
}
|
||||
c.apps[app.ApplicationId] = &HelmApp
|
||||
|
||||
var ctg, appVerName string
|
||||
var chartData []byte
|
||||
for _, ver := range app.Charts {
|
||||
chartsCount += 1
|
||||
if ver.Annotations != nil && ver.Annotations["category"] != "" {
|
||||
ctg = ver.Annotations["category"]
|
||||
}
|
||||
if builtin {
|
||||
appVerName = base64.StdEncoding.EncodeToString([]byte(ver.Name + ver.Version))
|
||||
chartData, err = loadBuiltinChartData(ver.Name, ver.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
appVerName = ver.ApplicationVersionId
|
||||
}
|
||||
|
||||
version := &v1alpha1.HelmApplicationVersion{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: appVerName,
|
||||
Annotations: map[string]string{constants.CreatorAnnotationKey: repo.GetCreator()},
|
||||
Labels: map[string]string{
|
||||
constants.ChartApplicationIdLabelKey: appName,
|
||||
constants.ChartRepoIdLabelKey: repo.GetHelmRepoId(),
|
||||
},
|
||||
CreationTimestamp: metav1.Time{Time: ver.Created},
|
||||
},
|
||||
Spec: v1alpha1.HelmApplicationVersionSpec{
|
||||
Metadata: &v1alpha1.Metadata{
|
||||
Name: ver.Name,
|
||||
AppVersion: ver.AppVersion,
|
||||
Version: ver.Version,
|
||||
},
|
||||
URLs: ver.URLs,
|
||||
Digest: ver.Digest,
|
||||
Data: chartData,
|
||||
},
|
||||
Status: v1alpha1.HelmApplicationVersionStatus{
|
||||
State: v1alpha1.StateActive,
|
||||
},
|
||||
}
|
||||
c.versions[ver.ApplicationVersionId] = version
|
||||
}
|
||||
|
||||
//modify application category
|
||||
ctgId := ""
|
||||
if ctg != "" {
|
||||
if c.apps[app.ApplicationId].Annotations == nil {
|
||||
c.apps[app.ApplicationId].Annotations = map[string]string{constants.CategoryIdLabelKey: ctg}
|
||||
} else {
|
||||
c.apps[app.ApplicationId].Annotations[constants.CategoryIdLabelKey] = ctg
|
||||
}
|
||||
ctgId = ctg
|
||||
} else {
|
||||
ctgId = v1alpha1.UncategorizedId
|
||||
}
|
||||
|
||||
if _, exists := c.repoCtgCounts[repoId][ctgId]; !exists {
|
||||
c.repoCtgCounts[repoId][ctgId] = 1
|
||||
} else {
|
||||
c.repoCtgCounts[repoId][ctgId] += 1
|
||||
}
|
||||
}
|
||||
|
||||
c.chartsInRepo[ws][repo.GetHelmRepoId()] = chartsCount
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *cachedRepos) ListApplicationsByRepoId(repoId string) (ret []*v1alpha1.HelmApplication, exists bool) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
if repo, exists := c.repos[repoId]; !exists {
|
||||
return nil, false
|
||||
} else {
|
||||
ret = make([]*v1alpha1.HelmApplication, 0, 10)
|
||||
for _, app := range c.apps {
|
||||
if app.GetHelmRepoId() == repo.Name {
|
||||
ret = append(ret, app)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func (c *cachedRepos) ListAppVersionsByAppId(appId string) (ret []*v1alpha1.HelmApplicationVersion, exists bool) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
if _, exists := c.apps[appId]; !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
ret = make([]*v1alpha1.HelmApplicationVersion, 0, 10)
|
||||
for _, ver := range c.versions {
|
||||
if ver.GetHelmApplicationId() == appId {
|
||||
ret = append(ret, ver)
|
||||
}
|
||||
}
|
||||
return ret, true
|
||||
}
|
||||
|
||||
func (c *cachedRepos) getAppVersion(versionId string, withData bool) (ret *v1alpha1.HelmApplicationVersion, exists bool, err error) {
|
||||
c.RLock()
|
||||
if version, exists := c.versions[versionId]; exists {
|
||||
//builtin chart data
|
||||
if withData {
|
||||
if len(version.Spec.Data) != 0 {
|
||||
c.RUnlock()
|
||||
return version, true, nil
|
||||
}
|
||||
|
||||
if len(version.Spec.URLs) == 0 {
|
||||
c.RUnlock()
|
||||
return nil, true, errors.New("invalid chart spec")
|
||||
}
|
||||
var repo *v1alpha1.HelmRepo
|
||||
var exists bool
|
||||
if repo, exists = c.repos[version.GetHelmRepoId()]; !exists {
|
||||
c.RUnlock()
|
||||
klog.Errorf("load repo for app version: %s/%s failed",
|
||||
version.GetWorkspace(), version.GetTrueName())
|
||||
return nil, true, err
|
||||
}
|
||||
|
||||
c.RUnlock()
|
||||
url := version.Spec.URLs[0]
|
||||
if !(strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "s3://")) {
|
||||
url = repo.Spec.Url + "/" + url
|
||||
}
|
||||
|
||||
buf, err := helmrepoindex.LoadChart(context.TODO(), url, &repo.Spec.Credential)
|
||||
if err != nil {
|
||||
klog.Errorf("load chart data for app version: %s/%s failed, error : %s", version.GetTrueName(),
|
||||
version.GetTrueName(), err)
|
||||
return nil, true, err
|
||||
}
|
||||
version.Spec.Data = buf.Bytes()
|
||||
return version, true, nil
|
||||
} else {
|
||||
c.RUnlock()
|
||||
return version, true, nil
|
||||
}
|
||||
} else {
|
||||
c.RUnlock()
|
||||
//version does not exists
|
||||
return nil, false, nil
|
||||
}
|
||||
}
|
||||
func (c *cachedRepos) GetAppVersion(versionId string) (ret *v1alpha1.HelmApplicationVersion, exists bool, err error) {
|
||||
return c.getAppVersion(versionId, false)
|
||||
}
|
||||
|
||||
func (c *cachedRepos) GetAppVersionWithData(versionId string) (ret *v1alpha1.HelmApplicationVersion, exists bool, err error) {
|
||||
return c.getAppVersion(versionId, true)
|
||||
}
|
||||
63
pkg/utils/resourceparse/resource_parse.go
Normal file
63
pkg/utils/resourceparse/resource_parse.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// /*
|
||||
// Copyright 2020 The KubeSphere Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// */
|
||||
//
|
||||
|
||||
package resourceparse
|
||||
|
||||
import (
|
||||
"io"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
"k8s.io/klog"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Parse(reader io.Reader, namespace, rlsName string, local bool) ([]*resource.Info, error) {
|
||||
if klog.V(2) {
|
||||
klog.Infof("parse resources, namespace: %s, release: %s", namespace, rlsName)
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
klog.Infof("parse resources end, namespace: %s, release: %s, cost: %v", namespace, rlsName, time.Now().Sub(start))
|
||||
}()
|
||||
}
|
||||
|
||||
kubeConfigFlags := genericclioptions.NewConfigFlags(true)
|
||||
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
|
||||
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
|
||||
builder := f.NewBuilder().Unstructured().NamespaceParam(namespace).ContinueOnError().Stream(reader, rlsName).Flatten()
|
||||
|
||||
if local == true {
|
||||
builder = builder.Local()
|
||||
}
|
||||
r := builder.Do()
|
||||
infos, err := r.Infos()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if local == false {
|
||||
for i := range infos {
|
||||
infos[i].Namespace = namespace
|
||||
err := infos[i].Get()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return infos, err
|
||||
}
|
||||
@@ -95,3 +95,10 @@ func Split(str string, sep string) []string {
|
||||
func StripAnsi(str string) string {
|
||||
return re.ReplaceAllString(str, "")
|
||||
}
|
||||
|
||||
func ShortenString(str string, n int) string {
|
||||
if len(str) <= n {
|
||||
return str
|
||||
}
|
||||
return str[:n]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user