|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package olm |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "os" |
| 22 | + |
| 23 | + olmapiv2 "github.com/operator-framework/api/pkg/operators/v2" |
| 24 | + conditions "github.com/operator-framework/operator-lib/conditions" |
| 25 | + errs "github.com/pkg/errors" |
| 26 | + synpkg "github.com/syndesisio/syndesis/install/operator/pkg" |
| 27 | + "github.com/syndesisio/syndesis/install/operator/pkg/syndesis/capabilities" |
| 28 | + "github.com/syndesisio/syndesis/install/operator/pkg/syndesis/clienttools" |
| 29 | + "github.com/syndesisio/syndesis/install/operator/pkg/syndesis/configuration" |
| 30 | + appsv1 "k8s.io/api/apps/v1" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 34 | +) |
| 35 | + |
| 36 | +var opCondLog = logf.Log.WithName("operator-condition-log") |
| 37 | + |
| 38 | +type ConditionState struct { |
| 39 | + // The value of the condition, either metav1.ConditionTrue or metav1.ConditionFalse |
| 40 | + Status metav1.ConditionStatus |
| 41 | + // The single word reason for the condition setting |
| 42 | + // Must start with a letter |
| 43 | + // Rest of the world can include letters, numbers, commas and colons |
| 44 | + // Cannot end with comma or colon |
| 45 | + Reason string |
| 46 | + // The description of the reason for the condition change. |
| 47 | + Message string |
| 48 | +} |
| 49 | + |
| 50 | +func GetConditionName(ctx context.Context, clientTools *clienttools.ClientTools, namespace string) (string, error) { |
| 51 | + opCondLog.V(synpkg.DEBUG_LOGGING_LVL).Info("Finding OLM Operator Condition") |
| 52 | + |
| 53 | + apiSpec, err := capabilities.ApiCapabilities(clientTools) |
| 54 | + if err != nil { |
| 55 | + return "", err |
| 56 | + } |
| 57 | + |
| 58 | + if !apiSpec.OlmSupport { |
| 59 | + // |
| 60 | + // This cluster does not support OLM so nothing to do |
| 61 | + // |
| 62 | + opCondLog.V(synpkg.DEBUG_LOGGING_LVL).Info("No OLM support ... aborting Operation Condition Search") |
| 63 | + return "", nil |
| 64 | + } |
| 65 | + |
| 66 | + rtClient, err := clientTools.RuntimeClient() |
| 67 | + if err != nil { |
| 68 | + return "", errs.Wrap(err, "Failed to initialise runtime client") |
| 69 | + } |
| 70 | + |
| 71 | + // |
| 72 | + // Find the operator condition associated with this operator |
| 73 | + // |
| 74 | + // deployment -> owned by CSV -> operator condition has the same name |
| 75 | + // |
| 76 | + configName, err := configuration.GetProductName(configuration.TemplateConfig) |
| 77 | + if err != nil { |
| 78 | + return "", errs.Wrap(err, "Failed to determine product name") |
| 79 | + } |
| 80 | + deploymentName := configName + "-operator" |
| 81 | + |
| 82 | + opCondLog.V(synpkg.DEBUG_LOGGING_LVL).Info("Finding Operator Deployment", "name", deploymentName) |
| 83 | + deployment := &appsv1.Deployment{} |
| 84 | + if err = rtClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: deploymentName}, deployment); err != nil { |
| 85 | + return "", err // Should find the deployment of this operator |
| 86 | + } |
| 87 | + |
| 88 | + opCondLog.V(synpkg.DEBUG_LOGGING_LVL).Info("Operator Deployment found", "name", deploymentName) |
| 89 | + ownerRefs := deployment.GetOwnerReferences() |
| 90 | + if len(ownerRefs) > 1 || len(ownerRefs) == 0 { |
| 91 | + // No operator condition as this is not owned by a CSV |
| 92 | + return "", nil |
| 93 | + } |
| 94 | + |
| 95 | + if ownerRefs[0].Kind != "ClusterServiceVersion" { |
| 96 | + // No operator condition as this is not owned by a CSV |
| 97 | + return "", nil |
| 98 | + } |
| 99 | + |
| 100 | + opCondLog.V(synpkg.DEBUG_LOGGING_LVL).Info("CSV Owned Deployment", "name", deploymentName, "owner", ownerRefs[0].Name) |
| 101 | + return ownerRefs[0].Name, nil |
| 102 | +} |
| 103 | + |
| 104 | +// |
| 105 | +// Creates the condition if it does not already exist |
| 106 | +// |
| 107 | +func SetUpgradeCondition(ctx context.Context, clientTools *clienttools.ClientTools, namespace string, state ConditionState) error { |
| 108 | + |
| 109 | + conditionName, err := GetConditionName(ctx, clientTools, namespace) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } else if conditionName == "" { |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + rtClient, err := clientTools.RuntimeClient() |
| 117 | + if err != nil { |
| 118 | + return errs.Wrap(err, "Failed to initialise runtime client") |
| 119 | + } |
| 120 | + |
| 121 | + clusterFactory := conditions.InClusterFactory{rtClient} |
| 122 | + err = os.Setenv("OPERATOR_CONDITION_NAME", conditionName) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + uc, err := clusterFactory.NewCondition(olmapiv2.ConditionType(olmapiv2.Upgradeable)) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + err = uc.Set(ctx, |
| 132 | + state.Status, |
| 133 | + conditions.WithReason(state.Reason), |
| 134 | + conditions.WithMessage(state.Message)) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments