Skip to content

Commit

Permalink
Refactored code to put repository implementations and cache implement…
Browse files Browse the repository at this point in the history
…ations behind interfaces
  • Loading branch information
liamfallon committed Jan 15, 2025
1 parent e72b5c9 commit 40de272
Show file tree
Hide file tree
Showing 76 changed files with 615 additions and 691 deletions.
1 change: 0 additions & 1 deletion api/porch/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ type NameMeta struct {
Namespace string `json:"namespace,omitempty"`
}


// PackageRevisionResources
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
3 changes: 1 addition & 2 deletions api/porch/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ type PackageRevisionResourcesStatus struct {
RenderStatus RenderStatus `json:"renderStatus,omitempty"`
}


// Package
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -559,4 +558,4 @@ type PackageStatus struct {
// LatestRevision identifies the package revision that is the latest
// published package revision belonging to this package
LatestRevision string `json:"latestRevision,omitempty"`
}
}
6 changes: 3 additions & 3 deletions api/porchconfig/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
type RepositoryContent string

const (
RepositoryContentPackage RepositoryContent = "Package"
RepositoryContentPackage RepositoryContent = "Package"
)

// RepositorySpec defines the desired state of Repository
Expand All @@ -60,12 +60,12 @@ type RepositorySpec struct {
Deployment bool `json:"deployment,omitempty"`
// Type of the repository (i.e. git, OCI)
Type RepositoryType `json:"type,omitempty"`
// The Content field is deprecated, please do not specify it in new manifests.
// The Content field is deprecated, please do not specify it in new manifests.
// For partial backward compatibility it is still recognized, but its only valid value is "Package", and if not specified its default value is also "Package".
// +kubebuilder:validation:XValidation:message="The 'content' field is deprecated, its only valid value is 'Package'",rule="self == '' || self == 'Package'"
// +kubebuilder:default="Package"
Content *RepositoryContent `json:"content,omitempty"`

// Git repository details. Required if `type` is `git`. Ignored if `type` is not `git`.
Git *GitRepository `json:"git,omitempty"`
// OCI repository details. Required if `type` is `oci`. Ignored if `type` is not `oci`.
Expand Down
49 changes: 25 additions & 24 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
internalapi "github.com/nephio-project/porch/internal/api/porchinternal/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/fnruntime"
"github.com/nephio-project/porch/pkg/cache"
"github.com/nephio-project/porch/pkg/cache/dbcache"
cachetypes "github.com/nephio-project/porch/pkg/cache/types"
"github.com/nephio-project/porch/pkg/engine"
"github.com/nephio-project/porch/pkg/meta"
"github.com/nephio-project/porch/pkg/registry/porch"
repoimpltypes "github.com/nephio-project/porch/pkg/repoimpl/types"
"google.golang.org/api/option"
"google.golang.org/api/sts/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -75,13 +76,13 @@ func init() {

// ExtraConfig holds custom apiserver config
type ExtraConfig struct {
CoreAPIKubeconfigPath string
CacheDirectory string
FunctionRunnerAddress string
DefaultImagePrefix string
RepoSyncFrequency time.Duration
UseGitCaBundle bool
MaxGrpcMessageSize int
CoreAPIKubeconfigPath string
CacheDirectory string
FunctionRunnerAddress string
DefaultImagePrefix string
RepoSyncFrequency time.Duration
UseUserDefinedCaBundle bool
MaxGrpcMessageSize int
}

// Config defines the config for the apiserver
Expand All @@ -94,7 +95,7 @@ type Config struct {
type PorchServer struct {
GenericAPIServer *genericapiserver.GenericAPIServer
coreClient client.WithWatch
cache cache.Cache
cache cachetypes.Cache
PeriodicRepoSyncFrequency time.Duration
}

Expand Down Expand Up @@ -227,22 +228,22 @@ func (c completedConfig) New() (*PorchServer, error) {
userInfoProvider := &porch.ApiserverUserInfoProvider{}

watcherMgr := engine.NewWatcherManager()
/*
cacheImpl := memorycache.NewCache(c.ExtraConfig.CacheDirectory, c.ExtraConfig.RepoSyncFrequency, c.ExtraConfig.UseGitCaBundle, memorycache.CacheOptions{
CredentialResolver: credentialResolver,
UserInfoProvider: userInfoProvider,
MetadataStore: metadataStore,
ObjectNotifier: watcherMgr,
cacheImpl, err := cache.CreateCacheImpl(
context.TODO(),
cachetypes.CacheOptions{
RepoImplOptions: repoimpltypes.RepoImplOptions{
LocalDirectory: c.ExtraConfig.CacheDirectory,
UseUserDefinedCaBundle: c.ExtraConfig.UseUserDefinedCaBundle,
CredentialResolver: credentialResolver,
UserInfoProvider: userInfoProvider,
},
RepoSyncFrequency: c.ExtraConfig.RepoSyncFrequency,
MetadataStore: metadataStore,
RepoPRChangeNotifier: watcherMgr,
})
*/
cacheImpl := dbcache.NewCache(c.ExtraConfig.CacheDirectory, c.ExtraConfig.RepoSyncFrequency, c.ExtraConfig.UseGitCaBundle, dbcache.CacheOptions{
Driver: "pgx",
DataSource: "postgresql://porch:porch@172.18.255.202:55432/porch",
CredentialResolver: credentialResolver,
UserInfoProvider: userInfoProvider,
MetadataStore: metadataStore,
ObjectNotifier: watcherMgr,
})
if err != nil {
return nil, fmt.Errorf("failed to creeate repository cache: %w", err)
}

runnerOptionsResolver := func(namespace string) fnruntime.RunnerOptions {
runnerOptions := fnruntime.RunnerOptions{}
Expand Down
18 changes: 12 additions & 6 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ package cache
import (
"context"

configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
"github.com/nephio-project/porch/pkg/repository"
memorycache "github.com/nephio-project/porch/pkg/cache/memorycache"
cachetypes "github.com/nephio-project/porch/pkg/cache/types"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)

type Cache interface {
OpenRepository(ctx context.Context, repositorySpec *configapi.Repository) (repository.Repository, error)
UpdateRepository(ctx context.Context, repositorySpec *configapi.Repository) error
CloseRepository(ctx context.Context, repositorySpec *configapi.Repository, allRepos []configapi.Repository) error
var tracer = otel.Tracer("cache")

func CreateCacheImpl(ctx context.Context, options cachetypes.CacheOptions) (cachetypes.Cache, error) {
ctx, span := tracer.Start(ctx, "Repository::RepositoryFactory", trace.WithAttributes())
defer span.End()

var cacheFactory = new(memorycache.MemoryCacheFactory)
return cacheFactory.NewCache(ctx, options)
}
Loading

0 comments on commit 40de272

Please sign in to comment.