Skip to content

Commit

Permalink
Merge pull request #1018 from jpeeler/add-fields-related-objects-2
Browse files Browse the repository at this point in the history
Bug 1717636: Add proper name to related objects
  • Loading branch information
openshift-merge-robot authored Sep 4, 2019
2 parents db97a17 + c799a51 commit 39a378f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
7 changes: 6 additions & 1 deletion cmd/catalog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
utilclock "k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/tools/clientcmd"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/catalog"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorstatus"
Expand Down Expand Up @@ -162,6 +163,10 @@ func main() {
log.Fatalf("error configuring client: %s", err.Error())
}
opClient := operatorclient.NewClientFromConfig(*kubeConfigPath, logger)
crClient, err := client.NewClient(*kubeConfigPath)
if err != nil {
log.Fatalf("error configuring client: %s", err.Error())
}

// Create a new instance of the operator.
op, err := catalog.NewOperator(ctx, *kubeConfigPath, utilclock.RealClock{}, logger, *wakeupInterval, *configmapServerImage, *catalogNamespace, namespaces...)
Expand All @@ -173,7 +178,7 @@ func main() {
<-op.Ready()

if *writeStatusName != "" {
operatorstatus.MonitorClusterStatus(*writeStatusName, *catalogNamespace, op.AtLevel(), op.Done(), opClient, configClient)
operatorstatus.MonitorClusterStatus(*writeStatusName, op.AtLevel(), op.Done(), opClient, configClient, crClient)
}

<-op.Done()
Expand Down
2 changes: 1 addition & 1 deletion cmd/olm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func main() {
<-op.Ready()

if *writeStatusName != "" {
operatorstatus.MonitorClusterStatus(*writeStatusName, *namespace, op.AtLevel(), ctx.Done(), opClient, configClient)
operatorstatus.MonitorClusterStatus(*writeStatusName, op.AtLevel(), ctx.Done(), opClient, configClient, crClient)
}

if *writePackageServerStatusName != "" {
Expand Down
82 changes: 57 additions & 25 deletions pkg/lib/operatorstatus/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (

configv1 "github.com/openshift/api/config/v1"
configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"

olmv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
olmv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version"
Expand All @@ -25,9 +26,10 @@ import (
const (
clusterOperatorOLM = "operator-lifecycle-manager"
clusterOperatorCatalogSource = "operator-lifecycle-manager-catalog"
openshiftNamespace = "openshift-operator-lifecycle-manager"
)

func MonitorClusterStatus(name, namespace string, syncCh <-chan error, stopCh <-chan struct{}, opClient operatorclient.ClientInterface, configClient configv1client.ConfigV1Interface) {
func MonitorClusterStatus(name string, syncCh <-chan error, stopCh <-chan struct{}, opClient operatorclient.ClientInterface, configClient configv1client.ConfigV1Interface, crClient versioned.Interface) {
var (
syncs int
successfulSyncs int
Expand Down Expand Up @@ -113,11 +115,14 @@ func MonitorClusterStatus(name, namespace string, syncCh <-chan error, stopCh <-
},
},
})
created.Status.RelatedObjects = relatedObjects(name, namespace)
if createErr != nil {
log.Errorf("Failed to create cluster operator: %v\n", createErr)
return
}
created.Status.RelatedObjects, err = relatedObjects(name, opClient, crClient)
if err != nil {
log.Errorf("Failed to get related objects: %v", err)
}
existing = created
err = nil
}
Expand Down Expand Up @@ -198,6 +203,16 @@ func MonitorClusterStatus(name, namespace string, syncCh <-chan error, stopCh <-
// TODO: use % errors within a window to report available
}

// always update the related objects in case changes have occurred
existing.Status.RelatedObjects, err = relatedObjects(name, opClient, crClient)
if err != nil {
log.Errorf("Failed to get related objects: %v", err)
}
if !reflect.DeepEqual(previousStatus.RelatedObjects, existing.Status.RelatedObjects) {
diffString := diff.ObjectDiff(previousStatus.RelatedObjects, existing.Status.RelatedObjects)
log.Debugf("Update required for related objects: %v", diffString)
}

// update the status
if !reflect.DeepEqual(previousStatus, &existing.Status) {
if _, err := configClient.ClusterOperators().UpdateStatus(existing); err != nil {
Expand Down Expand Up @@ -246,38 +261,55 @@ func findOperatorStatusCondition(conditions []configv1.ClusterOperatorStatusCond

// relatedObjects returns RelatedObjects in the ClusterOperator.Status.
// RelatedObjects are consumed by https://github.com/openshift/must-gather
func relatedObjects(name, namespace string) []configv1.ObjectReference {
func relatedObjects(name string, opClient operatorclient.ClientInterface, crClient versioned.Interface) ([]configv1.ObjectReference, error) {
var objectReferences []configv1.ObjectReference
log.Infof("Adding related objects for %v", name)
namespace := openshiftNamespace // hard-coded to constant

switch name {
case clusterOperatorOLM:
objectReferences = []configv1.ObjectReference{
{
Group: olmv1.GroupName,
Resource: olmv1.OperatorGroupKind,
Namespace: namespace,
Name: clusterOperatorOLM,
},
{
csvList, err := crClient.OperatorsV1alpha1().ClusterServiceVersions(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}

for _, csv := range csvList.Items {
if csv.IsCopied() {
continue
}
objectReferences = append(objectReferences, configv1.ObjectReference{
Group: olmv1alpha1.GroupName,
Resource: olmv1alpha1.ClusterServiceVersionKind,
Namespace: namespace,
Name: clusterOperatorOLM,
},
Namespace: csv.GetNamespace(),
Name: csv.GetName(),
})
}
case clusterOperatorCatalogSource:
objectReferences = []configv1.ObjectReference{
{
subList, err := crClient.OperatorsV1alpha1().Subscriptions(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}

installPlanList, err := crClient.OperatorsV1alpha1().InstallPlans(namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}

for _, sub := range subList.Items {
objectReferences = append(objectReferences, configv1.ObjectReference{
Group: olmv1alpha1.GroupName,
Resource: olmv1alpha1.SubscriptionKind,
Namespace: namespace,
Name: clusterOperatorCatalogSource,
},
{
Namespace: sub.GetNamespace(),
Name: sub.GetName(),
})
}
for _, ip := range installPlanList.Items {
objectReferences = append(objectReferences, configv1.ObjectReference{
Group: olmv1alpha1.GroupName,
Resource: olmv1alpha1.InstallPlanKind,
Namespace: namespace,
Name: clusterOperatorCatalogSource,
},
Namespace: ip.GetNamespace(),
Name: ip.GetName(),
})
}
}
namespaces := configv1.ObjectReference{
Expand All @@ -286,5 +318,5 @@ func relatedObjects(name, namespace string) []configv1.ObjectReference {
Name: namespace,
}
objectReferences = append(objectReferences, namespaces)
return objectReferences
return objectReferences, nil
}

0 comments on commit 39a378f

Please sign in to comment.