Skip to content

Commit

Permalink
chore: Configure the linting for the api module (#1999)
Browse files Browse the repository at this point in the history
* configure the linting for the api module as well

* test the removal of the gochecknoglobals

* add the api to the github workflow

* fix api module linting issues
  • Loading branch information
nesmabadr authored Oct 31, 2024
1 parent 2453efe commit 8097721
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/lint-golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ jobs:
with:
version: v1.60.3
args: --verbose
- name: golangci-lint for api module
uses: golangci/golangci-lint-action@v6.1.0
with:
version: v1.60.3
args: --verbose
working-directory: ./api
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ fmt: ## Run go fmt against code.
lint: ## Run golangci-lint against code.
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANG_CI_LINT_VERSION)
$(LOCALBIN)/golangci-lint run --verbose -c .golangci.yaml
cd api && $(LOCALBIN)/golangci-lint run --verbose -c ../.golangci.yaml
2 changes: 1 addition & 1 deletion api/v1beta2/kyma_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (kyma *Kyma) GetModuleStatusMap() map[string]*ModuleStatus {
return moduleStatusMap
}

// KymaStatus defines the observed state of Kyma
// KymaStatus defines the observed state of Kyma.
type KymaStatus struct {
// State signifies current state of Kyma.
// Value can be one of ("Ready", "Processing", "Error", "Deleting").
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta2/moduletemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (m *ModuleTemplate) IsInternal() bool {
var ErrInvalidVersion = errors.New("can't find valid semantic version")

// getVersionLegacy() returns the version of the ModuleTemplate from the annotation on the object.
// Remove once shared.ModuleVersionAnnotation is removed
// Remove once shared.ModuleVersionAnnotation is removed.
func (m *ModuleTemplate) getVersionLegacy() (string, error) {
if m.Annotations != nil {
moduleVersion, found := m.Annotations[shared.ModuleVersionAnnotation]
Expand Down
49 changes: 25 additions & 24 deletions api/v1beta2/moduletemplate_types_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package v1beta2
package v1beta2_test

import (
"strings"
"testing"

"github.com/kyma-project/lifecycle-manager/api/shared"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
)

func Test_GetVersion(t *testing.T) {
const testVersion = "1.0.1"
const otherVersion = "0.0.1"
tests := []struct {
name string
m *ModuleTemplate
m *v1beta2.ModuleTemplate
expectedVersion string
expectedErr string
}{
{
name: "Test GetVersion() by annotation (legacy)",
m: &ModuleTemplate{
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{
shared.ModuleVersionAnnotation: testVersion,
Expand All @@ -30,61 +32,59 @@ func Test_GetVersion(t *testing.T) {
},
{
name: "Test GetVersion() by explicit version in Spec",
m: &ModuleTemplate{
Spec: ModuleTemplateSpec{
m: &v1beta2.ModuleTemplate{
Spec: v1beta2.ModuleTemplateSpec{
Version: testVersion,
},
},
expectedVersion: testVersion,
},
{
name: "Test GetVersion() with both version in Spec and annotation",
m: &ModuleTemplate{
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{
shared.ModuleVersionAnnotation: otherVersion,
},
},
Spec: ModuleTemplateSpec{
Spec: v1beta2.ModuleTemplateSpec{
Version: testVersion,
},
},
expectedVersion: testVersion,
},
{
name: "Test GetVersion without any version info",
m: &ModuleTemplate{
m: &v1beta2.ModuleTemplate{
ObjectMeta: apimetav1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: ModuleTemplateSpec{},
Spec: v1beta2.ModuleTemplateSpec{},
},
expectedErr: ErrInvalidVersion.Error(),
expectedErr: v1beta2.ErrInvalidVersion.Error(),
},
{
name: "Test GetVersion with invalid version",
m: &ModuleTemplate{
Spec: ModuleTemplateSpec{
m: &v1beta2.ModuleTemplate{
Spec: v1beta2.ModuleTemplateSpec{
Version: "invalid",
},
},
expectedErr: "Invalid Semantic Version",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt := tt
actualVersion, err := tt.m.GetVersion()
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
actualVersion, err := testCase.m.GetVersion()
if err != nil {
if actualVersion != nil {
t.Errorf("GetVersion(): Returned version should be nil when error is not nil")
}
if tt.expectedErr == "" {
if testCase.expectedErr == "" {
t.Errorf("GetVersion(): Unexpected error: %v", err)
}
if !strings.Contains(err.Error(), tt.expectedErr) {
t.Errorf("GetVersion(): Actual error = %v, expected error: %v", err, tt.expectedErr)
if !strings.Contains(err.Error(), testCase.expectedErr) {
t.Errorf("GetVersion(): Actual error = %v, expected error: %v", err, testCase.expectedErr)
}
return
}
Expand All @@ -93,12 +93,13 @@ func Test_GetVersion(t *testing.T) {
t.Errorf("GetVersion(): Returned version should not be nil when error is nil")
}

if tt.expectedVersion == "" {
if testCase.expectedVersion == "" {
t.Errorf("GetVersion(): Expected version is empty but non-nil version is returned")
}

if actualVersion.String() != tt.expectedVersion {
t.Errorf("GetVersion(): actual version = %v, expected version: %v", actualVersion.String(), tt.expectedVersion)
if actualVersion != nil && actualVersion.String() != testCase.expectedVersion {
t.Errorf("GetVersion(): actual version = %v, expected version: %v", actualVersion.String(),
testCase.expectedVersion)
}
})
}
Expand Down
6 changes: 4 additions & 2 deletions api/v1beta2/moduletemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var errNotDescriptor = errors.New("provided Spec.Descriptor.Object is not a v1beta2.Descriptor")

func (m *ModuleTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
err := ctrl.NewWebhookManagedBy(mgr).
For(m).
Expand Down Expand Up @@ -86,15 +88,15 @@ func (m *ModuleTemplate) descriptor() (*Descriptor, error) {
if obj != nil {
desc, ok := obj.(*Descriptor)
if !ok {
return nil, errors.New("provided Spec.Descriptor.Object is not a v1beta2.Descriptor")
return nil, errNotDescriptor
}
return desc, nil
}

raw := m.Spec.Descriptor.Raw
desc, err := compdesc.Decode(raw, []compdesc.DecodeOption{compdesc.DisableValidation(true)}...)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to decode component descriptor, %w", err)
}

return &Descriptor{ComponentDescriptor: desc}, nil
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/operator.kyma-project.io_kymas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ spec:
- channel
type: object
status:
description: KymaStatus defines the observed state of Kyma
description: KymaStatus defines the observed state of Kyma.
properties:
activeChannel:
description: Active Channel
Expand Down Expand Up @@ -557,7 +557,7 @@ spec:
- channel
type: object
status:
description: KymaStatus defines the observed state of Kyma
description: KymaStatus defines the observed state of Kyma.
properties:
activeChannel:
description: Active Channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ spec:
version:
type: string
required:
- group
- kind
- name
- version
- group
- kind
- name
- version
type: object
mandatory:
description: |-
Expand Down

0 comments on commit 8097721

Please sign in to comment.