Skip to content

Commit

Permalink
merge(#362): fixed authentication apiversion in kubeconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
akijakya authored Jun 29, 2022
2 parents c9fefd0 + 2d8ef6f commit c4504a3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions internal/cli/command/cluster/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
65 changes: 65 additions & 0 deletions pkg/kubectlversion/kubectlversion.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c4504a3

Please sign in to comment.