Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: validate import alias via importas #2804

Merged
merged 6 commits into from Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 40 additions & 9 deletions .golangci.yml
Expand Up @@ -6,10 +6,45 @@ linters-settings:
min-occurrences: 2
gocyclo:
min-complexity: 15
govet:
check-shadowing: true
maligned:
suggest-new: true
Comment on lines -9 to -12
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid configs

importas:
no-extra-aliases: true
alias:
- pkg: go.mongodb.org/atlas-sdk/v[\d]+/admin
alias: atlasv2
- pkg: go.mongodb.org/atlas/mongodbatlas
alias: atlas
- pkg: go.mongodb.org/atlas/auth
alias: atlasauth
- pkg: github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1
alias: akov2
- pkg: github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common
alias: akov2common
- pkg: github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/provider
alias: akov2provider
- pkg: github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status
alias: akov2status
- pkg: github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/project
alias: akov2project
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: apisv1
- pkg: k8s.io/api/apps/v1
alias: appsv1
- pkg: k8s.io/api/core/v1
alias: corev1
- pkg: k8s.io/api/rbac/v1
alias: rbacv1
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
alias: apiextensionsv1
- pkg: k8s.io/apiserver/pkg/storage/names
alias: k8snames
- pkg: github.com/AlecAivazis/survey/v2/core
alias: survey
- pkg: github.com/creack/pty
alias: pseudotty
- pkg: cloud.google.com/go/kms/apiv1
alias: kmsv1
- pkg: github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/config
alias: cliconfig
misspell:
locale: US
ignore-words:
Expand Down Expand Up @@ -59,8 +94,6 @@ linters-settings:
sections:
- standard
- default
section-separators:
- newLine
Comment on lines -62 to -63
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid config

linters:
disable-all: true
enable:
Expand All @@ -83,6 +116,7 @@ linters:
- gosimple # (megacheck) Linter for Go source code that specializes in simplifying code [fast: false, auto-fix: false]
- govet # (vet, vetshadow) Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- importas # Enforces consistent import aliases [fast: false, auto-fix: false]
- makezero # Finds slice declarations with non-zero initial length [fast: false, auto-fix: false]
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
Expand Down Expand Up @@ -138,7 +172,6 @@ linters:
# - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. [fast: true, auto-fix: false]
# - grouper # An analyzer to analyze expression groups. [fast: true, auto-fix: false]
# - ifshort # [deprecated] Checks that your code uses short syntax for if-statements whenever possible [fast: true, auto-fix: false]
# - importas # Enforces consistent import aliases [fast: false, auto-fix: false]
# - interfacebloat # A linter that checks the number of methods inside an interface. [fast: true, auto-fix: false]
# - interfacer # [deprecated]: Linter that suggests narrower interface types [fast: false, auto-fix: false]
# - ireturn # Accept Interfaces, Return Concrete Types [fast: false, auto-fix: false]
Expand Down Expand Up @@ -174,8 +207,6 @@ run:
- e2e
- unit
- integration
skip-dirs:
- internal/mocks
Comment on lines -177 to -178
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid config

modules-download-mode: readonly

issues:
Expand Down
8 changes: 4 additions & 4 deletions internal/kubernetes/operator/crds/provider.go
Expand Up @@ -21,7 +21,7 @@ import (
"net/http"
"time"

apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/yaml"
)

