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

refine names and errors #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions pkg/library/libraryapplyconfiguration/apply_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ var (
_ AllDesiredMutationsGetter = &applyConfiguration{}
)

func ValidateAllDesiredMutationsGetter(allDesiredMutationsGetter AllDesiredMutationsGetter, allAllowedOutputResources *libraryoutputresources.OutputResources) error {
func ValidateUnfilteredMutations(allDesiredMutationsGetter AllDesiredMutationsGetter, allAllowedOutputResources *libraryoutputresources.OutputResources) []error {
errs := []error{}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: move closer to where it is used.


if allDesiredMutationsGetter == nil {
return fmt.Errorf("applyConfiguration is required")
return []error{fmt.Errorf("applyConfiguration is required")}
}

allMutationRequests := []manifestclient.SerializedRequestish{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that allMutationRequests contains duplicate entries based on the code at

Expand Down Expand Up @@ -74,7 +74,30 @@ func ValidateAllDesiredMutationsGetter(allDesiredMutationsGetter AllDesiredMutat
errs = append(errs, fmt.Errorf("%d output-resource were produced, but not present in the specified output: %v", len(unspecifiedOutputIdentifiers), strings.Join(unspecifiedOutputIdentifiers, ", ")))
}

return errors.Join(errs...)
return errs
}

func ValidateAllDesiredMutationsGetter(allDesiredMutationsGetter AllDesiredMutationsGetter, allAllowedOutputResources *libraryoutputresources.OutputResources) []error {
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous function (ValidateUnfilteredMutations) checks if a request is on the allowed list for all resources.

Shouldn't this function instead check if mutations for the given cluster type are on the allowed list specifically for that cluster type?

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, nvm, pruning/filtering is done at

filteredRequests := []manifestclient.SerializedRequestish{}

errs := []error{}

if allDesiredMutationsGetter == nil {
return []error{fmt.Errorf("applyConfiguration is required")}
}

allMutationRequests := []manifestclient.SerializedRequestish{}
for _, clusterType := range sets.List(AllClusterTypes) {
desiredMutationsGetter := allDesiredMutationsGetter.MutationsForClusterType(clusterType)
switch {
case desiredMutationsGetter == nil:
errs = append(errs, fmt.Errorf("mutations for %q are required even if empty", clusterType))
case desiredMutationsGetter.GetClusterType() != clusterType:
errs = append(errs, fmt.Errorf("mutations for %q reported type=%q", clusterType, desiredMutationsGetter.GetClusterType()))
}

allMutationRequests = append(allMutationRequests, desiredMutationsGetter.Requests().AllRequests()...)
Copy link
Member

Choose a reason for hiding this comment

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

It looks like we're just appending to this slice, but not using it for anything else. Can it be dropped?

Copy link
Member

Choose a reason for hiding this comment

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

This function duplicates logic from ValidateUnfilteredMutations, right? We could extract the shared code into a helper and use it in both places.

}

return errs
}

func WriteApplyConfiguration(desiredApplyConfiguration AllDesiredMutationsGetter, outputDirectory string) error {
Expand Down
20 changes: 10 additions & 10 deletions pkg/library/libraryapplyconfiguration/client_mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ func FilterAllDesiredMutationsGetter(
}

ret.desiredMutationsByClusterType[clusterType] = &filteringSingleClusterDesiredMutationGetter{
delegate: in.MutationsForClusterType(clusterType),
resourceList: clusterTypeFilter,
delegate: in.MutationsForClusterType(clusterType),
allowedResourceList: clusterTypeFilter,
}
}

return ret
}

type filteringSingleClusterDesiredMutationGetter struct {
delegate SingleClusterDesiredMutationGetter
resourceList *libraryoutputresources.ResourceList
delegate SingleClusterDesiredMutationGetter
allowedResourceList *libraryoutputresources.ResourceList
}

func (f filteringSingleClusterDesiredMutationGetter) GetClusterType() ClusterType {
Expand All @@ -83,8 +83,8 @@ func (f filteringSingleClusterDesiredMutationGetter) GetClusterType() ClusterTyp

func (f filteringSingleClusterDesiredMutationGetter) Requests() MutationActionReader {
return &filteringMutationActionReader{
delegate: f.delegate.Requests(),
resourceList: f.resourceList,
delegate: f.delegate.Requests(),
allowedResourceList: f.allowedResourceList,
}
}

Expand All @@ -94,20 +94,20 @@ var (
)

type filteringMutationActionReader struct {
delegate MutationActionReader
resourceList *libraryoutputresources.ResourceList
delegate MutationActionReader
allowedResourceList *libraryoutputresources.ResourceList
}

func (f filteringMutationActionReader) ListActions() []manifestclient.Action {
return f.delegate.ListActions()
}

func (f filteringMutationActionReader) RequestsForAction(action manifestclient.Action) []manifestclient.SerializedRequestish {
return FilterSerializedRequests(f.delegate.RequestsForAction(action), f.resourceList)
return FilterSerializedRequests(f.delegate.RequestsForAction(action), f.allowedResourceList)
}

func (f filteringMutationActionReader) AllRequests() []manifestclient.SerializedRequestish {
return FilterSerializedRequests(f.delegate.AllRequests(), f.resourceList)
return FilterSerializedRequests(f.delegate.AllRequests(), f.allowedResourceList)
}

func FilterSerializedRequests(requests []manifestclient.SerializedRequestish, allowedResources *libraryoutputresources.ResourceList) []manifestclient.SerializedRequestish {
Expand Down
8 changes: 4 additions & 4 deletions pkg/library/libraryapplyconfiguration/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (o *applyConfigurationOptions) Run(ctx context.Context) error {
}

// also validate the raw results because filtering may have eliminated "bad" output.
if err := ValidateAllDesiredMutationsGetter(result, allAllowedOutputResources); err != nil {
errs = append(errs, err)
if localErrs := ValidateUnfilteredMutations(result, allAllowedOutputResources); len(localErrs) > 0 {
errs = append(errs, localErrs...)
}

// now filter the results and check them
filteredResult := FilterAllDesiredMutationsGetter(result, allAllowedOutputResources)
if err := ValidateAllDesiredMutationsGetter(filteredResult, allAllowedOutputResources); err != nil {
errs = append(errs, err)
if localErrs := ValidateAllDesiredMutationsGetter(filteredResult, allAllowedOutputResources); len(localErrs) > 0 {
errs = append(errs, localErrs...)
}

if err := WriteApplyConfiguration(filteredResult, o.outputDirectory); err != nil {
Expand Down