Skip to content

Commit

Permalink
minor refractors
Browse files Browse the repository at this point in the history
Signed-off-by: Anik <anikbhattacharya93@gmail.com>
  • Loading branch information
anik120 committed Aug 17, 2023
1 parent 2d64104 commit e59cdff
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func main() {
os.Exit(1)
}

catalogServer := catalogserver.New(storageDir, catalogServerAddr)
catalogServer := catalogserver.Instance{Dir: storageDir, Addr: catalogServerAddr}
if err := mgr.Add(catalogServer); err != nil {
setupLog.Error(err, "unable to start catalog server")
os.Exit(1)
Expand All @@ -134,7 +134,7 @@ func main() {
if err = (&corecontrollers.CatalogReconciler{
Client: mgr.GetClient(),
Unpacker: unpacker,
Storage: storage.NewStorage(storageDir),
Storage: storage.Instance{RootDirectory: storageDir},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Catalog")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ spec:
name: https
volumeMounts:
- name: catalog-cache
mountPath: /var/cache/
mountPath: /var/cache/catalogs
securityContext:
allowPrivilegeEscalation: false
capabilities:
Expand Down
10 changes: 0 additions & 10 deletions pkg/catalogserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ type Instance struct {
ShutdownTimeout time.Duration
}

// New returns an Instance of a catalog server that serves
// the FBC content stored in the given directory on the given
// http address.
func New(dir, addr string) Instance {
return Instance{
Dir: dir,
Addr: addr,
}
}

func (s Instance) Start(ctx context.Context) error {
server := &http.Server{
Handler: ServerHandler(s.Dir),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/core/catalog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const fbcDeletionFinalizer = "catalogd.operatorframework.io/delete-server-cache"
type CatalogReconciler struct {
client.Client
Unpacker source.Unpacker
Storage storage.Storage
Storage storage.Instance
}

//+kubebuilder:rbac:groups=catalogd.operatorframework.io,resources=catalogs,verbs=get;list;watch;create;update;patch;delete
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/core/catalog_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var _ = Describe("Catalogd Controller Test", func() {
v1alpha1.SourceTypeImage: mockSource,
},
),
Storage: storage.NewStorage(tmpDir),
Storage: storage.Instance{RootDirectory: tmpDir},
}
testServer = httptest.NewServer(catalogserver.ServerHandler(tmpDir))
httpclient = &http.Client{}
Expand Down
25 changes: 8 additions & 17 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,30 @@ import (
"github.com/operator-framework/operator-registry/alpha/declcfg"
)

// Storage is a store of FBC content of catalogs added to a cluster.
// It can be used to Store or Delete FBC in the host's filesystem
type Storage struct {
// Instance is a storage instance that stores FBC content of catalogs
// added to a cluster. It can be used to Store or Delete FBC in the
// host's filesystem
type Instance struct {
RootDirectory string
}

func NewStorage(rootDir string) Storage {
return Storage{
RootDirectory: rootDir,
}
}

func (s *Storage) Store(owner string, fbc *declcfg.DeclarativeConfig) error {
func (s *Instance) Store(owner string, fbc *declcfg.DeclarativeConfig) error {
fbcFile, err := os.Create(s.fbcPath(owner))
if err != nil {
return err
}
defer fbcFile.Close()

if err := declcfg.WriteJSON(*fbc, fbcFile); err != nil {
return err
}
return nil
return declcfg.WriteJSON(*fbc, fbcFile)
}

func (s *Storage) Delete(owner string) error {
func (s *Instance) Delete(owner string) error {
err := os.Remove(s.fbcPath(owner))
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}

func (s *Storage) fbcPath(catalogName string) string {
func (s *Instance) fbcPath(catalogName string) string {
return filepath.Join(s.RootDirectory, fmt.Sprintf("%s.json", catalogName))
}

0 comments on commit e59cdff

Please sign in to comment.