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

0.35.x codegen link generation config #568

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions changelog/v0.35.4/add-delete-ns-snapshot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/gloo/issues/9274
resolvesIssue: false
description: >-
Adds the `RemoveMatches` method in `snapshot_template.go` that removes all resources that match the given predicate.
Adds the ability for event loops to determine if the Syncer passed is a `SyncDecider` to determine if a sync is necessary.

6 changes: 6 additions & 0 deletions changelog/v0.35.4/fix-duplicate-predicate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/gloo/issues/9274
resolvesIssue: false
description: Fixes the issue of multiple declarations of Predicate when multiple snapshots are generated

6 changes: 6 additions & 0 deletions changelog/v0.36.2/no-external-links.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/solo-projects/issues/6768
resolvesIssue: false
description: >-
Update protobuf processing to no longer render links to locally hosted external API docs, as they are being removed.
2 changes: 2 additions & 0 deletions pkg/api/v1/resources/core/metadata_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ func (m *Metadata) Ref() *ResourceRef {
func (m *Metadata) Match(ref *ResourceRef) bool {
return m.GetNamespace() == ref.GetNamespace() && m.GetName() == ref.GetName()
}

type Predicate func(metadata *Metadata) bool
20 changes: 20 additions & 0 deletions pkg/code-generator/codegen/templates/event_loop_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,33 @@ func (el *{{ lower_camel .GoName }}EventLoop) Run(namespaces []string, opts clie
go errutils.AggregateErrs(opts.Ctx, errs, emitterErrs, "{{ .Project.ProjectConfig.Version }}.emitter errors")
go func() {
var channelClosed bool

// create a new context for each loop, cancel it before each loop
var cancel context.CancelFunc = func() {}

// use closure to allow cancel function to be updated as context changes
defer func() { cancel() }()

// cache the previous snapshot for comparison
var previousSnapshot *{{ .GoName }}Snapshot

for {
select {
case snapshot, ok := <-watch:
if !ok {
return
}

if syncDecider, isDecider := el.syncer.({{ .GoName }}SyncDecider); isDecider {
if shouldSync := syncDecider.ShouldSync(previousSnapshot, snapshot); !shouldSync {
continue // skip syncing this syncer
}
} else if syncDeciderWithContext, isDecider := el.syncer.({{ .GoName }}SyncDeciderWithContext); isDecider {
if shouldSync := syncDeciderWithContext.ShouldSync(opts.Ctx, previousSnapshot, snapshot); !shouldSync {
continue // skip syncing this syncer
}
}

// cancel any open watches from previous loop
cancel()

Expand Down Expand Up @@ -129,6 +146,9 @@ func (el *{{ lower_camel .GoName }}EventLoop) Run(namespaces []string, opts clie
channelClosed = true
close(el.ready)
}

previousSnapshot = snapshot

case <-opts.Ctx.Done():
return
}
Expand Down
22 changes: 18 additions & 4 deletions pkg/code-generator/codegen/templates/snapshot_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rotisserie/eris"
"github.com/solo-io/go-utils/hashutils"
"go.uber.org/zap"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
)

type {{ .GoName }}Snapshot struct {
Expand Down Expand Up @@ -106,15 +107,28 @@ func (s *{{ .GoName }}Snapshot) RemoveFromResourceList(resource resources.Resour
break
}
}
return nil
return nil
{{- end }}
default:
return eris.Errorf("did not remove the resource because its type does not exist [%T]", resource)
}
}

func (s *{{ .GoName }}Snapshot) RemoveMatches(predicate core.Predicate) {
{{- range .Resources }}
var {{ upper_camel .PluralName }} {{ .ImportPrefix }}{{ .Name }}List
for _, res := range s.{{ upper_camel .PluralName }} {
if matches := predicate(res.GetMetadata()); !matches {
{{ upper_camel .PluralName }} = append({{ upper_camel .PluralName }}, res)
}
}
s.{{ upper_camel .PluralName }} = {{ upper_camel .PluralName }}
{{- end }}
}


func (s *{{ .GoName }}Snapshot) UpsertToResourceList(resource resources.Resource) error {
refKey := resource.GetMetadata().Ref().Key()
refKey := resource.GetMetadata().Ref().Key()
switch typed := resource.(type) {
{{- range .Resources }}
case *{{ .ImportPrefix }}{{ .Name }}:
Expand All @@ -129,7 +143,7 @@ func (s *{{ .GoName }}Snapshot) UpsertToResourceList(resource resources.Resource
s.{{ upper_camel .PluralName }} = append(s.{{ upper_camel .PluralName }}, typed)
}
s.{{ upper_camel .PluralName }}.Sort()
return nil
return nil
{{- end }}
default:
return eris.Errorf("did not add/replace the resource type because it does not exist %T", resource)
Expand Down Expand Up @@ -176,7 +190,7 @@ func (s {{ .GoName }}Snapshot) Stringer() {{ .GoName }}SnapshotStringer {
var {{.GoName }}GvkToHashableResource = map[schema.GroupVersionKind]func() resources.HashableResource {
{{- range .Resources}}
{{ .ImportPrefix }}{{ .Name }}GVK: {{ .ImportPrefix }}New{{ .Name }}HashableResource,
{{- end }}
{{- end }}
}

`))
9 changes: 8 additions & 1 deletion pkg/code-generator/docgen/funcs/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,15 @@ func linkForField(project *model.Project, docsOptions *options.DocsOptions) func
linkedFile = filepath.Base(file.GetName())
//return "", errors.Errorf("failed to get generated file path for proto %v in list %v", file.GetName(), project.Request.FileToGenerate)
}
linkedFile = relativeFilename(forFile.GetName(), linkedFile)

// Skip links for packages that are configured to be skipped
for _, pkg := range docsOptions.RenderOptions.GetSkipLinksForPathPrefixes() {
if strings.HasPrefix(linkedFile, pkg) {
return typeName, nil
}
}

linkedFile = relativeFilename(forFile.GetName(), linkedFile)
if docsOptions.Output == options.Restructured {
linkText = ":ref:`message." + strings.TrimPrefix(field.GetTypeName(), ".") + "`"
} else {
Expand Down
19 changes: 17 additions & 2 deletions pkg/code-generator/docgen/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,23 @@ type HugoOptions struct {
}

type DocsOptions struct {
Output DocsOutput
HugoOptions *HugoOptions
Output DocsOutput
HugoOptions *HugoOptions
RenderOptions *RenderOptions
}

// RenderOptions provides options for rendering documentation
type RenderOptions struct {
// SkipLinksForPathPrefixes is a list of file path prefixes of APIs to which we should not be attempting to link
// For example: "github.com/solo-io/gloo/projects/gloo/api/external"
SkipLinksForPathPrefixes []string
}

func (o *RenderOptions) GetSkipLinksForPathPrefixes() []string {
if o == nil {
return nil
}
return o.SkipLinksForPathPrefixes
}

const (
Expand Down
20 changes: 20 additions & 0 deletions test/mocks/v1/testing_event_loop.sk.go

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

60 changes: 60 additions & 0 deletions test/mocks/v1/testing_snapshot.sk.go

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

20 changes: 20 additions & 0 deletions test/mocks/v1alpha1/testing_event_loop.sk.go

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

11 changes: 11 additions & 0 deletions test/mocks/v1alpha1/testing_snapshot.sk.go

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

Loading
Loading