Skip to content

Commit

Permalink
Add conditions.Factory
Browse files Browse the repository at this point in the history
The conditions.NewCondition function reads cluster state to determine
the namespace the operator is running in so that it can manipulate the
correct OperatorConditions object in a cluster.

However this function's dependency on a specific file existing in a
specific location makes it difficult to test code that uses it.

The Factory interface is introduced to enable users to provide their own
implementations for building a Condition object to manage OperatorCondition
resources. Users can now build a factory and inject it into code that builds
new conditions. This enables code under test to use the Factory abstraction
and enables operator authors to inject custom functionality needed when
writing tests.

Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford committed Apr 20, 2021
1 parent 0449c0c commit 8ad7a7a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 57 deletions.
26 changes: 0 additions & 26 deletions conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"os"

apiv1 "github.com/operator-framework/api/pkg/operators/v1"
"github.com/operator-framework/operator-lib/internal/utils"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -30,16 +29,6 @@ import (
var (
// ErrNoOperatorCondition indicates that the operator condition CRD is nil
ErrNoOperatorCondition = fmt.Errorf("operator Condition CRD is nil")

// readNamespace gets the namespacedName of the operator.
readNamespace = utils.GetOperatorNamespace
)

const (
// operatorCondEnvVar is the env variable which
// contains the name of the Condition CR associated to the operator,
// set by OLM.
operatorCondEnvVar = "OPERATOR_CONDITION_NAME"
)

// condition is a Condition that gets and sets a specific
Expand All @@ -52,21 +41,6 @@ type condition struct {

var _ Condition = &condition{}

// NewCondition returns a new Condition interface using the provided client
// for the specified conditionType. The condition will internally fetch the namespacedName
// of the operatorConditionCRD.
func NewCondition(cl client.Client, condType apiv1.ConditionType) (Condition, error) {
objKey, err := GetNamespacedName()
if err != nil {
return nil, err
}
return &condition{
namespacedName: *objKey,
condType: condType,
client: cl,
}, nil
}

// Get implements conditions.Get
func (c *condition) Get(ctx context.Context) (*metav1.Condition, error) {
operatorCond := &apiv1.OperatorCondition{}
Expand Down
94 changes: 94 additions & 0 deletions conditions/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2020 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package conditions

import (
"fmt"
"os"

apiv1 "github.com/operator-framework/api/pkg/operators/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/operator-framework/operator-lib/internal/utils"
)

// Factory define the interface for building Conditions.
type Factory interface {
NewCondition(apiv1.ConditionType) (Condition, error)
}

// InClusterFactory is an implementation of Factory that build Conditions
// ready to be used in a cluster.
type InClusterFactory struct {
Client client.Client
}

// NewCondition creates a new Condition using the provided client and condition
// type. The condition's name and namespace are determined by the Factory's GetName
// and GetNamespace functions.
func (f InClusterFactory) NewCondition(condType apiv1.ConditionType) (Condition, error) {
conditionName, err := f.getConditionName()
if err != nil {
return nil, fmt.Errorf("get operator condition name: %v", err)
}
conditionNamespace, err := f.getConditionNamespace()
if err != nil {
return nil, fmt.Errorf("get operator condition namespace: %v", err)
}

objKey := types.NamespacedName{Name: conditionName, Namespace: conditionNamespace}
return &condition{
namespacedName: objKey,
condType: condType,
client: f.Client,
}, nil
}

const (
// operatorCondEnvVar is the env variable which
// contains the name of the Condition CR associated to the operator,
// set by OLM.
operatorCondEnvVar = "OPERATOR_CONDITION_NAME"
)

// DefaultGetConditionName reads and returns the OPERATOR_CONDITION_NAME environment
// variable. If the variable is unset or empty, it returns an error.
func (f InClusterFactory) getConditionName() (string, error) {
name := os.Getenv(operatorCondEnvVar)
if name == "" {
return "", fmt.Errorf("could not determine operator condition name: environment variable %s not set", operatorCondEnvVar)
}
return name, nil
}

// readNamespace gets the namespacedName of the operator.
var readNamespace = utils.GetOperatorNamespace

// DefaultGetConditionNamespace reads the namespace file mounted into a pod in a
// cluster via its service account volume. If the file is not found or cannot be
// read, this function returns an error.
func (f InClusterFactory) getConditionNamespace() (string, error) {
return readNamespace()
}

// NewCondition returns a new Condition interface using the provided client
// for the specified conditionType. The condition will internally fetch the namespacedName
// of the operatorConditionCRD.
//
// Deprecated: Use InClusterFactory{}.NewCondition() instead.
func NewCondition(cl client.Client, condType apiv1.ConditionType) (Condition, error) {
return InClusterFactory{cl}.NewCondition(condType)
}
Loading

0 comments on commit 8ad7a7a

Please sign in to comment.