Skip to content

Commit

Permalink
feat(application): get helmclient with app provider (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielXLee authored Sep 20, 2022
1 parent 625c032 commit 9c8f476
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/application/controller/app/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/controller/app/action/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/controller/app/action/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/controller/app/action/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/application/controller/app/action/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package action

import (
"context"

appconfig "tkestack.io/tke/pkg/application/config"

"helm.sh/helm/v3/pkg/release"
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/controller/app/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/application/provider/application/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -82,13 +87,18 @@ 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 {
Name() string

ControllerProvider
HooksProvider
RestConfigProvider
}

var _ Provider = &DelegateProvider{}
Expand All @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions pkg/application/registry/application/storage/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/application/registry/application/storage/rest_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/application/registry/application/storage/rest_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/application/util/helmclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9c8f476

Please sign in to comment.