|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package conditions |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + |
| 22 | + apiv1 "github.com/operator-framework/api/pkg/operators/v1" |
| 23 | + "github.com/operator-framework/operator-lib/internal/utils" |
| 24 | + "k8s.io/apimachinery/pkg/api/meta" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + // ErrNoOperatorCondition indicates that the operator condition CRD is nil |
| 32 | + ErrNoOperatorCondition = fmt.Errorf("operator Condition CRD is nil") |
| 33 | + |
| 34 | + // readNamespace gets the namespacedName of the operator. |
| 35 | + readNamespace = utils.GetOperatorNamespace |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + // operatorCondEnvVar is the env variable which |
| 40 | + // contains the name of the Condition CR associated to the operator, |
| 41 | + // set by OLM. |
| 42 | + operatorCondEnvVar = "OPERATOR_CONDITION_NAME" |
| 43 | +) |
| 44 | + |
| 45 | +// condition is a Condition that gets and sets a specific |
| 46 | +// conditionType in the OperatorCondition CR. |
| 47 | +type condition struct { |
| 48 | + namespacedName types.NamespacedName |
| 49 | + condType apiv1.ConditionType |
| 50 | + client client.Client |
| 51 | +} |
| 52 | + |
| 53 | +var _ Condition = &condition{} |
| 54 | + |
| 55 | +// NewCondition returns a new Condition interface using the provided client |
| 56 | +// for the specified conditionType. The condition will internally fetch the namespacedName |
| 57 | +// of the operatorConditionCRD. |
| 58 | +func NewCondition(cl client.Client, condType apiv1.ConditionType) (Condition, error) { |
| 59 | + objKey, err := GetNamespacedName() |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + return &condition{ |
| 64 | + namespacedName: *objKey, |
| 65 | + condType: condType, |
| 66 | + client: cl, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +// Get implements conditions.Get |
| 71 | +func (c *condition) Get(ctx context.Context) (*metav1.Condition, error) { |
| 72 | + operatorCond := &apiv1.OperatorCondition{} |
| 73 | + err := c.client.Get(ctx, c.namespacedName, operatorCond) |
| 74 | + if err != nil { |
| 75 | + return nil, ErrNoOperatorCondition |
| 76 | + } |
| 77 | + con := meta.FindStatusCondition(operatorCond.Status.Conditions, string(c.condType)) |
| 78 | + |
| 79 | + if con == nil { |
| 80 | + return nil, fmt.Errorf("conditionType %v not found", c.condType) |
| 81 | + } |
| 82 | + return con, nil |
| 83 | +} |
| 84 | + |
| 85 | +// Set implements conditions.Set |
| 86 | +func (c *condition) Set(ctx context.Context, status metav1.ConditionStatus, option ...Option) error { |
| 87 | + operatorCond := &apiv1.OperatorCondition{} |
| 88 | + err := c.client.Get(ctx, c.namespacedName, operatorCond) |
| 89 | + if err != nil { |
| 90 | + return ErrNoOperatorCondition |
| 91 | + } |
| 92 | + |
| 93 | + newCond := &metav1.Condition{ |
| 94 | + Type: string(c.condType), |
| 95 | + Status: status, |
| 96 | + } |
| 97 | + |
| 98 | + if len(option) != 0 { |
| 99 | + for _, opt := range option { |
| 100 | + opt(newCond) |
| 101 | + } |
| 102 | + } |
| 103 | + meta.SetStatusCondition(&operatorCond.Status.Conditions, *newCond) |
| 104 | + err = c.client.Status().Update(ctx, operatorCond) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// GetNamespacedName returns the NamespacedName of the CR. It returns an error |
| 112 | +// when the name of the CR cannot be found from the environment variable set by |
| 113 | +// OLM. Hence, GetNamespacedName() can provide the NamespacedName when the operator |
| 114 | +// is running on cluster and is being managed by OLM. If running locally, operator |
| 115 | +// writers are encouraged to skip this method or gracefully handle the errors by logging |
| 116 | +// a message. |
| 117 | +func GetNamespacedName() (*types.NamespacedName, error) { |
| 118 | + conditionName := os.Getenv(operatorCondEnvVar) |
| 119 | + if conditionName == "" { |
| 120 | + return nil, fmt.Errorf("could not determine operator condition name: environment variable %s not set", operatorCondEnvVar) |
| 121 | + } |
| 122 | + operatorNs, err := readNamespace() |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("could not determine operator namespace: %v", err) |
| 125 | + } |
| 126 | + return &types.NamespacedName{Name: conditionName, Namespace: operatorNs}, nil |
| 127 | +} |
0 commit comments