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

Track2 sdk:add etag support in network resources #7458

Merged
merged 1 commit into from
Nov 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/azclient/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ build: ## Build manager binary.
pushd client-gen; CGO_ENABLED=0 go build -o ../bin/client-gen ./cmd/client-gen/ ;popd
pushd client-gen; CGO_ENABLED=0 go build -o ../bin/typescaffold ./cmd/typescaffold/;popd

.PHONY: test
test: ## Run tests.
go test -v ./...

.PHONY: generate
generate: install-dependencies build generatecode generateimpl fmt vet-all

Expand Down
5 changes: 4 additions & 1 deletion pkg/azclient/client-gen/cmd/typescaffold/type_scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type TypeScaffoldOptions struct {
Expand bool
RateLimitKey string
CrossSubFactory bool
Etag bool
}

var (
Expand Down Expand Up @@ -71,7 +72,7 @@ import (
)
`
TypeResourceTemplate = `
// +azure:client:verbs={{join .Verbs ";"}},resource={{.Resource}},packageName={{.Package}},packageAlias={{tolower .PackageAlias}},clientName={{.ClientName}},expand={{.Expand}}{{with .RateLimitKey}},rateLimitKey={{.}}{{end}}
// +azure:client:verbs={{join .Verbs ";"}},resource={{.Resource}},packageName={{.Package}},packageAlias={{tolower .PackageAlias}},clientName={{.ClientName}},expand={{.Expand}}{{with .RateLimitKey}},rateLimitKey={{.}}{{end}}{{with .Etag}},etag={{.}}{{end}}
type Interface interface {
{{ $expandable := .Expand}}
{{ $packageAlias := .PackageAlias}}
Expand Down Expand Up @@ -193,6 +194,8 @@ func main() {
rootCmd.Flags().StringVar(&scaffoldOptions.SubResource, "subresource", "", "subresource name")
rootCmd.Flags().StringVar(&scaffoldOptions.RateLimitKey, "ratelimitkey", "", "ratelimit config key")
rootCmd.Flags().BoolVar(&scaffoldOptions.CrossSubFactory, "cross-sub-factory-support", false, "cross sub factory support")
rootCmd.Flags().BoolVar(&scaffoldOptions.Etag, "etag", false, "support etag")

err := rootCmd.Execute()
if err != nil {
fmt.Printf("failed to generate interface %s\n", err.Error())
Expand Down
9 changes: 1 addition & 8 deletions pkg/azclient/client-gen/generator/codegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
if err := factoryGenerator.Generate(ctx); err != nil {
return err
}

fmt.Println("Run go test ")

//nolint:gosec // G204 ignore this!
cmd = exec.Command("go", "test", "./...", "--timeout=30m")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
return nil
}

func (Generator) CheckFilter() loader.NodeFilter {
Expand Down
5 changes: 2 additions & 3 deletions pkg/azclient/client-gen/generator/genclientfactory_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ func (generator *ClientFactoryGenerator) Generate(_ *genall.GenerationContext) e
}
for _, v := range generator.clientRegistry {
if v.ClientGenConfig.CrossSubFactory {
importList["sync"] = map[string]struct{}{}
importList["strings"] = map[string]struct{}{}
break
importList["sync"] = make(map[string]struct{})
importList["strings"] = make(map[string]struct{})
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/azclient/client-gen/generator/gencode_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func generateClient(ctx *genall.GenerationContext, root *loader.Package, _ strin
if markerConf.OutOfSubscriptionScope && len(markerConf.Verbs) > 0 {
importList["go.opentelemetry.io/otel/attribute"] = make(map[string]struct{})
}
if markerConf.Etag {
importList["sigs.k8s.io/cloud-provider-azure/pkg/azclient/utils"] = make(map[string]struct{})
importList["sigs.k8s.io/cloud-provider-azure/pkg/azclient/policy/etag"] = make(map[string]struct{})
}
if err := WriteToFile(ctx, root, "zz_generated_client.go", headerText, importList, &outContent); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/azclient/client-gen/generator/gentest_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func generateTestCase(ctx *genall.GenerationContext, root *loader.Package, _ str

importList["context"] = make(map[string]struct{})
importList["github.com/onsi/ginkgo/v2"] = map[string]struct{}{}
if markerConf.Etag {
importList["github.com/Azure/azure-sdk-for-go/sdk/azcore/to"] = make(map[string]struct{})
}
return WriteToFile(ctx, root, root.Name+"_test.go", headerText, importList, &outContent)
}

Expand Down
40 changes: 25 additions & 15 deletions pkg/azclient/client-gen/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ClientGenConfig struct {
Expand bool `marker:"expand,optional"`
RateLimitKey string `marker:"rateLimitKey,optional"`
CrossSubFactory bool `marker:"crossSubFactory,optional"`
Etag bool `marker:"etag,optional"`
}

var ClientTemplate = template.Must(template.New("object-scaffolding-client-struct").Parse(`
Expand All @@ -45,7 +46,9 @@ func New({{if not .OutOfSubscriptionScope}}subscriptionID string, {{end}}credent
options = utils.GetDefaultOption()
}
tr := options.TracingProvider.NewTracer(utils.ModuleName, utils.ModuleVersion)

{{with .Etag}}
options.ClientOptions.PerCallPolicies = append(options.ClientOptions.PerCallPolicies, utils.FuncPolicyWrapper(etag.AppendEtag))
{{- end}}
client, err := {{.PackageAlias}}.New{{.ClientName}}({{if not .OutOfSubscriptionScope}}subscriptionID,{{end}} credential, options)
if err != nil {
return nil, err
Expand Down Expand Up @@ -294,8 +297,8 @@ var _ = ginkgo.Describe("{{.ClientName}}",ginkgo.Ordered, func() {
}

{{if $HasCreateOrUpdate}}
ginkgo.When("creation requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
ginkgo.When("creation requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
newResource, err := realClient.CreateOrUpdate(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName, *newResource)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(newResource).NotTo(gomega.BeNil())
Expand All @@ -304,51 +307,58 @@ ginkgo.It("should not return error", func(ctx context.Context) {
})
{{end -}}
{{if $HasGet}}
ginkgo.When("get requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
ginkgo.When("get requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
newResource, err := realClient.Get(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName{{if .Expand}}, nil{{end}})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(newResource).NotTo(gomega.BeNil())
})
})
ginkgo.When("invalid get requests are raised", func() {
ginkgo.It("should return 404 error", func(ctx context.Context) {
ginkgo.When("invalid get requests are raised", func() {
ginkgo.It("should return 404 error", func(ctx context.Context) {
newResource, err := realClient.Get(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName+"notfound"{{if .Expand}}, nil{{end}})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(newResource).To(gomega.BeNil())
})
})
{{end -}}
{{if $HasCreateOrUpdate}}
ginkgo.When("update requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
ginkgo.When("update requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
newResource, err := realClient.CreateOrUpdate(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName, *newResource)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(newResource).NotTo(gomega.BeNil())
})
{{if .Etag}}
ginkgo.It("should return error", func(ctx context.Context) {
newResource.Etag = to.Ptr("invalid")
_, err := realClient.CreateOrUpdate(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName, *newResource)
gomega.Expect(err).To(gomega.HaveOccurred())
})
{{end -}}
})
{{end -}}
{{if or $HasListByRG $HasList}}
ginkgo.When("list requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
ginkgo.When("list requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
resourceList, err := realClient.List(ctx, resourceGroupName,{{with .SubResource}}parentResourceName{{end}})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(resourceList).NotTo(gomega.BeNil())
gomega.Expect(len(resourceList)).To(gomega.Equal(1))
gomega.Expect(*resourceList[0].Name).To(gomega.Equal(resourceName))
})
})
ginkgo.When("invalid list requests are raised", func() {
ginkgo.It("should return error", func(ctx context.Context) {
ginkgo.When("invalid list requests are raised", func() {
ginkgo.It("should return error", func(ctx context.Context) {
resourceList, err := realClient.List(ctx, resourceGroupName+"notfound",{{with .SubResource}}parentResourceName{{end}})
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(resourceList).To(gomega.BeNil())
})
})
{{end -}}
{{if $HasDelete}}
ginkgo.When("deletion requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
ginkgo.When("deletion requests are raised", func() {
ginkgo.It("should not return error", func(ctx context.Context) {
err = realClient.Delete(ctx, resourceGroupName,{{with .SubResource}}parentResourceName,{{end}} resourceName)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/azclient/loadbalancerclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/utils"
)

// +azure:client:verbs=get;createorupdate;delete;list,resource=LoadBalancer,packageName=github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6,packageAlias=armnetwork,clientName=LoadBalancersClient,expand=true,rateLimitKey=loadBalancerRateLimit
// +azure:client:verbs=get;createorupdate;delete;list,resource=LoadBalancer,packageName=github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6,packageAlias=armnetwork,clientName=LoadBalancersClient,expand=true,rateLimitKey=loadBalancerRateLimit,etag=true
type Interface interface {
utils.GetWithExpandFunc[armnetwork.LoadBalancer]
utils.CreateOrUpdateFunc[armnetwork.LoadBalancer]
Expand Down
7 changes: 7 additions & 0 deletions pkg/azclient/loadbalancerclient/loadbalancerclient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading