Skip to content

Commit

Permalink
refactor(subs): add objectreference installplan ref
Browse files Browse the repository at this point in the history
  • Loading branch information
njhale committed Apr 17, 2019
1 parent 3d99743 commit 357931e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
61 changes: 46 additions & 15 deletions pkg/api/apis/operators/v1alpha1/subscription_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -36,24 +37,34 @@ type SubscriptionSpec struct {
InstallPlanApproval Approval `json:"installPlanApproval,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient
type Subscription struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
type SubscriptionStatus struct {
// CurrentCSV is the CSV the Subscription is progressing to.
// +optional
CurrentCSV string `json:"currentCSV,omitempty"`

Spec *SubscriptionSpec `json:"spec"`
Status SubscriptionStatus `json:"status"`
}
// InstalledCSV is the CSV currently installed by the Subscription.
// +optional
InstalledCSV string `json:"installedCSV,omitempty"`

type SubscriptionStatus struct {
CurrentCSV string `json:"currentCSV,omitempty"`
InstalledCSV string `json:"installedCSV, omitempty"`
Install *InstallPlanReference `json:"installplan,omitempty"`
// Install is a reference to the latest InstallPlan generated for the Subscription.
// DEPRECATED: InstallPlanRef
// +optional
Install *InstallPlanReference `json:"installplan,omitempty"`

// State represents the current state of the Subscription
// +optional
State SubscriptionState `json:"state,omitempty"`

// Reason is the reason the Subscription was transitioned to its current state.
// +optional
Reason ConditionReason `json:"reason,omitempty"`

// InstallPlanRef is a reference to the latest InstallPlan that contains the Subscription's current CSV.
// +optional
InstallPlanRef *corev1.ObjectReference `json:"installPlanRef,omitempty"`

State SubscriptionState `json:"state,omitempty"`
Reason ConditionReason `json:"reason,omitempty"`
LastUpdated metav1.Time `json:"lastUpdated"`
// LastUpdated represents the last time that the Subscription status was updated.
LastUpdated metav1.Time `json:"lastUpdated"`
}

type InstallPlanReference struct {
Expand All @@ -63,6 +74,16 @@ type InstallPlanReference struct {
UID types.UID `json:"uuid"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient
type Subscription struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`

Spec *SubscriptionSpec `json:"spec"`
Status SubscriptionStatus `json:"status"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type SubscriptionList struct {
metav1.TypeMeta `json:",inline"`
Expand All @@ -78,3 +99,13 @@ func (s *Subscription) GetInstallPlanApproval() Approval {
}
return ApprovalAutomatic
}

// NewInstallPlanReference returns an InstallPlanReference for the given ObjectReference.
func NewInstallPlanReference(ref *corev1.ObjectReference) *InstallPlanReference {
return &InstallPlanReference{
APIVersion: ref.APIVersion,
Kind: ref.Kind,
Name: ref.Name,
UID: ref.UID,
}
}
6 changes: 6 additions & 0 deletions pkg/api/apis/operators/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions pkg/controller/operators/catalog/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
Expand Down Expand Up @@ -807,13 +808,14 @@ func (o *Operator) ensureSubscriptionCSVState(logger *logrus.Entry, sub *v1alpha
return sub, true, nil
}

func (o *Operator) updateSubscriptionStatus(namespace string, subs []*v1alpha1.Subscription, installPlanRef *v1alpha1.InstallPlanReference) error {
func (o *Operator) updateSubscriptionStatus(namespace string, subs []*v1alpha1.Subscription, installPlanRef *corev1.ObjectReference) error {
// TODO: parallel, sync waitgroup
var err error
for _, sub := range subs {
sub.Status.LastUpdated = timeNow()
if installPlanRef != nil {
sub.Status.Install = installPlanRef
sub.Status.InstallPlanRef = installPlanRef
sub.Status.Install = v1alpha1.NewInstallPlanReference(installPlanRef)
sub.Status.State = v1alpha1.SubscriptionStateUpgradePending
}
if _, subErr := o.client.OperatorsV1alpha1().Subscriptions(namespace).UpdateStatus(sub); subErr != nil {
Expand All @@ -823,7 +825,7 @@ func (o *Operator) updateSubscriptionStatus(namespace string, subs []*v1alpha1.S
return err
}

func (o *Operator) ensureInstallPlan(logger *logrus.Entry, namespace string, subs []*v1alpha1.Subscription, installPlanApproval v1alpha1.Approval, steps []*v1alpha1.Step) (*v1alpha1.InstallPlanReference, error) {
func (o *Operator) ensureInstallPlan(logger *logrus.Entry, namespace string, subs []*v1alpha1.Subscription, installPlanApproval v1alpha1.Approval, steps []*v1alpha1.Step) (*corev1.ObjectReference, error) {
if len(steps) == 0 {
return nil, nil
}
Expand All @@ -837,15 +839,15 @@ func (o *Operator) ensureInstallPlan(logger *logrus.Entry, namespace string, sub
for _, installPlan := range installPlans {
if installPlan.Status.CSVManifestsMatch(steps) {
logger.Infof("found InstallPlan with matching manifests: %s", installPlan.GetName())
return o.referenceForInstallPlan(installPlan), nil
return operators.ObjectReferenceFor(installPlan)
}
}
logger.Warn("no installplan found with matching manifests, creating new one")

return o.createInstallPlan(namespace, subs, installPlanApproval, steps)
}

func (o *Operator) createInstallPlan(namespace string, subs []*v1alpha1.Subscription, installPlanApproval v1alpha1.Approval, steps []*v1alpha1.Step) (*v1alpha1.InstallPlanReference, error) {
func (o *Operator) createInstallPlan(namespace string, subs []*v1alpha1.Subscription, installPlanApproval v1alpha1.Approval, steps []*v1alpha1.Step) (*corev1.ObjectReference, error) {
if len(steps) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -896,7 +898,7 @@ func (o *Operator) createInstallPlan(namespace string, subs []*v1alpha1.Subscrip
if err != nil {
return nil, err
}
return o.referenceForInstallPlan(res), nil
return operators.ObjectReferenceFor(res)

}

Expand Down
15 changes: 15 additions & 0 deletions pkg/controller/operators/catalog/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func TestSyncSubscriptions(t *testing.T) {
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.InstallPlanAPIVersion,
},
InstallPlanRef: &v1.ObjectReference{
Namespace: testNamespace,
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.InstallPlanAPIVersion,
},
LastUpdated: nowTime,
},
},
Expand Down Expand Up @@ -272,6 +277,11 @@ func TestSyncSubscriptions(t *testing.T) {
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.InstallPlanAPIVersion,
},
InstallPlanRef: &v1.ObjectReference{
Namespace: testNamespace,
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.InstallPlanAPIVersion,
},
LastUpdated: nowTime,
},
},
Expand Down Expand Up @@ -430,6 +440,11 @@ func TestSyncSubscriptions(t *testing.T) {
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.InstallPlanAPIVersion,
},
InstallPlanRef: &v1.ObjectReference{
Namespace: testNamespace,
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.InstallPlanAPIVersion,
},
LastUpdated: nowTime,
},
},
Expand Down

0 comments on commit 357931e

Please sign in to comment.