Skip to content

Commit 8fc20db

Browse files
author
Billy
authored
Merge branch 'v2.9' into feat/add-username-flag
2 parents 4f5c122 + a85cc10 commit 8fc20db

32 files changed

Lines changed: 885 additions & 1283 deletions

.github/workflows/ci.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
pull_request:
7+
8+
jobs:
9+
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Repo
14+
uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: '1.22'
20+
cache: false
21+
22+
- name: Lint
23+
uses: golangci/golangci-lint-action@v4
24+
25+
- name: Test
26+
run: go test -v ./...
27+
28+
- name: Build
29+
run: make build
30+
31+
fossa:
32+
runs-on: ubuntu-latest
33+
permissions:
34+
contents: read
35+
id-token: write # needed for the Vault authentication
36+
37+
steps:
38+
- name: Load Secrets from Vault
39+
uses: rancher-eio/read-vault-secrets@main
40+
with:
41+
secrets: |
42+
secret/data/github/repo/${{ github.repository }}/fossa/credentials token | FOSSA
43+
44+
- name: Checkout Repo
45+
uses: actions/checkout@v3
46+
47+
- name: Check FOSSA compliance
48+
uses: fossas/fossa-action@main
49+
with:
50+
api-key: ${{ env.FOSSA }}

.github/workflows/release.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
tags:
7+
- v*
8+
9+
jobs:
10+
release:
11+
permissions:
12+
contents: read
13+
id-token: write # needed for the Vault authentication
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Load Secrets from Vault
18+
uses: rancher-eio/read-vault-secrets@main
19+
with:
20+
secrets: |
21+
secret/data/github/repo/${{ github.repository }}/dockerhub/rancher/credentials username | DOCKER_USERNAME ;
22+
secret/data/github/repo/${{ github.repository }}/dockerhub/rancher/credentials password | DOCKER_PASSWORD ;
23+
24+
- name: Checkout Repo
25+
uses: actions/checkout@v3
26+
27+
- name: Login to Docker Hub
28+
uses: docker/login-action@v3
29+
with:
30+
username: ${{ env.DOCKER_USERNAME }}
31+
password: ${{ env.DOCKER_PASSWORD }}

