Skip to content

Commit 5742f05

Browse files
committed
Add DownwardAPILabels support for Bucket
1 parent 40eb0c7 commit 5742f05

14 files changed

Lines changed: 98 additions & 29 deletions

File tree

broker/bucketbroker/apiutils/apiutils.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010

1111
"github.com/ironcore-dev/controller-utils/metautils"
12-
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
1312
bucketbrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/bucketbroker/api/v1alpha1"
1413
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
1514
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -128,10 +127,6 @@ func GetAnnotationsAnnotation(o metav1.Object) (map[string]string, error) {
128127
return annotations, nil
129128
}
130129

131-
func SetBucketManagerLabel(bucket *storagev1alpha1.Bucket, manager string) {
132-
metautils.SetLabel(bucket, bucketbrokerv1alpha1.ManagerLabel, manager)
133-
}
134-
135130
func IsManagedBy(o metav1.Object, manager string) bool {
136131
actual, ok := o.GetLabels()[bucketbrokerv1alpha1.ManagerLabel]
137132
return ok && actual == manager

broker/bucketbroker/cmd/bucketbroker/app/app.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
)
2525

2626
type Options struct {
27-
GetConfigOptions config.GetConfigOptions
28-
Address string
27+
GetConfigOptions config.GetConfigOptions
28+
Address string
29+
BrokerDownwardAPILabels map[string]string
2930

3031
QPS float32
3132
Burst int
@@ -38,6 +39,8 @@ type Options struct {
3839
func (o *Options) AddFlags(fs *pflag.FlagSet) {
3940
o.GetConfigOptions.BindFlags(fs)
4041
fs.StringVar(&o.Address, "address", "/var/run/iri-bucketbroker.sock", "Address to listen on.")
42+
fs.StringToStringVar(&o.BrokerDownwardAPILabels, "broker-downward-api-label", nil, "The labels to broker via downward API."+
43+
"Example: broker 'root-bucket-uid' from 'bucketpoollet.ironcore.dev/bucket-uid'.")
4144

4245
fs.StringVar(&o.Namespace, "namespace", o.Namespace, "Target Kubernetes namespace to use.")
4346
fs.StringVar(&o.BucketPoolName, "bucket-pool-name", o.BucketPoolName, "Name of the target bucket pool to pin buckets to, if any.")
@@ -86,9 +89,10 @@ func Run(ctx context.Context, opts Options) error {
8689
}
8790

8891
srv, err := server.New(cfg, server.Options{
89-
Namespace: opts.Namespace,
90-
BucketPoolName: opts.BucketPoolName,
91-
BucketPoolSelector: opts.BucketPoolSelector,
92+
BrokerDownwardAPILabels: opts.BrokerDownwardAPILabels,
93+
Namespace: opts.Namespace,
94+
BucketPoolName: opts.BucketPoolName,
95+
BucketPoolSelector: opts.BucketPoolSelector,
9296
})
9397
if err != nil {
9498
return fmt.Errorf("error creating server: %w", err)

broker/bucketbroker/server/bucket_create.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
bucketbrokerv1alpha1 "github.com/ironcore-dev/ironcore/broker/bucketbroker/api/v1alpha1"
1313
"github.com/ironcore-dev/ironcore/broker/bucketbroker/apiutils"
1414
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
15+
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"
16+
poolletutils "github.com/ironcore-dev/ironcore/poollet/common/utils"
17+
"github.com/ironcore-dev/ironcore/utils/maps"
1518
corev1 "k8s.io/api/core/v1"
1619
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1720
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -22,17 +25,37 @@ type AggregateIronCoreBucket struct {
2225
AccessSecret *corev1.Secret
2326
}
2427

28+
func (s *Server) prepareIronCoreBucketLabels(bucket *iri.Bucket) map[string]string {
29+
labels := make(map[string]string)
30+
31+
for downwardAPILabelName, defaultLabelName := range s.brokerDownwardAPILabels {
32+
value := bucket.GetMetadata().GetLabels()[poolletutils.DownwardAPILabel(bucketpoolletv1alpha1.BucketDownwardAPIPrefix, downwardAPILabelName)]
33+
if value == "" {
34+
value = bucket.GetMetadata().GetLabels()[defaultLabelName]
35+
}
36+
if value != "" {
37+
labels[poolletutils.DownwardAPILabel(bucketpoolletv1alpha1.BucketDownwardAPIPrefix, downwardAPILabelName)] = value
38+
}
39+
}
40+
41+
return labels
42+
}
43+
2544
func (s *Server) getIronCoreBucketConfig(_ context.Context, bucket *iri.Bucket) (*AggregateIronCoreBucket, error) {
2645
var bucketPoolRef *corev1.LocalObjectReference
2746
if s.bucketPoolName != "" {
2847
bucketPoolRef = &corev1.LocalObjectReference{
2948
Name: s.bucketPoolName,
3049
}
3150
}
51+
labels := s.prepareIronCoreBucketLabels(bucket)
3252
ironcoreBucket := &storagev1alpha1.Bucket{
3353
ObjectMeta: metav1.ObjectMeta{
3454
Namespace: s.namespace,
3555
Name: s.generateID(),
56+
Labels: maps.AppendMap(labels, map[string]string{
57+
bucketbrokerv1alpha1.ManagerLabel: bucketbrokerv1alpha1.BucketBrokerManager,
58+
}),
3659
},
3760
Spec: storagev1alpha1.BucketSpec{
3861
BucketClassRef: &corev1.LocalObjectReference{Name: bucket.Spec.Class},
@@ -43,7 +66,6 @@ func (s *Server) getIronCoreBucketConfig(_ context.Context, bucket *iri.Bucket)
4366
if err := apiutils.SetObjectMetadata(ironcoreBucket, bucket.Metadata); err != nil {
4467
return nil, err
4568
}
46-
apiutils.SetBucketManagerLabel(ironcoreBucket, bucketbrokerv1alpha1.BucketBrokerManager)
4769

4870
return &AggregateIronCoreBucket{
4971
Bucket: ironcoreBucket,

broker/bucketbroker/server/bucket_create_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
iri "github.com/ironcore-dev/ironcore/iri/apis/bucket/v1alpha1"
1111
irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1"
1212
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"
13+
poolletutils "github.com/ironcore-dev/ironcore/poollet/common/utils"
1314

1415
. "github.com/onsi/ginkgo/v2"
1516
. "github.com/onsi/gomega"
@@ -46,6 +47,7 @@ var _ = Describe("CreateBucket", func() {
4647

4748
By("inspecting the ironcore bucket")
4849
Expect(ironcoreBucket.Labels).To(Equal(map[string]string{
50+
poolletutils.DownwardAPILabel(bucketpoolletv1alpha1.BucketDownwardAPIPrefix, "root-bucket-uid"): "foobar",
4951
bucketbrokerv1alpha1.CreatedLabel: "true",
5052
bucketbrokerv1alpha1.ManagerLabel: bucketbrokerv1alpha1.BucketBrokerManager,
5153
}))

broker/bucketbroker/server/server.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Server struct {
4545
client client.Client
4646
iri.UnimplementedBucketRuntimeServer
4747

48+
brokerDownwardAPILabels map[string]string
49+
4850
namespace string
4951
bucketPoolName string
5052
bucketPoolSelector map[string]string
@@ -90,9 +92,14 @@ func (s *Server) setupCleaner(ctx context.Context, log logr.Logger, retErr *erro
9092
}
9193

9294
type Options struct {
93-
Namespace string
94-
BucketPoolName string
95-
BucketPoolSelector map[string]string
95+
// BrokerDownwardAPILabels specifies which labels to broker via downward API and what the default
96+
// label name is to obtain the value in case there is no value for the downward API.
97+
// Example usage is e.g. to broker the root UID (map "root-bucket-uid" to bucketpoollet's
98+
// "bucketpoollet.ironcore.dev/bucket-uid")
99+
BrokerDownwardAPILabels map[string]string
100+
Namespace string
101+
BucketPoolName string
102+
BucketPoolSelector map[string]string
96103
}
97104

98105
func setOptionsDefaults(o *Options) {
@@ -118,10 +125,11 @@ func New(cfg *rest.Config, opts Options) (*Server, error) {
118125
}
119126

120127
return &Server{
121-
client: c,
122-
namespace: opts.Namespace,
123-
bucketPoolName: opts.BucketPoolName,
124-
bucketPoolSelector: opts.BucketPoolSelector,
128+
brokerDownwardAPILabels: opts.BrokerDownwardAPILabels,
129+
client: c,
130+
namespace: opts.Namespace,
131+
bucketPoolName: opts.BucketPoolName,
132+
bucketPoolSelector: opts.BucketPoolSelector,
125133
}, nil
126134
}
127135

broker/bucketbroker/server/server_suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
corev1alpha1 "github.com/ironcore-dev/ironcore/api/core/v1alpha1"
1717
storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1"
1818
"github.com/ironcore-dev/ironcore/broker/bucketbroker/server"
19+
bucketpoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/bucketpoollet/api/v1alpha1"
1920
utilsenvtest "github.com/ironcore-dev/ironcore/utils/envtest"
2021
"github.com/ironcore-dev/ironcore/utils/envtest/apiserver"
2122
. "github.com/onsi/ginkgo/v2"
@@ -138,6 +139,9 @@ func SetupTest() (*corev1.Namespace, *storagev1alpha1.BucketPool, *server.Server
138139
BucketPoolSelector: map[string]string{
139140
"pool": "test-pool",
140141
},
142+
BrokerDownwardAPILabels: map[string]string{
143+
"root-bucket-uid": bucketpoolletv1alpha1.BucketUIDLabel,
144+
},
141145
})
142146
Expect(err).NotTo(HaveOccurred())
143147
*srv = *newSrv

config/bucketpoollet-broker/manager/manager.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ spec:
3232
args:
3333
- --health-probe-bind-address=:8081
3434
- --leader-elect
35+
- --bucket-downward-api-label=root-bucket-namespace=metadata.labels['downward-api.bucketpoollet.ironcore.dev/root-bucket-namespace']
36+
- --bucket-downward-api-label=root-bucket-name=metadata.labels['downward-api.bucketpoollet.ironcore.dev/root-bucket-name']
37+
- --bucket-downward-api-label=root-bucket-uid=metadata.labels['downward-api.bucketpoollet.ironcore.dev/root-bucket-uid']
3538
image: bucketpoollet:latest
3639
name: manager
3740
securityContext:
@@ -62,6 +65,10 @@ spec:
6265
- /bucketbroker
6366
image: bucketbroker:latest
6467
name: broker
68+
args:
69+
- --broker-downward-api-label=root-bucket-namespace=bucketpoollet.ironcore.dev/root-bucket-namespace
70+
- --broker-downward-api-label=root-bucket-name=bucketpoollet.ironcore.dev/root-bucket-name
71+
- --broker-downward-api-label=root-bucket-uid=bucketpoollet.ironcore.dev/root-bucket-uid
6572
securityContext:
6673
allowPrivilegeEscalation: false
6774
livenessProbe:

config/volumepoollet-broker/manager/manager.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ spec:
6666
image: volumebroker:latest
6767
name: broker
6868
args:
69-
- --broker-downward-api-label=root-volume-namespace=metadata.labels['downward-api.volumepoollet.ironcore.dev/root-volume-namespace']
70-
- --broker-downward-api-label=root-volume-name=metadata.labels['downward-api.volumepoollet.ironcore.dev/root-volume-name']
71-
- --broker-downward-api-label=root-volume-uid=metadata.labels['downward-api.volumepoollet.ironcore.dev/root-volume-uid']
69+
- --broker-downward-api-label=root-volume-namespace=volumepoollet.ironcore.dev/root-volume-namespace
70+
- --broker-downward-api-label=root-volume-name=volumepoollet.ironcore.dev/root-volume-name
71+
- --broker-downward-api-label=root-volume-uid=volumepoollet.ironcore.dev/root-volume-uid
7272
securityContext:
7373
allowPrivilegeEscalation: false
7474
livenessProbe:

poollet/bucketpoollet/api/v1alpha1/common_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ const (
1010

1111
FieldOwner = "bucketpoollet.ironcore.dev/field-owner"
1212
BucketFinalizer = "bucketpoollet.ironcore.dev/bucket"
13+
14+
// DownwardAPIPrefix is the prefix for any downward label.
15+
BucketDownwardAPIPrefix = "downward-api.bucketpoollet.ironcore.dev/"
1316
)

poollet/bucketpoollet/bem/bem_suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const (
4141
apiServiceTimeout = 5 * time.Minute
4242

4343
controllerManagerService = "controller-manager"
44+
45+
fooDownwardAPILabel = "custom-downward-api-label"
46+
fooAnnotation = "foo"
4447
)
4548

4649
func TestControllers(t *testing.T) {

0 commit comments

Comments
 (0)