Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added linter and removed deprecated and unused packages #357

Merged
merged 14 commits into from
Apr 10, 2024
Merged
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on:
push:
pull_request:

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false

- name: Lint
uses: golangci/golangci-lint-action@v4

- name: Test
run: go test -v ./...

- name: Build
run: make build
8 changes: 0 additions & 8 deletions .golangci.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"linters": {
"disable-all": true,
"enable": [
"govet",
"golint",
"goimports"
]
},
"run": {
"timeout": "10m"
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -799,7 +798,7 @@ func walkTemplateDirectory(templatePath string) (string, map[string]string, erro
return nil
}
version := &chartVersion{}
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return err
}
Expand All @@ -816,7 +815,7 @@ func walkTemplateDirectory(templatePath string) (string, map[string]string, erro
if info.IsDir() {
return nil
}
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -1254,7 +1253,7 @@ func parseAnswersFile(location string, answers map[string]string) error {
}

func parseFile(location string) (map[string]interface{}, error) {
bytes, err := ioutil.ReadFile(location)
bytes, err := os.ReadFile(location)
if err != nil {
return nil, err
}
Expand Down
28 changes: 21 additions & 7 deletions cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -255,9 +256,14 @@ func clusterCreate(ctx *cli.Context) error {
return err
}

if ctx.String("k8s-version") != "" {
k8sVersions := getClusterK8sOptions(c)
if ok := findStringInArray(ctx.String("k8s-version"), k8sVersions); !ok {
k8sVersion := ctx.String("k8s-version")
if k8sVersion != "" {
k8sVersions, err := getClusterK8sOptions(c)
if err != nil {
return err
}

if slices.Contains(k8sVersions, k8sVersion) {
fmt.Println("Available Kubernetes versions:")
for _, val := range k8sVersions {
fmt.Println(val)
Expand Down Expand Up @@ -742,16 +748,24 @@ func getClusterPods(cluster managementClient.Cluster) string {
return cluster.Requested["pods"] + "/" + cluster.Allocatable["pods"]
}

func getClusterK8sOptions(c *cliclient.MasterClient) []string {
func getClusterK8sOptions(c *cliclient.MasterClient) ([]string, error) {
var options []string
setting, _ := c.ManagementClient.Setting.ByID("k8s-version-to-images")

setting, err := c.ManagementClient.Setting.ByID("k8s-version-to-images")
if err != nil {
return nil, err
}

var objmap map[string]*json.RawMessage
err = json.Unmarshal([]byte(setting.Value), &objmap)
if err != nil {
return nil, err
}

json.Unmarshal([]byte(setting.Value), &objmap)
for key := range objmap {
options = append(options, key)
}
return options
return options, nil
}

func getClusterConfig(ctx *cli.Context) (*managementClient.Cluster, error) {
Expand Down
71 changes: 12 additions & 59 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package cmd
import (
"bufio"
"bytes"
"context"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/url"
"os"
Expand All @@ -22,7 +20,6 @@ import (
"time"
"unicode"

"github.com/docker/docker/pkg/namesgenerator"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/rancher/cli/cliclient"
Expand All @@ -33,6 +30,8 @@ import (
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/client-go/tools/clientcmd/api"
)

Expand All @@ -43,7 +42,6 @@ const (
)

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

focusedServer := cf.FocusedServer()
kubeConfig, _ := focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())]
kubeConfig := focusedServer.KubeConfigs[fmt.Sprintf(kubeConfigKeyFormat, user, focusedServer.FocusedCluster())]
return kubeConfig, nil
}

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

func loadAndVerifyCert(path string) (string, error) {
caCert, err := ioutil.ReadFile(path)
caCert, err := os.ReadFile(path)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -301,21 +299,21 @@ func GetClient(ctx *cli.Context) (*cliclient.MasterClient, error) {
func GetResourceType(c *cliclient.MasterClient, resource string) (string, error) {
if c.ManagementClient != nil {
for key := range c.ManagementClient.APIBaseClient.Types {
if strings.ToLower(key) == strings.ToLower(resource) {
if strings.EqualFold(key, resource) {
return key, nil
}
}
}
if c.ProjectClient != nil {
for key := range c.ProjectClient.APIBaseClient.Types {
if strings.ToLower(key) == strings.ToLower(resource) {
if strings.EqualFold(key, resource) {
return key, nil
}
}
}
if c.ClusterClient != nil {
for key := range c.ClusterClient.APIBaseClient.Types {
if strings.ToLower(key) == strings.ToLower(resource) {
if strings.EqualFold(key, resource) {
return key, nil
}
}
Expand Down Expand Up @@ -417,13 +415,8 @@ func Lookup(c *cliclient.MasterClient, name string, types ...string) (*ntypes.Re
return byName, nil
}

func RandomName() string {
return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
}

// RandomLetters returns a string with random letters of length n
func RandomLetters(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
Expand Down Expand Up @@ -461,8 +454,7 @@ func SimpleFormat(values [][]string) (string, string) {
func defaultAction(fn func(ctx *cli.Context) error) func(ctx *cli.Context) error {
return func(ctx *cli.Context) error {
if ctx.Bool("help") {
cli.ShowAppHelp(ctx)
return nil
return cli.ShowAppHelp(ctx)
}
return fn(ctx)
}
Expand Down Expand Up @@ -519,14 +511,6 @@ func SplitOnColon(s string) []string {
return strings.Split(s, ":")
}

func parseClusterAndProjectName(name string) (string, string, error) {
parts := strings.SplitN(name, "/", 2)
if len(parts) == 2 {
return parts[0], parts[1], nil
}
return "", "", fmt.Errorf("Unable to extract clustername and projectname from [%s]", name)
}

func parseClusterAndProjectID(id string) (string, string, error) {
// Validate id
// Examples:
Expand All @@ -543,7 +527,7 @@ func parseClusterAndProjectID(id string) (string, string, error) {

// Return a JSON blob of the file at path
func readFileReturnJSON(path string) ([]byte, error) {
file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)
if err != nil {
return []byte{}, err
}
Expand Down Expand Up @@ -577,21 +561,6 @@ func hasPrefix(buf []byte, prefix []byte) bool {
return bytes.HasPrefix(trim, prefix)
}

func settingsToMap(client *cliclient.MasterClient) (map[string]string, error) {
configMap := make(map[string]string)

settings, err := client.ManagementClient.Setting.List(baseListOpts())
if err != nil {
return nil, err
}

for _, setting := range settings.Data {
configMap[setting.Name] = setting.Value
}

return configMap, nil
}

// getClusterNames maps cluster ID to name and defaults to ID if name is blank
func getClusterNames(ctx *cli.Context, c *cliclient.MasterClient) (map[string]string, error) {
clusterNames := make(map[string]string)
Expand All @@ -617,15 +586,6 @@ func getClusterName(cluster *managementClient.Cluster) string {
return cluster.ID
}

func findStringInArray(s string, a []string) bool {
for _, val := range a {
if s == val {
return true
}
}
return false
}

func createdTimetoHuman(t string) (string, error) {
parsedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
Expand All @@ -652,9 +612,11 @@ func outputMembers(ctx *cli.Context, c *cliclient.MasterClient, members []manage
if err != nil {
return err
}

memberType := fmt.Sprintf("%s %s", principal.Provider, principal.PrincipalType)
writer.Write(&MemberData{
Name: principal.Name,
MemberType: strings.Title(fmt.Sprintf("%s %s", principal.Provider, principal.PrincipalType)),
MemberType: cases.Title(language.Und).String(memberType),
AccessType: m.AccessType,
})
}
Expand Down Expand Up @@ -699,15 +661,6 @@ func deleteMembersByNames(ctx *cli.Context, c *cliclient.MasterClient, members [
return members, nil
}

func tickerContext(ctx context.Context, duration time.Duration) <-chan time.Time {
ticker := time.NewTicker(duration)
go func() {
<-ctx.Done()
ticker.Stop()
}()
return ticker.C
}

func ConfigDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down
36 changes: 0 additions & 36 deletions cmd/delete.go

This file was deleted.

3 changes: 1 addition & 2 deletions cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -98,7 +97,7 @@ func runKubectl(ctx *cli.Context) error {
}
}

tmpfile, err := ioutil.TempFile("", "rancher-")
tmpfile, err := os.CreateTemp("", "rancher-")
if err != nil {
return err
}
Expand Down
Loading
Loading