.golangci.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
{
2-
"linters": {
3-
"disable-all": true,
4-
"enable": [
5-
"govet",
6-
"golint",
7-
"goimports"
8-
]
9-
},
102
"run": {
113
"timeout": "10m"
124
}

Dockerfile.dapper

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM registry.suse.com/bci/golang:1.20
1+
FROM registry.suse.com/bci/golang:1.22
22

33
RUN zypper -n install docker rsync xz zip
44

5-
ENV GOLANGCI_LINT v1.53.3
5+
ENV GOLANGCI_LINT v1.57.1
66
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin "$GOLANGCI_LINT"
77

88
ENV DAPPER_SOURCE /go/src/github.com/rancher/cli

cmd/app.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
98
"net/http"
109
"net/url"
1110
"os"
@@ -799,7 +798,7 @@ func walkTemplateDirectory(templatePath string) (string, map[string]string, erro
799798
return nil
800799
}
801800
version := &chartVersion{}
802-
content, err := ioutil.ReadFile(path)
801+
content, err := os.ReadFile(path)
803802
if err != nil {
804803
return err
805804
}
@@ -816,7 +815,7 @@ func walkTemplateDirectory(templatePath string) (string, map[string]string, erro
816815
if info.IsDir() {
817816
return nil
818817
}
819-
content, err := ioutil.ReadFile(path)
818+
content, err := os.ReadFile(path)
820819
if err != nil {
821820
return err
822821
}
@@ -1254,7 +1253,7 @@ func parseAnswersFile(location string, answers map[string]string) error {
12541253
}
12551254

12561255
func parseFile(location string) (map[string]interface{}, error) {
1257-
bytes, err := ioutil.ReadFile(location)
1256+
bytes, err := os.ReadFile(location)
12581257
if err != nil {
12591258
return nil, err
12601259
}

cmd/cluster.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"slices"
78
"strconv"
89
"strings"
910

@@ -255,9 +256,14 @@ func clusterCreate(ctx *cli.Context) error {
255256
return err
256257
}
257258

258-
if ctx.String("k8s-version") != "" {
259-
k8sVersions := getClusterK8sOptions(c)
260-
if ok := findStringInArray(ctx.String("k8s-version"), k8sVersions); !ok {
259+
k8sVersion := ctx.String("k8s-version")
260+
if k8sVersion != "" {
261+
k8sVersions, err := getClusterK8sOptions(c)
262+
if err != nil {
263+
return err
264+
}
265+
266+
if slices.Contains(k8sVersions, k8sVersion) {
261267
fmt.Println("Available Kubernetes versions:")
262268
for _, val := range k8sVersions {
263269
fmt.Println(val)
@@ -742,16 +748,24 @@ func getClusterPods(cluster managementClient.Cluster) string {
742748
return cluster.Requested["pods"] + "/" + cluster.Allocatable["pods"]
743749
}
744750

745-
func getClusterK8sOptions(c *cliclient.MasterClient) []string {
751+
func getClusterK8sOptions(c *cliclient.MasterClient) ([]string, error) {
746752
var options []string
747-
setting, _ := c.ManagementClient.Setting.ByID("k8s-version-to-images")
753+
754+
setting, err := c.ManagementClient.Setting.ByID("k8s-version-to-images")
755+
if err != nil {
756+
return nil, err
757+
}
758+
748759
var objmap map[string]*json.RawMessage
760+
err = json.Unmarshal([]byte(setting.Value), &objmap)
761+
if err != nil {
762+
return nil, err
763+
}
749764

750-
json.Unmarshal([]byte(setting.Value), &objmap)
751765
for key := range objmap {
752766
options = append(options, key)
753767
}
754-
return options
768+
return options, nil
755769
}
756770

757771
func getClusterConfig(ctx *cli.Context) (*managementClient.Cluster, error) {

cmd/common.go

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package cmd
33
import (
44
"bufio"
55
"bytes"
6-
"context"
76
"crypto/x509"
87
"encoding/pem"
98
"fmt"
109
"io"
11-
"io/ioutil"
1210
"math/rand"
1311
"net/url"
1412
"os"
@@ -22,7 +20,6 @@ import (
2220
"time"
2321
"unicode"
2422

25-
"github.com/docker/docker/pkg/namesgenerator"
2623
"github.com/ghodss/yaml"
2724
"github.com/pkg/errors"
2825
"github.com/rancher/cli/cliclient"
@@ -33,6 +30,8 @@ import (
3330
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
3431
"github.com/sirupsen/logrus"
3532
"github.com/urfave/cli"
33+
"golang.org/x/text/cases"
34+
"golang.org/x/text/language"
3635
"k8s.io/client-go/tools/clientcmd/api"
3736
)
3837

@@ -43,7 +42,6 @@ const (
4342
)
4443

4544
var (
46-
errNoURL = errors.New("RANCHER_URL environment or --Url is not set, run `login`")
4745
// ManagementResourceTypes lists the types we use the management client for
4846
ManagementResourceTypes = []string{"cluster", "node", "project"}
4947
// ProjectResourceTypes lists the types we use the cluster client for
@@ -151,7 +149,7 @@ func getKubeConfigForUser(ctx *cli.Context, user string) (*api.Config, error) {
151149
}
152150

153151
focusedServer := cf.FocusedServer()
154-
kubeConfig, _ := focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())]
152+
kubeConfig := focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())]
155153
return kubeConfig, nil
156154
}
157155

@@ -230,7 +228,7 @@ func getRancherServerVersion(c *cliclient.MasterClient) (string, error) {
230228
}
231229

232230
func loadAndVerifyCert(path string) (string, error) {
233-
caCert, err := ioutil.ReadFile(path)
231+
caCert, err := os.ReadFile(path)
234232
if err != nil {
235233
return "", err
236234
}
@@ -301,21 +299,21 @@ func GetClient(ctx *cli.Context) (*cliclient.MasterClient, error) {
301299
func GetResourceType(c *cliclient.MasterClient, resource string) (string, error) {
302300
if c.ManagementClient != nil {
303301
for key := range c.ManagementClient.APIBaseClient.Types {
304-
if strings.ToLower(key) == strings.ToLower(resource) {
302+
if strings.EqualFold(key, resource) {
305303
return key, nil
306304
}
307305
}
308306
}
309307
if c.ProjectClient != nil {
310308
for key := range c.ProjectClient.APIBaseClient.Types {
311-
if strings.ToLower(key) == strings.ToLower(resource) {
309+
if strings.EqualFold(key, resource) {
312310
return key, nil
313311
}
314312
}
315313
}
316314
if c.ClusterClient != nil {
317315
for key := range c.ClusterClient.APIBaseClient.Types {
318-
if strings.ToLower(key) == strings.ToLower(resource) {
316+
if strings.EqualFold(key, resource) {
319317
return key, nil
320318
}
321319
}
@@ -417,13 +415,8 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
417415
return byName, nil
418416
}
419417

420-
func RandomName() string {
421-
return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
422-
}
423-
424418
// RandomLetters returns a string with random letters of length n
425419
func RandomLetters(n int) string {
426-
rand.Seed(time.Now().UnixNano())
427420
b := make([]byte, n)
428421
for i := range b {
429422
b[i] = letters[rand.Intn(len(letters))]
@@ -461,8 +454,7 @@ func SimpleFormat(values [][]string) (string, string) {
461454
func defaultAction(fn func(ctx *cli.Context) error) func(ctx *cli.Context) error {
462455
return func(ctx *cli.Context) error {
463456
if ctx.Bool("help") {
464-
cli.ShowAppHelp(ctx)
465-
return nil
457+
return cli.ShowAppHelp(ctx)
466458
}
467459
return fn(ctx)
468460
}
@@ -519,14 +511,6 @@ func SplitOnColon(s string) []string {
519511
return strings.Split(s, ":")
520512
}
521513

522-
func parseClusterAndProjectName(name string) (string, string, error) {
523-
parts := strings.SplitN(name, "/", 2)
524-
if len(parts) == 2 {
525-
return parts[0], parts[1], nil
526-
}
527-
return "", "", fmt.Errorf("Unable to extract clustername and projectname from [%s]", name)
528-
}
529-
530514
func parseClusterAndProjectID(id string) (string, string, error) {
531515
// Validate id
532516
// Examples:
@@ -543,7 +527,7 @@ func parseClusterAndProjectID(id string) (string, string, error) {
543527

544528
// Return a JSON blob of the file at path
545529
func readFileReturnJSON(path string) ([]byte, error) {
546-
file, err := ioutil.ReadFile(path)
530+
file, err := os.ReadFile(path)
547531
if err != nil {
548532
return []byte{}, err
549533
}
@@ -577,21 +561,6 @@ func hasPrefix(buf []byte, prefix []byte) bool {
577561
return bytes.HasPrefix(trim, prefix)
578562
}
579563

580-
func settingsToMap(client *cliclient.MasterClient) (map[string]string, error) {
581-
configMap := make(map[string]string)
582-
583-
settings, err := client.ManagementClient.Setting.List(baseListOpts())
584-
if err != nil {
585-
return nil, err
586-
}
587-
588-
for _, setting := range settings.Data {
589-
configMap[setting.Name] = setting.Value
590-
}
591-
592-
return configMap, nil
593-
}
594-
595564
// getClusterNames maps cluster ID to name and defaults to ID if name is blank
596565
func getClusterNames(ctx *cli.Context, c *cliclient.MasterClient) (map[string]string, error) {
597566
clusterNames := make(map[string]string)
@@ -617,15 +586,6 @@ func getClusterName(cluster *managementClient.Cluster) string {
617586
return cluster.ID
618587
}
619588

620-
func findStringInArray(s string, a []string) bool {
621-
for _, val := range a {
622-
if s == val {
623-
return true
624-
}
625-
}
626-
return false
627-
}
628-
629589
func createdTimetoHuman(t string) (string, error) {
630590
parsedTime, err := time.Parse(time.RFC3339, t)
631591
if err != nil {
@@ -652,9 +612,11 @@ func outputMembers(ctx *cli.Context, c *cliclient.MasterClient, members []manage
652612
if err != nil {
653613
return err
654614
}
615+
616+
memberType := fmt.Sprintf("%s %s", principal.Provider, principal.PrincipalType)
655617
writer.Write(&MemberData{
656618
Name: principal.Name,
657-
MemberType: strings.Title(fmt.Sprintf("%s %s", principal.Provider, principal.PrincipalType)),
619+
MemberType: cases.Title(language.Und).String(memberType),
658620
AccessType: m.AccessType,
659621
})
660622
}
@@ -699,15 +661,6 @@ func deleteMembersByNames(ctx *cli.Context, c *cliclient.MasterClient, members [
699661
return members, nil
700662
}
701663

702-
func tickerContext(ctx context.Context, duration time.Duration) <-chan time.Time {
703-
ticker := time.NewTicker(duration)
704-
go func() {
705-
<-ctx.Done()
706-
ticker.Stop()
707-
}()
708-
return ticker.C
709-
}
710-
711664
func ConfigDir() (string, error) {
712665
homeDir, err := os.UserHomeDir()
713666
if err != nil {

0 commit comments

Comments
 (0)