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