func (v *Versioner) EnsureCompatibleKubectlAvailable(version semver.Version, allowDownload bool) (string, error) {
	kubectl, err := v.kFinder.FindCompatibleKubectl(version)
	if err == nil {
		return kubectl.Path, nil
	}
	if !allowDownload {
		return "", errors.New("The right kubectl is missing, binary downloads from kubernetes' upstream mirror are disabled")
	}
	klog.Infof("Right kubectl missing, downloading version %s", version.String())
	filename := filepath.Join(
		common.LocalDownloadDir(),
		common.BuildKubectlNameForLocalBin(version))
	if err := v.downloader.GetKubectlBinary(version, filename); err != nil {
		return "", err
	}
	return filename, nil
}
EnsureCompatibleKubectlAvailable ensures the kubectl binary with the specified
version is available on the system. It will return the full path to the
binary
package finder
import (
	"errors"
	"github.com/flavio/kuberlr/internal/common"
	"github.com/flavio/kuberlr/internal/downloader"
	"github.com/flavio/kuberlr/internal/kubehelper"
	"path/filepath"
	"github.com/blang/semver/v4"
	"k8s.io/klog"
)
type downloadHelper interface {
	GetKubectlBinary(version semver.Version, destination string) error
	UpstreamStableVersion() (semver.Version, error)
}
type kubeAPIHelper interface {
	Version(timeout int64) (semver.Version, error)
}
type iFinder interface {
	SystemKubectlBinaries() (KubectlBinaries, error)
	LocalKubectlBinaries() (KubectlBinaries, error)
	AllKubectlBinaries(reverseSort bool) KubectlBinaries
	FindCompatibleKubectl(requestedVersion semver.Version) (KubectlBinary, error)
	MostRecentKubectlAvailable() (KubectlBinary, error)
}
type Versioner struct {
	kFinder    iFinder
	downloader downloadHelper
	apiServer  kubeAPIHelper
}
func NewVersioner(f iFinder) *Versioner {
	return &Versioner{
		kFinder:    f,
		downloader: &downloader.Downloder{},
		apiServer:  &kubehelper.KubeAPI{},
	}
}
func (v *Versioner) KubectlVersionToUse(timeout int64) (semver.Version, error) {
	version, err := v.apiServer.Version(timeout)
	if err != nil {
		if isTimeout(err) {
			klog.Info("Remote kubernetes server unreachable")
		} else {
			klog.Info(err)
		}
		kubectl, err := v.kFinder.MostRecentKubectlAvailable()
		if err == nil {
			return kubectl.Version, nil
		} else if common.IsNoVersionFound(err) {
			klog.Info("No local kubectl binary found, fetching latest stable release version")
			return v.downloader.UpstreamStableVersion()
		}
	}
	return version, err
}