Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
chore(release): add missing cherry picks (#4932)
Browse files Browse the repository at this point in the history
* chore(cmd/cli): Reduce cyclomatic complexity of uninstall command (#4825)

* chore(cmd/cli): Reduce cyclomatic complexity of uninstall command

Separate code into functions to reduce the cyclomatic complexity
And adds unit tests to functions created
Helps unblock #4555

Signed-off-by: Shalier Xia <[email protected]>
(cherry picked from commit d5d3a25)

* Golang errors pkg (#4904)

This PR partially resolves #4524

* Removes "github.com/pkg/errors" package from the repo
* Wrap errors wherever possible rather than using the string format of the error as part of the message

Signed-off-by: Allen Leigh <[email protected]>
(cherry picked from commit 8030047)

* fix golints and security scan issues (#4915)

fix golints and security for:

1. G112 (ReadHeaderTimeout)
2. prometheus client_go version pinned to bad version

Signed-off-by: Sean Teeling <[email protected]>

(cherry picked from commit f768f64)

* Fix lints by removing superfluous var type from var instantiation (#4917)

Remove unnecessary var types during instantiation to fix lints

Signed-off-by: Sean Teeling <[email protected]>
(cherry picked from commit 9e9f712)

Co-authored-by: Shalier Xia <[email protected]>
Co-authored-by: allenlsy <[email protected]>
Co-authored-by: steeling <[email protected]>
  • Loading branch information
4 people authored Jul 20, 2022
1 parent 929c114 commit 4c832d1
Show file tree
Hide file tree
Showing 117 changed files with 814 additions and 600 deletions.
9 changes: 4 additions & 5 deletions cmd/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
helm "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -86,12 +85,12 @@ func newInstallCmd(config *helm.Configuration, out io.Writer) *cobra.Command {
RunE: func(_ *cobra.Command, args []string) error {
kubeconfig, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
inst.clientSet = clientset
return inst.run(config)
Expand Down Expand Up @@ -156,7 +155,7 @@ func (i *installCmd) loadOSMChart() error {
}

if err != nil {
return fmt.Errorf("error loading chart for installation: %s", err)
return fmt.Errorf("error loading chart for installation: %w", err)
}

return nil
Expand All @@ -166,7 +165,7 @@ func (i *installCmd) resolveValues() (map[string]interface{}, error) {
finalValues := map[string]interface{}{}

if err := parseVal(i.setOptions, finalValues); err != nil {
return nil, errors.Wrap(err, "invalid format for --set")
return nil, fmt.Errorf("invalid format for --set: %w", err)
}

valuesConfig := []string{
Expand Down
5 changes: 2 additions & 3 deletions cmd/cli/mesh_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -48,12 +47,12 @@ func newMeshList(out io.Writer) *cobra.Command {
RunE: func(_ *cobra.Command, args []string) error {
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}
listCmd.config = config
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
listCmd.clientSet = clientset
return listCmd.run()
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/mesh_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
helm "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -120,7 +119,7 @@ func (u *meshUpgradeCmd) resolveValues() (map[string]interface{}, error) {
vals := make(map[string]interface{})
for _, val := range u.setOptions {
if err := strvals.ParseInto(val, vals); err != nil {
return nil, errors.Wrap(err, "invalid format for --set")
return nil, fmt.Errorf("invalid format for --set: %w", err)
}
}
return vals, nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/cli/metrics.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"io"

mapset "github.com/deckarep/golang-set"
"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -37,11 +37,11 @@ func isMonitoredNamespace(ns corev1.Namespace, meshList mapset.Set) (bool, error
return false, nil
}
if meshName == "" {
return false, errors.Errorf("Label %q on namespace %q cannot be empty",
return false, fmt.Errorf("Label %q on namespace %q cannot be empty",
constants.OSMKubeResourceMonitorAnnotation, ns.Name)
}
if !meshList.Contains(meshName) {
return false, errors.Errorf("Invalid mesh name %q used with label %q on namespace %q, must be one of %v",
return false, fmt.Errorf("Invalid mesh name %q used with label %q on namespace %q, must be one of %v",
meshName, constants.OSMKubeResourceMonitorAnnotation, ns.Name, meshList.ToSlice())
}

Expand Down
13 changes: 6 additions & 7 deletions cmd/cli/metrics_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -39,12 +38,12 @@ func newMetricsDisable(out io.Writer) *cobra.Command {
RunE: func(_ *cobra.Command, args []string) error {
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
disableCmd.clientSet = clientset
return disableCmd.run()
Expand All @@ -66,7 +65,7 @@ func (cmd *metricsDisableCmd) run() error {

namespace, err := cmd.clientSet.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{})
if err != nil {
return errors.Errorf("Failed to retrieve namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to retrieve namespace [%s]: %w", ns, err)
}

// Check if the namespace belongs to a mesh, if not return an error
Expand All @@ -75,7 +74,7 @@ func (cmd *metricsDisableCmd) run() error {
return err
}
if !monitored {
return errors.Errorf("Namespace [%s] does not belong to a mesh, missing annotation %q",
return fmt.Errorf("Namespace [%s] does not belong to a mesh, missing annotation %q",
ns, constants.OSMKubeResourceMonitorAnnotation)
}

Expand All @@ -91,12 +90,12 @@ func (cmd *metricsDisableCmd) run() error {

_, err = cmd.clientSet.CoreV1().Namespaces().Patch(ctx, ns, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
if err != nil {
return errors.Errorf("Failed to disable metrics in namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to disable metrics in namespace [%s]: %w", ns, err)
}

// Disable metrics on pods belonging to this namespace
if err := cmd.disableMetricsForPods(ns); err != nil {
return errors.Errorf("Failed to disable metrics for existing pod in namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to disable metrics for existing pod in namespace [%s]: %w", ns, err)
}

fmt.Fprintf(cmd.out, "Metrics successfully disabled in namespace [%s]\n", ns)
Expand Down
13 changes: 6 additions & 7 deletions cmd/cli/metrics_enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -42,12 +41,12 @@ func newMetricsEnable(out io.Writer) *cobra.Command {
RunE: func(_ *cobra.Command, args []string) error {
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
enableCmd.clientSet = clientset
return enableCmd.run()
Expand All @@ -71,7 +70,7 @@ func (cmd *metricsEnableCmd) run() error {

namespace, err := cmd.clientSet.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{})
if err != nil {
return errors.Errorf("Failed to retrieve namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to retrieve namespace [%s]: %w", ns, err)
}

// Check if the namespace belongs to a mesh, if not return an error
Expand All @@ -80,7 +79,7 @@ func (cmd *metricsEnableCmd) run() error {
return err
}
if !monitored {
return errors.Errorf("Namespace [%s] does not belong to a mesh, missing annotation %q",
return fmt.Errorf("Namespace [%s] does not belong to a mesh, missing annotation %q",
ns, constants.OSMKubeResourceMonitorAnnotation)
}

Expand All @@ -97,13 +96,13 @@ func (cmd *metricsEnableCmd) run() error {

_, err = cmd.clientSet.CoreV1().Namespaces().Patch(ctx, ns, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
if err != nil {
return errors.Errorf("Failed to enable metrics in namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to enable metrics in namespace [%s]: %w", ns, err)
}

// For existing pods in this namespace that are already part of the mesh, add the prometheus
// scraping annotations.
if err := cmd.enableMetricsForPods(ns); err != nil {
return errors.Errorf("Failed to enable metrics for existing pod in namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to enable metrics for existing pod in namespace [%s]: %w", ns, err)
}

fmt.Fprintf(cmd.out, "Metrics successfully enabled in namespace [%s]\n", ns)
Expand Down
15 changes: 7 additions & 8 deletions cmd/cli/namespace_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -62,12 +61,12 @@ func newNamespaceAdd(out io.Writer) *cobra.Command {
namespaceAdd.namespaces = args
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
namespaceAdd.clientSet = clientset
return namespaceAdd.run()
Expand Down Expand Up @@ -95,7 +94,7 @@ func (a *namespaceAddCmd) run() error {
return err
}
if !exists {
return errors.Errorf("mesh [%s] does not exist, please specify another mesh using --mesh-name or create a new mesh", a.meshName)
return fmt.Errorf("mesh [%s] does not exist, please specify another mesh using --mesh-name or create a new mesh", a.meshName)
}

deploymentsClient := a.clientSet.AppsV1().Deployments(ns)
Expand All @@ -115,7 +114,7 @@ func (a *namespaceAddCmd) run() error {
// if the namespace is already a part of the mesh then don't add it again
namespace, err := a.clientSet.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{})
if err != nil {
return errors.Errorf("Could not add namespace [%s] to mesh [%s]: %v", ns, a.meshName, err)
return fmt.Errorf("Could not add namespace [%s] to mesh [%s]: %w", ns, a.meshName, err)
}
meshName := namespace.Labels[constants.OSMKubeResourceMonitorAnnotation]
if a.meshName == meshName {
Expand All @@ -125,7 +124,7 @@ func (a *namespaceAddCmd) run() error {

// if ignore label exits don`t add namespace
if val, ok := namespace.ObjectMeta.Labels[constants.IgnoreLabel]; ok && val == trueValue {
return errors.Errorf("Cannot add ignored namespace")
return fmt.Errorf("Cannot add ignored namespace")
}

var patch string
Expand Down Expand Up @@ -161,7 +160,7 @@ func (a *namespaceAddCmd) run() error {

_, err = a.clientSet.CoreV1().Namespaces().Patch(ctx, ns, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
if err != nil {
return errors.Errorf("Could not add namespace [%s] to mesh [%s]: %v", ns, a.meshName, err)
return fmt.Errorf("Could not add namespace [%s] to mesh [%s]: %w", ns, a.meshName, err)
}

_, _ = fmt.Fprintf(a.out, "Namespace [%s] successfully added to mesh [%s]\n", ns, a.meshName)
Expand All @@ -181,7 +180,7 @@ func meshExists(clientSet kubernetes.Interface, meshName string) (bool, error) {
}
osmControllerDeployments, err := deploymentsClient.List(context.TODO(), listOptions)
if err != nil {
return false, errors.Errorf("Cannot obtain information about the mesh [%s]: [%v]", meshName, err)
return false, fmt.Errorf("Cannot obtain information about the mesh [%s]: [%w]", meshName, err)
}
// the mesh is present if there are osm controllers for the mesh
return len(osmControllerDeployments.Items) != 0, nil
Expand Down
9 changes: 4 additions & 5 deletions cmd/cli/namespace_ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -43,12 +42,12 @@ func newNamespaceIgnore(out io.Writer) *cobra.Command {
ignoreCmd.namespaces = args
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
ignoreCmd.clientSet = clientset
return ignoreCmd.run()
Expand All @@ -66,7 +65,7 @@ func (cmd *namespaceIgnoreCmd) run() error {
defer cancel()

if _, err := cmd.clientSet.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{}); err != nil {
return errors.Errorf("Failed to retrieve namespace [%s]: %v", ns, err)
return fmt.Errorf("Failed to retrieve namespace [%s]: %w", ns, err)
}

// Patch the namespace with ignore label
Expand All @@ -81,7 +80,7 @@ func (cmd *namespaceIgnoreCmd) run() error {

_, err := cmd.clientSet.CoreV1().Namespaces().Patch(ctx, ns, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
if err != nil {
return errors.Errorf("Failed to configure namespace [%s] to be ignored: %v", ns, err)
return fmt.Errorf("Failed to configure namespace [%s] to be ignored: %w", ns, err)
}

fmt.Fprintf(cmd.out, "Successfully configured namespace [%s] to be ignored\n", ns)
Expand Down
7 changes: 3 additions & 4 deletions cmd/cli/namespace_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -41,12 +40,12 @@ func newNamespaceList(out io.Writer) *cobra.Command {

config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Errorf("Error fetching kubeconfig: %s", err)
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
namespaceList.clientSet = clientset
return namespaceList.run()
Expand All @@ -63,7 +62,7 @@ func newNamespaceList(out io.Writer) *cobra.Command {
func (l *namespaceListCmd) run() error {
namespaces, err := selectNamespacesMonitoredByMesh(l.meshName, l.clientSet)
if err != nil {
return errors.Errorf("Could not list namespaces related to osm [%s]: %v", l.meshName, err)
return fmt.Errorf("Could not list namespaces related to osm [%s]: %w", l.meshName, err)
}

if len(namespaces.Items) == 0 {
Expand Down
Loading

0 comments on commit 4c832d1

Please sign in to comment.