Skip to content

Commit f0f45b4

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 c7ee4c5 commit f0f45b4

File tree

9 files changed

+108
-2
lines changed

9 files changed

+108
-2
lines changed

Diff for: Documentation/install/local-values.yaml

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

Diff for: cmd/olm/main.go

+85-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ import (
1111
"github.com/prometheus/client_golang/prometheus/promhttp"
1212
log "github.com/sirupsen/logrus"
1313
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"
1522
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
1623
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
1724
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm"
1825
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1926
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/signals"
2027
"github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
2128
olmversion "github.com/operator-framework/operator-lifecycle-manager/pkg/version"
29+
"k8s.io/client-go/tools/clientcmd"
2230
)
2331

2432
const (
2533
defaultWakeupInterval = 5 * time.Minute
34+
defaultOperatorName = "openshift-operator-lifecycle-manager"
2635
)
2736

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

50+
writeStatusName = flag.String(
51+
"writeStatusName", defaultOperatorName, "ClusterOperator name in which to write status, set to \"\" to disable.")
52+
4153
debug = flag.Bool(
4254
"debug", false, "use debug log level")
4355

@@ -89,6 +101,16 @@ func main() {
89101

90102
opClient := operatorclient.NewClientFromConfig(*kubeConfigPath, logger)
91103

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

@@ -105,6 +127,67 @@ func main() {
105127
http.Handle("/metrics", promhttp.Handler())
106128
go http.ListenAndServe(":8081", nil)
107129

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+
109191
<-done
110192
}
193+

Diff for: deploy/chart/templates/0000_50_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:

Diff for: deploy/chart/values.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ rbacApiVersion: rbac.authorization.k8s.io
22
namespace: operator-lifecycle-manager
33
catalog_namespace: operator-lifecycle-manager
44
operator_namespace: operators
5+
<<<<<<< HEAD
56
minKubeVersion: 1.11.0
7+
=======
8+
writeStatusName: '""'
9+
>>>>>>> feat(olm): write operator status by default
610
imagestream: false
711
debug: false
812
olm:

Diff for: 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:

Diff for: manifests/0000_30_20-operatorstatus.yaml

+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

Diff for: 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+
}

Diff for: 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:

Diff for: test/e2e/e2e-values.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
writeStatusName: '""'
2+
13
olm:
24
replicaCount: 1
35
image:

0 commit comments

Comments
 (0)