Skip to content

Commit

Permalink
Enable type assertion linter (#1707)
Browse files Browse the repository at this point in the history
* Check type assertions

* Change error

* Fix assertion within map value check

* Fix some explicitly disabled type assertions

* Add timeout to make target

* Set timeout for lint step

* Set timeout in config file

* Try higher timeout

* Try gha lint pipeline

* Try different format

* Just copy paste from sample

* Remove workflow

* retrigger jobs
  • Loading branch information
lindnerby authored Aug 1, 2023
1 parent c54edeb commit 4ce9a79
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ linters:
enable:
- errcheck
- exportloopref
- forcetypeassert
- gochecknoinits
- gofmt
- gosec
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
GOLANG_CI_LINT = $(LOCALBIN)/golangci-lint
GOLANG_CI_LINT_VERSION ?= v1.52.2
GOLANG_CI_LINT_VERSION ?= v1.53.3

.PHONY: lint
lint:
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANG_CI_LINT_VERSION)
$(LOCALBIN)/golangci-lint run -v ./...
$(LOCALBIN)/golangci-lint run -v

FLAGS = -ldflags '-s -w -X github.com/kyma-project/cli/cmd/kyma/version.Version=$(VERSION)'

Expand Down
1 change: 1 addition & 0 deletions cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func (cmd *command) Run(ctx context.Context) error {
}

t, err := module.Template(componentVersionAccess, resourceName, channel, modDef.DefaultCR, labels, annotations)

if err != nil {
cmd.CurrentStep.Failure()
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/kyma/alpha/enable/module/mock/mock_interactor.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:forcetypeassert
package mock

import (
Expand Down
23 changes: 17 additions & 6 deletions cmd/kyma/alpha/list/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ func (cmd *command) printKymaActiveTemplates(ctx context.Context, kyma *unstruct
templateList := &unstructured.UnstructuredList{Items: make([]unstructured.Unstructured, 0, len(statusItems))}

for i := range statusItems {
item, _ := statusItems[i].(map[string]interface{})
item, ok := statusItems[i].(map[string]interface{})
if !ok {
continue
}
tmplt, _, err := unstructured.NestedMap(item, "template")
if err != nil {
return fmt.Errorf("could not parse template: %w", err)
Expand All @@ -214,12 +217,20 @@ func (cmd *command) printKymaActiveTemplates(ctx context.Context, kyma *unstruct
if err != nil {
return err
}
anns := tpl.GetAnnotations()
if anns == nil {
anns = make(map[string]string)
annotations := tpl.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
stateValue, ok := item["state"]
if !ok {
continue
}
stateAnnotationValue, ok := stateValue.(string)
if !ok {
continue
}
anns["state.cmd.kyma-project.io"] = item["state"].(string)
tpl.SetAnnotations(anns)
annotations["state.cmd.kyma-project.io"] = stateAnnotationValue
tpl.SetAnnotations(annotations)
templateList.Items = append(templateList.Items, *tpl)
if templateList.GetKind() == "" {
templateList.SetGroupVersionKind(moduleTemplateResource.GroupVersion().WithKind(tpl.GetKind() + "List"))
Expand Down
6 changes: 5 additions & 1 deletion internal/cli/alpha/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package module
import (
"context"
"fmt"
"github.com/kyma-project/cli/pkg/errs"
"time"

"github.com/avast/retry-go"
Expand Down Expand Up @@ -167,7 +168,10 @@ func (i *DefaultInteractor) WaitUntilReady(ctx context.Context) error {
// It checks for v1beta2.StateReady, and if it is set, determines if this state can be trusted by observing
// if the status fields match the desired state, and if the lastOperation is filled by the lifecycle-manager.
func IsKymaReady(l *zap.SugaredLogger, obj runtime.Object) error {
kyma := obj.(*v1beta2.Kyma)
kyma, ok := obj.(*v1beta2.Kyma)
if !ok {
return errs.ErrTypeAssertKyma
}
l.Info(kyma.Status)
switch kyma.Status.State {
case v1beta2.StateReady:
Expand Down
10 changes: 9 additions & 1 deletion internal/deploy/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deploy
import (
"context"
"encoding/json"
"github.com/pkg/errors"
"regexp"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -113,12 +114,19 @@ func patchDeploymentWithInKcpModeFlag(manifestObjs []ctrlClient.Object) error {
return nil
}

var ErrPatchManifestType = errors.New("failed to cast manifest object to Unstructured")

func patchManifest(deployment *appsv1.Deployment, manifest ctrlClient.Object) error {
manifestJSON, err := json.Marshal(deployment)
if err != nil {
return err
}
err = json.Unmarshal(manifestJSON, &manifest.(*unstructured.Unstructured).Object)
unstrct, ok := manifest.(*unstructured.Unstructured)
if !ok {
return ErrPatchManifestType
}

err = json.Unmarshal(manifestJSON, &unstrct.Object)
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions internal/deploy/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,21 @@ func TestPatchDeploymentWithInKcpModeFlag(t *testing.T) {
t.Errorf("PatchDeploymentWithInKcpMode() error = %v, wantErr %v", err, tt.wantErr)
}

hasKcpFlag := validateDeploymentHasKcpFlag(tt.args.manifestObjs)
hasKcpFlag := validateDeploymentHasKcpFlag(t, tt.args.manifestObjs)
if !assert.Equal(t, tt.want, hasKcpFlag) {
t.Errorf("PatchDeploymentWithInKcpModeFlag() got = %t, want %t", hasKcpFlag, tt.want)
}
})
}
}

func validateDeploymentHasKcpFlag(objs []ctrlClient.Object) bool {
func validateDeploymentHasKcpFlag(t *testing.T, objs []ctrlClient.Object) bool {
for _, obj := range objs {
if obj.GetObjectKind().GroupVersionKind().Kind == "Deployment" {
manifestJSON, _ := json.Marshal(obj.(*unstructured.Unstructured).Object)
unstr, ok := obj.(*unstructured.Unstructured)
assert.True(t, ok)
manifestJSON, err := json.Marshal(unstr.Object)
assert.NoError(t, err)
deployment := &appsv1.Deployment{}
if err := json.Unmarshal(manifestJSON, deployment); err == nil {
if slices.Contains(deployment.Spec.Template.Spec.Containers[0].Args, "--in-kcp-mode") {
Expand Down
9 changes: 7 additions & 2 deletions internal/deploy/kyma.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deploy
import (
"context"
"fmt"
"github.com/kyma-project/cli/pkg/errs"
"os"

"github.com/avast/retry-go"
Expand Down Expand Up @@ -65,8 +66,12 @@ func Kyma(

if err := k8s.WatchObject(
ctx, kyma,
func(kyma ctrlClient.Object) (bool, error) {
return string(kyma.(*v1beta2.Kyma).Status.State) == string(v1beta2.StateReady), nil
func(obj ctrlClient.Object) (bool, error) {
tKyma, ok := obj.(*v1beta2.Kyma)
if !ok {
return false, errs.ErrTypeAssertKyma
}
return string(tKyma.Status.State) == string(v1beta2.StateReady), nil
},
); err != nil {
return fmt.Errorf("kyma custom resource did not get ready: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/errs/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package errs

import "errors"

var ErrTypeAssertKyma = errors.New("not of type v1beta2.Kyma")

0 comments on commit 4ce9a79

Please sign in to comment.