Skip to content

Commit 8933ba6

Browse files
committed
Use SA from spec
1 parent bfd4142 commit 8933ba6

19 files changed

+874
-90
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ build-push-e2e-catalog: ## Build the testdata catalog used for e2e tests and pus
152152
test-e2e: KIND_CLUSTER_NAME := operator-controller-e2e
153153
test-e2e: KUSTOMIZE_BUILD_DIR := config/overlays/e2e
154154
test-e2e: GO_BUILD_FLAGS := -cover
155-
test-e2e: run image-registry build-push-e2e-catalog registry-load-bundles e2e e2e-coverage kind-clean #HELP Run e2e test suite on local kind cluster
155+
test-e2e: run image-registry build-push-e2e-catalog registry-load-bundles apply-rbac e2e e2e-coverage kind-clean #HELP Run e2e test suite on local kind cluster
156156

157157
.PHONY: extension-developer-e2e
158158
extension-developer-e2e: KIND_CLUSTER_NAME := operator-controller-ext-dev-e2e #EXHELP Run extension-developer e2e on local kind cluster
@@ -193,6 +193,9 @@ registry-load-bundles: ## Load selected e2e testdata container images created in
193193
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(REGISTRY_ROOT)/bundles/registry-v1/prometheus-operator:v1.2.0 prometheus-operator.v1.2.0 prometheus-operator.v1.0.0
194194
testdata/bundles/registry-v1/build-push-e2e-bundle.sh ${E2E_REGISTRY_NAMESPACE} $(REGISTRY_ROOT)/bundles/registry-v1/prometheus-operator:v2.0.0 prometheus-operator.v2.0.0 prometheus-operator.v1.0.0
195195

196+
apply-rbac: ## Apply RBAC expected when using service account from spec
197+
kubectl apply -f testdata/rbac/prometheus-operator-bundle-rbac.yaml -n default
198+
196199
#SECTION Build
197200

198201
ifeq ($(origin VERSION), undefined)
@@ -238,7 +241,7 @@ run: docker-build kind-cluster kind-load kind-deploy #HELP Build the operator-co
238241

239242
.PHONY: docker-build
240243
docker-build: build-linux #EXHELP Build docker image for operator-controller with GOOS=linux and local GOARCH.
241-
$(CONTAINER_RUNTIME) build -t $(IMG) -f Dockerfile ./bin/linux
244+
$(CONTAINER_RUNTIME) build -t $(IMG) -f Dockerfile ./bin/linux --load
242245

243246
#SECTION Release
244247
ifeq ($(origin ENABLE_RELEASE_PIPELINE), undefined)

api/v1alpha1/clusterextension_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ type ClusterExtensionSpec struct {
7878
// the bundle may contain resources that are cluster-scoped or that are
7979
// installed in a different namespace. This namespace is expected to exist.
8080
InstallNamespace string `json:"installNamespace"`
81+
82+
//+kubebuilder:validation:Pattern:=^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$
83+
//+kubebuilder:validation:MaxLength:=253
84+
// ServiceAccountName is the name of the ServiceAccount to use to manage the resources in the bundle.
85+
// The service account is expected to exist in the InstallNamespace.
86+
ServiceAccountName string `json:"serviceAccountName"`
8187
}
8288

