1
0
mirror of https://github.com/coreos/prometheus-operator.git synced 2026-02-05 06:45:27 +01:00
Files
prometheus-operator/test/framework/role_binding.go

90 lines
2.8 KiB
Go
Raw Normal View History

2017-07-14 16:06:30 +02:00
// Copyright 2017 The prometheus-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package framework
import (
"context"
"fmt"
2017-11-24 16:19:04 +01:00
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
2017-07-14 16:06:30 +02:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
)
func (f *Framework) createOrUpdateRoleBinding(ctx context.Context, ns string, cr *rbacv1.ClusterRole, relativePath string) (FinalizerFn, error) {
return f.createOrUpdateRoleBindingForSubjectNamespace(ctx, ns, "", cr, relativePath)
2017-07-14 16:06:30 +02:00
}
func (f *Framework) createOrUpdateRoleBindingForSubjectNamespace(ctx context.Context, ns, subjectNs string, cr *rbacv1.ClusterRole, source string) (FinalizerFn, error) {
roleBinding, err := f.parseRoleBindingYaml(source)
if err != nil {
return nil, fmt.Errorf("failed to parse role binding manifest: %w", err)
}
if subjectNs != "" {
for i := range roleBinding.Subjects {
roleBinding.Subjects[i].Namespace = subjectNs
}
}
roleBinding.RoleRef.Name = cr.Name
finalizerFn := func() error { return f.deleteRoleBinding(ctx, ns, roleBinding.Name) }
_, err = f.KubeClient.RbacV1().RoleBindings(ns).Get(ctx, roleBinding.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
_, err = f.KubeClient.RbacV1().RoleBindings(ns).Create(ctx, roleBinding, metav1.CreateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to create role binding: %w", err)
}
return finalizerFn, nil
}
return nil, fmt.Errorf("failed to get role binding: %w", err)
}
_, err = f.KubeClient.RbacV1().RoleBindings(ns).Update(ctx, roleBinding, metav1.UpdateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to update role binding: %w", err)
}
return finalizerFn, nil
}
func (f *Framework) deleteRoleBinding(ctx context.Context, ns, name string) error {
err := f.KubeClient.RbacV1().RoleBindings(ns).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
2017-07-14 16:06:30 +02:00
return err
}
return nil
2017-07-14 16:06:30 +02:00
}
func (f *Framework) parseRoleBindingYaml(source string) (*rbacv1.RoleBinding, error) {
manifest, err := SourceToIOReader(source)
2017-07-14 16:06:30 +02:00
if err != nil {
return nil, err
}
2017-11-24 16:19:04 +01:00
roleBinding := rbacv1.RoleBinding{}
2017-07-14 16:06:30 +02:00
if err := yaml.NewYAMLOrJSONDecoder(manifest, 100).Decode(&roleBinding); err != nil {
return nil, err
}
return &roleBinding, nil
}