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

Render status cleanup through "PorchFile" implementation #108

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
changing to using PorchFile instead of storing meta in Kptfile
  • Loading branch information
Catalin-Stratulat-Ericsson committed Aug 23, 2024
commit b6567744f474cb71b1a7799e0d0fd3f411dcdf90
8 changes: 8 additions & 0 deletions api/porch/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,14 @@ const (
LatestPackageRevisionValue = "true"
)

// Porchfile
type PorchFile struct {
metav1.TypeMeta `yaml:",inline"`
metav1.ObjectMeta `yaml:"metadata,omitempty"`

Status *RenderStatus `yaml:"status,omitempty"`
}

// PackageRevisionList
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type PackageRevisionList struct {
19 changes: 19 additions & 0 deletions internal/kpt/pkg/pkg.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ import (
"sort"

rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1"
"github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/errors"
"github.com/nephio-project/porch/internal/kpt/types"
"github.com/nephio-project/porch/internal/kpt/util/git"
@@ -266,6 +267,24 @@ func DecodeKptfile(in io.Reader) (*kptfilev1.KptFile, error) {
return kf, nil
}

func DecodePorchfile(in io.Reader) (*v1alpha1.PorchFile, error) {
pf := &v1alpha1.PorchFile{}
c, err := io.ReadAll(in)
if err != nil {
return pf, err
}
// if err := CheckKptfileVersion(c); err != nil {
// return kf, err
// }

d := yaml.NewDecoder(bytes.NewBuffer(c))
d.KnownFields(true)
if err := d.Decode(pf); err != nil {
return pf, err
}
return pf, nil
}

// CheckKptfileVersion verifies the apiVersion and kind of the resource
// within the Kptfile. If the legacy version is found, the DeprecatedKptfileError
// is returned. If the currently supported apiVersion and kind is found, no
35 changes: 23 additions & 12 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ import (
"go.opentelemetry.io/otel/trace"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -1035,14 +1036,18 @@ func (cad *cadEngine) UpdatePackageResources(ctx context.Context, repositoryObj
runtime: cad.runtime,
}})

var kptFileContents kptfile.KptFile
unmarshErr := yaml.Unmarshal([]byte(appliedResources.Contents["Kptfile"]), &kptFileContents)
if unmarshErr != nil {
klog.Errorf("Error in Umarshaling Kptfile: %v", unmarshErr)
}

kptFileContents.Status = &kptfile.Status{
RenderStatus: &api.RenderStatus{
porchFileContents := api.PorchFile{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1alpha1",
Kind: "PorchFile",
},
ObjectMeta: metav1.ObjectMeta{
Name: "Porch-Metadata",
Annotations: map[string]string{
"config.kubernetes.io/local-config": "true",
},
},
Status: &api.RenderStatus{
Result: api.ResultList{
TypeMeta: renderStatus.Result.TypeMeta,
// ignore object meta (not relevant)
@@ -1053,14 +1058,20 @@ func (cad *cadEngine) UpdatePackageResources(ctx context.Context, repositoryObj
},
}

byteKptfile, marshErr := yaml.Marshal(&kptFileContents)
if value, ok := appliedResources.Contents["PorchFile"]; ok {
unmarshErr := yaml.Unmarshal([]byte(value), &porchFileContents)
if unmarshErr != nil {
klog.Errorf("Error in Umarshaling PorchFile: %v", unmarshErr)
}
}

bytePorchfile, marshErr := yaml.Marshal(&porchFileContents)
if marshErr != nil {
klog.Errorf("Error in Marshaling Kptfile: %v", marshErr)
klog.Errorf("Error in Marshaling PorchFile: %v", marshErr)
}
postRenderPkgRev := new

postRenderPkgRev.Spec.Resources["Kptfile"] = string(byteKptfile)
postRenderPkgRev.Status.RenderStatus = *renderStatus
postRenderPkgRev.Spec.Resources["PorchFile"] = string(bytePorchfile)

mutations = []mutation{
&mutationReplaceResources{
26 changes: 21 additions & 5 deletions pkg/git/package.go
Original file line number Diff line number Diff line change
@@ -193,13 +193,13 @@ func (p *gitPackageRevision) GetResources(ctx context.Context) (*v1alpha1.Packag

key := p.Key()

kf, err := p.GetKptfile(ctx)
pf, err := p.GetPorchfile(ctx)
if err != nil {
klog.Errorf("error: %v", err)
}
var dummyrenderstatus v1alpha1.RenderStatus
if kf.Status != nil {
dummyrenderstatus = *kf.Status.RenderStatus
var rs v1alpha1.RenderStatus
if pf.Status != nil {
rs = *pf.Status
}
return &v1alpha1.PackageRevisionResources{
TypeMeta: metav1.TypeMeta{
@@ -225,7 +225,7 @@ func (p *gitPackageRevision) GetResources(ctx context.Context) (*v1alpha1.Packag
Resources: resources,
},
Status: v1alpha1.PackageRevisionResourcesStatus{
RenderStatus: dummyrenderstatus,
RenderStatus: rs,
},
}, nil
}
@@ -246,6 +246,22 @@ func (p *gitPackageRevision) GetKptfile(ctx context.Context) (kptfile.KptFile, e
return *kf, nil
}

func (p *gitPackageRevision) GetPorchfile(ctx context.Context) (v1alpha1.PorchFile, error) {
resources, err := p.repo.GetResources(p.tree)
if err != nil {
return v1alpha1.PorchFile{}, fmt.Errorf("error loading package resources: %w", err)
}
pfString, found := resources["PorchFile"]
if !found {
return v1alpha1.PorchFile{}, nil
}
pf, err := pkg.DecodePorchfile(strings.NewReader(pfString))
if err != nil {
return v1alpha1.PorchFile{}, fmt.Errorf("error decoding PorchFile: %w", err)
}
return *pf, nil
}

// GetUpstreamLock returns the upstreamLock info present in the Kptfile of the package.
func (p *gitPackageRevision) GetUpstreamLock(ctx context.Context) (kptfile.Upstream, kptfile.UpstreamLock, error) {
kf, err := p.GetKptfile(ctx)
2 changes: 0 additions & 2 deletions pkg/kpt/api/kptfile/v1/types.go
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@ package v1
import (
"fmt"

api "github.com/nephio-project/porch/api/porch/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
@@ -380,7 +379,6 @@ func (i Inventory) IsValid() bool {

type Status struct {
Conditions []Condition `yaml:"conditions,omitempty" json:"conditions,omitempty"`
RenderStatus *api.RenderStatus `json:"renderStatus,omitempty"`
}

type Condition struct {