@@ -11,18 +11,27 @@ import (
11
11
"github.com/prometheus/client_golang/prometheus/promhttp"
12
12
log "github.com/sirupsen/logrus"
13
13
v1 "k8s.io/api/core/v1"
14
-
14
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
15
+ "k8s.io/apimachinery/pkg/api/meta"
16
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
+
18
+ configv1 "github.com/openshift/api/config/v1"
19
+ configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
20
+ clusteroperatorv1helpers "github.com/openshift/library-go/pkg/config/clusteroperator/v1helpers"
21
+ operatorv1helpers "github.com/openshift/library-go/pkg/operator/v1helpers"
15
22
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
16
23
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
17
24
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm"
18
25
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
19
26
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/signals"
20
27
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
21
28
olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version"
29
+ "k8s.io/client-go/tools/clientcmd"
22
30
)
23
31
24
32
const (
25
33
defaultWakeupInterval = 5 * time .Minute
34
+ defaultOperatorName = "operator-lifecycle-manager"
26
35
)
27
36
28
37
// config flags defined globally so that they appear on the test binary as well
38
47
"If not set, or set to the empty string (e.g. `-watchedNamespaces=\" \" `), " +
39
48
"olm operator will watch all namespaces in the cluster." )
40
49
50
+ writeStatusName = flag .String (
51
+ "writeStatusName" , defaultOperatorName , "ClusterOperator name in which to write status, set to \" \" to disable." )
52
+
41
53
debug = flag .Bool (
42
54
"debug" , false , "use debug log level" )
43
55
@@ -89,6 +101,16 @@ func main() {
89
101
90
102
opClient := operatorclient .NewClientFromConfig (* kubeConfigPath , logger )
91
103
104
+ // create a config client for operator status
105
+ config , err := clientcmd .BuildConfigFromFlags ("" , * kubeConfigPath )
106
+ if err != nil {
107
+ log .Fatalf ("error configuring client: %s" , err .Error ())
108
+ }
109
+ configClient , err := configv1client .NewForConfig (config )
110
+ if err != nil {
111
+ log .Fatalf ("error configuring client: %s" , err .Error ())
112
+ }
113
+
92
114
// Create a new instance of the operator.
93
115
operator , err := olm .NewOperator (logger , crClient , opClient , & install.StrategyResolver {}, * wakeupInterval , namespaces )
94
116
@@ -105,6 +127,91 @@ func main() {
105
127
http .Handle ("/metrics" , promhttp .Handler ())
106
128
go http .ListenAndServe (":8081" , nil )
107
129
108
- _ , done := operator .Run (stopCh )
130
+ ready , done := operator .Run (stopCh )
131
+ <- ready
132
+
133
+ if * writeStatusName != "" {
134
+ existing , err := configClient .ClusterOperators ().Get (* writeStatusName , metav1.GetOptions {})
135
+ if meta .IsNoMatchError (err ) {
136
+ log .Infof ("ClusterOperator api not present, skipping update" )
137
+ } else if k8serrors .IsNotFound (err ) {
138
+ log .Info ("Existing cluster operator not found, creating" )
139
+ created , err := configClient .ClusterOperators ().Create (& configv1.ClusterOperator {
140
+ ObjectMeta : metav1.ObjectMeta {
141
+ Name : * writeStatusName ,
142
+ },
143
+ })
144
+ if err != nil {
145
+ log .Fatalf ("ClusterOperator create failed: %v\n " , err )
146
+ }
147
+
148
+ created .Status = configv1.ClusterOperatorStatus {
149
+ Conditions : []configv1.ClusterOperatorStatusCondition {
150
+ configv1.ClusterOperatorStatusCondition {
151
+ Type : configv1 .OperatorProgressing ,
152
+ Status : configv1 .ConditionFalse ,
153
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
154
+ LastTransitionTime : metav1 .Now (),
155
+ },
156
+ configv1.ClusterOperatorStatusCondition {
157
+ Type : configv1 .OperatorFailing ,
158
+ Status : configv1 .ConditionFalse ,
159
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
160
+ LastTransitionTime : metav1 .Now (),
161
+ },
162
+ configv1.ClusterOperatorStatusCondition {
163
+ Type : configv1 .OperatorAvailable ,
164
+ Status : configv1 .ConditionTrue ,
165
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
166
+ LastTransitionTime : metav1 .Now (),
167
+ },
168
+ },
169
+ Versions : []configv1.OperandVersion {{
170
+ Name : "operator" ,
171
+ Version : olmversion .Full (),
172
+ }},
173
+ }
174
+ _ , err = configClient .ClusterOperators ().UpdateStatus (created )
175
+ if err != nil {
176
+ log .Fatalf ("ClusterOperator update status failed: %v" , err )
177
+ }
178
+ } else if err != nil {
179
+ log .Fatalf ("ClusterOperators get failed: %v" , err )
180
+ } else {
181
+ clusteroperatorv1helpers .SetStatusCondition (& existing .Status .Conditions , configv1.ClusterOperatorStatusCondition {
182
+ Type : configv1 .OperatorProgressing ,
183
+ Status : configv1 .ConditionFalse ,
184
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
185
+ LastTransitionTime : metav1 .Now (),
186
+ })
187
+ clusteroperatorv1helpers .SetStatusCondition (& existing .Status .Conditions , configv1.ClusterOperatorStatusCondition {
188
+ Type : configv1 .OperatorFailing ,
189
+ Status : configv1 .ConditionFalse ,
190
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
191
+ LastTransitionTime : metav1 .Now (),
192
+ })
193
+ clusteroperatorv1helpers .SetStatusCondition (& existing .Status .Conditions , configv1.ClusterOperatorStatusCondition {
194
+ Type : configv1 .OperatorAvailable ,
195
+ Status : configv1 .ConditionTrue ,
196
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
197
+ LastTransitionTime : metav1 .Now (),
198
+ })
199
+
200
+ olmOperandVersion := configv1.OperandVersion {Name : "operator" , Version : olmversion .Full ()}
201
+ // look for operator version, even though in OLM's case should only be one
202
+ for _ , item := range existing .Status .Versions {
203
+ if item .Name == "operator" && item != olmOperandVersion {
204
+ // if a cluster wide upgrade has occurred, hopefully any existing operator statuses have been deleted
205
+ log .Infof ("Updating version from %v to %v\n " , item .Version , olmversion .Full ())
206
+ }
207
+ }
208
+ operatorv1helpers .SetOperandVersion (& existing .Status .Versions , olmOperandVersion )
209
+ _ , err = configClient .ClusterOperators ().UpdateStatus (existing )
210
+ if err != nil {
211
+ log .Fatalf ("ClusterOperator update status failed: %v" , err )
212
+ }
213
+ }
214
+ }
215
+
109
216
<- done
110
217
}
0 commit comments