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

Commit 4c832d1

Browse files
nojnhuhshalierallenlsysteeling
authored
chore(release): add missing cherry picks (#4932)
* 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]>
1 parent 929c114 commit 4c832d1

File tree

117 files changed

+814
-600
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+814
-600
lines changed

cmd/cli/install.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"time"
1111

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

9291
clientset, err := kubernetes.NewForConfig(kubeconfig)
9392
if err != nil {
94-
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
93+
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
9594
}
9695
inst.clientSet = clientset
9796
return inst.run(config)
@@ -156,7 +155,7 @@ func (i *installCmd) loadOSMChart() error {
156155
}
157156

158157
if err != nil {
159-
return fmt.Errorf("error loading chart for installation: %s", err)
158+
return fmt.Errorf("error loading chart for installation: %w", err)
160159
}
161160

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

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

172171
valuesConfig := []string{

cmd/cli/mesh_list.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"io"
66

7-
"github.com/pkg/errors"
87
"github.com/spf13/cobra"
98
"k8s.io/client-go/kubernetes"
109
"k8s.io/client-go/rest"
@@ -48,12 +47,12 @@ func newMeshList(out io.Writer) *cobra.Command {
4847
RunE: func(_ *cobra.Command, args []string) error {
4948
config, err := settings.RESTClientGetter().ToRESTConfig()
5049
if err != nil {
51-
return errors.Errorf("Error fetching kubeconfig: %s", err)
50+
return fmt.Errorf("Error fetching kubeconfig: %w", err)
5251
}
5352
listCmd.config = config
5453
clientset, err := kubernetes.NewForConfig(config)
5554
if err != nil {
56-
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
55+
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
5756
}
5857
listCmd.clientSet = clientset
5958
return listCmd.run()

cmd/cli/mesh_upgrade.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"time"
88

9-
"github.com/pkg/errors"
109
"github.com/spf13/cobra"
1110
helm "helm.sh/helm/v3/pkg/action"
1211
"helm.sh/helm/v3/pkg/chart"
@@ -120,7 +119,7 @@ func (u *meshUpgradeCmd) resolveValues() (map[string]interface{}, error) {
120119
vals := make(map[string]interface{})
121120
for _, val := range u.setOptions {
122121
if err := strvals.ParseInto(val, vals); err != nil {
123-
return nil, errors.Wrap(err, "invalid format for --set")
122+
return nil, fmt.Errorf("invalid format for --set: %w", err)
124123
}
125124
}
126125
return vals, nil

cmd/cli/metrics.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"io"
56

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

@@ -37,11 +37,11 @@ func isMonitoredNamespace(ns corev1.Namespace, meshList mapset.Set) (bool, error
3737
return false, nil
3838
}
3939
if meshName == "" {
40-
return false, errors.Errorf("Label %q on namespace %q cannot be empty",
40+
return false, fmt.Errorf("Label %q on namespace %q cannot be empty",
4141
constants.OSMKubeResourceMonitorAnnotation, ns.Name)
4242
}
4343
if !meshList.Contains(meshName) {
44-
return false, errors.Errorf("Invalid mesh name %q used with label %q on namespace %q, must be one of %v",
44+
return false, fmt.Errorf("Invalid mesh name %q used with label %q on namespace %q, must be one of %v",
4545
meshName, constants.OSMKubeResourceMonitorAnnotation, ns.Name, meshList.ToSlice())
4646
}
4747

cmd/cli/metrics_disable.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"strings"
88

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

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

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

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

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

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

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

102101
fmt.Fprintf(cmd.out, "Metrics successfully disabled in namespace [%s]\n", ns)

cmd/cli/metrics_enable.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"strings"
88

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

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

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

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

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

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

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

109108
fmt.Fprintf(cmd.out, "Metrics successfully enabled in namespace [%s]\n", ns)

cmd/cli/namespace_add.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77

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

6867
clientset, err := kubernetes.NewForConfig(config)
6968
if err != nil {
70-
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
69+
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
7170
}
7271
namespaceAdd.clientSet = clientset
7372
return namespaceAdd.run()
@@ -95,7 +94,7 @@ func (a *namespaceAddCmd) run() error {
9594
return err
9695
}
9796
if !exists {
98-
return errors.Errorf("mesh [%s] does not exist, please specify another mesh using --mesh-name or create a new mesh", a.meshName)
97+
return fmt.Errorf("mesh [%s] does not exist, please specify another mesh using --mesh-name or create a new mesh", a.meshName)
9998
}
10099

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

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

131130
var patch string
@@ -161,7 +160,7 @@ func (a *namespaceAddCmd) run() error {
161160

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

167166
_, _ = fmt.Fprintf(a.out, "Namespace [%s] successfully added to mesh [%s]\n", ns, a.meshName)
@@ -181,7 +180,7 @@ func meshExists(clientSet kubernetes.Interface, meshName string) (bool, error) {
181180
}
182181
osmControllerDeployments, err := deploymentsClient.List(context.TODO(), listOptions)
183182
if err != nil {
184-
return false, errors.Errorf("Cannot obtain information about the mesh [%s]: [%v]", meshName, err)
183+
return false, fmt.Errorf("Cannot obtain information about the mesh [%s]: [%w]", meshName, err)
185184
}
186185
// the mesh is present if there are osm controllers for the mesh
187186
return len(osmControllerDeployments.Items) != 0, nil

cmd/cli/namespace_ignore.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"strings"
88

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

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

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

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

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

8786
fmt.Fprintf(cmd.out, "Successfully configured namespace [%s] to be ignored\n", ns)

cmd/cli/namespace_list.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77

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

4241
config, err := settings.RESTClientGetter().ToRESTConfig()
4342
if err != nil {
44-
return errors.Errorf("Error fetching kubeconfig: %s", err)
43+
return fmt.Errorf("Error fetching kubeconfig: %w", err)
4544
}
4645

4746
clientset, err := kubernetes.NewForConfig(config)
4847
if err != nil {
49-
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
48+
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
5049
}
5150
namespaceList.clientSet = clientset
5251
return namespaceList.run()
@@ -63,7 +62,7 @@ func newNamespaceList(out io.Writer) *cobra.Command {
6362
func (l *namespaceListCmd) run() error {
6463
namespaces, err := selectNamespacesMonitoredByMesh(l.meshName, l.clientSet)
6564
if err != nil {
66-
return errors.Errorf("Could not list namespaces related to osm [%s]: %v", l.meshName, err)
65+
return fmt.Errorf("Could not list namespaces related to osm [%s]: %w", l.meshName, err)
6766
}
6867

6968
if len(namespaces.Items) == 0 {

0 commit comments

Comments
 (0)