Skip to content

Commit 70bee6a

Browse files
authored
chore: enable early-return, indent-error-flow and unnecessary-stmt from revive (#674)
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent b111e50 commit 70bee6a

File tree

11 files changed

+78
-104
lines changed

11 files changed

+78
-104
lines changed

.golangci.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ linters-settings:
6161
- name: duplicated-imports
6262
disabled: true
6363
- name: early-return
64-
disabled: true
6564
arguments:
6665
- 'preserveScope'
6766
- name: empty-block
@@ -77,7 +76,6 @@ linters-settings:
7776
- name: increment-decrement
7877
disabled: true
7978
- name: indent-error-flow
80-
disabled: true
8179
arguments:
8280
- 'preserveScope'
8381
- name: modifies-parameter
@@ -97,7 +95,6 @@ linters-settings:
9795
- name: unexported-return
9896
disabled: true
9997
- name: unnecessary-stmt
100-
disabled: true
10198
- name: unreachable-code
10299
- name: unused-parameter
103100
- name: use-any

pkg/cache/cluster.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,9 @@ func (c *clusterCache) loadInitialState(ctx context.Context, api kube.APIResourc
646646
c.replaceResourceCache(api.GroupKind, items, ns)
647647
return nil
648648
})
649-
} else {
650-
c.replaceResourceCache(api.GroupKind, items, ns)
651-
return resourceVersion, nil
652649
}
650+
c.replaceResourceCache(api.GroupKind, items, ns)
651+
return resourceVersion, nil
653652
}
654653

655654
func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo, resClient dynamic.ResourceInterface, ns string, resourceVersion string) {
@@ -845,10 +844,10 @@ func (c *clusterCache) checkPermission(ctx context.Context, reviewInterface auth
845844
}
846845
if resp != nil && resp.Status.Allowed {
847846
return true, nil
848-
} else {
849-
// unsupported, remove from watch list
850-
return false, nil
851847
}
848+
// unsupported, remove from watch list
849+
//nolint:staticcheck //FIXME
850+
return false, nil
852851
}
853852
}
854853
// checkPermission follows the same logic of determining namespace/cluster resource as the processApi function
@@ -1157,13 +1156,12 @@ func buildGraph(nsNodes map[kube.ResourceKey]*Resource) map[kube.ResourceKey]map
11571156
continue
11581157
}
11591158
graphKeyNode, ok := nsNodes[kube.ResourceKey{Group: group.Group, Kind: ownerRef.Kind, Namespace: childNode.Ref.Namespace, Name: ownerRef.Name}]
1160-
if ok {
1161-
ownerRef.UID = graphKeyNode.Ref.UID
1162-
childNode.OwnerRefs[i] = ownerRef
1163-
} else {
1159+
if !ok {
11641160
// No resource found with the given graph key, so move on.
11651161
continue
11661162
}
1163+
ownerRef.UID = graphKeyNode.Ref.UID
1164+
childNode.OwnerRefs[i] = ownerRef
11671165
}
11681166

11691167
// Now that we have the UID of the parent, update the graph.

pkg/diff/diff.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ func handleResourceCreateOrDeleteDiff(config, live *unstructured.Unstructured) (
474474
return nil, err
475475
}
476476
return &DiffResult{Modified: true, NormalizedLive: []byte("null"), PredictedLive: predictedLiveData}, nil
477-
} else {
478-
return nil, errors.New("both live and config are null objects")
479477
}
478+
return nil, errors.New("both live and config are null objects")
480479
}
481480

482481
// generateSchemeDefaultPatch runs the scheme default functions on the given parameter, and
@@ -740,22 +739,21 @@ func threeWayMergePatch(orig, config, live *unstructured.Unstructured) ([]byte,
740739
return scheme.Scheme.New(orig.GroupVersionKind())
741740
}
742741
return patch, newVersionedObject, nil
743-
} else {
744-
// Remove defaulted fields from the live object.
745-
// This subtracts any extra fields in the live object which are not present in last-applied-configuration.
746-
live = &unstructured.Unstructured{Object: jsonutil.RemoveMapFields(orig.Object, live.Object)}
742+
}
743+
// Remove defaulted fields from the live object.
744+
// This subtracts any extra fields in the live object which are not present in last-applied-configuration.
745+
live = &unstructured.Unstructured{Object: jsonutil.RemoveMapFields(orig.Object, live.Object)}
747746

