<commit_before>package maintenance_helper

import (
	"bytes"
	"context"
	"fmt"

	"k8s.io/cli-runtime/pkg/resource"
	"k8s.io/kubectl/pkg/cmd/util"

	"github.com/werf/kubedog/pkg/kube"
	"github.com/werf/logboek"

	helm2to3_v3 "github.com/helm/helm-2to3/pkg/v3"

	v3_action "helm.sh/helm/v3/pkg/action"
	v3_rspb "helm.sh/helm/v3/pkg/release"

	v2_rspb "k8s.io/helm/pkg/proto/hapi/release"
	v2_releaseutil "k8s.io/helm/pkg/releaseutil"
	v2_storage "k8s.io/helm/pkg/storage"
	v2_driver "k8s.io/helm/pkg/storage/driver"
)

type Helm2ReleaseData struct {
	Release *v2_rspb.Release
}

type Helm3ReleaseData struct {
	Release *v3_rspb.Release
}

type MaintenanceHelperOptions struct {
	Helm2ReleaseStorageNamespace string
	Helm2ReleaseStorageType      string
	KubeConfigOptions            kube.KubeConfigOptions
}

func NewMaintenanceHelper(v3ActionConfig *v3_action.Configuration, opts MaintenanceHelperOptions) *MaintenanceHelper {
	releaseStorageType := opts.Helm2ReleaseStorageType
	if releaseStorageType == "" {
		releaseStorageType = "configmap"
	}

	releaseStorageNamespace := opts.Helm2ReleaseStorageNamespace
	if releaseStorageNamespace == "" {
		releaseStorageNamespace = "kube-system"
	}

	return &MaintenanceHelper{
		Helm2ReleaseStorageNamespace: releaseStorageNamespace,
		Helm2ReleaseStorageType:      releaseStorageType,
		KubeConfigOptions:            opts.KubeConfigOptions,
		v3ActionConfig:               v3ActionConfig,
	}
}

type MaintenanceHelper struct {
	KubeConfigOptions kube.KubeConfigOptions

	Helm2ReleaseStorageNamespace string
	Helm2ReleaseStorageType      string

	v2Storage      *v2_storage.Storage
	v3ActionConfig *v3_action.Configuration
}

func (helper *MaintenanceHelper) initHelm2Storage() (*v2_storage.Storage, error) {
	if helper.v2Storage != nil {
		return helper.v2Storage, nil
	}

	var drv v2_driver.Driver
	switch helper.Helm2ReleaseStorageType {
	case "configmap":
		drv = v2_driver.NewConfigMaps(kube.Client.CoreV1().ConfigMaps(helper.Helm2ReleaseStorageNamespace))
	case "secret":
		drv = v2_driver.NewSecrets(kube.Client.CoreV1().Secrets(helper.Helm2ReleaseStorageNamespace))
	default:
		return nil, fmt.Errorf("unknown helm 2 release v2Storage type %q", helper.Helm2ReleaseStorageType)
	}
	helper.v2Storage = v2_storage.Init(drv)

	return helper.v2Storage, nil
}

func (helper *MaintenanceHelper) getResourcesFactory() (util.Factory, error) {
	configGetter, err := kube.NewKubeConfigGetter(kube.KubeConfigGetterOptions{KubeConfigOptions: helper.KubeConfigOptions})
	if err != nil {
		return nil, fmt.Errorf("error creating kube config getter: %s", err)
	}
	return util.NewFactory(configGetter), nil
}

