-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from Nordix/refactor-mutations
Refactor task package to make mutations more visible
- Loading branch information
Showing
6 changed files
with
223 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2024 The kpt and Nephio Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package task | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
api "github.com/nephio-project/porch/api/porch/v1alpha1" | ||
"github.com/nephio-project/porch/pkg/repository" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
var _ mutation = &replaceResourcesMutation{} | ||
|
||
type replaceResourcesMutation struct { | ||
newResources *api.PackageRevisionResources | ||
oldResources *api.PackageRevisionResources | ||
} | ||
|
||
func (m *replaceResourcesMutation) apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.TaskResult, error) { | ||
_, span := tracer.Start(ctx, "mutationReplaceResources::Apply", trace.WithAttributes()) | ||
defer span.End() | ||
|
||
patch := &api.PackagePatchTaskSpec{} | ||
|
||
old := resources.Contents | ||
new, err := healConfig(old, m.newResources.Spec.Resources) | ||
if err != nil { | ||
return repository.PackageResources{}, nil, fmt.Errorf("failed to heal resources: %w", err) | ||
} | ||
|
||
for k, newV := range new { | ||
oldV, ok := old[k] | ||
// New config or changed config | ||
if !ok { | ||
patchSpec := api.PatchSpec{ | ||
File: k, | ||
PatchType: api.PatchTypeCreateFile, | ||
Contents: newV, | ||
} | ||
patch.Patches = append(patch.Patches, patchSpec) | ||
} else if newV != oldV { | ||
patchSpec, err := GeneratePatch(k, oldV, newV) | ||
if err != nil { | ||
return repository.PackageResources{}, nil, fmt.Errorf("error generating patch: %w", err) | ||
} | ||
if patchSpec.Contents == "" { | ||
continue | ||
} | ||
patch.Patches = append(patch.Patches, patchSpec) | ||
} | ||
} | ||
for k := range old { | ||
// Deleted config | ||
if _, ok := new[k]; !ok { | ||
patchSpec := api.PatchSpec{ | ||
File: k, | ||
PatchType: api.PatchTypeDeleteFile, | ||
} | ||
patch.Patches = append(patch.Patches, patchSpec) | ||
} | ||
} | ||
// If patch is empty, don't create a Task. | ||
var taskResult *api.TaskResult | ||
if len(patch.Patches) > 0 { | ||
taskResult = &api.TaskResult{ | ||
Task: &api.Task{ | ||
Type: api.TaskTypePatch, | ||
Patch: patch, | ||
}, | ||
} | ||
} | ||
return repository.PackageResources{Contents: new}, taskResult, nil | ||
} |
Oops, something went wrong.