@@ -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 = "openshift-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,67 @@ 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 .OperatorAvailable ,
152
+ Status : configv1 .ConditionTrue ,
153
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
154
+ LastTransitionTime : metav1 .Now (),
155
+ },
156
+ },
157
+ Versions : []configv1.OperandVersion {{
158
+ Name : "operator" ,
159
+ Version : olmversion .Full (),
160
+ }},
161
+ }
162
+ _ , err = configClient .ClusterOperators ().UpdateStatus (created )
163
+ if err != nil {
164
+ log .Fatalf ("ClusterOperator update status failed: %v" , err )
165
+ }
166
+ } else if err != nil {
167
+ log .Fatalf ("ClusterOperators get failed: %v" , err )
168
+ } else {
169
+ clusteroperatorv1helpers .SetStatusCondition (& existing .Status .Conditions , configv1.ClusterOperatorStatusCondition {
170
+ Type : configv1 .OperatorAvailable ,
171
+ Status : configv1 .ConditionTrue ,
172
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
173
+ LastTransitionTime : metav1 .Now (),
174
+ })
175
+ olmOperandVersion := configv1.OperandVersion {Name : "operator" , Version : olmversion .Full ()}
176
+ // look for operator version, even though in OLM's case should only be one
177
+ for _ , item := range existing .Status .Versions {
178
+ if item .Name == "operator" && item != olmOperandVersion {
179
+ // if a cluster wide upgrade has occurred, hopefully any existing operator statuses have been deleted
180
+ log .Infof ("Updating version from %v to %v\n " , item .Version , olmversion .Full ())
181
+ }
182
+ }
183
+ operatorv1helpers .SetOperandVersion (& existing .Status .Versions , olmOperandVersion )
184
+ _ , err = configClient .ClusterOperators ().UpdateStatus (existing )
185
+ if err != nil {
186
+ log .Fatalf ("ClusterOperator update status failed: %v" , err )
187
+ }
188
+ }
189
+ }
190
+
109
191
<- done
110
192
}
193
+
0 commit comments