8389
const (

cmd/manager/main.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ import (
2828
"go.uber.org/zap/zapcore"
2929
k8slabels "k8s.io/apimachinery/pkg/labels"
3030
"k8s.io/apimachinery/pkg/selection"
31+
"k8s.io/apimachinery/pkg/types"
32+
"k8s.io/client-go/informers"
33+
"k8s.io/client-go/kubernetes"
34+
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
3135
_ "k8s.io/client-go/plugin/pkg/client/auth"
36+
"k8s.io/client-go/rest"
3237
ctrl "sigs.k8s.io/controller-runtime"
3338
crcache "sigs.k8s.io/controller-runtime/pkg/cache"
3439
"sigs.k8s.io/controller-runtime/pkg/client"
40+
"sigs.k8s.io/controller-runtime/pkg/event"
3541
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
3642
"sigs.k8s.io/controller-runtime/pkg/healthz"
3743
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -44,6 +50,7 @@ import (
4450
"github.com/operator-framework/rukpak/pkg/storage"
4551

4652
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
53+
"github.com/operator-framework/operator-controller/internal/authentication"
4754
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
4855
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
4956
"github.com/operator-framework/operator-controller/internal/controllers"
@@ -159,19 +166,44 @@ func main() {
159166
cl := mgr.GetClient()
160167
catalogClient := catalogclient.New(cl, cache.NewFilesystemCache(cachePath, httpClient))
161168

162-
installNamespaceMapper := helmclient.ObjectToStringMapper(func(obj client.Object) (string, error) {
163-
ext := obj.(*ocv1alpha1.ClusterExtension)
169+
saGetter, err := corev1client.NewForConfig(ctrl.GetConfigOrDie())
170+
if err != nil {
171+
setupLog.Error(err, "unable to create service account client")
172+
os.Exit(1)
173+
}
174+
175+
tg := authentication.NewTokenGetter(saGetter, 3600)
176+
nsMapper := func(obj client.Object) (string, error) {
177+
ext, ok := obj.(*ocv1alpha1.ClusterExtension)
178+
if !ok {
179+
return "", fmt.Errorf("cannot derive namespace from object of type %T", obj)
180+
}
164181
return ext.Spec.InstallNamespace, nil
165-
})
182+
}
183+
184+
rcm := func(ctx context.Context, obj client.Object, baseRestConfig *rest.Config) (*rest.Config, error) {
185+
cfg := rest.AnonymousClientConfig(rest.CopyConfig(baseRestConfig))
186+
ext, ok := obj.(*ocv1alpha1.ClusterExtension)
187+
if !ok {
188+
return cfg, nil
189+
}
190+
token, err := tg.Get(ctx, types.NamespacedName{Namespace: ext.Spec.InstallNamespace, Name: ext.Spec.ServiceAccountName})
191+
if err != nil {
192+
return nil, err
193+
}
194+
cfg.BearerToken = token
195+
return cfg, nil
196+
}
197+
166198
cfgGetter, err := helmclient.NewActionConfigGetter(mgr.GetConfig(), mgr.GetRESTMapper(),
167-
helmclient.StorageNamespaceMapper(installNamespaceMapper),
168-
helmclient.ClientNamespaceMapper(installNamespaceMapper),
199+
helmclient.ClientNamespaceMapper(nsMapper),
200+
helmclient.StorageNamespaceMapper(nsMapper),
201+
helmclient.RestConfigMapper(rcm),
169202
)
170203
if err != nil {
171204
setupLog.Error(err, "unable to config for creating helm client")
172205
os.Exit(1)
173206
}
174-
175207
acg, err := helmclient.NewActionClientGetter(cfgGetter)
176208
if err != nil {
177209
setupLog.Error(err, "unable to create helm client")
@@ -217,6 +249,9 @@ func main() {
217249
InstalledBundleGetter: &controllers.DefaultInstalledBundleGetter{ActionClientGetter: acg},
218250
Handler: registryv1handler.HandlerFunc(registry.HandleBundleDeployment),
219251
Finalizers: clusterExtensionFinalizers,
252+
InformerClientMap: make(map[types.UID]kubernetes.Interface),
253+
InformerFactoryMap: make(map[types.UID]informers.SharedInformerFactory),
254+
EventChannel: make(chan event.GenericEvent),
220255
}).SetupWithManager(mgr); err != nil {
221256
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
222257
os.Exit(1)

config/base/crd/bases/olm.operatorframework.io_clusterextensions.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ spec:
5656
maxLength: 48
5757
pattern: ^[a-z0-9]+(-[a-z0-9]+)*$
5858
type: string
59+
serviceAccountName:
60+
description: |-
61+
ServiceAccountName is the name of the ServiceAccount to use to manage the resources in the bundle.
62+
The service account is expected to exist in the InstallNamespace.
63+
maxLength: 253
64+
pattern: ^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$
65+
type: string
5966
upgradeConstraintPolicy:
6067
default: Enforce
6168
description: Defines the policy for how to handle upgrade constraints
@@ -77,6 +84,7 @@ spec:
7784
required:
7885
- installNamespace
7986
- packageName
87+
- serviceAccountName
8088
type: object
8189
status:
8290
description: ClusterExtensionStatus defines the observed state of ClusterExtension

config/base/rbac/role.yaml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ kind: ClusterRole
44
metadata:
55
name: manager-role
66
rules:
7-
- apiGroups:
8-
- '*'
9-
resources:
10-
- '*'
11-
verbs:
12-
- '*'
137
- apiGroups:
148
- catalogd.operatorframework.io
159
resources:
@@ -27,22 +21,24 @@ rules:
2721
- apiGroups:
2822
- ""
2923
resources:
30-
- secrets
24+
- configmaps
3125
verbs:
32-
- create
33-
- delete
34-
- get
3526
- list
36-
- patch
37-
- update
3827
- watch
28+
- apiGroups:
29+
- ""
30+
resources:
31+
- serviceaccounts/token
32+
verbs:
33+
- create
3934
- apiGroups:
4035
- olm.operatorframework.io
4136
resources:
4237
- clusterextensions
4338
verbs:
4439
- get
4540
- list
41+
- update
4642
- watch
4743
- apiGroups:
4844
- olm.operatorframework.io

config/samples/olm_v1alpha1_clusterextension.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ metadata:
44
name: clusterextension-sample
55
spec:
66
installNamespace: default
7+
serviceAccountName: argocd-operator-bundle-sa
78
packageName: argocd-operator
89
version: 0.6.0

0 commit comments

Comments
 (0)