Skip to content

Commit

Permalink
Merge pull request #6177 from jandubois/revert-golintci-changes
Browse files Browse the repository at this point in the history
Revert "WSL-Helper: Satisfy the linter"
  • Loading branch information
ericpromislow authored Dec 11, 2023
2 parents 3cf6e03 + 43ae0e2 commit e2b9d7d
Show file tree
Hide file tree
Showing 34 changed files with 196 additions and 332 deletions.
5 changes: 0 additions & 5 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ dports
dri
DSL
Duex
dupl
dword
eagerzeroedthick
eastus
Expand Down Expand Up @@ -274,8 +273,6 @@ globalrole
globalrolebinding
Gluster
gname
gocritic
gocyclo
gofmt
goland
golangci
Expand Down Expand Up @@ -731,8 +728,6 @@ storybookjs
strem
stretchr
stringifying
structcheck
stylecheck
subshells
subvar
sumologic
Expand Down
10 changes: 2 additions & 8 deletions .github/workflows/config/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
linters-settings:
dogsled:
# syscall.Syscall() has three return values, and we often don't care about
# the results, especially on Windows.
max-blank-identifiers: 3
dupl:
threshold: 100
funlen:
Expand Down Expand Up @@ -40,8 +36,6 @@ linters-settings:
- '3'
ignored-functions:
- strings.SplitN
- os.FileMode # file permissions
- os.MkdirAll # file permissions

lll:
line-length: 140
Expand All @@ -57,6 +51,7 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- dogsled
- dupl
- errcheck
Expand Down Expand Up @@ -111,7 +106,6 @@ issues:
exclude-rules:
- path: _test\.go
linters:
- dupl # Ignore code duplication in tests
- gosec
- errcheck
- gocritic
- gosec
16 changes: 11 additions & 5 deletions .github/workflows/wsl-helper-build.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: 'Lint: WSL-Helper'
# This workflow builds the WSL Helper
name: 'GitHub Runner: WSL Helper'

