-
Notifications
You must be signed in to change notification settings - Fork 104
/
resourceRequest_controller_methods.go
139 lines (124 loc) · 4.34 KB
/
resourceRequest_controller_methods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2019-2022 The Liqo 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 resourcerequestoperator
import (
"context"
"fmt"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
)
func (r *ResourceRequestReconciler) ensureClusterRole(ctx context.Context,
remoteClusterIdentity discoveryv1alpha1.ClusterIdentity) error {
clusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("liqo-tenant-remote-%s", GetTenantName(remoteClusterIdentity)),
},
}
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, clusterRole, func() error {
clusterRole.Rules = []rbacv1.PolicyRule{
{
APIGroups: []string{"metrics.liqo.io"},
Resources: []string{"scrape", "scrape/metrics"},
Verbs: []string{"get"},
ResourceNames: []string{remoteClusterIdentity.ClusterID},
},
}
return nil
})
return err
}
func (r *ResourceRequestReconciler) ensureClusterRoleBinding(ctx context.Context,
remoteClusterIdentity discoveryv1alpha1.ClusterIdentity) error {
clusterRoleBinding := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("liqo-tenant-remote-%s", GetTenantName(remoteClusterIdentity)),
},
}
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, clusterRoleBinding, func() error {
clusterRoleBinding.RoleRef = rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: fmt.Sprintf("liqo-tenant-remote-%s", GetTenantName(remoteClusterIdentity)),
}
clusterRoleBinding.Subjects = []rbacv1.Subject{
{
Kind: rbacv1.UserKind,
Name: remoteClusterIdentity.ClusterID,
},
}
return nil
})
return err
}
func (r *ResourceRequestReconciler) deleteClusterRole(ctx context.Context,
remoteClusterIdentity discoveryv1alpha1.ClusterIdentity) error {
clusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("liqo-tenant-remote-%s", GetTenantName(remoteClusterIdentity)),
},
}
err := r.Client.Delete(ctx, clusterRole)
if client.IgnoreNotFound(err) != nil {
klog.Error(err)
return err
}
return nil
}
func (r *ResourceRequestReconciler) deleteClusterRoleBinding(ctx context.Context,
remoteClusterIdentity discoveryv1alpha1.ClusterIdentity) error {
clusterRoleBinding := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("liqo-tenant-remote-%s", GetTenantName(remoteClusterIdentity)),
},
}
err := r.Client.Delete(ctx, clusterRoleBinding)
if client.IgnoreNotFound(err) != nil {
klog.Error(err)
return err
}
return nil
}
func (r *ResourceRequestReconciler) checkOfferState(ctx context.Context,
resourceRequest *discoveryv1alpha1.ResourceRequest) error {
var resourceOffer sharingv1alpha1.ResourceOffer
err := r.Client.Get(ctx, types.NamespacedName{
Name: getOfferName(r.HomeCluster),
Namespace: resourceRequest.GetNamespace(),
}, &resourceOffer)
if err != nil && !apierrors.IsNotFound(err) {
klog.Error(err)
return err
}
if apierrors.IsNotFound(err) {
resourceRequest.Status.OfferState = discoveryv1alpha1.OfferStateNone
} else {
resourceRequest.Status.OfferState = discoveryv1alpha1.OfferStateCreated
}
return nil
}
// getOfferName returns the name of the ResourceOffer coming from the given cluster.
func getOfferName(cluster discoveryv1alpha1.ClusterIdentity) string {
return cluster.ClusterName
}
// GetTenantName returns the name of the Tenant for the given cluster.
func GetTenantName(cluster discoveryv1alpha1.ClusterIdentity) string {
return cluster.ClusterName
}