func (helper *MaintenanceHelper) CheckHelm2StorageAvailable(ctx context.Context) (bool, error) {
	storage, err := helper.initHelm2Storage()
	if err != nil {
		return false, fmt.Errorf("error initializing helm 2 v2Storage: %s", err)
	}

	_, err = storage.ListReleases()<commit_msg>Actualize helm maintenance_helper package changes from v1.1<commit_after>package maintenance_helper

import (
	"bytes"
	"context"
	"fmt"

	"k8s.io/cli-runtime/pkg/resource"
	"k8s.io/kubectl/pkg/cmd/util"

	"github.com/werf/kubedog/pkg/kube"
	"github.com/werf/logboek"

	helm2to3_v3 "github.com/helm/helm-2to3/pkg/v3"

	v3_action "helm.sh/helm/v3/pkg/action"
	v3_rspb "helm.sh/helm/v3/pkg/release"

	v2_rspb "k8s.io/helm/pkg/proto/hapi/release"
	v2_releaseutil "k8s.io/helm/pkg/releaseutil"
	v2_storage "k8s.io/helm/pkg/storage"
	v2_driver "k8s.io/helm/pkg/storage/driver"
)

type Helm2ReleaseData struct {
	Release *v2_rspb.Release
}

type Helm3ReleaseData struct {
	Release *v3_rspb.Release
}

type MaintenanceHelperOptions struct {
	Helm2ReleaseStorageNamespace string
	Helm2ReleaseStorageType      string
	KubeConfigOptions            kube.KubeConfigOptions
}

func NewMaintenanceHelper(v3ActionConfig *v3_action.Configuration, opts MaintenanceHelperOptions) *MaintenanceHelper {
	releaseStorageType := opts.Helm2ReleaseStorageType
	if releaseStorageType == "" {
		releaseStorageType = "configmap"
	}

	releaseStorageNamespace := opts.Helm2ReleaseStorageNamespace
	if releaseStorageNamespace == "" {
		releaseStorageNamespace = "kube-system"
	}

	return &MaintenanceHelper{
		Helm2ReleaseStorageNamespace: releaseStorageNamespace,
		Helm2ReleaseStorageType:      releaseStorageType,
		KubeConfigOptions:            opts.KubeConfigOptions,
		v3ActionConfig:               v3ActionConfig,
	}
}

type MaintenanceHelper struct {
	KubeConfigOptions kube.KubeConfigOptions

	Helm2ReleaseStorageNamespace string
	Helm2ReleaseStorageType      string

	v2Storage      *v2_storage.Storage
	v3ActionConfig *v3_action.Configuration
}

func (helper *MaintenanceHelper) initHelm2Storage() (*v2_storage.Storage, error) {
	if helper.v2Storage != nil {
		return helper.v2Storage, nil
	}

	var drv v2_driver.Driver
	switch helper.Helm2ReleaseStorageType {
	case "configmap":
		drv = v2_driver.NewConfigMaps(kube.Client.CoreV1().ConfigMaps(helper.Helm2ReleaseStorageNamespace))
	case "secret":
		drv = v2_driver.NewSecrets(kube.Client.CoreV1().Secrets(helper.Helm2ReleaseStorageNamespace))
	default:
		return nil, fmt.Errorf("unknown helm 2 release v2Storage type %q", helper.Helm2ReleaseStorageType)
	}
	helper.v2Storage = v2_storage.Init(drv)

	return helper.v2Storage, nil
}

func (helper *MaintenanceHelper) getResourcesFactory() (util.Factory, error) {
	configGetter, err := kube.NewKubeConfigGetter(kube.KubeConfigGetterOptions{KubeConfigOptions: helper.KubeConfigOptions})
	if err != nil {
		return nil, fmt.Errorf("error creating kube config getter: %s", err)
	}
	return util.NewFactory(configGetter), nil
}

func (helper *MaintenanceHelper) CheckHelm3StorageAvailable(ctx context.Context) (bool, error) {
	_, err := helper.GetHelm3ReleasesList(ctx)
	if err != nil {
		logboek.Context(ctx).Info().LogFDetails("- Helm 3 storage is not available: %s\n", err)
		return false, nil
	}

	logboek.Context(ctx).Info().LogFDetails("+ Helm 3 storage available\n")
	return true, nil
}

func (helper *MaintenanceHelper) CheckHelm2StorageAvailable(ctx context.Context) (bool, error) {
	storage, err := helper.initHelm2Storage()
	if err != nil {
		return false, fmt.Errorf("error initializing helm 2 v2Storage: %s", err)
	}

	_, err = storage.ListReleases()
{'fraction_non_alphanumeric': 0.1017897091722595, 'fraction_numerical': 0.020773410035155003, 'mean_word_length': 5.252747252747253, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6076788', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2153, 'n_words': 385}