Skip to content

Commit

Permalink
Create Lock if not exist in revision controller
Browse files Browse the repository at this point in the history
Updates revision controllers to create the lock if it does not exist
when resolving dependencies. If a package revision is inactive it will
not create the lock as its only purpose would be to remove itself from
it. If a package opts out of dependency management it will not create
the lock as it does not attempt to consider dependencies.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
  • Loading branch information
hasheddan committed May 24, 2021
1 parent 6622b16 commit 0f5d56d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions internal/controller/pkg/revision/dependency.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Masterminds/semver"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -37,7 +38,7 @@ const (
lockName = "lock"

errNotMeta = "meta type is not a valid package"
errGetLock = "cannot get lock"
errGetOrCreateLock = "cannot get or create lock"
errIncompatibleDependencyFmt = "incompatible dependencies: %+v"
errMissingDependenciesFmt = "missing dependencies: %+v"
errDependencyNotInGraph = "dependency is not present in graph"
Expand Down Expand Up @@ -92,8 +93,18 @@ func (m *PackageDependencyManager) Resolve(ctx context.Context, pkg runtime.Obje

// Get the lock.
lock := &v1alpha1.Lock{}
if err := m.client.Get(ctx, types.NamespacedName{Name: lockName}, lock); err != nil {
return found, installed, invalid, errors.Wrap(err, errGetLock)
err = m.client.Get(ctx, types.NamespacedName{Name: lockName}, lock)
if kerrors.IsNotFound(err) {
// If lock does not exist and we are inactive then we can return early
// because our only operation would be to remove self.
if pr.GetDesiredState() == v1.PackageRevisionInactive {
return found, installed, invalid, nil
}
lock.Name = lockName
err = m.client.Create(ctx, lock, &client.CreateOptions{})
}
if err != nil {
return found, installed, invalid, errors.Wrap(err, errGetOrCreateLock)
}

prRef, err := name.ParseReference(pr.GetSource(), name.WithDefaultRegistry(""))
Expand Down Expand Up @@ -210,7 +221,12 @@ func (m *PackageDependencyManager) RemoveSelf(ctx context.Context, pr v1.Package

// Get the lock.
lock := &v1alpha1.Lock{}
if err := m.client.Get(ctx, types.NamespacedName{Name: lockName}, lock); err != nil {
err = m.client.Get(ctx, types.NamespacedName{Name: lockName}, lock)
if kerrors.IsNotFound(err) {
// If lock does not exist then we don't need to remove self.
return nil
}
if err != nil {
return err
}

Expand Down

0 comments on commit 0f5d56d

Please sign in to comment.