Skip to content

Commit 95190ba

Browse files
committed
Use Helm List operator to determine Deployed status
Signed-off-by: Todd Short <[email protected]>
1 parent b7674d8 commit 95190ba

File tree

6 files changed

+70
-30
lines changed

6 files changed

+70
-30
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/opencontainers/go-digest v1.0.0
1919
github.com/operator-framework/api v0.27.0
2020
github.com/operator-framework/catalogd v0.32.0
21-
github.com/operator-framework/helm-operator-plugins v0.5.0
21+
github.com/operator-framework/helm-operator-plugins v0.6.0
2222
github.com/operator-framework/operator-registry v1.47.0
2323
github.com/spf13/pflag v1.0.5
2424
github.com/stretchr/testify v1.9.0
@@ -179,7 +179,7 @@ require (
179179
github.com/pkg/errors v0.9.1 // indirect
180180
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
181181
github.com/proglottis/gpgme v0.1.3 // indirect
182-
github.com/prometheus/client_golang v1.20.4 // indirect
182+
github.com/prometheus/client_golang v1.20.5 // indirect
183183
github.com/prometheus/client_model v0.6.1 // indirect
184184
github.com/prometheus/common v0.55.0 // indirect
185185
github.com/prometheus/procfs v0.15.1 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ github.com/operator-framework/api v0.27.0 h1:OrVaGKZJvbZo58HTv2guz7aURkhVKYhFqZ/
537537
github.com/operator-framework/api v0.27.0/go.mod h1:lg2Xx+S8NQWGYlEOvFwQvH46E5EK5IrAIL7HWfAhciM=
538538
github.com/operator-framework/catalogd v0.32.0 h1:VKD+7wfEF6CnJgR4aUYyT85KP2Te7zjhaPvgvWy25Uw=
539539
github.com/operator-framework/catalogd v0.32.0/go.mod h1:FrFSCwRXr4aPslcXIv48dan5AdM37k/B9tK/RpdvZCU=
540-
github.com/operator-framework/helm-operator-plugins v0.5.0 h1:qph2OoECcI9mpuUBtOsWOMgvpx52mPTTSvzVxICsT04=
541-
github.com/operator-framework/helm-operator-plugins v0.5.0/go.mod h1:yVncrZ/FJNqedMil+055fk6sw8aMKRrget/AqGM0ig0=
540+
github.com/operator-framework/helm-operator-plugins v0.6.0 h1:MAb1oKsqlsb2gVTFJkytIySK+mzT5sdICLM9HfYF49A=
541+
github.com/operator-framework/helm-operator-plugins v0.6.0/go.mod h1:fUUCJR3bWtMBZ1qdDhbwjacsBHi9uT576tF4u/DwOgQ=
542542
github.com/operator-framework/operator-lib v0.15.0 h1:0QeRM4PMtThqINpcFGCEBnIV3Z8u7/8fYLEx6mUtdcM=
543543
github.com/operator-framework/operator-lib v0.15.0/go.mod h1:ZxLvFuQ7bRWiTNBOqodbuNvcsy/Iq0kOygdxhlbNdI0=
544544
github.com/operator-framework/operator-registry v1.47.0 h1:Imr7X/W6FmXczwpIOXfnX8d6Snr1dzwWxkMG+lLAfhg=
@@ -569,8 +569,8 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
569569
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
570570
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
571571
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
572-
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
573-
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
572+
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
573+
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
574574
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
575575
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
576576
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

internal/action/helm_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ func (m *mockActionClient) Get(name string, opts ...actionclient.GetOption) (*re
3030
return args.Get(0).(*release.Release), args.Error(1)
3131
}
3232

33+
func (m *mockActionClient) History(name string, opts ...actionclient.HistoryOption) ([]*release.Release, error) {
34+
args := m.Called(name, opts)
35+
if args.Get(0) == nil {
36+
return nil, args.Error(1)
37+
}
38+
rel := []*release.Release{
39+
args.Get(0).(*release.Release),
40+
}
41+
return rel, args.Error(1)
42+
}
43+
3344
func (m *mockActionClient) Install(name, namespace string, chrt *chart.Chart, vals map[string]interface{}, opts ...actionclient.InstallOption) (*release.Release, error) {
3445
args := m.Called(name, namespace, chrt, vals, opts)
3546
if args.Get(0) == nil {

internal/controllers/clusterextension_controller.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type Applier interface {
8484
}
8585

8686
type InstalledBundleGetter interface {
87-
GetInstalledBundle(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*ocv1alpha1.BundleMetadata, error)
87+
GetInstalledBundle(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*ocv1alpha1.BundleMetadata, release.Status, error)
8888
}
8989

9090
//+kubebuilder:rbac:groups=olm.operatorframework.io,resources=clusterextensions,verbs=get;list;watch;update;patch
@@ -203,21 +203,34 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp
203203
}
204204

205205
l.Info("getting installed bundle")
206-
installedBundle, err := r.InstalledBundleGetter.GetInstalledBundle(ctx, ext)
206+
// second return is status of the highest semver, if we need it
207+
installedBundle, _, err := r.InstalledBundleGetter.GetInstalledBundle(ctx, ext)
207208
if err != nil {
208209
setInstallStatus(ext, nil)
209-
// TODO: use Installed=Unknown
210-
setInstalledStatusConditionFailed(ext, err.Error())
210+
setInstalledStatusConditionUnknown(ext, err.Error())
211211
setStatusProgressing(ext, err)
212212
return ctrl.Result{}, err
213213
}
214214

215+
// Update current install status conditions before we start trying to install something new
216+
if installedBundle != nil {
217+
installStatus := &ocv1alpha1.ClusterExtensionInstallStatus{
218+
Bundle: *installedBundle,
219+
}
220+
setInstallStatus(ext, installStatus)
221+
if !apimeta.IsStatusConditionTrue(ext.Status.Conditions, ocv1alpha1.TypeInstalled) {
222+
setInstalledStatusConditionSuccess(ext, "Installed extension successfully")
223+
}
224+
} else {
225+
setInstallStatus(ext, nil)
226+
apimeta.RemoveStatusCondition(&ext.Status.Conditions, ocv1alpha1.TypeInstalled)
227+
}
228+
215229
// run resolution
216230
l.Info("resolving bundle")
217231
resolvedBundle, resolvedBundleVersion, resolvedDeprecation, err := r.Resolver.Resolve(ctx, ext, installedBundle)
218232
if err != nil {
219233
// Note: We don't distinguish between resolution-specific errors and generic errors
220-
setInstallStatus(ext, nil)
221234
setStatusProgressing(ext, err)
222235
ensureAllConditionsWithReason(ext, ocv1alpha1.ReasonFailed, err.Error())
223236
return ctrl.Result{}, err
@@ -466,32 +479,36 @@ type DefaultInstalledBundleGetter struct {
466479
helmclient.ActionClientGetter
467480
}
468481

469-
func (d *DefaultInstalledBundleGetter) GetInstalledBundle(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*ocv1alpha1.BundleMetadata, error) {
482+
func (d *DefaultInstalledBundleGetter) GetInstalledBundle(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*ocv1alpha1.BundleMetadata, release.Status, error) {
483+
status := release.StatusUnknown
470484
cl, err := d.ActionClientFor(ctx, ext)
471485
if err != nil {
472-
return nil, err
486+
return nil, status, err
473487
}
474488

475-
rel, err := cl.Get(ext.GetName())
489+
relhis, err := cl.History(ext.GetName())
476490
if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) {
477-
return nil, err
491+
return nil, status, err
492+
}
493+
if relhis == nil || len(relhis) == 0 {
494+
return nil, status, nil
478495
}
479-
if rel == nil {
480-
return nil, nil
496+
var found *release.Release
497+
498+
status = relhis[0].Info.Status
499+
for _, rel := range relhis {
500+
if rel.Info.Status == release.StatusDeployed {
501+
found = rel
502+
break
503+
}
481504
}
482505

483-
switch rel.Info.Status {
484-
case release.StatusUnknown:
485-
return nil, fmt.Errorf("installation status is unknown")
486-
case release.StatusDeployed, release.StatusUninstalled, release.StatusSuperseded, release.StatusFailed:
487-
case release.StatusUninstalling, release.StatusPendingInstall, release.StatusPendingRollback, release.StatusPendingUpgrade:
488-
return nil, fmt.Errorf("installation is still pending: %s", rel.Info.Status)
489-
default:
490-
return nil, fmt.Errorf("unknown installation status: %s", rel.Info.Status)
506+
if found == nil {
507+
return nil, status, nil
491508
}
492509

493510
return &ocv1alpha1.BundleMetadata{
494-
Name: rel.Labels[labels.BundleNameKey],
495-
Version: rel.Labels[labels.BundleVersionKey],
496-
}, nil
511+
Name: found.Labels[labels.BundleNameKey],
512+
Version: found.Labels[labels.BundleVersionKey],
513+
}, status, nil
497514
}

internal/controllers/common_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ func setInstalledStatusConditionFailed(ext *ocv1alpha1.ClusterExtension, message
4848
})
4949
}
5050

51+
// setInstalledStatusConditionUnknown sets the installed status condition to unknown.
52+
func setInstalledStatusConditionUnknown(ext *ocv1alpha1.ClusterExtension, message string) {
53+
apimeta.SetStatusCondition(&ext.Status.Conditions, metav1.Condition{
54+
Type: ocv1alpha1.TypeInstalled,
55+
Status: metav1.ConditionUnknown,
56+
Reason: ocv1alpha1.ReasonFailed,
57+
Message: message,
58+
ObservedGeneration: ext.GetGeneration(),
59+
})
60+
}
61+
5162
func setInstallStatus(ext *ocv1alpha1.ClusterExtension, installStatus *ocv1alpha1.ClusterExtensionInstallStatus) {
5263
ext.Status.Install = installStatus
5364
}

internal/controllers/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626

2727
"github.com/stretchr/testify/require"
28+
"helm.sh/helm/v3/pkg/release"
2829
"k8s.io/apimachinery/pkg/api/meta"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -80,8 +81,8 @@ func (m *MockInstalledBundleGetter) SetBundle(bundle *ocv1alpha1.BundleMetadata)
8081
m.bundle = bundle
8182
}
8283

83-
func (m *MockInstalledBundleGetter) GetInstalledBundle(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*ocv1alpha1.BundleMetadata, error) {
84-
return m.bundle, nil
84+
func (m *MockInstalledBundleGetter) GetInstalledBundle(ctx context.Context, ext *ocv1alpha1.ClusterExtension) (*ocv1alpha1.BundleMetadata, release.Status, error) {
85+
return m.bundle, release.StatusUnknown, nil
8586
}
8687

8788
var _ controllers.Applier = (*MockApplier)(nil)

0 commit comments

Comments
 (0)