748-
liveBytes, err := json.Marshal(live.Object)
749-
if err != nil {
750-
return nil, nil, err
751-
}
747+
liveBytes, err := json.Marshal(live.Object)
748+
if err != nil {
749+
return nil, nil, err
750+
}
752751

753-
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(origBytes, configBytes, liveBytes)
754-
if err != nil {
755-
return nil, nil, err
756-
}
757-
return patch, nil, nil
752+
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(origBytes, configBytes, liveBytes)
753+
if err != nil {
754+
return nil, nil, err
758755
}
756+
return patch, nil, nil
759757
}
760758

761759
func GetLastAppliedConfigAnnotation(live *unstructured.Unstructured) (*unstructured.Unstructured, error) {

pkg/health/health.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,19 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst
112112
return getDaemonSetHealth
113113
}
114114
case "extensions":
115-
switch gvk.Kind {
116-
case kube.IngressKind:
115+
if gvk.Kind == kube.IngressKind {
117116
return getIngressHealth
118117
}
119118
case "argoproj.io":
120-
switch gvk.Kind {
121-
case "Workflow":
119+
if gvk.Kind == "Workflow" {
122120
return getArgoWorkflowHealth
123121
}
124122
case "apiregistration.k8s.io":
125-
switch gvk.Kind {
126-
case kube.APIServiceKind:
123+
if gvk.Kind == kube.APIServiceKind {
127124
return getAPIServiceHealth
128125
}
129126
case "networking.k8s.io":
130-
switch gvk.Kind {
131-
case kube.IngressKind:
127+
if gvk.Kind == kube.IngressKind {
132128
return getIngressHealth
133129
}
134130
case "":
@@ -141,13 +137,11 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst
141137
return getPodHealth
142138
}
143139
case "batch":
144-
switch gvk.Kind {
145-
case kube.JobKind:
140+
if gvk.Kind == kube.JobKind {
146141
return getJobHealth
147142
}
148143
case "autoscaling":
149-
switch gvk.Kind {
150-
case kube.HorizontalPodAutoscalerKind:
144+
if gvk.Kind == kube.HorizontalPodAutoscalerKind {
151145
return getHPAHealth
152146
}
153147
}

pkg/health/health_apiservice.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ func getAPIServiceHealth(obj *unstructured.Unstructured) (*HealthStatus, error)
3535

3636
func getApiregistrationv1APIServiceHealth(apiservice *apiregistrationv1.APIService) (*HealthStatus, error) {
3737
for _, c := range apiservice.Status.Conditions {
38-
switch c.Type {
39-
case apiregistrationv1.Available:
38+
if c.Type == apiregistrationv1.Available {
4039
if c.Status == apiregistrationv1.ConditionTrue {
4140
return &HealthStatus{
4241
Status: HealthStatusHealthy,
4342
Message: fmt.Sprintf("%s: %s", c.Reason, c.Message),
4443
}, nil
45-
} else {
46-
return &HealthStatus{
47-
Status: HealthStatusProgressing,
48-
Message: fmt.Sprintf("%s: %s", c.Reason, c.Message),
49-
}, nil
5044
}
45+
return &HealthStatus{
46+
Status: HealthStatusProgressing,
47+
Message: fmt.Sprintf("%s: %s", c.Reason, c.Message),
48+
}, nil
5149
}
5250
}
5351
return &HealthStatus{
@@ -58,19 +56,17 @@ func getApiregistrationv1APIServiceHealth(apiservice *apiregistrationv1.APIServi
5856

5957
func getApiregistrationv1beta1APIServiceHealth(apiservice *apiregistrationv1beta1.APIService) (*HealthStatus, error) {
6058
for _, c := range apiservice.Status.Conditions {
61-
switch c.Type {
62-
case apiregistrationv1beta1.Available:
59+
if c.Type == apiregistrationv1beta1.Available {
6360
if c.Status == apiregistrationv1beta1.ConditionTrue {
6461
return &HealthStatus{
6562
Status: HealthStatusHealthy,
6663
Message: fmt.Sprintf("%s: %s", c.Reason, c.Message),
6764
}, nil
68-
} else {
69-
return &HealthStatus{
70-
Status: HealthStatusProgressing,
71-
Message: fmt.Sprintf("%s: %s", c.Reason, c.Message),
72-
}, nil
7365
}
66+
return &HealthStatus{
67+
Status: HealthStatusProgressing,
68+
Message: fmt.Sprintf("%s: %s", c.Reason, c.Message),
69+
}, nil
7470
}
7571
}
7672
return &HealthStatus{

pkg/health/health_daemonset.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,30 @@ func getDaemonSetHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
2727

2828
func getAppsv1DaemonSetHealth(daemon *appsv1.DaemonSet) (*HealthStatus, error) {
2929
// Borrowed at kubernetes/kubectl/rollout_status.go https://github.com/kubernetes/kubernetes/blob/5232ad4a00ec93942d0b2c6359ee6cd1201b46bc/pkg/kubectl/rollout_status.go#L110
30-
if daemon.Generation <= daemon.Status.ObservedGeneration {
31-
if daemon.Spec.UpdateStrategy.Type == appsv1.OnDeleteDaemonSetStrategyType {
32-
return &HealthStatus{
33-
Status: HealthStatusHealthy,
34-
Message: fmt.Sprintf("daemon set %d out of %d new pods have been updated", daemon.Status.UpdatedNumberScheduled, daemon.Status.DesiredNumberScheduled),
35-
}, nil
36-
}
37-
if daemon.Status.UpdatedNumberScheduled < daemon.Status.DesiredNumberScheduled {
38-
return &HealthStatus{
39-
Status: HealthStatusProgressing,
40-
Message: fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d out of %d new pods have been updated...", daemon.Name, daemon.Status.UpdatedNumberScheduled, daemon.Status.DesiredNumberScheduled),
41-
}, nil
42-
}
43-
if daemon.Status.NumberAvailable < daemon.Status.DesiredNumberScheduled {
44-
return &HealthStatus{
45-
Status: HealthStatusProgressing,
46-
Message: fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d of %d updated pods are available...", daemon.Name, daemon.Status.NumberAvailable, daemon.Status.DesiredNumberScheduled),
47-
}, nil
48-
}
49-
} else {
30+
if daemon.Generation > daemon.Status.ObservedGeneration {
5031
return &HealthStatus{
5132
Status: HealthStatusProgressing,
5233
Message: "Waiting for rollout to finish: observed daemon set generation less than desired generation",
5334
}, nil
5435
}
36+
if daemon.Spec.UpdateStrategy.Type == appsv1.OnDeleteDaemonSetStrategyType {
37+
return &HealthStatus{
38+
Status: HealthStatusHealthy,
39+
Message: fmt.Sprintf("daemon set %d out of %d new pods have been updated", daemon.Status.UpdatedNumberScheduled, daemon.Status.DesiredNumberScheduled),
40+
}, nil
41+
}
42+
if daemon.Status.UpdatedNumberScheduled < daemon.Status.DesiredNumberScheduled {
43+
return &HealthStatus{
44+
Status: HealthStatusProgressing,
45+
Message: fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d out of %d new pods have been updated...", daemon.Name, daemon.Status.UpdatedNumberScheduled, daemon.Status.DesiredNumberScheduled),
46+
}, nil
47+
}
48+
if daemon.Status.NumberAvailable < daemon.Status.DesiredNumberScheduled {
49+
return &HealthStatus{
50+
Status: HealthStatusProgressing,
51+
Message: fmt.Sprintf("Waiting for daemon set %q rollout to finish: %d of %d updated pods are available...", daemon.Name, daemon.Status.NumberAvailable, daemon.Status.DesiredNumberScheduled),
52+
}, nil
53+
}
5554
return &HealthStatus{
5655
Status: HealthStatusHealthy,
5756
}, nil

pkg/health/health_job.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ func getBatchv1JobHealth(job *batchv1.Job) (*HealthStatus, error) {
6565
Status: HealthStatusSuspended,
6666
Message: failMsg,
6767
}, nil
68-
} else {
69-
return &HealthStatus{
70-
Status: HealthStatusHealthy,
71-
Message: message,
72-
}, nil
7368
}
69+
return &HealthStatus{
70+
Status: HealthStatusHealthy,
71+
Message: message,
72+
}, nil
7473
}

pkg/sync/sync_context.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,8 @@ func isNamespaceKind(res *unstructured.Unstructured) bool {
905905
func obj(a, b *unstructured.Unstructured) *unstructured.Unstructured {
906906
if a != nil {
907907
return a
908-
} else {
909-
return b
910908
}
909+
return b
911910
}
912911

913912
func (sc *syncContext) liveObj(obj *unstructured.Unstructured) *unstructured.Unstructured {
@@ -1021,21 +1020,19 @@ func (sc *syncContext) pruneObject(liveObj *unstructured.Unstructured, prune, dr
10211020
return common.ResultCodePruneSkipped, "ignored (requires pruning)"
10221021
} else if resourceutil.HasAnnotationOption(liveObj, common.AnnotationSyncOptions, common.SyncOptionDisablePrune) {
10231022
return common.ResultCodePruneSkipped, "ignored (no prune)"
1024-
} else {
1025-
if dryRun {
1026-
return common.ResultCodePruned, "pruned (dry run)"
1027-
} else {
1028-
// Skip deletion if object is already marked for deletion, so we don't cause a resource update hotloop
1029-
deletionTimestamp := liveObj.GetDeletionTimestamp()
1030-
if deletionTimestamp == nil || deletionTimestamp.IsZero() {
1031-
err := sc.kubectl.DeleteResource(context.TODO(), sc.config, liveObj.GroupVersionKind(), liveObj.GetName(), liveObj.GetNamespace(), sc.getDeleteOptions())
1032-
if err != nil {
1033-
return common.ResultCodeSyncFailed, err.Error()
1034-
}
1035-
}
1036-
return common.ResultCodePruned, "pruned"
1023+
}
1024+
if dryRun {
1025+
return common.ResultCodePruned, "pruned (dry run)"
1026+
}
1027+
// Skip deletion if object is already marked for deletion, so we don't cause a resource update hotloop
1028+
deletionTimestamp := liveObj.GetDeletionTimestamp()
1029+
if deletionTimestamp == nil || deletionTimestamp.IsZero() {
1030+
err := sc.kubectl.DeleteResource(context.TODO(), sc.config, liveObj.GroupVersionKind(), liveObj.GetName(), liveObj.GetNamespace(), sc.getDeleteOptions())
1031+
if err != nil {
1032+
return common.ResultCodeSyncFailed, err.Error()
10371033
}
10381034
}
1035+
return common.ResultCodePruned, "pruned"
10391036
}
10401037

10411038
func (sc *syncContext) getDeleteOptions() metav1.DeleteOptions {

pkg/sync/sync_phase.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func syncPhases(obj *unstructured.Unstructured) []common.SyncPhase {
2323
phases = append(phases, phase)
2424
}
2525
return phases
26-
} else {
27-
return []common.SyncPhase{common.SyncPhaseSync}
2826
}
27+
return []common.SyncPhase{common.SyncPhaseSync}
2928
}

pkg/sync/sync_task.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ type syncTask struct {
2929
func ternary(val bool, a, b string) string {
3030
if val {
3131
return a
32-
} else {
33-
return b
3432
}
33+
return b
3534
}
3635

3736
func (t *syncTask) String() string {
@@ -115,9 +114,8 @@ func (t *syncTask) pruned() bool {
115114
func (t *syncTask) hookType() common.HookType {
116115
if t.isHook() {
117116
return common.HookType(t.phase)
118-
} else {
119-
return ""
120117
}
118+
return ""
121119
}
122120

123121
func (t *syncTask) hasHookDeletePolicy(policy common.HookDeletePolicy) bool {

0 commit comments

Comments
 (0)