Skip to content

Commit f442ff5

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 81104ff commit f442ff5

File tree

9 files changed

+101
-2
lines changed

9 files changed

+101
-2
lines changed

Documentation/install/local-values.yaml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 82 additions & 2 deletions
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"
1416

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"
1520
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
1621
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
1722
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm"
1823
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1924
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/signals"
2025
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
2126
olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version"
27+
"k8s.io/client-go/tools/clientcmd"
2228
)
2329

2430
const (
25-
defaultWakeupInterval = 5 * time.Minute
31+
defaultWakeupInterval = 5 * time.Minute
32+
defaultOperatorName = "openshift-operator-lifecycle-manager"
33+
openshiftConfigServiceName = "v1.config.openshift.io"
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,65 @@ 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+
_, 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+
110189
<-done
111190
}
191+

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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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:
Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)