Skip to content

Commit c4504a3

Browse files
authored
merge(#362): fixed authentication apiversion in kubeconfig
2 parents c9fefd0 + 2d8ef6f commit c4504a3

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION}
8989
@ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint
9090
bin/golangci-lint-${GOLANGCI_VERSION}:
9191
@mkdir -p bin
92-
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION}
92+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- -b ./bin/ v${GOLANGCI_VERSION}
9393
@mv bin/golangci-lint $@
9494

9595
.PHONY: lint

internal/cli/command/cluster/shell.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/banzaicloud/banzai-cli/internal/cli"
3131
"github.com/banzaicloud/banzai-cli/internal/cli/auth"
3232
clustercontext "github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/context"
33+
"github.com/banzaicloud/banzai-cli/pkg/kubectlversion"
3334
log "github.com/sirupsen/logrus"
3435
"github.com/spf13/cobra"
3536
"github.com/ttacon/chalk"
@@ -87,6 +88,13 @@ func getKubeConfig(ctx context.Context, client *pipeline.APIClient, orgId, id in
8788
return
8889
}
8990

91+
// in kubectl version 1.24 client.authentication.k8s.io/v1alpha1 has been deprecated
92+
if less, kubectlVersionErr := kubectlversion.LessThan("1.24"); !less && kubectlVersionErr == nil {
93+
config.Data = strings.Replace(config.Data, "apiVersion: client.authentication.k8s.io/v1alpha1", "apiVersion: client.authentication.k8s.io/v1beta1", -1)
94+
} else if kubectlVersionErr != nil {
95+
err = errors.WrapIf(kubectlVersionErr, "could not get kubectl version")
96+
}
97+
9098
return
9199
}
92100

pkg/kubectlversion/kubectlversion.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright © 2020 Banzai Cloud
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package kubectlversion
16+
17+
import (
18+
"encoding/json"
19+
"os/exec"
20+
21+
"emperror.dev/errors"
22+
"github.com/Masterminds/semver"
23+
)
24+
25+
type clientVersion struct {
26+
GitVersion string `json:"gitVersion,omitempty"`
27+
}
28+
29+
type kubectlVersionOutput struct {
30+
ClientVersion clientVersion `json:"clientVersion,omitempty"`
31+
}
32+
33+
func getLocalKubectlVersion() (clientVersion, error) {
34+
c := exec.Command("kubectl", "version", "--client=true", "-o", "json")
35+
out, err := c.Output()
36+
if err != nil {
37+
return clientVersion{}, errors.WrapIf(err, "failed to determine kubectl version")
38+
}
39+
40+
var parsed kubectlVersionOutput
41+
if err := json.Unmarshal(out, &parsed); err != nil {
42+
return clientVersion{}, errors.WrapIf(err, "failed to parse kubectl version")
43+
}
44+
45+
return parsed.ClientVersion, nil
46+
}
47+
48+
func LessThan(v string) (bool, error) {
49+
inputVersion, err := semver.NewVersion(v)
50+
if err != nil {
51+
return false, errors.WrapIf(err, "failed to parse kubectl input version")
52+
}
53+
54+
localKubectlVersion, err := getLocalKubectlVersion()
55+
if err != nil {
56+
return false, errors.WrapIf(err, "failed to get local kubectl version")
57+
}
58+
59+
localVersion, err := semver.NewVersion(localKubectlVersion.GitVersion)
60+
if err != nil {
61+
return false, errors.WrapIf(err, "failed to parse kubectl local semantic version")
62+
}
63+
64+
return localVersion.LessThan(inputVersion), nil
65+
}

0 commit comments

Comments
 (0)