diff --git a/Makefile b/Makefile index 45d71739..1ed1b3f6 100644 --- a/Makefile +++ b/Makefile @@ -89,7 +89,7 @@ bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION} @ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint bin/golangci-lint-${GOLANGCI_VERSION}: @mkdir -p bin - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION} + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION} @mv bin/golangci-lint $@ .PHONY: lint diff --git a/internal/cli/command/cluster/shell.go b/internal/cli/command/cluster/shell.go index 7b934eed..72ac4eba 100644 --- a/internal/cli/command/cluster/shell.go +++ b/internal/cli/command/cluster/shell.go @@ -30,6 +30,7 @@ import ( "github.com/banzaicloud/banzai-cli/internal/cli" "github.com/banzaicloud/banzai-cli/internal/cli/auth" clustercontext "github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/context" + "github.com/banzaicloud/banzai-cli/pkg/kubectlversion" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/ttacon/chalk" @@ -87,6 +88,13 @@ func getKubeConfig(ctx context.Context, client *pipeline.APIClient, orgId, id in return } + // in kubectl version 1.24 client.authentication.k8s.io/v1alpha1 has been deprecated + if less, kubectlVersionErr := kubectlversion.LessThan("1.24"); !less && kubectlVersionErr == nil { + config.Data = strings.Replace(config.Data, "apiVersion: client.authentication.k8s.io/v1alpha1", "apiVersion: client.authentication.k8s.io/v1beta1", -1) + } else if kubectlVersionErr != nil { + err = errors.WrapIf(kubectlVersionErr, "could not get kubectl version") + } + return } diff --git a/pkg/kubectlversion/kubectlversion.go b/pkg/kubectlversion/kubectlversion.go new file mode 100644 index 00000000..0fb377c6 --- /dev/null +++ b/pkg/kubectlversion/kubectlversion.go @@ -0,0 +1,65 @@ +// Copyright © 2020 Banzai Cloud +// +// 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 kubectlversion + +import ( + "encoding/json" + "os/exec" + + "emperror.dev/errors" + "github.com/Masterminds/semver" +) + +type clientVersion struct { + GitVersion string `json:"gitVersion,omitempty"` +} + +type kubectlVersionOutput struct { + ClientVersion clientVersion `json:"clientVersion,omitempty"` +} + +func getLocalKubectlVersion() (clientVersion, error) { + c := exec.Command("kubectl", "version", "--client=true", "-o", "json") + out, err := c.Output() + if err != nil { + return clientVersion{}, errors.WrapIf(err, "failed to determine kubectl version") + } + + var parsed kubectlVersionOutput + if err := json.Unmarshal(out, &parsed); err != nil { + return clientVersion{}, errors.WrapIf(err, "failed to parse kubectl version") + } + + return parsed.ClientVersion, nil +} + +func LessThan(v string) (bool, error) { + inputVersion, err := semver.NewVersion(v) + if err != nil { + return false, errors.WrapIf(err, "failed to parse kubectl input version") + } + + localKubectlVersion, err := getLocalKubectlVersion() + if err != nil { + return false, errors.WrapIf(err, "failed to get local kubectl version") + } + + localVersion, err := semver.NewVersion(localKubectlVersion.GitVersion) + if err != nil { + return false, errors.WrapIf(err, "failed to parse kubectl local semantic version") + } + + return localVersion.LessThan(inputVersion), nil +}