on:
push:
paths: [ src/go/wsl-helper/** ]
pull_request:
paths: [ src/go/wsl-helper/** ]
#push:
# paths: [ src/go/wsl-helper/** ]
#pull_request:
# paths: [ src/go/wsl-helper/** ]
workflow_dispatch:

permissions:
Expand Down Expand Up @@ -39,6 +40,11 @@ jobs:
env:
GOOS: linux
CGO_ENABLED: '0'
- uses: actions/upload-artifact@v3
with:
name: wsl-helper-linux
path: src/go/wsl-helper/wsl-helper
if-no-files-found: error
- uses: golangci/[email protected]
# This is only safe because this workflow does not allow writing
with:
Expand Down
2 changes: 1 addition & 1 deletion src/go/wsl-helper/cmd/certificates_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ var certificatesCmd = &cobra.Command{
func init() {
certificatesCmd.Flags().StringSlice("stores", []string{"CA", "ROOT"}, "Certificate stores to enumerate")
certificatesViper.AutomaticEnv()
_ = certificatesViper.BindPFlags(certificatesCmd.Flags())
certificatesViper.BindPFlags(certificatesCmd.Flags())
rootCmd.AddCommand(certificatesCmd)
}
2 changes: 1 addition & 1 deletion src/go/wsl-helper/cmd/dockerproxy_kill_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ var dockerproxyKillCmd = &cobra.Command{

func init() {
dockerproxyKillViper.AutomaticEnv()
_ = dockerproxyKillViper.BindPFlags(dockerproxyKillCmd.Flags())
dockerproxyKillViper.BindPFlags(dockerproxyKillCmd.Flags())
dockerproxyCmd.AddCommand(dockerproxyKillCmd)
}
2 changes: 1 addition & 1 deletion src/go/wsl-helper/cmd/dockerproxy_serve_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func init() {
dockerproxyServeCmd.Flags().String("endpoint", platform.DefaultEndpoint, "Endpoint to listen on")
dockerproxyServeCmd.Flags().String("proxy-endpoint", defaultProxyEndpoint, "Endpoint dockerd is listening on")
dockerproxyServeViper.AutomaticEnv()
_ = dockerproxyServeViper.BindPFlags(dockerproxyServeCmd.Flags())
dockerproxyServeViper.BindPFlags(dockerproxyServeCmd.Flags())
dockerproxyCmd.AddCommand(dockerproxyServeCmd)
}
2 changes: 1 addition & 1 deletion src/go/wsl-helper/cmd/dockerproxy_serve_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func init() {
dockerproxyServeCmd.Flags().String("endpoint", platform.DefaultEndpoint, "Endpoint to listen on")
dockerproxyServeCmd.Flags().Uint32("port", dockerproxy.DefaultPort, "Vsock port docker is listening on")
dockerproxyServeViper.AutomaticEnv()
_ = dockerproxyServeViper.BindPFlags(dockerproxyServeCmd.Flags())
dockerproxyServeViper.BindPFlags(dockerproxyServeCmd.Flags())
dockerproxyCmd.AddCommand(dockerproxyServeCmd)
}
2 changes: 1 addition & 1 deletion src/go/wsl-helper/cmd/dockerproxy_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func init() {
dockerproxyStartCmd.Flags().Uint32("port", dockerproxy.DefaultPort, "Vsock port to listen on")
dockerproxyStartCmd.Flags().String("endpoint", defaultProxyEndpoint, "Dockerd socket endpoint")
dockerproxyStartViper.AutomaticEnv()
_ = dockerproxyStartViper.BindPFlags(dockerproxyStartCmd.Flags())
dockerproxyStartViper.BindPFlags(dockerproxyStartCmd.Flags())
dockerproxyCmd.AddCommand(dockerproxyStartCmd)
}
28 changes: 11 additions & 17 deletions src/go/wsl-helper/cmd/k3s_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ import (
"gopkg.in/yaml.v3"
)

type kubeConfigCluster struct {
Cluster struct {
Server string
Extras map[string]interface{} `yaml:",inline"`
} `yaml:"cluster"`
Name string `yaml:"name"`
Extras map[string]interface{} `yaml:",inline"`
}
type kubeConfig struct {
Clusters []kubeConfigCluster `yaml:"clusters"`
Clusters []struct {
Cluster struct {
Server string
Extras map[string]interface{} `yaml:",inline"`
} `yaml:"cluster"`
Name string `yaml:"name"`
Extras map[string]interface{} `yaml:",inline"`
} `yaml:"clusters"`
Contexts []struct {
Name string `yaml:"name"`
Extras map[string]interface{} `yaml:",inline"`
Expand All @@ -54,11 +53,6 @@ type kubeConfig struct {
Extras map[string]interface{} `yaml:",inline"`
}

const (
// kubeconfigExistTimeout is the time we wait for kubeconfig to appear.
kubeconfigExistTimeout = 10 * time.Second
)

var (
k3sKubeconfigViper = viper.New()
rdNetworking bool
Expand Down Expand Up @@ -87,11 +81,11 @@ var k3sKubeconfigCmd = &cobra.Command{
}
}()
var err error
timeout := time.After(kubeconfigExistTimeout)
timeout := time.After(10 * time.Second)
var configFile *os.File
select {
case <-timeout:
return fmt.Errorf("timed out waiting for k3s kubeconfig to exist")
return fmt.Errorf("Timed out waiting for k3s kubeconfig to exist")
case configFile = <-ch:
break
}
Expand Down Expand Up @@ -178,6 +172,6 @@ func init() {
k3sKubeconfigCmd.Flags().String("k3sconfig", "/etc/rancher/k3s/k3s.yaml", "Path to k3s kubeconfig")
k3sKubeconfigCmd.Flags().BoolVar(&rdNetworking, "rd-networking", false, "Enable the experimental Rancher Desktop Networking")
k3sKubeconfigViper.AutomaticEnv()
_ = k3sKubeconfigViper.BindPFlags(k3sKubeconfigCmd.Flags())
k3sKubeconfigViper.BindPFlags(k3sKubeconfigCmd.Flags())
k3sCmd.AddCommand(k3sKubeconfigCmd)
}
2 changes: 1 addition & 1 deletion src/go/wsl-helper/cmd/kill_process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ var killProcessCmd = &cobra.Command{
func init() {
killProcessCmd.Flags().Int("pid", 0, "PID of process to kill")
killProcessViper.AutomaticEnv()
_ = killProcessViper.BindPFlags(killProcessCmd.Flags())
killProcessViper.BindPFlags(killProcessCmd.Flags())
rootCmd.AddCommand(killProcessCmd)
}
52 changes: 25 additions & 27 deletions src/go/wsl-helper/cmd/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ var kubeconfigCmd = &cobra.Command{

if winConfigPath == "" {
//lint:ignore ST1005 The capitalization is for a proper noun.
//nolint:stylecheck // The capitalization is for a proper noun.
return errors.New("Windows kubeconfig not supplied")
}

Expand Down Expand Up @@ -87,9 +86,9 @@ var kubeconfigCmd = &cobra.Command{
return err
}

cleanConfig := removeExistingRDConfig(rdCluster, linuxConfig)
cleanConfig := removeExistingRDConfig(rdCluster, &linuxConfig)

kubeConfig, err := updateKubeConfig(winConfig, cleanConfig, rdNetworking)
kubeConfig, err := updateKubeConfig(winConfig, *cleanConfig, rdNetworking)
if err != nil {
return fmt.Errorf("failed to construct kubeconfig: %w", err)
}
Expand All @@ -111,32 +110,29 @@ var kubeconfigCmd = &cobra.Command{
},
}

func readKubeConfig(configPath string) (*kubeConfig, error) {
func readKubeConfig(configPath string) (kubeConfig, error) {
var config kubeConfig
configFile, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("could not open kubeconfig file %s: %w", configPath, err)
return config, fmt.Errorf("could not open kubeconfig file %s: %w", configPath, err)
}
defer configFile.Close()
err = yaml.NewDecoder(configFile).Decode(&config)
if err != nil {
return nil, fmt.Errorf("could not read kubeconfig %s: %w", configPath, err)
return config, fmt.Errorf("could not read kubeconfig %s: %w", configPath, err)
}

return &config, nil
return config, nil
}

// updateKubeConfig updates the Linux-side kubeconfig with the rancher-desktop
// cluster read from Windows (ignoring all other clusters from the Windows side).
// While doing so, it ensures that the server address will be correct.
// It updates the Linux kubeconfig in-place and returns it.
func updateKubeConfig(winConfig, linuxConfig *kubeConfig, rdNetworking bool) (*kubeConfig, error) {
// We act on a copy of the Windows config to avoid mutating it.
winConfigCopy := *winConfig
winConfigCopy.Clusters = append([]kubeConfigCluster{}, winConfig.Clusters...)
// updateKubeConfig reads the kube config from windows side it also
// modifies the cluster's server host to an appropriate address.
// It then merges the config with an existing configuration on
// users distro and returns the merged config.
func updateKubeConfig(winConfig, linuxConfig kubeConfig, rdNetworking bool) (kubeConfig, error) {
for clusterIdx, cluster := range winConfig.Clusters {
// Ignore any non rancher-desktop clusters
if winConfigCopy.Clusters[clusterIdx].Name != rdCluster {
if winConfig.Clusters[clusterIdx].Name != rdCluster {
continue
}
server, err := url.Parse(cluster.Cluster.Server)
Expand All @@ -148,7 +144,7 @@ func updateKubeConfig(winConfig, linuxConfig *kubeConfig, rdNetworking bool) (*k
if !rdNetworking {
ip, err := getClusterIP()
if err != nil {
return nil, err
return winConfig, err
}

host = ip.String()
Expand All @@ -157,16 +153,21 @@ func updateKubeConfig(winConfig, linuxConfig *kubeConfig, rdNetworking bool) (*k
host = net.JoinHostPort(host, server.Port())
}
server.Host = host
winConfigCopy.Clusters[clusterIdx].Cluster.Server = server.String()
winConfig.Clusters[clusterIdx].Cluster.Server = server.String()
}
return mergeKubeConfigs(&winConfigCopy, linuxConfig), nil
return mergeKubeConfigs(winConfig, linuxConfig), nil
}

// removeExistingRDConfig removes the rancher-desktop configuration from the
// passed in kubeconfig in-place, and returns the modified input.
func removeExistingRDConfig(name string, config *kubeConfig) *kubeConfig {
// Remove clusters with the specified name
var filteredClusters []kubeConfigCluster
var filteredClusters []struct {
Cluster struct {
Server string
Extras map[string]interface{} `yaml:",inline"`
} `yaml:"cluster"`
Name string `yaml:"name"`
Extras map[string]interface{} `yaml:",inline"`
}
for _, cluster := range config.Clusters {
if cluster.Name != name {
filteredClusters = append(filteredClusters, cluster)
Expand Down Expand Up @@ -201,10 +202,7 @@ func removeExistingRDConfig(name string, config *kubeConfig) *kubeConfig {
return config
}

// mergeKubeConfig updates the Linux kubeconfig with the Rancher Desktop
// cluster configuration read from the Windows kubeconfig, returning the
// Linux kubeconfig that has been updated in-place.
func mergeKubeConfigs(winConfig, linuxConfig *kubeConfig) *kubeConfig {
func mergeKubeConfigs(winConfig, linuxConfig kubeConfig) kubeConfig {
for _, ctx := range winConfig.Clusters {
if ctx.Name == rdCluster {
linuxConfig.Clusters = append(linuxConfig.Clusters, ctx)
Expand All @@ -230,6 +228,6 @@ func init() {
kubeconfigCmd.PersistentFlags().String("kubeconfig", "", "Path to Windows kubeconfig, in /mnt/... form.")
kubeconfigCmd.Flags().BoolVar(&rdNetworking, "rd-networking", false, "Enable the experimental Rancher Desktop Networking")
kubeconfigViper.AutomaticEnv()
_ = kubeconfigViper.BindPFlags(kubeconfigCmd.PersistentFlags())
kubeconfigViper.BindPFlags(kubeconfigCmd.PersistentFlags())
rootCmd.AddCommand(kubeconfigCmd)
}
4 changes: 2 additions & 2 deletions src/go/wsl-helper/cmd/wsl_integration_docker_plugin_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ var wslIntegrationDockerPluginCmd = &cobra.Command{
func init() {
wslIntegrationDockerPluginCmd.Flags().String("plugin", "", "Full path to plugin")
wslIntegrationDockerPluginCmd.Flags().Bool("state", false, "Desired state")
_ = wslIntegrationDockerPluginCmd.MarkFlagRequired("plugin")
wslIntegrationDockerPluginCmd.MarkFlagRequired("plugin")
wslIntegrationDockerPluginViper.AutomaticEnv()
_ = wslIntegrationDockerPluginViper.BindPFlags(wslIntegrationDockerPluginCmd.Flags())
wslIntegrationDockerPluginViper.BindPFlags(wslIntegrationDockerPluginCmd.Flags())
wslIntegrationCmd.AddCommand(wslIntegrationDockerPluginCmd)
}
4 changes: 2 additions & 2 deletions src/go/wsl-helper/cmd/wsl_integration_state_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ var wslIntegrationStateCmd = &cobra.Command{

func init() {
wslIntegrationStateCmd.Flags().Var(&enumValue{val: "show", allowed: []string{"show", "set", "delete"}}, "mode", "Operation mode")
_ = wslIntegrationStateCmd.MarkFlagRequired("mode")
wslIntegrationStateCmd.MarkFlagRequired("mode")
wslIntegrationStateViper.AutomaticEnv()
_ = wslIntegrationStateViper.BindPFlags(wslIntegrationStateCmd.Flags())
wslIntegrationStateViper.BindPFlags(wslIntegrationStateCmd.Flags())
wslIntegrationCmd.AddCommand(wslIntegrationStateCmd)
}
2 changes: 1 addition & 1 deletion src/go/wsl-helper/pkg/certificates/certificates_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetSystemCertificates(storeName string) (<-chan Entry, error) {
ch := make(chan Entry)
go func() {
defer close(ch)
defer windows.CertCloseStore(store, 0) //nolint:errcheck // We can't do anything about it if this fails
defer windows.CertCloseStore(store, 0)
var certCtx *windows.CertContext
var err error
for {
Expand Down
Loading

0 comments on commit e2b9d7d

Please sign in to comment.