Skip to content

Commit 973850e

Browse files
Add helpers for operator-conditions (#41)
* Operator Conditions: Add helpers for olm operator conditions This PR adds helpers for operator conditions CR created by OLM for the operator. * Bump k8s dependencies and controller-runtime * clean up go mod file * Implement getOperatorNamespace * Make linter happy
1 parent 6700ae5 commit 973850e

File tree

5 files changed

+567
-23
lines changed

5 files changed

+567
-23
lines changed

conditions/operator_conditions.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
"errors"
19+
"fmt"
20+
"io/ioutil"
21+
"os"
22+
"strings"
23+
24+
api "github.com/operator-framework/api/pkg/operators/v1"
25+
"k8s.io/apimachinery/pkg/api/meta"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
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+
35+
// TODO: verify from OLM if this will be the name of the environment variable
36+
// which is set for the Condition resource owned by the operator.
37+
const operatorCondEnvVar = "OPERATOR_CONDITION_NAME"
38+
39+
var readNamespace = func() ([]byte, error) {
40+
return ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
41+
}
42+
43+
// GetNamespacedName returns the NamespacedName of the CR. It returns an error
44+
// when the name of the CR cannot be found from the environment variable set by
45+
// OLM, or when the namespace cannot be found from the associated service account
46+
// secret.
47+
func GetNamespacedName() (*types.NamespacedName, error) {
48+
conditionName := os.Getenv(operatorCondEnvVar)
49+
if conditionName == "" {
50+
return nil, fmt.Errorf("required env %s not set, cannot find operator condition CR for the operator", operatorCondEnvVar)
51+
}
52+
operatorNs, err := getOperatorNamespace()
53+
if err != nil {
54+
return nil, err
55+
}
56+
return &types.NamespacedName{Name: conditionName, Namespace: operatorNs}, nil
57+
}
58+
59+
// SetOperatorCondition adds the specific condition to the Condition CR or
60+
// updates the provided status of the condition if already present.
61+
func SetOperatorCondition(operatorCondition *api.OperatorCondition, newCond metav1.Condition) error {
62+
if operatorCondition == nil {
63+
return ErrNoOperatorCondition
64+
}
65+
66+
meta.SetStatusCondition(&operatorCondition.Status.Conditions, newCond)
67+
return nil
68+
}
69+
70+
// RemoveOperatorCondition removes the specific condition present in Condition CR.
71+
func RemoveOperatorCondition(operatorCondition *api.OperatorCondition, conditionType string) error {
72+
if operatorCondition == nil {
73+
return ErrNoOperatorCondition
74+
}
75+
meta.RemoveStatusCondition(&operatorCondition.Status.Conditions, conditionType)
76+
return nil
77+
}
78+
79+
// FindOperatorCondition returns the specific condition present in the Condition CR.
80+
func FindOperatorCondition(operatorCondition *api.OperatorCondition, conditionType string) (*metav1.Condition, error) {
81+
if operatorCondition == nil {
82+
return nil, ErrNoOperatorCondition
83+
}
84+
85+
con := meta.FindStatusCondition(operatorCondition.Status.Conditions, conditionType)
86+
87+
if con == nil {
88+
return nil, fmt.Errorf("conditionType %s not found", conditionType)
89+
}
90+
return con, nil
91+
}
92+
93+
// IsConditionStatusTrue returns true when the condition is present in "True" state in the CR.
94+
func IsConditionStatusTrue(operatorCondition *api.OperatorCondition, conditionType string) (bool, error) {
95+
return IsConditionStatusPresentAndEqual(operatorCondition, conditionType, metav1.ConditionTrue)
96+
}
97+
98+
// IsConditionStatusFalse returns true when the condition is present in "False" state in the CR.
99+
func IsConditionStatusFalse(operatorCondition *api.OperatorCondition, conditionType string) (bool, error) {
100+
return IsConditionStatusPresentAndEqual(operatorCondition, conditionType, metav1.ConditionFalse)
101+
}
102+
103+
// IsConditionStatusUnknown returns true when the condition is present in "Unknown" state in the CR.
104+
func IsConditionStatusUnknown(operatorCondition *api.OperatorCondition, conditionType string) (bool, error) {
105+
return IsConditionStatusPresentAndEqual(operatorCondition, conditionType, metav1.ConditionUnknown)
106+
}
107+
108+
// IsConditionStatusPresentAndEqual returns true when the condition is present in the CR and is in the
109+
// specified state.
110+
func IsConditionStatusPresentAndEqual(operatorCondition *api.OperatorCondition, conditionType string, conditionStatus metav1.ConditionStatus) (bool, error) {
111+
c, err := FindOperatorCondition(operatorCondition, conditionType)
112+
if err != nil {
113+
return false, err
114+
}
115+
116+
if c.Status == conditionStatus {
117+
return true, nil
118+
}
119+
return false, nil
120+
}
121+
122+
// getOperatorNamespace returns the namespace the operator should be running in.
123+
func getOperatorNamespace() (string, error) {
124+
nsBytes, err := readNamespace()
125+
if err != nil {
126+
if os.IsNotExist(err) {
127+
return "", errors.New("cannot find namespace of the operator")
128+
}
129+
return "", err
130+
}
131+
ns := strings.TrimSpace(string(nsBytes))
132+
return ns, nil
133+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestLeader(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Conditions Suite")
27+
}

0 commit comments

Comments
 (0)