Skip to content

Commit 0908fd8

Browse files
clean-up code and added delete path for rbac resources new & legacy
1 parent 3838f75 commit 0908fd8

File tree

6 files changed

+62
-38
lines changed

6 files changed

+62
-38
lines changed

operator/internal/handlers/lokistack_cluster_scope_resources_create.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/ViaQ/logerr/v2/kverrors"
88
"github.com/go-logr/logr"
99
rbacv1 "k8s.io/api/rbac/v1"
10+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1011
"k8s.io/apimachinery/pkg/runtime"
1112
ctrl "sigs.k8s.io/controller-runtime"
1213
"sigs.k8s.io/controller-runtime/pkg/client" //nolint:typecheck
@@ -37,20 +38,10 @@ func CreateClusterScopedResources(ctx context.Context, log logr.Logger, dashboar
3738
}
3839
opts := openshift.NewOptionsClusterScope(operatorNs, manifests.ClusterScopeLabels(), gatewaySubjects, rulerSubjects)
3940

40-
var objs []client.Object
41+
objs := openshift.BuildRBAC(opts)
4142
if dashboards {
42-
dashObjs, err := openshift.BuildDashboards(opts)
43-
if err != nil {
44-
return kverrors.Wrap(err, "failed to build dashboard manifests")
45-
}
46-
objs = append(objs, dashObjs...)
47-
}
48-
49-
rbacOBjs, err := openshift.BuildRBAC(opts)
50-
if err != nil {
51-
return kverrors.Wrap(err, "failed to build RBAC manifests")
43+
objs = append(objs, openshift.BuildDashboards(opts)...)
5244
}
53-
objs = append(objs, rbacOBjs...)
5445

