diff --git a/pkg/application/controller/app/action/install.go b/pkg/application/controller/app/action/install.go index bd687d43c..75ceb4876 100644 --- a/pkg/application/controller/app/action/install.go +++ b/pkg/application/controller/app/action/install.go @@ -48,7 +48,7 @@ func Install(ctx context.Context, if err != nil { return nil, err } - client, err := util.NewHelmClient(ctx, platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + client, err := util.NewHelmClientWithProvider(ctx, platformClient, app) if err != nil { return nil, err } diff --git a/pkg/application/controller/app/action/list.go b/pkg/application/controller/app/action/list.go index f9fca0cd1..77f672e36 100644 --- a/pkg/application/controller/app/action/list.go +++ b/pkg/application/controller/app/action/list.go @@ -34,7 +34,7 @@ func List(ctx context.Context, applicationClient applicationversionedclient.ApplicationV1Interface, platformClient platformversionedclient.PlatformV1Interface, app *applicationv1.App) ([]*release.Release, error) { - client, err := util.NewHelmClient(ctx, platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + client, err := util.NewHelmClientWithProvider(ctx, platformClient, app) if err != nil { return nil, err } diff --git a/pkg/application/controller/app/action/pull.go b/pkg/application/controller/app/action/pull.go index 9935f4c0f..baa5a6c68 100644 --- a/pkg/application/controller/app/action/pull.go +++ b/pkg/application/controller/app/action/pull.go @@ -38,7 +38,7 @@ func Pull(ctx context.Context, app *applicationv1.App, repo appconfig.RepoConfiguration, updateStatusFunc applicationprovider.UpdateStatusFunc) (string, error) { - client, err := util.NewHelmClient(ctx, platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + client, err := util.NewHelmClientWithProvider(ctx, platformClient, app) if err != nil { return "", err } diff --git a/pkg/application/controller/app/action/rollback.go b/pkg/application/controller/app/action/rollback.go index bddec079d..013072fa0 100644 --- a/pkg/application/controller/app/action/rollback.go +++ b/pkg/application/controller/app/action/rollback.go @@ -44,7 +44,7 @@ func Rollback(ctx context.Context, if err != nil { return nil, err } - client, err := util.NewHelmClient(ctx, platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + client, err := util.NewHelmClientWithProvider(ctx, platformClient, app) if err != nil { return nil, err } diff --git a/pkg/application/controller/app/action/uninstall.go b/pkg/application/controller/app/action/uninstall.go index a21a661cf..f309011d9 100644 --- a/pkg/application/controller/app/action/uninstall.go +++ b/pkg/application/controller/app/action/uninstall.go @@ -20,6 +20,7 @@ package action import ( "context" + appconfig "tkestack.io/tke/pkg/application/config" "helm.sh/helm/v3/pkg/release" @@ -41,7 +42,7 @@ func Uninstall(ctx context.Context, if err != nil { return nil, err } - client, err := util.NewHelmClient(ctx, platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + client, err := util.NewHelmClientWithProvider(ctx, platformClient, app) if err != nil { return nil, err } diff --git a/pkg/application/controller/app/action/upgrade.go b/pkg/application/controller/app/action/upgrade.go index d085cd94b..12eefb1e4 100644 --- a/pkg/application/controller/app/action/upgrade.go +++ b/pkg/application/controller/app/action/upgrade.go @@ -48,7 +48,7 @@ func Upgrade(ctx context.Context, if err != nil { return nil, err } - client, err := util.NewHelmClient(ctx, platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + client, err := util.NewHelmClientWithProvider(ctx, platformClient, app) if err != nil { return nil, err } diff --git a/pkg/application/provider/application/interface.go b/pkg/application/provider/application/interface.go index baba35e5b..8ecffd7bb 100644 --- a/pkg/application/provider/application/interface.go +++ b/pkg/application/provider/application/interface.go @@ -20,10 +20,15 @@ package application import ( "context" + "fmt" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/rest" applicationv1 "tkestack.io/tke/api/application/v1" applicationversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/application/v1" platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" + platformv1 "tkestack.io/tke/api/platform/v1" appconfig "tkestack.io/tke/pkg/application/config" ) @@ -82,6 +87,10 @@ type HooksProvider interface { repo appconfig.RepoConfiguration) error } +type RestConfigProvider interface { + GetRestConfig(ctx context.Context, platformClient platformversionedclient.PlatformV1Interface, app *applicationv1.App) (*rest.Config, error) +} + // Provider defines a set of response interfaces for specific cluster // types in cluster management. type Provider interface { @@ -89,6 +98,7 @@ type Provider interface { ControllerProvider HooksProvider + RestConfigProvider } var _ Provider = &DelegateProvider{} @@ -108,6 +118,26 @@ func (p *DelegateProvider) OnFilter(ctx context.Context, app *applicationv1.App) return true } +func (p *DelegateProvider) GetRestConfig(ctx context.Context, platformClient platformversionedclient.PlatformV1Interface, app *applicationv1.App) (*rest.Config, error) { + var credential *platformv1.ClusterCredential + cluster, err := platformClient.Clusters().Get(ctx, app.Spec.TargetCluster, metav1.GetOptions{}) + if err != nil { + return nil, err + } + if cluster == nil { + return nil, errors.NewBadRequest(fmt.Sprintf("can not found cluster by name %s", cluster)) + } + if cluster.Spec.ClusterCredentialRef != nil { + credential, err = platformClient.ClusterCredentials().Get(ctx, cluster.Spec.ClusterCredentialRef.Name, metav1.GetOptions{}) + if err != nil { + return nil, fmt.Errorf("get cluster's credential error: %w", err) + } + } else { + return nil, fmt.Errorf("get cluster's credential error, no cluster credential") + } + return credential.RESTConfig(cluster), nil +} + func (DelegateProvider) PreInstall(ctx context.Context, applicationClient applicationversionedclient.ApplicationV1Interface, platformClient platformversionedclient.PlatformV1Interface, diff --git a/pkg/application/registry/application/storage/rest.go b/pkg/application/registry/application/storage/rest.go index f2c628ae9..19958938f 100644 --- a/pkg/application/registry/application/storage/rest.go +++ b/pkg/application/registry/application/storage/rest.go @@ -34,6 +34,7 @@ import ( "k8s.io/apiserver/pkg/registry/rest" "tkestack.io/tke/api/application" applicationapi "tkestack.io/tke/api/application" + v1 "tkestack.io/tke/api/application/v1" applicationinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/application/internalversion" platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" registryversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/registry/v1" @@ -337,8 +338,11 @@ func (rs *REST) dryRun(ctx context.Context, app *application.App) (*release.Rele if err != nil { return nil, errors.NewInternalError(err) } - - client, err := util.NewHelmClient(ctx, rs.platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + appv1 := &v1.App{} + if err := v1.Convert_application_App_To_v1_App(app, appv1, nil); err != nil { + return nil, err + } + client, err := util.NewHelmClientWithProvider(ctx, rs.platformClient, appv1) if err != nil { return nil, errors.NewBadRequest(err.Error()) } diff --git a/pkg/application/registry/application/storage/rest_history.go b/pkg/application/registry/application/storage/rest_history.go index a82e0c893..eb1145de5 100644 --- a/pkg/application/registry/application/storage/rest_history.go +++ b/pkg/application/registry/application/storage/rest_history.go @@ -25,6 +25,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "tkestack.io/tke/api/application" + v1 "tkestack.io/tke/api/application/v1" applicationinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/application/internalversion" platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" helmaction "tkestack.io/tke/pkg/application/helm/action" @@ -67,8 +68,11 @@ func (rs *HistoryREST) Get(ctx context.Context, name string, options *metav1.Get return nil, errors.NewInternalError(err) } app := obj.(*application.App) - - client, err := util.NewHelmClient(ctx, rs.platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + appv1 := &v1.App{} + if err := v1.Convert_application_App_To_v1_App(app, appv1, nil); err != nil { + return nil, err + } + client, err := util.NewHelmClientWithProvider(ctx, rs.platformClient, appv1) if err != nil { return nil, errors.NewInternalError(err) } diff --git a/pkg/application/registry/application/storage/rest_resource.go b/pkg/application/registry/application/storage/rest_resource.go index ed0a1ebae..4cd67d20e 100644 --- a/pkg/application/registry/application/storage/rest_resource.go +++ b/pkg/application/registry/application/storage/rest_resource.go @@ -25,6 +25,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "tkestack.io/tke/api/application" + v1 "tkestack.io/tke/api/application/v1" applicationinternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/application/internalversion" platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" helmaction "tkestack.io/tke/pkg/application/helm/action" @@ -67,8 +68,11 @@ func (rs *ResourceREST) Get(ctx context.Context, name string, options *metav1.Ge return nil, errors.NewInternalError(err) } app := obj.(*application.App) - - client, err := util.NewHelmClient(ctx, rs.platformClient, app.Spec.TargetCluster, app.Spec.TargetNamespace) + appv1 := &v1.App{} + if err := v1.Convert_application_App_To_v1_App(app, appv1, nil); err != nil { + return nil, err + } + client, err := util.NewHelmClientWithProvider(ctx, rs.platformClient, appv1) if err != nil { return nil, errors.NewInternalError(err) } diff --git a/pkg/application/util/helmclient.go b/pkg/application/util/helmclient.go index 3b98a0793..e8671d02d 100644 --- a/pkg/application/util/helmclient.go +++ b/pkg/application/util/helmclient.go @@ -24,12 +24,31 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + applicationv1 "tkestack.io/tke/api/application/v1" platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" platformv1 "tkestack.io/tke/api/platform/v1" helmaction "tkestack.io/tke/pkg/application/helm/action" helmconfig "tkestack.io/tke/pkg/application/helm/config" + applicationprovider "tkestack.io/tke/pkg/application/provider/application" ) +// NewHelmClientWithProvider return a new helm client used to run helm cmd +func NewHelmClientWithProvider(ctx context.Context, platformClient platformversionedclient.PlatformV1Interface, app *applicationv1.App) (*helmaction.Client, error) { + provider, err := applicationprovider.GetProvider(app) + if err != nil { + return nil, err + } + restConfig, err := provider.GetRestConfig(ctx, platformClient, app) + if err != nil { + return nil, err + } + restClientGetter := &helmconfig.RESTClientGetter{RestConfig: restConfig} + // we should set namespace here. If not, release will be installed in target namespace, but resources will not be installed in target namespace + restClientGetter.Namespace = &app.Spec.TargetNamespace + client := helmaction.NewClient("", restClientGetter) + return client, nil +} + // NewHelmClient return a new client used to run helm cmd func NewHelmClient(ctx context.Context, platformClient platformversionedclient.PlatformV1Interface,