Skip to content

Commit a35b4d0

Browse files
committed
feat(operators): add v2alpha1 operator controller
Add support for reconciling the v2alpha1 Operator API: - generate Operator resources when none exist and labeled components found - aggregate component references in the status of their respective Operators - aggregate component status conditions to their respective Operators - enabled by specifying the "OperatorLifecycleManagerV2" feature gate option when invoking the olm command Also: - regenerate clients and fakes
1 parent 9f126de commit a35b4d0

File tree

14 files changed

+1529
-38
lines changed

14 files changed

+1529
-38
lines changed

cmd/olm/main.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"context"
55
"crypto/tls"
6-
"flag"
76
"fmt"
87
"net/http"
98
"os"
@@ -14,12 +13,13 @@ import (
1413
configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
1514
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/queueinformer"
1615
"github.com/prometheus/client_golang/prometheus/promhttp"
17-
log "github.com/sirupsen/logrus"
16+
"github.com/sirupsen/logrus"
17+
"github.com/spf13/pflag"
1818
v1 "k8s.io/api/core/v1"
19-
"k8s.io/client-go/tools/clientcmd"
2019

21-
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client"
20+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
2221
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm"
22+
"github.com/operator-framework/operator-lifecycle-manager/pkg/feature"
2323
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/filemonitor"
2424
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
2525
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorstatus"
@@ -37,53 +37,53 @@ const (
3737

3838
// config flags defined globally so that they appear on the test binary as well
3939
var (
40-
kubeConfigPath = flag.String(
41-
"kubeconfig", "", "absolute path to the kubeconfig file")
42-
43-
wakeupInterval = flag.Duration(
40+
wakeupInterval = pflag.Duration(
4441
"interval", defaultWakeupInterval, "wake up interval")
4542

46-
watchedNamespaces = flag.String(
43+
watchedNamespaces = pflag.String(
4744
"watchedNamespaces", "", "comma separated list of namespaces for olm operator to watch. "+
4845
"If not set, or set to the empty string (e.g. `-watchedNamespaces=\"\"`), "+
4946
"olm operator will watch all namespaces in the cluster.")
5047

51-
writeStatusName = flag.String(
48+
writeStatusName = pflag.String(
5249
"writeStatusName", defaultOperatorName, "ClusterOperator name in which to write status, set to \"\" to disable.")
5350

54-
writePackageServerStatusName = flag.String(
51+
writePackageServerStatusName = pflag.String(
5552
"writePackageServerStatusName", defaultPackageServerStatusName, "ClusterOperator name in which to write status for package API server, set to \"\" to disable.")
5653

57-
debug = flag.Bool(
54+
debug = pflag.Bool(
5855
"debug", false, "use debug log level")
5956

60-
version = flag.Bool("version", false, "displays olm version")
57+
version = pflag.Bool("version", false, "displays olm version")
6158

62-
tlsKeyPath = flag.String(
59+
tlsKeyPath = pflag.String(
6360
"tls-key", "", "Path to use for private key (requires tls-cert)")
6461

65-
tlsCertPath = flag.String(
62+
tlsCertPath = pflag.String(
6663
"tls-cert", "", "Path to use for certificate key (requires tls-key)")
6764

68-
profiling = flag.Bool(
65+
profiling = pflag.Bool(
6966
"profiling", false, "serve profiling data (on port 8080)")
7067

71-
namespace = flag.String(
68+
namespace = pflag.String(
7269
"namespace", "", "namespace where cleanup runs")
7370
)
7471

7572
func init() {
7673
metrics.RegisterOLM()
74+
75+
// Add feature gates before parsing
76+
feature.AddFlag(pflag.CommandLine)
7777
}
7878

7979
// main function - entrypoint to OLM operator
8080
func main() {
8181
// Get exit signal context
8282
ctx, cancel := context.WithCancel(signals.Context())
8383
defer cancel()
84+
pflag.Parse()
8485

8586
// Parse the command-line flags.
86-
flag.Parse()
8787

8888
// Check if version flag was set
8989
if *version {
@@ -104,9 +104,9 @@ func main() {
104104
}
105105

106106
// Set log level to debug if `debug` flag set
107-
logger := log.New()
107+
logger := logrus.New()
108108
if *debug {
109-
logger.SetLevel(log.DebugLevel)
109+
logger.SetLevel(logrus.DebugLevel)
110110
}
111111
logger.Infof("log level %s", logger.Level)
112112

@@ -169,24 +169,27 @@ func main() {
169169
}()
170170
}
171171

172-
// create a config client for operator status
173-
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPath)
172+
mgr, err := Manager(ctx)
174173
if err != nil {
175-
log.Fatalf("error configuring client: %s", err.Error())
174+
logger.WithError(err).Fatalf("error configuring controller manager")
176175
}
176+
config := mgr.GetConfig()
177+
177178
versionedConfigClient, err := configclientset.NewForConfig(config)
178179
if err != nil {
179-
err = fmt.Errorf("error configuring OpenShift Proxy client: %v", err)
180-
return
180+
logger.WithError(err).Fatal("error configuring openshift proxy client")
181181
}
182182
configClient, err := configv1client.NewForConfig(config)
183183
if err != nil {
184-
log.Fatalf("error configuring client: %s", err.Error())
184+
logger.WithError(err).Fatal("error configuring config client")
185185
}
186-
opClient := operatorclient.NewClientFromConfig(*kubeConfigPath, logger)
187-
crClient, err := client.NewClient(*kubeConfigPath)
186+
opClient, err := operatorclient.NewClientFromRestConfig(config)
188187
if err != nil {
189-
log.Fatalf("error configuring client: %s", err.Error())
188+
logger.WithError(err).Fatal("error configuring operator client")
189+
}
190+
crClient, err := versioned.NewForConfig(config)
191+
if err != nil {
192+
logger.WithError(err).Fatal("error configuring custom resource client")
190193
}
191194

192195
cleanup(logger, opClient, crClient)
@@ -203,7 +206,7 @@ func main() {
203206
olm.WithConfigClient(versionedConfigClient),
204207
)
205208
if err != nil {
206-
log.WithError(err).Fatalf("error configuring operator")
209+
logger.WithError(err).Fatalf("error configuring operator")
207210
return
208211
}
209212

@@ -227,5 +230,10 @@ func main() {
227230
go monitor.Run(op.Done())
228231
}
229232

233+
// Start the controller manager
234+
if err := mgr.Start(ctx.Done()); err != nil {
235+
logger.WithError(err).Fatal("controller manager stopped")
236+
}
237+
230238
<-op.Done()
231239
}

cmd/olm/manager.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/operator-framework/api/crds"
8+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
9+
apierrors "k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
ctrl "sigs.k8s.io/controller-runtime"
12+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
13+
14+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators"
15+
"github.com/operator-framework/operator-lifecycle-manager/pkg/feature"
16+
)
17+
18+
var log = ctrl.Log.WithName("manager")
19+
20+
func Manager(ctx context.Context) (ctrl.Manager, error) {
21+
ctrl.SetLogger(zap.Logger(true))
22+
setupLog := log.WithName("setup").V(1)
23+
24+
// Setup a Manager
25+
setupLog.Info("configuring manager")
26+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{MetricsBindAddress: "0"}) // TODO(njhale): Enable metrics on non-conflicting port (not 8080)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
// Setup a new controller to reconcile Operators
32+
setupLog.Info("configuring controller")
33+
client, err := apiextensionsv1.NewForConfig(mgr.GetConfig())
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
if feature.Gate.Enabled(feature.OperatorLifecycleManagerV2) {
39+
setupLog.Info(fmt.Sprintf("feature enabled: %v", feature.OperatorLifecycleManagerV2))
40+
41+
reconciler, err := operators.NewOperatorReconciler(
42+
mgr.GetClient(),
43+
ctrl.Log.WithName("controllers").WithName("operator"),
44+
mgr.GetScheme(),
45+
)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
crd, err := client.CustomResourceDefinitions().Create(crds.Operator())
51+
if err != nil {
52+
if !apierrors.IsAlreadyExists(err) {
53+
return nil, err
54+
}
55+
56+
// Already exists, try to update
57+
if crd, err = client.CustomResourceDefinitions().Get(crds.Operator().GetName(), metav1.GetOptions{}); err != nil {
58+
return nil, err
59+
}
60+
61+
crd.Spec = crds.Operator().Spec
62+
if _, err = client.CustomResourceDefinitions().Update(crd); err != nil {
63+
return nil, err
64+
}
65+
}
66+
setupLog.Info("v2alpha1 CRDs installed")
67+
68+
if err = reconciler.SetupWithManager(mgr); err != nil {
69+
return nil, err
70+
}
71+
}
72+
73+
setupLog.Info("manager configured")
74+
75+
return mgr, nil
76+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,32 @@ spec:
2626
command:
2727
- /bin/olm
2828
args:
29-
- -namespace
29+
- --namespace
3030
- $(OPERATOR_NAMESPACE)
3131
{{- if .Values.watchedNamespaces }}
32-
- -watchedNamespaces
32+
- --watchedNamespaces
3333
- {{ .Values.watchedNamespaces }}
3434
{{- end }}
3535
{{- if .Values.olm.commandArgs }}
3636
- {{ .Values.olm.commandArgs }}
3737
{{- end }}
3838
{{- if .Values.debug }}
39-
- -debug
39+
- --debug
4040
{{- end }}
4141
{{- if .Values.writeStatusName }}
42-
- -writeStatusName
42+
- --writeStatusName
4343
- {{ .Values.writeStatusName }}
4444
{{- end }}
4545
{{- if .Values.writePackageServerStatusName }}
46-
- -writePackageServerStatusName
46+
- --writePackageServerStatusName
4747
- {{ .Values.writePackageServerStatusName }}
4848
{{- end }}
4949
{{- if .Values.olm.tlsCertPath }}
50-
- -tls-cert
50+
- --tls-cert
5151
- {{ .Values.olm.tlsCertPath }}
5252
{{- end }}
5353
{{- if .Values.olm.tlsKeyPath }}
54-
- -tls-key
54+
- --tls-key
5555
- {{ .Values.olm.tlsKeyPath }}
5656
{{- end }}
5757
image: {{ .Values.olm.image.ref }}

pkg/controller/bundle/bundle_unpacker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
listerscorev1 "k8s.io/client-go/listers/core/v1"
1919
listersrbacv1 "k8s.io/client-go/listers/rbac/v1"
2020

21-
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2221
"github.com/operator-framework/api/pkg/operators/reference"
22+
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2323
listersoperatorsv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
2424
)
2525

0 commit comments

Comments
 (0)