@@ -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"
16
+ "k8s.io/apimachinery/pkg/api/meta"
14
17
18
+ configv1 "github.com/openshift/api/config/v1"
19
+ configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
20
+ v1helpers "github.com/openshift/library-go/pkg/config/clusteroperator/v1helpers"
15
21
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
16
22
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
17
23
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm"
18
24
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
19
25
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/signals"
20
26
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
21
27
olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version"
28
+ "k8s.io/client-go/tools/clientcmd"
22
29
)
23
30
24
31
const (
25
- defaultWakeupInterval = 5 * time .Minute
32
+ defaultWakeupInterval = 5 * time .Minute
33
+ defaultOperatorName = "openshift-operator-lifecycle-manager"
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,60 @@ 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
+ 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
+ Version : olmversion .Full (),
158
+ }
159
+ _ , err = configClient .ClusterOperators ().UpdateStatus (created )
160
+ if err != nil {
161
+ log .Fatalf ("ClusterOperator update status failed: %v" , err )
162
+ }
163
+ } else if err != nil {
164
+ log .Fatalf ("ClusterOperators get failed: %v" , err )
165
+ } else {
166
+ v1helpers .SetStatusCondition (& existing .Status .Conditions , configv1.ClusterOperatorStatusCondition {
167
+ Type : configv1 .OperatorAvailable ,
168
+ Status : configv1 .ConditionTrue ,
169
+ Message : fmt .Sprintf ("Done deploying %s." , olmversion .OLMVersion ),
170
+ LastTransitionTime : metav1 .Now (),
171
+ })
172
+ if existing .Status .Version != olmversion .Full () {
173
+ // if a cluster wide upgrade has occurred, hopefully any existing operator statuses have been deleted
174
+ log .Infof ("Updating version from %v to %v\n " , existing .Status .Version , olmversion .Full ())
175
+ }
176
+ existing .Status .Version = olmversion .Full ()
177
+ _ , err = configClient .ClusterOperators ().UpdateStatus (existing )
178
+ if err != nil {
179
+ log .Fatalf ("ClusterOperator update status failed: %v" , err )
180
+ }
181
+ }
182
+ }
183
+
110
184
<- done
111
185
}
186
+
0 commit comments