Skip to content

Commit

Permalink
make repository sync frequency configurable (#4076)
Browse files Browse the repository at this point in the history
Signed-off-by: himanshuchahar <himanshuchahar@google.com>
  • Loading branch information
himanshu19w authored Nov 2, 2023
1 parent 5f1d54b commit a88f1fc
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 12 deletions.
3 changes: 2 additions & 1 deletion porch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ run-local: porch
--standalone-debug-mode \
--kubeconfig="$(KUBECONFIG)" \
--cache-directory="$(CACHEDIR)" \
--function-runner 192.168.8.202:9445
--function-runner 192.168.8.202:9445 \
--repo-sync-frequency=60s

.PHONY: run-jaeger
run-jaeger:
Expand Down
1 change: 1 addition & 0 deletions porch/deployments/porch/3-porch-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ spec:
- --cache-directory=/cache
- --cert-dir=/tmp/certs
- --secure-port=4443
- --repo-sync-frequency=60s

---
apiVersion: v1
Expand Down
4 changes: 3 additions & 1 deletion porch/pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/GoogleContainerTools/kpt/internal/fnruntime"
"github.com/GoogleContainerTools/kpt/porch/api/porch/install"
Expand Down Expand Up @@ -76,6 +77,7 @@ type ExtraConfig struct {
CacheDirectory string
FunctionRunnerAddress string
DefaultImagePrefix string
RepoSyncFrequency time.Duration
}

// Config defines the config for the apiserver
Expand Down Expand Up @@ -220,7 +222,7 @@ func (c completedConfig) New() (*PorchServer, error) {

watcherMgr := engine.NewWatcherManager()

cache := cache.NewCache(c.ExtraConfig.CacheDirectory, cache.CacheOptions{
cache := cache.NewCache(c.ExtraConfig.CacheDirectory, c.ExtraConfig.RepoSyncFrequency, cache.CacheOptions{
CredentialResolver: credentialResolver,
UserInfoProvider: userInfoProvider,
MetadataStore: metadataStore,
Expand Down
12 changes: 7 additions & 5 deletions porch/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"path/filepath"
"sync"
"time"

kptoci "github.com/GoogleContainerTools/kpt/pkg/oci"
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
Expand Down Expand Up @@ -48,8 +49,8 @@ type Cache struct {
credentialResolver repository.CredentialResolver
userInfoProvider repository.UserInfoProvider
metadataStore meta.MetadataStore

objectNotifier objectNotifier
repoSyncFrequency time.Duration
objectNotifier objectNotifier
}

type objectNotifier interface {
Expand All @@ -63,14 +64,15 @@ type CacheOptions struct {
ObjectNotifier objectNotifier
}

func NewCache(cacheDir string, opts CacheOptions) *Cache {
func NewCache(cacheDir string, repoSyncFrequency time.Duration, opts CacheOptions) *Cache {
return &Cache{
repositories: make(map[string]*cachedRepository),
cacheDir: cacheDir,
credentialResolver: opts.CredentialResolver,
userInfoProvider: opts.UserInfoProvider,
metadataStore: opts.MetadataStore,
objectNotifier: opts.ObjectNotifier,
repoSyncFrequency: repoSyncFrequency,
}
}

Expand Down Expand Up @@ -101,7 +103,7 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
if err != nil {
return nil, err
}
cr = newRepository(key, repositorySpec, r, c.objectNotifier, c.metadataStore)
cr = newRepository(key, repositorySpec, r, c.objectNotifier, c.metadataStore, c.repoSyncFrequency)
c.repositories[key] = cr
}
return cr, nil
Expand Down Expand Up @@ -137,7 +139,7 @@ func (c *Cache) OpenRepository(ctx context.Context, repositorySpec *configapi.Re
}); err != nil {
return nil, err
} else {
cr = newRepository(key, repositorySpec, r, c.objectNotifier, c.metadataStore)
cr = newRepository(key, repositorySpec, r, c.objectNotifier, c.metadataStore, c.repoSyncFrequency)
c.repositories[key] = cr
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion porch/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
Expand Down Expand Up @@ -134,7 +135,7 @@ func openRepositoryFromArchive(t *testing.T, ctx context.Context, testPath, name
repo, address := git.ServeGitRepository(t, tarfile, tempdir)
metadataStore := createMetadataStoreFromArchive(t, "", "")

cache := NewCache(t.TempDir(), CacheOptions{
cache := NewCache(t.TempDir(), 60*time.Second, CacheOptions{
MetadataStore: metadataStore,
ObjectNotifier: &fakecache.ObjectNotifier{},
})
Expand Down
8 changes: 4 additions & 4 deletions porch/pkg/cache/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type cachedRepository struct {
metadataStore meta.MetadataStore
}

func newRepository(id string, repoSpec *configapi.Repository, repo repository.Repository, objectNotifier objectNotifier, metadataStore meta.MetadataStore) *cachedRepository {
func newRepository(id string, repoSpec *configapi.Repository, repo repository.Repository, objectNotifier objectNotifier, metadataStore meta.MetadataStore, repoSyncFrequency time.Duration) *cachedRepository {
ctx, cancel := context.WithCancel(context.Background())
r := &cachedRepository{
id: id,
Expand All @@ -81,7 +81,7 @@ func newRepository(id string, repoSpec *configapi.Repository, repo repository.Re

// TODO: Should we fetch the packages here?

go r.pollForever(ctx)
go r.pollForever(ctx, repoSyncFrequency)

return r
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (r *cachedRepository) Close() error {
}

// pollForever will continue polling until signal channel is closed or ctx is done.
func (r *cachedRepository) pollForever(ctx context.Context) {
func (r *cachedRepository) pollForever(ctx context.Context, repoSyncFrequency time.Duration) {
r.pollOnce(ctx)
for {
select {
Expand All @@ -348,7 +348,7 @@ func (r *cachedRepository) pollForever(ctx context.Context) {
return
default:
r.pollOnce(ctx)
time.Sleep(60 * time.Second)
time.Sleep(repoSyncFrequency)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions porch/pkg/cmd/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"net"
"os"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -51,6 +52,7 @@ type PorchServerOptions struct {
CoreAPIKubeconfigPath string
FunctionRunnerAddress string
DefaultImagePrefix string
RepoSyncFrequency time.Duration

SharedInformerFactory informers.SharedInformerFactory
StdOut io.Writer
Expand Down Expand Up @@ -182,6 +184,7 @@ func (o *PorchServerOptions) Config() (*apiserver.Config, error) {
ExtraConfig: apiserver.ExtraConfig{
CoreAPIKubeconfigPath: o.CoreAPIKubeconfigPath,
CacheDirectory: o.CacheDirectory,
RepoSyncFrequency: o.RepoSyncFrequency,
FunctionRunnerAddress: o.FunctionRunnerAddress,
DefaultImagePrefix: o.DefaultImagePrefix,
},
Expand Down Expand Up @@ -229,4 +232,5 @@ func (o *PorchServerOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.FunctionRunnerAddress, "function-runner", "", "Address of the function runner gRPC service.")
fs.StringVar(&o.DefaultImagePrefix, "default-image-prefix", "gcr.io/kpt-fn/", "Default prefix for unqualified function names")
fs.StringVar(&o.CacheDirectory, "cache-directory", "", "Directory where Porch server stores repository and package caches.")
fs.DurationVar(&o.RepoSyncFrequency, "repo-sync-frequency", 60*time.Second, "Frequency in seconds at which registered repositories will be synced.")
}
41 changes: 41 additions & 0 deletions porch/pkg/cmd/server/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 The kpt 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 server

import (
porchv1alpha1 "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/GoogleContainerTools/kpt/porch/pkg/apiserver"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime/schema"
genericoptions "k8s.io/apiserver/pkg/server/options"
"testing"
"time"
)

func TestAddFlags(t *testing.T) {
versions := schema.GroupVersions{
porchv1alpha1.SchemeGroupVersion,
}
o := PorchServerOptions{
RecommendedOptions: genericoptions.NewRecommendedOptions(
defaultEtcdPathPrefix,
apiserver.Codecs.LegacyCodec(versions...),
),
}
o.AddFlags(&pflag.FlagSet{})
if o.RepoSyncFrequency < 30*time.Second {
t.Fatalf("AddFlags(): repo-sync-frequency cannot be less that 30 seconds.")
}
}

0 comments on commit a88f1fc

Please sign in to comment.