|
| 1 | +// Copyright 2021 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 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + apiv2 "github.com/operator-framework/api/pkg/operators/v2" |
| 22 | + "k8s.io/apimachinery/pkg/types" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | + |
| 25 | + "github.com/operator-framework/operator-lib/internal/utils" |
| 26 | +) |
| 27 | + |
| 28 | +// Factory define the interface for building Conditions. |
| 29 | +type Factory interface { |
| 30 | + NewCondition(apiv2.ConditionType) (Condition, error) |
| 31 | + GetNamespacedName() (*types.NamespacedName, error) |
| 32 | +} |
| 33 | + |
| 34 | +// InClusterFactory is a conditions factory that can build conditions and get |
| 35 | +// the namespaced name of the operator's condition based on an in-cluster |
| 36 | +// configuration. |
| 37 | +type InClusterFactory struct { |
| 38 | + Client client.Client |
| 39 | +} |
| 40 | + |
| 41 | +// NewCondition creates a new Condition using the provided client and condition |
| 42 | +// type. The condition's name and namespace are determined by the Factory's GetName |
| 43 | +// and GetNamespace functions. |
| 44 | +func (f InClusterFactory) NewCondition(condType apiv2.ConditionType) (Condition, error) { |
| 45 | + objKey, err := f.GetNamespacedName() |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + return &condition{ |
| 50 | + namespacedName: *objKey, |
| 51 | + condType: condType, |
| 52 | + client: f.Client, |
| 53 | + }, nil |
| 54 | +} |
| 55 | + |
| 56 | +// GetNamespacedName returns the NamespacedName of the CR. It returns an error |
| 57 | +// when the name of the CR cannot be found from the environment variable set by |
| 58 | +// OLM. Hence, GetNamespacedName() can provide the NamespacedName when the operator |
| 59 | +// is running on cluster and is being managed by OLM. |
| 60 | +func (f InClusterFactory) GetNamespacedName() (*types.NamespacedName, error) { |
| 61 | + conditionName, err := f.getConditionName() |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("get operator condition name: %v", err) |
| 64 | + } |
| 65 | + conditionNamespace, err := f.getConditionNamespace() |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("get operator condition namespace: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + return &types.NamespacedName{Name: conditionName, Namespace: conditionNamespace}, nil |
| 71 | +} |
| 72 | + |
| 73 | +const ( |
| 74 | + // operatorCondEnvVar is the env variable which |
| 75 | + // contains the name of the Condition CR associated to the operator, |
| 76 | + // set by OLM. |
| 77 | + operatorCondEnvVar = "OPERATOR_CONDITION_NAME" |
| 78 | +) |
| 79 | + |
| 80 | +// getConditionName reads and returns the OPERATOR_CONDITION_NAME environment |
| 81 | +// variable. If the variable is unset or empty, it returns an error. |
| 82 | +func (f InClusterFactory) getConditionName() (string, error) { |
| 83 | + name := os.Getenv(operatorCondEnvVar) |
| 84 | + if name == "" { |
| 85 | + return "", fmt.Errorf("could not determine operator condition name: environment variable %s not set", operatorCondEnvVar) |
| 86 | + } |
| 87 | + return name, nil |
| 88 | +} |
| 89 | + |
| 90 | +// readNamespace gets the namespacedName of the operator. |
| 91 | +var readNamespace = utils.GetOperatorNamespace |
| 92 | + |
| 93 | +// getConditionNamespace reads the namespace file mounted into a pod in a |
| 94 | +// cluster via its service account volume. If the file is not found or cannot be |
| 95 | +// read, this function returns an error. |
| 96 | +func (f InClusterFactory) getConditionNamespace() (string, error) { |
| 97 | + return readNamespace() |
| 98 | +} |
| 99 | + |
| 100 | +// NewCondition returns a new Condition interface using the provided client |
| 101 | +// for the specified conditionType. The condition will internally fetch the namespacedName |
| 102 | +// of the operatorConditionCRD. |
| 103 | +// |
| 104 | +// Deprecated: Use InClusterFactory{cl}.NewCondition() instead. |
| 105 | +func NewCondition(cl client.Client, condType apiv2.ConditionType) (Condition, error) { |
| 106 | + return InClusterFactory{cl}.NewCondition(condType) |
| 107 | +} |
| 108 | + |
| 109 | +// GetNamespacedName returns the NamespacedName of the CR. It returns an error |
| 110 | +// when the name of the CR cannot be found from the environment variable set by |
| 111 | +// OLM. Hence, GetNamespacedName() can provide the NamespacedName when the operator |
| 112 | +// is running on cluster and is being managed by OLM. If running locally, operator |
| 113 | +// writers are encouraged to skip this method or gracefully handle the errors by logging |
| 114 | +// a message. |
| 115 | +// |
| 116 | +// Deprecated: InClusterFactory{}.GetNamespacedName(). |
| 117 | +func GetNamespacedName() (*types.NamespacedName, error) { |
| 118 | + return InClusterFactory{}.GetNamespacedName() |
| 119 | +} |
0 commit comments