Expand All @@ -32,7 +32,7 @@ const (

//go:generate mockgen -destination=../../../mocks/mock_atlas_operator_crd_provider.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/crds AtlasOperatorCRDProvider
type AtlasOperatorCRDProvider interface {
GetAtlasOperatorResource(resourceName, version string) (*apiextensions.CustomResourceDefinition, error)
GetAtlasOperatorResource(resourceName, version string) (*apiextensionsv1.CustomResourceDefinition, error)
}

type GithubAtlasCRDProvider struct {
Expand All @@ -43,7 +43,7 @@ func NewGithubAtlasCRDProvider() *GithubAtlasCRDProvider {
return &GithubAtlasCRDProvider{client: &http.Client{}}
}

func (p *GithubAtlasCRDProvider) GetAtlasOperatorResource(resourceName, version string) (*apiextensions.CustomResourceDefinition, error) {
func (p *GithubAtlasCRDProvider) GetAtlasOperatorResource(resourceName, version string) (*apiextensionsv1.CustomResourceDefinition, error) {
ctx, cancelF := context.WithTimeout(context.Background(), requestTimeout)
defer cancelF()

Expand All @@ -63,7 +63,7 @@ func (p *GithubAtlasCRDProvider) GetAtlasOperatorResource(resourceName, version
return nil, err
}

decoded := &apiextensions.CustomResourceDefinition{}
decoded := &apiextensionsv1.CustomResourceDefinition{}
err = yaml.Unmarshal(data, decoded)
if err != nil {
return nil, err
Expand Down
Expand Up @@ -24,7 +24,7 @@ import (
akov2common "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apisv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is not idiomatic in kube upstream, metav1 is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

)

const (
Expand All @@ -41,11 +41,11 @@ func BuildAtlasDataFederation(dataFederationStore store.DataFederationStore, dat
return nil, nil
}
atlasDataFederation := &akov2.AtlasDataFederation{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
APIVersion: "atlas.mongodb.com/v1",
Kind: "AtlasDataFederation",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s", projectName, dataFederation.GetName()), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down
Expand Up @@ -30,7 +30,7 @@ import (
akov2common "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apisv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

)

const (
Expand Down Expand Up @@ -113,11 +113,11 @@ func Test_BuildAtlasDataFederation(t *testing.T) {
dataFederationStore.EXPECT().DataFederation(projectID, dataFederationName).Return(dataFederation, nil)

expected := &akov2.AtlasDataFederation{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDataFederation",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s", projectName, dataFederation.GetName()), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down
6 changes: 3 additions & 3 deletions internal/kubernetes/operator/dbusers/dbusers.go
Expand Up @@ -28,7 +28,7 @@ import (
akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apisv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const timeFormatISO8601 = "2006-01-02T15:04:05.999Z"
Expand Down Expand Up @@ -57,11 +57,11 @@ func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, t
scopes := convertUserScopes(user)

mappedUsers[resourceName] = &akov2.AtlasDatabaseUser{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDatabaseUser",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resourceName,
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down
10 changes: 5 additions & 5 deletions internal/kubernetes/operator/dbusers/dbusers_test.go
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/stretchr/testify/assert"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apisv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const resourceVersion = "x.y.z"
Expand Down Expand Up @@ -103,11 +103,11 @@ func Test_buildUserSecret(t *testing.T) {
}

expectedSecret := &corev1.Secret{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: strings.ToLower(fmt.Sprintf("%s-%s", projectName, atlasUser.Username)),
Namespace: "TestNamespace",
Labels: map[string]string{
Expand Down Expand Up @@ -181,11 +181,11 @@ func TestBuildDBUsers(t *testing.T) {
}

expectedUser := &akov2.AtlasDatabaseUser{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDatabaseUser",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s", projectName, user.Username), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down
18 changes: 9 additions & 9 deletions internal/kubernetes/operator/deployment/deployment.go
Expand Up @@ -26,7 +26,7 @@ import (
akov2provider "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/provider"
akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apisv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -109,11 +109,11 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va
}

atlasDeployment := &akov2.AtlasDeployment{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDeployment",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s", projectName, clusterID), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down Expand Up @@ -295,11 +295,11 @@ func buildBackups(backupsProvider store.ScheduleDescriber, projectName, projectI
})
}
policies = append(policies, &akov2.AtlasBackupPolicy{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasBackupPolicy",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s-backuppolicy", projectName, clusterName), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand All @@ -322,11 +322,11 @@ func buildBackups(backupsProvider store.ScheduleDescriber, projectName, projectI
}

schedule := &akov2.AtlasBackupSchedule{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasBackupSchedule",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s-backupschedule", projectName, clusterName), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down Expand Up @@ -475,11 +475,11 @@ func BuildServerlessDeployments(deploymentStore store.OperatorClusterStore, vali

atlasName := fmt.Sprintf("%s-%s", projectName, store.StringOrEmpty(deployment.Name))
atlasDeployment := &akov2.AtlasDeployment{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDeployment",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: resources.NormalizeAtlasName(atlasName, dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down
20 changes: 10 additions & 10 deletions internal/kubernetes/operator/deployment/deployment_test.go
Expand Up @@ -35,7 +35,7 @@ import (
akov2provider "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/provider"
akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apisv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const resourceVersion = "x.y.z"
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestBuildAtlasAdvancedDeployment(t *testing.T) {
SampleRefreshIntervalBIConnector: pointer.Get(10),
}
processArgs.OplogSizeMB = pointer.Get(10)
processArgs.OplogMinRetentionHours = pointer.Get(float64(10.1))
processArgs.OplogMinRetentionHours = pointer.Get[float64](10.1)
backupSchedule := &atlasv2.DiskBackupSnapshotSchedule{
ClusterId: pointer.Get("testClusterID"),
ClusterName: pointer.Get(clusterName),
Expand Down Expand Up @@ -206,11 +206,11 @@ func TestBuildAtlasAdvancedDeployment(t *testing.T) {
clusterStore.EXPECT().DescribeSchedule(projectName, clusterName).Return(backupSchedule, nil)

expectCluster := &akov2.AtlasDeployment{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDeployment",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: strings.ToLower(fmt.Sprintf("%s-%s", projectName, clusterName)),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down Expand Up @@ -331,11 +331,11 @@ func TestBuildAtlasAdvancedDeployment(t *testing.T) {

expectPolicies := []*akov2.AtlasBackupPolicy{
{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasBackupPolicy",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: strings.ToLower(fmt.Sprintf("%s-%s-backuppolicy", projectName, clusterName)),
Namespace: targetNamespace,
Labels: map[string]string{
Expand All @@ -357,11 +357,11 @@ func TestBuildAtlasAdvancedDeployment(t *testing.T) {
}

expectSchedule := &akov2.AtlasBackupSchedule{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasBackupSchedule",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: strings.ToLower(fmt.Sprintf("%s-%s-backupschedule", projectName, clusterName)),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down Expand Up @@ -467,11 +467,11 @@ func TestBuildServerlessDeployments(t *testing.T) {
clusterStore.EXPECT().ServerlessPrivateEndpoints(projectName, clusterName).Return(spe, nil)

expected := &akov2.AtlasDeployment{
TypeMeta: v1.TypeMeta{
TypeMeta: apisv1.TypeMeta{
Kind: "AtlasDeployment",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: apisv1.ObjectMeta{
Name: strings.ToLower(fmt.Sprintf("%s-%s", projectName, clusterName)),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down