Skip to content

Commit 83f488a

Browse files
author
Jeff Peeler
committed
feat(olm): write operator status by default
`writeStatusName` may be passed as an argument set to the name in which to write the status. The default is `openshift-operator-lifecycle-manager` with an empty string disabling the behavior. If the proper openshift API group isn't found, the status update will be skipped.
1 parent 5a4031b commit 83f488a

File tree

9 files changed

+96
-2
lines changed

9 files changed

+96
-2
lines changed

Documentation/install/local-values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
rbacApiVersion: rbac.authorization.k8s.io
22
namespace: local
33
watchedNamespaces: local
4+
writeStatusName: '""'
45
catalog_namespace: local
56
operator_namespace: local-operators
67
debug: true

cmd/olm/main.go

+77-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,26 @@ import (
1111
"github.com/prometheus/client_golang/prometheus"
1212
log "github.com/sirupsen/logrus"
1313
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"
1417

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"
1521
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
1622
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
1723
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm"
1824
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1925
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/signals"
2026
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
2127
olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version"
28+
"k8s.io/client-go/tools/clientcmd"
2229
)
2330

2431
const (
25-
defaultWakeupInterval = 5 * time.Minute
32+
defaultWakeupInterval = 5 * time.Minute
33+
defaultOperatorName = "openshift-operator-lifecycle-manager"
2634
)
2735

2836
// config flags defined globally so that they appear on the test binary as well
@@ -38,6 +46,9 @@ var (
3846
"If not set, or set to the empty string (e.g. `-watchedNamespaces=\"\"`), "+
3947
"olm operator will watch all namespaces in the cluster.")
4048

49+
writeStatusName = flag.String(
50+
"writeStatusName", defaultOperatorName, "ClusterOperator name in which to write status, set to \"\" to disable.")
51+
4152
debug = flag.Bool(
4253
"debug", false, "use debug log level")
4354

@@ -89,6 +100,16 @@ func main() {
89100

90101
opClient := operatorclient.NewClientFromConfig(*kubeConfigPath, logger)
91102

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+
92113
// Create a new instance of the operator.
93114
operator, err := olm.NewOperator(logger, crClient, opClient, &install.StrategyResolver{}, *wakeupInterval, namespaces)
94115

@@ -106,6 +127,60 @@ func main() {
106127
http.Handle("/metrics", prometheus.Handler())
107128
go http.ListenAndServe(":8080", nil)
108129

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+
110184
<-done
111185
}
186+

deploy/chart/templates/0000_30_10-olm-operator.deployment.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ spec:
3333
{{- if .Values.debug }}
3434
- -debug
3535
{{- end }}
36+
{{- if .Values.writeStatusName }}
37+
- -writeStatusName
38+
- {{ .Values.writeStatusName }}
39+
{{- end }}
3640
image: {{ .Values.olm.image.ref }}
3741
imagePullPolicy: {{ .Values.olm.image.pullPolicy }}
3842
ports:

deploy/chart/values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ rbacApiVersion: rbac.authorization.k8s.io
22
namespace: operator-lifecycle-manager
33
catalog_namespace: operator-lifecycle-manager
44
operator_namespace: operators
5+
writeStatusName: '""'
56
imagestream: false
67
debug: false
78
olm:

deploy/upstream/values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace: olm
33
catalog_namespace: olm
44
operator_namespace: operators
55
imagestream: false
6+
writeStatusName: '""'
67
olm:
78
replicaCount: 1
89
image:
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: config.openshift.io/v1
2+
kind: ClusterOperator
3+
metadata:
4+
namespace: openshift-operator-lifecycle-manager
5+
name: openshift-operator-lifecycle-manager

pkg/version/version.go

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ var GitCommit string
1212
func String() string {
1313
return fmt.Sprintf("OLM version: %s\ngit commit: %s\n", OLMVersion, GitCommit)
1414
}
15+
16+
// Full returns a hypenated concatenation of just OLMVersion and GitCommit
17+
func Full() string {
18+
return fmt.Sprintf("%s-%s", OLMVersion, GitCommit)
19+
}

test/e2e/e2e-bare-values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
rbacApiVersion: rbac.authorization.k8s.io
22
namespace: operator-lifecycle-manager
33
catalog_namespace: operator-lifecycle-manager
4+
writeStatusName: '""'
45
olm:
56
replicaCount: 1
67
image:

test/e2e/e2e-values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rbacApiVersion: rbac.authorization.k8s.io
2+
writeStatusName: '""'
23

34
olm:
45
replicaCount: 1

0 commit comments

Comments
 (0)