5546
var errCount int32
5647
for _, obj := range objs {
@@ -74,7 +65,26 @@ func CreateClusterScopedResources(ctx context.Context, log logr.Logger, dashboar
7465
}
7566

7667
if errCount > 0 {
77-
return kverrors.New("failed to configure lokistack dashboard resources")
68+
return kverrors.New("failed to configure lokistack cluster-scoped resources")
69+
}
70+
71+
// Delete legacy RBAC resources
72+
var legacyObjs []client.Object
73+
for _, stack := range stacks.Items {
74+
// This name would clash with the new cluster-scoped resources. Skip it.
75+
if stack.Name == "lokistack" {
76+
continue
77+
}
78+
legacyObjs = append(legacyObjs, openshift.LegacyRBAC(manifests.GatewayName(stack.Name), manifests.RulerName(stack.Name))...)
79+
}
80+
for _, obj := range legacyObjs {
81+
key := client.ObjectKeyFromObject(obj)
82+
if err := k.Delete(ctx, obj, &client.DeleteOptions{}); err != nil {
83+
if apierrors.IsNotFound(err) {
84+
continue
85+
}
86+
return kverrors.Wrap(err, "failed to delete resource", "kind", obj.GetObjectKind(), "key", key)
87+
}
7888
}
7989

8090
return nil

operator/internal/handlers/lokistack_cluster_scope_resources_delete.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ import (
1616

1717
// DeleteClusterScopedResources removes all cluster-scoped resources.
1818
func DeleteClusterScopedResources(ctx context.Context, k k8s.Client, operatorNs string, stacks lokiv1.LokiStackList) error {
19+
// Since we are deleting we don't need to worry about the subjects.
1920
opts := openshift.NewOptionsClusterScope(operatorNs, manifests.ClusterScopeLabels(), []rbacv1.Subject{}, []rbacv1.Subject{})
20-
objs, err := openshift.BuildDashboards(opts)
21-
if err != nil {
22-
return kverrors.Wrap(err, "failed to build dashboards manifests")
23-
}
2421

25-
// Delete all re
22+
objs := openshift.BuildRBAC(opts)
23+
objs = append(objs, openshift.BuildDashboards(opts)...)
2624

2725
for _, obj := range objs {
2826
key := client.ObjectKeyFromObject(obj)
2927
if err := k.Delete(ctx, obj, &client.DeleteOptions{}); err != nil {
3028
if apierrors.IsNotFound(err) {
3129
continue
3230
}
33-
return kverrors.Wrap(err, "failed to delete dashboard", "key", key)
31+
return kverrors.Wrap(err, "failed to delete dashboard", "kind", obj.GetObjectKind(), "key", key)
3432
}
3533
}
3634
return nil

operator/internal/handlers/lokistack_cluster_scope_resources_delete_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616

1717
func TestDeleteDashboards(t *testing.T) {
1818
opts := openshift.NewOptionsClusterScope("operator-ns", nil, nil, nil)
19-
objs, err := openshift.BuildDashboards(opts)
20-
require.NoError(t, err)
19+
objs := openshift.BuildRBAC(opts)
20+
objs = append(objs, openshift.BuildDashboards(opts)...)
2121

2222
k := &k8sfakes.FakeClient{}
2323

24-
err = DeleteClusterScopedResources(context.TODO(), k, "operator-ns", v1.LokiStackList{})
24+
err := DeleteClusterScopedResources(context.TODO(), k, "operator-ns", v1.LokiStackList{})
2525
require.NoError(t, err)
2626
require.Equal(t, k.DeleteCallCount(), len(objs))
2727
}

operator/internal/manifests/openshift/dashboards.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@ const (
1616
managedConfigNamespace = "openshift-config-managed"
1717
)
1818

19-
func BuildDashboards(opts OptionsClusterScope) ([]client.Object, error) {
19+
func BuildDashboards(opts OptionsClusterScope) []client.Object {
2020
ds, rules := dashboards.Content()
2121

2222
var objs []client.Object
2323
for name, content := range ds {
2424
objs = append(objs, newDashboardConfigMap(name, content))
2525
}
2626

27-
promRule, err := newDashboardPrometheusRule(opts.BuildOpts.OperatorNs, rules)
28-
if err != nil {
29-
return nil, err
30-
}
27+
promRule := newDashboardPrometheusRule(opts.BuildOpts.OperatorNs, rules)
3128
objs = append(objs, promRule)
3229

33-
return objs, nil
30+
return objs
3431
}
3532

3633
func newDashboardConfigMap(filename string, content []byte) *corev1.ConfigMap {
@@ -54,7 +51,7 @@ func newDashboardConfigMap(filename string, content []byte) *corev1.ConfigMap {
5451
}
5552
}
5653

57-
func newDashboardPrometheusRule(namespace string, spec *monitoringv1.PrometheusRuleSpec) (*monitoringv1.PrometheusRule, error) {
54+
func newDashboardPrometheusRule(namespace string, spec *monitoringv1.PrometheusRuleSpec) *monitoringv1.PrometheusRule {
5855
return &monitoringv1.PrometheusRule{
5956
TypeMeta: metav1.TypeMeta{
6057
Kind: "PrometheusRule",
@@ -65,5 +62,5 @@ func newDashboardPrometheusRule(namespace string, spec *monitoringv1.PrometheusR
6562
Namespace: namespace,
6663
},
6764
Spec: *spec,
68-
}, nil
65+
}
6966
}

operator/internal/manifests/openshift/dashboards_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import (
1010

1111
func TestBuildDashboards_ReturnsDashboardConfigMaps(t *testing.T) {
1212
opts := NewOptionsClusterScope("test", nil, nil, nil)
13-
objs, err := BuildDashboards(opts)
14-
require.NoError(t, err)
13+
objs := BuildDashboards(opts)
1514

1615
for _, d := range objs {
1716
switch d.(type) {
@@ -24,8 +23,7 @@ func TestBuildDashboards_ReturnsDashboardConfigMaps(t *testing.T) {
2423

2524
func TestBuildDashboards_ReturnsPrometheusRules(t *testing.T) {
2625
opts := NewOptionsClusterScope("test", nil, nil, nil)
27-
objs, err := BuildDashboards(opts)
28-
require.NoError(t, err)
26+
objs := BuildDashboards(opts)
2927

3028
rules := objs[len(objs)-1].(*monitoringv1.PrometheusRule)
3129
require.Equal(t, rules.GetName(), dashboardPrometheusRulesName)

operator/internal/manifests/openshift/rbac.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,34 @@ const (
1111
rulerName = "lokistack-ruler"
1212
)
1313

14-
func BuildRBAC(opts OptionsClusterScope) ([]client.Object, error) {
14+
func BuildRBAC(opts OptionsClusterScope) []client.Object {
1515
objs := make([]client.Object, 0, 4)
16-
objs = append(objs, BuildGatewayClusterRole(opts))
17-
objs = append(objs, BuildGatewayClusterRoleBinding(opts))
1816
objs = append(objs, BuildRulerClusterRole(opts))
1917
objs = append(objs, BuildRulerClusterRoleBinding(opts))
20-
return objs, nil
18+
return objs
19+
}
20+
21+
func LegacyRBAC(gatewayName, rulerName string) []client.Object {
22+
opts := NewOptionsClusterScope("", map[string]string{}, []rbacv1.Subject{}, []rbacv1.Subject{})
23+
objs := make([]client.Object, 0, 4)
24+
25+
cr := BuildGatewayClusterRole(opts)
26+
cr.Name = authorizerRbacName(gatewayName)
27+
objs = append(objs, cr)
28+
29+
crb := BuildGatewayClusterRoleBinding(opts)
30+
crb.Name = authorizerRbacName(gatewayName)
31+
objs = append(objs, crb)
32+
33+
cr = BuildRulerClusterRole(opts)
34+
cr.Name = authorizerRbacName(rulerName)
35+
objs = append(objs, cr)
36+
37+
crb = BuildRulerClusterRoleBinding(opts)
38+
crb.Name = authorizerRbacName(rulerName)
39+
objs = append(objs, crb)
40+
41+
return objs
2142
}
2243

2344
// BuildGatewayClusterRole returns a k8s ClusterRole object for the

0 commit comments

Comments
 (0)