Skip to content

Commit 4db5feb

Browse files
authored
Support editing of packages (#3060)
1 parent 6f1fc3e commit 4db5feb

File tree

11 files changed

+306
-28
lines changed

11 files changed

+306
-28
lines changed

porch/api/generated/openapi/zz_generated.openapi.go

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

porch/api/porch/types_packagerevisions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const (
8686
TaskTypeInit TaskType = "init"
8787
TaskTypeClone TaskType = "clone"
8888
TaskTypePatch TaskType = "patch"
89+
TaskTypeEdit TaskType = "edit"
8990
TaskTypeEval TaskType = "eval"
9091
)
9192

@@ -94,6 +95,7 @@ type Task struct {
9495
Init *PackageInitTaskSpec `json:"init,omitempty"`
9596
Clone *PackageCloneTaskSpec `json:"clone,omitempty"`
9697
Patch *PackagePatchTaskSpec `json:"patch,omitempty"`
98+
Edit *PackageEditTaskSpec `json:"edit,omitempty"`
9799
Eval *FunctionEvalTaskSpec `json:"eval,omitempty"`
98100
}
99101

@@ -153,6 +155,10 @@ type PatchSpec struct {
153155
PatchType PatchType `json:"patchType,omitempty"`
154156
}
155157

158+
type PackageEditTaskSpec struct {
159+
Source *PackageRevisionRef `json:"sourceRef,omitempty"`
160+
}
161+
156162
type RepositoryType string
157163

158164
const (

porch/api/porch/v1alpha1/types_packagerevisions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const (
9494
TaskTypeInit TaskType = "init"
9595
TaskTypeClone TaskType = "clone"
9696
TaskTypePatch TaskType = "patch"
97+
TaskTypeEdit TaskType = "edit"
9798
TaskTypeEval TaskType = "eval"
9899
)
99100

@@ -102,6 +103,7 @@ type Task struct {
102103
Init *PackageInitTaskSpec `json:"init,omitempty"`
103104
Clone *PackageCloneTaskSpec `json:"clone,omitempty"`
104105
Patch *PackagePatchTaskSpec `json:"patch,omitempty"`
106+
Edit *PackageEditTaskSpec `json:"edit,omitempty"`
105107
Eval *FunctionEvalTaskSpec `json:"eval,omitempty"`
106108
}
107109

@@ -161,6 +163,10 @@ type PatchSpec struct {
161163
PatchType PatchType `json:"patchType,omitempty"`
162164
}
163165

166+
type PackageEditTaskSpec struct {
167+
Source *PackageRevisionRef `json:"sourceRef,omitempty"`
168+
}
169+
164170
type RepositoryType string
165171

166172
const (

porch/api/porch/v1alpha1/zz_generated.conversion.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

porch/api/porch/v1alpha1/zz_generated.deepcopy.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

porch/api/porch/zz_generated.deepcopy.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

porch/engine/pkg/engine/clone.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,14 @@ func (m *clonePackageMutation) Apply(ctx context.Context, resources repository.P
7474
func (m *clonePackageMutation) cloneFromRegisteredRepository(ctx context.Context, ref *api.PackageRevisionRef) (repository.PackageResources, error) {
7575
if ref.Name == "" {
7676
return repository.PackageResources{}, fmt.Errorf("upstreamRef.name is required")
77-
78-
}
79-
repositoryName, err := parseUpstreamRepository(ref.Name)
80-
if err != nil {
81-
return repository.PackageResources{}, err
82-
}
83-
var resolved configapi.Repository
84-
if err := m.referenceResolver.ResolveReference(ctx, m.namespace, repositoryName, &resolved); err != nil {
85-
return repository.PackageResources{}, fmt.Errorf("cannot find repository %s/%s: %w", m.namespace, repositoryName, err)
8677
}
8778

88-
open, err := m.cad.OpenRepository(ctx, &resolved)
79+
revision, err := (&PackageFetcher{
80+
cad: m.cad,
81+
referenceResolver: m.referenceResolver,
82+
}).FetchRevision(ctx, ref, m.namespace)
8983
if err != nil {
90-
return repository.PackageResources{}, err
91-
}
92-
93-
revisions, err := open.ListPackageRevisions(ctx, repository.ListPackageRevisionFilter{KubeObjectName: ref.Name})
94-
if err != nil {
95-
return repository.PackageResources{}, err
96-
}
97-
98-
var revision repository.PackageRevision
99-
for _, rev := range revisions {
100-
if rev.KubeObjectName() == ref.Name {
101-
revision = rev
102-
break
103-
}
104-
}
105-
if revision == nil {
106-
return repository.PackageResources{}, fmt.Errorf("cannot find package revision %q", ref.Name)
84+
return repository.PackageResources{}, fmt.Errorf("failed to fetch package revision %q: %w", ref.Name, err)
10785
}
10886

10987
resources, err := revision.GetResources(ctx)

porch/engine/pkg/engine/edit.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package engine
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
22+
"github.com/GoogleContainerTools/kpt/porch/engine/pkg/kpt"
23+
"github.com/GoogleContainerTools/kpt/porch/repository/pkg/repository"
24+
"go.opentelemetry.io/otel/trace"
25+
)
26+
27+
type editPackageMutation struct {
28+
task *api.Task
29+
name string
30+
namespace string
31+
cad CaDEngine
32+
referenceResolver ReferenceResolver
33+
}
34+
35+
var _ mutation = &editPackageMutation{}
36+
37+
func (m *editPackageMutation) Apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.Task, error) {
38+
ctx, span := tracer.Start(ctx, "editPackageMutation::Apply", trace.WithAttributes())
39+
defer span.End()
40+
41+
sourceRef := m.task.Edit.Source
42+
43+
sourceResources, err := (&PackageFetcher{
44+
cad: m.cad,
45+
referenceResolver: m.referenceResolver,
46+
}).FetchResources(ctx, sourceRef, m.namespace)
47+
if err != nil {
48+
return repository.PackageResources{}, nil, fmt.Errorf("failed to fetch resources for package %q: %w", sourceRef.Name, err)
49+
}
50+
51+
// Update Kptfile
52+
if err := kpt.UpdateKptfileName(m.name, sourceResources.Spec.Resources); err != nil {
53+
return repository.PackageResources{}, nil, fmt.Errorf("failed to update package name %q: %w", sourceRef.Name, err)
54+
}
55+
56+
return repository.PackageResources{
57+
Contents: sourceResources.Spec.Resources,
58+
}, &api.Task{}, nil
59+
}

porch/engine/pkg/engine/engine.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ func (cad *cadEngine) mapTaskToMutation(ctx context.Context, obj *api.PackageRev
183183
case api.TaskTypePatch:
184184
return buildPatchMutation(ctx, task)
185185

186+
case api.TaskTypeEdit:
187+
if task.Edit == nil {
188+
return nil, fmt.Errorf("edit not set for task of type %q", task.Type)
189+
}
190+
return &editPackageMutation{
191+
task: task,
192+
namespace: obj.Namespace,
193+
name: obj.Spec.PackageName,
194+
cad: cad,
195+
referenceResolver: cad.referenceResolver,
196+
}, nil
197+
186198
case api.TaskTypeEval:
187199
if task.Eval == nil {
188200
return nil, fmt.Errorf("eval not set for task of type %q", task.Type)

0 commit comments

Comments
 (0)