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 all commits
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: metav1
- 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
28 changes: 14 additions & 14 deletions internal/kubernetes/operator/datafederation/datafederation.go
Expand Up @@ -23,8 +23,8 @@ import (
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
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"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -41,11 +41,11 @@ func BuildAtlasDataFederation(dataFederationStore store.DataFederationStore, dat
return nil, nil
}
atlasDataFederation := &akov2.AtlasDataFederation{
TypeMeta: v1.TypeMeta{
TypeMeta: metav1.TypeMeta{
APIVersion: "atlas.mongodb.com/v1",
Kind: "AtlasDataFederation",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s", projectName, dataFederation.GetName()), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand All @@ -62,12 +62,12 @@ func BuildAtlasDataFederation(dataFederationStore store.DataFederationStore, dat
return atlasDataFederation, nil
}

func isDataFederationExportable(dataFederation *admin.DataLakeTenant) bool {
func isDataFederationExportable(dataFederation *atlasv2.DataLakeTenant) bool {
state := dataFederation.GetState()
return state != DeletingState && state != DeletedState
}

func getDataFederationSpec(dataFederationSpec *admin.DataLakeTenant, targetNamespace, projectName string) akov2.DataFederationSpec {
func getDataFederationSpec(dataFederationSpec *atlasv2.DataLakeTenant, targetNamespace, projectName string) akov2.DataFederationSpec {
return akov2.DataFederationSpec{
Project: akov2common.ResourceRefNamespaced{Name: projectName, Namespace: targetNamespace},
Name: dataFederationSpec.GetName(),
Expand All @@ -77,7 +77,7 @@ func getDataFederationSpec(dataFederationSpec *admin.DataLakeTenant, targetNames
}
}

func getCloudProviderConfig(cloudProviderConfig *admin.DataLakeCloudProviderConfig) *akov2.CloudProviderConfig {
func getCloudProviderConfig(cloudProviderConfig *atlasv2.DataLakeCloudProviderConfig) *akov2.CloudProviderConfig {
if cloudProviderConfig == nil {
return &akov2.CloudProviderConfig{}
}
Expand All @@ -89,7 +89,7 @@ func getCloudProviderConfig(cloudProviderConfig *admin.DataLakeCloudProviderConf
}
}

func getDataProcessRegion(dataProcessRegion *admin.DataLakeDataProcessRegion) *akov2.DataProcessRegion {
func getDataProcessRegion(dataProcessRegion *atlasv2.DataLakeDataProcessRegion) *akov2.DataProcessRegion {
if dataProcessRegion == nil {
return &akov2.DataProcessRegion{}
}
Expand All @@ -99,7 +99,7 @@ func getDataProcessRegion(dataProcessRegion *admin.DataLakeDataProcessRegion) *a
}
}

func getStorage(storage *admin.DataLakeStorage) *akov2.Storage {
func getStorage(storage *atlasv2.DataLakeStorage) *akov2.Storage {
if storage == nil {
return &akov2.Storage{}
}
Expand All @@ -109,7 +109,7 @@ func getStorage(storage *admin.DataLakeStorage) *akov2.Storage {
}
}

func getDatabases(database []admin.DataLakeDatabaseInstance) []akov2.Database {
func getDatabases(database []atlasv2.DataLakeDatabaseInstance) []akov2.Database {
if database == nil {
return []akov2.Database{}
}
Expand All @@ -126,7 +126,7 @@ func getDatabases(database []admin.DataLakeDatabaseInstance) []akov2.Database {
return result
}

func getCollection(collections []admin.DataLakeDatabaseCollection) []akov2.Collection {
func getCollection(collections []atlasv2.DataLakeDatabaseCollection) []akov2.Collection {
if collections == nil {
return []akov2.Collection{}
}
Expand All @@ -141,7 +141,7 @@ func getCollection(collections []admin.DataLakeDatabaseCollection) []akov2.Colle
return result
}

func getDataSources(dataSources []admin.DataLakeDatabaseDataSourceSettings) []akov2.DataSource {
func getDataSources(dataSources []atlasv2.DataLakeDatabaseDataSourceSettings) []akov2.DataSource {
if dataSources == nil {
return []akov2.DataSource{}
}
Expand All @@ -164,7 +164,7 @@ func getDataSources(dataSources []admin.DataLakeDatabaseDataSourceSettings) []ak
return result
}

func getViews(views []admin.DataLakeApiBase) []akov2.View {
func getViews(views []atlasv2.DataLakeApiBase) []akov2.View {
if views == nil {
return []akov2.View{}
}
Expand All @@ -180,7 +180,7 @@ func getViews(views []admin.DataLakeApiBase) []akov2.View {
return result
}

func getStores(stores []admin.DataLakeStoreSettings) []akov2.Store {
func getStores(stores []atlasv2.DataLakeStoreSettings) []akov2.Store {
if stores == nil {
return []akov2.Store{}
}
Expand Down
Expand Up @@ -29,8 +29,8 @@ import (
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
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"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -47,26 +47,26 @@ func Test_BuildAtlasDataFederation(t *testing.T) {
dictionary := resources.AtlasNameToKubernetesName()

t.Run("Can import Data Federations", func(t *testing.T) {
dataFederation := &admin.DataLakeTenant{
CloudProviderConfig: &admin.DataLakeCloudProviderConfig{
Aws: admin.DataLakeAWSCloudProviderConfig{
dataFederation := &atlasv2.DataLakeTenant{
CloudProviderConfig: &atlasv2.DataLakeCloudProviderConfig{
Aws: atlasv2.DataLakeAWSCloudProviderConfig{
RoleId: "TestRoleID",
TestS3Bucket: "TestBucket",
},
},
DataProcessRegion: &admin.DataLakeDataProcessRegion{
DataProcessRegion: &atlasv2.DataLakeDataProcessRegion{
CloudProvider: "TestProvider",
Region: "TestRegion",
},
Hostnames: &[]string{"TestHostname"},
Name: pointer.Get(dataFederationName),
State: pointer.Get("TestState"),
Storage: &admin.DataLakeStorage{
Databases: &[]admin.DataLakeDatabaseInstance{
Storage: &atlasv2.DataLakeStorage{
Databases: &[]atlasv2.DataLakeDatabaseInstance{
{
Collections: &[]admin.DataLakeDatabaseCollection{
Collections: &[]atlasv2.DataLakeDatabaseCollection{
{
DataSources: &[]admin.DataLakeDatabaseDataSourceSettings{
DataSources: &[]atlasv2.DataLakeDatabaseDataSourceSettings{
{
AllowInsecure: pointer.Get(true),
Collection: pointer.Get("TestCollection"),
Expand All @@ -85,7 +85,7 @@ func Test_BuildAtlasDataFederation(t *testing.T) {
},
MaxWildcardCollections: pointer.Get(10),
Name: pointer.Get("TestName"),
Views: &[]admin.DataLakeApiBase{
Views: &[]atlasv2.DataLakeApiBase{
{
Name: pointer.Get("TestName"),
Pipeline: pointer.Get("TestPipeline"),
Expand All @@ -94,7 +94,7 @@ func Test_BuildAtlasDataFederation(t *testing.T) {
},
},
},
Stores: &[]admin.DataLakeStoreSettings{
Stores: &[]atlasv2.DataLakeStoreSettings{
{
Name: pointer.Get("TestName"),
Provider: "TestProvider",
Expand All @@ -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: metav1.TypeMeta{
Kind: "AtlasDataFederation",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.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"
metav1 "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: metav1.TypeMeta{
Kind: "AtlasDatabaseUser",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.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"
metav1 "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: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.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: metav1.TypeMeta{
Kind: "AtlasDatabaseUser",
APIVersion: "atlas.mongodb.com/v1",
},
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: resources.NormalizeAtlasName(fmt.Sprintf("%s-%s", projectName, user.Username), dictionary),
Namespace: targetNamespace,
Labels: map[string]string{
Expand Down