Skip to content

Commit ee59551

Browse files
authored
Merge pull request #1309 from dongjiang1989/update-importas
🌱chore: update importas in golangci config
2 parents cf302a3 + f35dd2b commit ee59551

20 files changed

+361
-348
lines changed

.golangci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ linters:
6464
- shadow
6565
enable-all: true
6666
importas:
67+
alias:
68+
- pkg: k8s.io/api/core/v1
69+
alias: corev1
70+
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
71+
alias: apiextensionsv1
72+
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
73+
alias: metav1
74+
- pkg: k8s.io/apimachinery/pkg/api/errors
75+
alias: apierrors
76+
- pkg: k8s.io/apimachinery/pkg/util/errors
77+
alias: kerrors
78+
- pkg: sigs.k8s.io/controller-runtime
79+
alias: ctrl
6780
no-unaliased: true
6881
revive:
6982
# By default, revive will enable only the linting rules that are named in the configuration file.

pkg/crd/conv.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
apiextinternal "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
7-
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
7+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
88
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
99
"k8s.io/apimachinery/pkg/runtime"
1010
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -18,13 +18,13 @@ func init() {
1818
if err := apiextinternal.AddToScheme(conversionScheme); err != nil {
1919
panic("must be able to add internal apiextensions to the CRD conversion Scheme")
2020
}
21-
if err := apiext.AddToScheme(conversionScheme); err != nil {
21+
if err := apiextensionsv1.AddToScheme(conversionScheme); err != nil {
2222
panic("must be able to add apiextensions/v1 to the CRD conversion Scheme")
2323
}
2424
}
2525

2626
// AsVersion converts a CRD from the canonical internal form (currently v1) to some external form.
27-
func AsVersion(original apiext.CustomResourceDefinition, gv schema.GroupVersion) (runtime.Object, error) {
27+
func AsVersion(original apiextensionsv1.CustomResourceDefinition, gv schema.GroupVersion) (runtime.Object, error) {
2828
// TODO: Do we need to keep maintaining this conversion function
2929
// post 1.22 when only CRDv1 is served by the apiserver?
3030
if gv == apiextv1beta1.SchemeGroupVersion {

pkg/crd/desc_visitor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import (
2020
"strings"
2121
"unicode"
2222

23-
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
23+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2424
)
2525

2626
// TruncateDescription truncates the description of fields in given schema if it
2727
// exceeds maxLen.
2828
// It tries to chop off the description at the closest sentence boundary.
29-
func TruncateDescription(schema *apiext.JSONSchemaProps, maxLen int) {
29+
func TruncateDescription(schema *apiextensionsv1.JSONSchemaProps, maxLen int) {
3030
EditSchema(schema, descVisitor{maxLen: maxLen})
3131
}
3232

@@ -37,7 +37,7 @@ type descVisitor struct {
3737
maxLen int
3838
}
3939

40-
func (v descVisitor) Visit(schema *apiext.JSONSchemaProps) SchemaVisitor {
40+
func (v descVisitor) Visit(schema *apiextensionsv1.JSONSchemaProps) SchemaVisitor {
4141
if schema == nil {
4242
return v
4343
}

pkg/crd/desc_visitor_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,34 @@ package crd_test
1919
import (
2020
. "github.com/onsi/ginkgo"
2121
. "github.com/onsi/gomega"
22-
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
22+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2323
"sigs.k8s.io/controller-tools/pkg/crd"
2424
)
2525

2626
var _ = Describe("TruncateDescription", func() {
2727

2828
It("should drop the description for all fields for zero value of maxLen", func() {
29-
schema := &apiext.JSONSchemaProps{
29+
schema := &apiextensionsv1.JSONSchemaProps{
3030
Description: "top level description",
31-
Properties: map[string]apiext.JSONSchemaProps{
31+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
3232
"Spec": {
3333
Description: "specification for the API type",
3434
},
3535
},
3636
}
3737
crd.TruncateDescription(schema, 0)
38-
Expect(schema).To(Equal(&apiext.JSONSchemaProps{
39-
Properties: map[string]apiext.JSONSchemaProps{
38+
Expect(schema).To(Equal(&apiextensionsv1.JSONSchemaProps{
39+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
4040
"Spec": {},
4141
},
4242
}))
4343

4444
})
4545

4646
It("should truncate the description only in cases where description exceeds maxLen", func() {
47-
schema := &apiext.JSONSchemaProps{
47+
schema := &apiextensionsv1.JSONSchemaProps{
4848
Description: "top level description of the root object.",
49-
Properties: map[string]apiext.JSONSchemaProps{
49+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
5050
"Spec": {
5151
Description: "specification of a field",
5252
},
@@ -58,21 +58,21 @@ var _ = Describe("TruncateDescription", func() {
5858
})
5959

6060
It("should truncate the description at closest sentence boundary", func() {
61-
schema := &apiext.JSONSchemaProps{
61+
schema := &apiextensionsv1.JSONSchemaProps{
6262
Description: `This is top level description. There is an empty schema. More to come`,
6363
}
6464
crd.TruncateDescription(schema, len(schema.Description)-5)
65-
Expect(schema).To(Equal(&apiext.JSONSchemaProps{
65+
Expect(schema).To(Equal(&apiextensionsv1.JSONSchemaProps{
6666
Description: `This is top level description. There is an empty schema.`,
6767
}))
6868
})
6969

7070
It("should truncate the description at maxLen in absence of sentence boundary", func() {
71-
schema := &apiext.JSONSchemaProps{
71+
schema := &apiextensionsv1.JSONSchemaProps{
7272
Description: `This is top level description of the root object`,
7373
}
7474
crd.TruncateDescription(schema, len(schema.Description)-2)
75-
Expect(schema).To(Equal(&apiext.JSONSchemaProps{
75+
Expect(schema).To(Equal(&apiextensionsv1.JSONSchemaProps{
7676
Description: `This is top level description of the root...`,
7777
}))
7878
})

pkg/crd/flatten.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"strings"
2424
"sync"
2525

26-
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
26+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2727
"sigs.k8s.io/controller-tools/pkg/loader"
2828
)
2929

@@ -49,7 +49,7 @@ func isOrNil(val reflect.Value, valInt any, zeroInt any) bool {
4949

5050
// flattenAllOfInto copies properties from src to dst, then copies the properties
5151
// of each item in src's allOf to dst's properties as well.
52-
func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, errRec ErrorRecorder) {
52+
func flattenAllOfInto(dst *apiextensionsv1.JSONSchemaProps, src apiextensionsv1.JSONSchemaProps, errRec ErrorRecorder) {
5353
if len(src.AllOf) > 0 {
5454
for _, embedded := range src.AllOf {
5555
flattenAllOfInto(dst, embedded, errRec)
@@ -60,9 +60,9 @@ func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, e
6060
srcVal := reflect.ValueOf(src)
6161
typ := dstVal.Type()
6262

63-
srcRemainder := apiext.JSONSchemaProps{}
63+
srcRemainder := apiextensionsv1.JSONSchemaProps{}
6464
srcRemVal := reflect.Indirect(reflect.ValueOf(&srcRemainder))
65-
dstRemainder := apiext.JSONSchemaProps{}
65+
dstRemainder := apiextensionsv1.JSONSchemaProps{}
6666
dstRemVal := reflect.Indirect(reflect.ValueOf(&dstRemainder))
6767
hoisted := false
6868

@@ -104,8 +104,8 @@ func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, e
104104
switch fieldName {
105105
case "Properties":
106106
// merge if possible, use all of otherwise
107-
srcMap := srcInt.(map[string]apiext.JSONSchemaProps)
108-
dstMap := dstInt.(map[string]apiext.JSONSchemaProps)
107+
srcMap := srcInt.(map[string]apiextensionsv1.JSONSchemaProps)
108+
dstMap := dstInt.(map[string]apiextensionsv1.JSONSchemaProps)
109109

110110
for k, v := range srcMap {
111111
dstProp, exists := dstMap[k]
@@ -132,14 +132,14 @@ func flattenAllOfInto(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps, e
132132
// - Definitions: common named validation sets that can be references (merge, bail if duplicate)
133133
case "AdditionalProperties":
134134
// as of the time of writing, `allows: false` is not allowed, so we don't have to handle it
135-
srcProps := srcInt.(*apiext.JSONSchemaPropsOrBool)
135+
srcProps := srcInt.(*apiextensionsv1.JSONSchemaPropsOrBool)
136136
if srcProps.Schema == nil {
137137
// nothing to merge
138138
continue
139139
}
140-
dstProps := dstInt.(*apiext.JSONSchemaPropsOrBool)
140+
dstProps := dstInt.(*apiextensionsv1.JSONSchemaPropsOrBool)
141141
if dstProps.Schema == nil {
142-
dstProps.Schema = &apiext.JSONSchemaProps{}
142+
dstProps.Schema = &apiextensionsv1.JSONSchemaProps{}
143143
}
144144
flattenAllOfInto(dstProps.Schema, *srcProps.Schema, errRec)
145145
case "XPreserveUnknownFields":
@@ -188,7 +188,7 @@ type allOfVisitor struct {
188188
errRec ErrorRecorder
189189
}
190190

191-
func (v *allOfVisitor) Visit(schema *apiext.JSONSchemaProps) SchemaVisitor {
191+
func (v *allOfVisitor) Visit(schema *apiextensionsv1.JSONSchemaProps) SchemaVisitor {
192192
if schema == nil {
193193
return v
194194
}
@@ -210,7 +210,7 @@ func (v *allOfVisitor) Visit(schema *apiext.JSONSchemaProps) SchemaVisitor {
210210
// FlattenEmbedded flattens embedded fields (represented via AllOf) which have
211211
// already had their references resolved into simple properties in the containing
212212
// schema.
213-
func FlattenEmbedded(schema *apiext.JSONSchemaProps, errRec ErrorRecorder) *apiext.JSONSchemaProps {
213+
func FlattenEmbedded(schema *apiextensionsv1.JSONSchemaProps, errRec ErrorRecorder) *apiextensionsv1.JSONSchemaProps {
214214
outSchema := schema.DeepCopy()
215215
EditSchema(outSchema, &allOfVisitor{errRec: errRec})
216216
return outSchema
@@ -226,27 +226,27 @@ type Flattener struct {
226226
LookupReference func(ref string, contextPkg *loader.Package) (TypeIdent, error)
227227

228228
// flattenedTypes hold the flattened version of each seen type for later reuse.
229-
flattenedTypes map[TypeIdent]apiext.JSONSchemaProps
229+
flattenedTypes map[TypeIdent]apiextensionsv1.JSONSchemaProps
230230
initOnce sync.Once
231231
}
232232

233233
func (f *Flattener) init() {
234234
f.initOnce.Do(func() {
235-
f.flattenedTypes = make(map[TypeIdent]apiext.JSONSchemaProps)
235+
f.flattenedTypes = make(map[TypeIdent]apiextensionsv1.JSONSchemaProps)
236236
if f.LookupReference == nil {
237237
f.LookupReference = identFromRef
238238
}
239239
})
240240
}
241241

242242
// cacheType saves the flattened version of the given type for later reuse
243-
func (f *Flattener) cacheType(typ TypeIdent, schema apiext.JSONSchemaProps) {
243+
func (f *Flattener) cacheType(typ TypeIdent, schema apiextensionsv1.JSONSchemaProps) {
244244
f.init()
245245
f.flattenedTypes[typ] = schema
246246
}
247247

248248
// loadUnflattenedSchema fetches a fresh, unflattened schema from the parser.
249-
func (f *Flattener) loadUnflattenedSchema(typ TypeIdent) (*apiext.JSONSchemaProps, error) {
249+
func (f *Flattener) loadUnflattenedSchema(typ TypeIdent) (*apiextensionsv1.JSONSchemaProps, error) {
250250
f.Parser.NeedSchemaFor(typ)
251251

252252
baseSchema, found := f.Parser.Schemata[typ]
@@ -258,7 +258,7 @@ func (f *Flattener) loadUnflattenedSchema(typ TypeIdent) (*apiext.JSONSchemaProp
258258

259259
// FlattenType flattens the given pre-loaded type, removing any references from it.
260260
// It deep-copies the schema first, so it won't affect the parser's version of the schema.
261-
func (f *Flattener) FlattenType(typ TypeIdent) *apiext.JSONSchemaProps {
261+
func (f *Flattener) FlattenType(typ TypeIdent) *apiextensionsv1.JSONSchemaProps {
262262
f.init()
263263
if cachedSchema, isCached := f.flattenedTypes[typ]; isCached {
264264
return &cachedSchema
@@ -275,7 +275,7 @@ func (f *Flattener) FlattenType(typ TypeIdent) *apiext.JSONSchemaProps {
275275

276276
// FlattenSchema flattens the given schema, removing any references.
277277
// It deep-copies the schema first, so the input schema won't be affected.
278-
func (f *Flattener) FlattenSchema(baseSchema apiext.JSONSchemaProps, currentPackage *loader.Package) *apiext.JSONSchemaProps {
278+
func (f *Flattener) FlattenSchema(baseSchema apiextensionsv1.JSONSchemaProps, currentPackage *loader.Package) *apiextensionsv1.JSONSchemaProps {
279279
resSchema := baseSchema.DeepCopy()
280280
EditSchema(resSchema, &flattenVisitor{
281281
Flattener: f,
@@ -332,7 +332,7 @@ func identFromRef(ref string, contextPkg *loader.Package) (TypeIdent, error) {
332332
// preserveFields copies documentation fields from src into dst, preserving
333333
// field-level documentation when flattening, and preserving field-level validation
334334
// as allOf entries.
335-
func preserveFields(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps) {
335+
func preserveFields(dst *apiextensionsv1.JSONSchemaProps, src apiextensionsv1.JSONSchemaProps) {
336336
srcDesc := src.Description
337337
srcTitle := src.Title
338338
srcExDoc := src.ExternalDocs
@@ -341,8 +341,8 @@ func preserveFields(dst *apiext.JSONSchemaProps, src apiext.JSONSchemaProps) {
341341
src.Description, src.Title, src.ExternalDocs, src.Example = "", "", nil, nil
342342

343343
src.Ref = nil
344-
*dst = apiext.JSONSchemaProps{
345-
AllOf: []apiext.JSONSchemaProps{*dst, src},
344+
*dst = apiextensionsv1.JSONSchemaProps{
345+
AllOf: []apiextensionsv1.JSONSchemaProps{*dst, src},
346346

347347
// keep these, in case the source field doesn't specify anything useful
348348
Description: dst.Description,
@@ -371,11 +371,11 @@ type flattenVisitor struct {
371371

372372
currentPackage *loader.Package
373373
currentType *TypeIdent
374-
currentSchema *apiext.JSONSchemaProps
375-
originalField apiext.JSONSchemaProps
374+
currentSchema *apiextensionsv1.JSONSchemaProps
375+
originalField apiextensionsv1.JSONSchemaProps
376376
}
377377

378-
func (f *flattenVisitor) Visit(baseSchema *apiext.JSONSchemaProps) SchemaVisitor {
378+
func (f *flattenVisitor) Visit(baseSchema *apiextensionsv1.JSONSchemaProps) SchemaVisitor {
379379
if baseSchema == nil {
380380
// end-of-node marker, cache the results
381381
if f.currentType != nil {
@@ -421,7 +421,7 @@ func (f *flattenVisitor) Visit(baseSchema *apiext.JSONSchemaProps) SchemaVisitor
421421

422422
// avoid loops (which shouldn't exist, but just in case)
423423
// by marking a nil cached pointer before we start recursing
424-
f.cacheType(refIdent, apiext.JSONSchemaProps{})
424+
f.cacheType(refIdent, apiextensionsv1.JSONSchemaProps{})
425425

426426
return &flattenVisitor{
427427
Flattener: f.Flattener,

0 commit comments

Comments
 (0)