-
Notifications
You must be signed in to change notification settings - Fork 220
Publish operator status #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 2 commits into
openshift:master
from
Miciah:publish-OperatorStatus
Oct 30, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package stub | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| ingressv1alpha1 "github.com/openshift/cluster-ingress-operator/pkg/apis/ingress/v1alpha1" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/util/clusteroperator" | ||
| operatorversion "github.com/openshift/cluster-ingress-operator/version" | ||
| osv1 "github.com/openshift/cluster-version-operator/pkg/apis/operatorstatus.openshift.io/v1" | ||
|
|
||
| "github.com/operator-framework/operator-sdk/pkg/sdk" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // syncOperatorStatus computes the operator's current status and therefrom | ||
| // creates or updates the ClusterOperator resource for the operator. | ||
| func (h *Handler) syncOperatorStatus() { | ||
| co := &osv1.ClusterOperator{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| Kind: "ClusterOperator", | ||
| APIVersion: "operatorstatus.openshift.io/v1", | ||
| }, | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Namespace: h.Namespace, | ||
| // TODO Use a named constant or get name from config. | ||
| Name: "openshift-ingress", | ||
| }, | ||
| } | ||
| err := sdk.Get(co) | ||
| isNotFound := errors.IsNotFound(err) | ||
| if err != nil && !isNotFound { | ||
| logrus.Errorf("syncOperatorStatus: error getting ClusterOperator %s/%s: %v", | ||
| co.Namespace, co.Name, err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| ns, ingresses, daemonsets, err := h.getOperatorState() | ||
| if err != nil { | ||
| logrus.Errorf("syncOperatorStatus: getOperatorState: %v", err) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| oldConditions := co.Status.Conditions | ||
| co.Status.Conditions = computeStatusConditions(oldConditions, ns, | ||
| ingresses, daemonsets) | ||
|
|
||
| if isNotFound { | ||
| co.Status.Version = operatorversion.Version | ||
|
|
||
| if err := sdk.Create(co); err != nil { | ||
| logrus.Errorf("syncOperatorStatus: failed to create ClusterOperator %s/%s: %v", | ||
| co.Namespace, co.Name, err) | ||
| } else { | ||
| logrus.Infof("syncOperatorStatus: created ClusterOperator %s/%s (UID %v)", | ||
| co.Namespace, co.Name, co.UID) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if clusteroperator.ConditionsEqual(oldConditions, co.Status.Conditions) { | ||
| return | ||
| } | ||
|
|
||
| if err := sdk.Update(co); err != nil { | ||
| logrus.Errorf("syncOperatorStatus: failed to update status of ClusterOperator %s/%s: %v", | ||
| co.Namespace, co.Name, err) | ||
| } | ||
| } | ||
|
|
||
| // getOperatorState gets and returns the resources necessary to compute the | ||
| // operator's current state. | ||
| func (h *Handler) getOperatorState() (*corev1.Namespace, []ingressv1alpha1.ClusterIngress, []appsv1.DaemonSet, error) { | ||
| ns, err := h.ManifestFactory.RouterNamespace() | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf( | ||
| "error building router namespace: %v", err) | ||
| } | ||
|
|
||
| if err := sdk.Get(ns); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return nil, nil, nil, nil | ||
| } | ||
|
|
||
| return nil, nil, nil, fmt.Errorf( | ||
| "error getting Namespace %s: %v", ns.Name, err) | ||
| } | ||
|
|
||
| ingressList := &ingressv1alpha1.ClusterIngressList{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| Kind: "ClusterIngress", | ||
| APIVersion: "ingress.openshift.io/v1alpha1", | ||
| }, | ||
| } | ||
| err = sdk.List(h.Namespace, ingressList, | ||
| sdk.WithListOptions(&metav1.ListOptions{})) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf( | ||
| "failed to list ClusterIngresses: %v", err) | ||
| } | ||
|
|
||
| daemonsetList := &appsv1.DaemonSetList{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| Kind: "DaemonSet", | ||
| APIVersion: "apps/v1", | ||
| }, | ||
| } | ||
| err = sdk.List(ns.Name, daemonsetList, | ||
| sdk.WithListOptions(&metav1.ListOptions{})) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf( | ||
| "failed to list DaemonSets: %v", err) | ||
| } | ||
|
|
||
| return ns, ingressList.Items, daemonsetList.Items, nil | ||
| } | ||
|
|
||
| // computeStatusConditions computes the operator's current state. | ||
| func computeStatusConditions(conditions []osv1.ClusterOperatorStatusCondition, ns *corev1.Namespace, ingresses []ingressv1alpha1.ClusterIngress, daemonsets []appsv1.DaemonSet) []osv1.ClusterOperatorStatusCondition { | ||
| failingCondition := &osv1.ClusterOperatorStatusCondition{ | ||
| Type: osv1.OperatorFailing, | ||
| Status: osv1.ConditionUnknown, | ||
| } | ||
| if ns == nil { | ||
| failingCondition.Status = osv1.ConditionTrue | ||
| failingCondition.Reason = "NoNamespace" | ||
| failingCondition.Message = "router namespace does not exist" | ||
| } else { | ||
| failingCondition.Status = osv1.ConditionFalse | ||
| } | ||
| conditions = clusteroperator.SetStatusCondition(conditions, | ||
| failingCondition) | ||
|
|
||
| progressingCondition := &osv1.ClusterOperatorStatusCondition{ | ||
| Type: osv1.OperatorProgressing, | ||
| Status: osv1.ConditionUnknown, | ||
| } | ||
| numIngresses := len(ingresses) | ||
| numDaemonsets := len(daemonsets) | ||
| if numIngresses == numDaemonsets { | ||
| progressingCondition.Status = osv1.ConditionFalse | ||
| } else { | ||
| progressingCondition.Status = osv1.ConditionTrue | ||
| progressingCondition.Reason = "Reconciling" | ||
| progressingCondition.Message = fmt.Sprintf( | ||
| "have %d ingresses, want %d", | ||
| numDaemonsets, numIngresses) | ||
| } | ||
| conditions = clusteroperator.SetStatusCondition(conditions, | ||
| progressingCondition) | ||
|
|
||
| availableCondition := &osv1.ClusterOperatorStatusCondition{ | ||
| Type: osv1.OperatorAvailable, | ||
| Status: osv1.ConditionUnknown, | ||
| } | ||
| dsAvailable := map[string]bool{} | ||
| for _, ds := range daemonsets { | ||
| dsAvailable[ds.Name] = ds.Status.NumberAvailable > 0 | ||
| } | ||
| unavailable := []string{} | ||
| for _, ingress := range ingresses { | ||
| // TODO Use the manifest to derive the name, or use labels or | ||
| // owner references. | ||
| name := "router-" + ingress.Name | ||
| if available, exists := dsAvailable[name]; !exists { | ||
| msg := fmt.Sprintf("no router for ingress %q", | ||
| ingress.Name) | ||
| unavailable = append(unavailable, msg) | ||
| } else if !available { | ||
| msg := fmt.Sprintf("ingress %q not available", | ||
| ingress.Name) | ||
| unavailable = append(unavailable, msg) | ||
| } | ||
| } | ||
| if len(unavailable) == 0 { | ||
| availableCondition.Status = osv1.ConditionTrue | ||
| } else { | ||
| availableCondition.Status = osv1.ConditionFalse | ||
| availableCondition.Reason = "IngressUnavailable" | ||
| availableCondition.Message = strings.Join(unavailable, | ||
| "\n") | ||
| } | ||
| conditions = clusteroperator.SetStatusCondition(conditions, | ||
| availableCondition) | ||
|
|
||
| return conditions | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an Ingress CR is deleted, corresponding daemonset may not be deleted yet (pending op) and the length of numIngresses and numDaemonsets will defer, don't we want to say OperatorProgressing = True in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we do want to say "Progressing" is true in that case. The docstring for
osv1.OperatorProgressingsays, "OperatorProgressing indicates that the operator is actively making changes to the binary maintained by the operator". When a ClusterIngress is deleted, then the operator reconciles, and then it is actively making changes to the binary (deleting the DaemonSet for the router) until the DaemonSet is gone.